《C语言程序设计》课后习题答案(第四版)谭浩强

《C语言程序设计》课后习题答案(第四版)谭浩强
《C语言程序设计》课后习题答案(第四版)谭浩强

1-5 #include

int main ( )

{ printf ("**************************\n\n");

printf(" Very Good!\n\n");

printf ("**************************\n");

return 0;

}

1-6#include

int main()

{int a,b,c,max;

printf("please input a,b,c:\n");

scanf("%d,%d,%d",&a,&b,&c);

max=a;

if (max

max=b;

if (max

max=c;

printf("The largest number is %d\n",max); return 0;

}

4-4-1

#include

int main()

{

int a,b,c;

printf("请输入三个整数:");

scanf("%d,%d,%d",&a,&b,&c);

if (a

if (b

printf("max=%d\n",c);

else

printf("max=%d\n",b);

else if (a

printf("max=%d\n",c);

else

printf("max=%d\n",a);

return 0;

}

4-6.

#include

int main()

{ int x,y;

printf("输入x:");

scanf("%d",&x);

if(x<1) /* x<1 */

{ y=x;

printf("x=%3d, y=x=%d\n" ,x,y);

}

else if(x<10) /* 1=

{ y=2*x-1;

printf("x=%d, y=2*x-1=%d\n",x,y);

}

else /* x>=10 */

{ y=3*x-11;

printf("x=%d, y=3*x-11=%d\n",x,y);

}

return 0;

}

4-7-1

#include

int main()

{

int x,y;

printf("enter x:");

scanf("%d",&x);

y=-1;

if(x!=0)

if(x>0)

y=1;

else

y=0;

printf("x=%d,y=%d\n",x,y);

return 0;

}

4-7-2

#include

int main()

{

int x,y;

printf("please enter x:");

scanf("%d",&x);

y=0;

if(x>=0)

if(x>0) y=1;

else y=-1;

printf("x=%d,y=%d\n",x,y);

}

4-8

#include

int main()

{ float score;

char grade;

printf("请输入学生成绩:");

scanf("%f",&score);

while (score>100||score<0)

{printf("\n 输入有误,请重输");

scanf("%f",&score);

}

switch((int)(score/10))

{case 10:

case 9: grade='A';break;

case 8: grade='B';break;

case 7: grade='C';break;

case 6: grade='D';break;

case 5:

case 4:

case 3:

case 2:

case 1:

case 0: grade='E';

}

printf("成绩是%5.1f,相应的等级是%c\n ",score,grade);

return 0;

}

4-11

#include

int main()

{int t,a,b,c,d;

printf("请输入四个数:");

scanf("%d,%d,%d,%d",&a,&b,&c,&d);

printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);

if (a>b)

{ t=a;a=b;b=t;}

if (a>c)

{ t=a;a=c;c=t;}

if (a>d)

{ t=a;a=d;d=t;}

{ t=b;b=c;c=t;}

if (b>d)

{ t=b;b=d;d=t;}

if (c>d)

{ t=c;c=d;d=t;}

printf("排序结果如下: \n");

printf("%d %d %d %d \n" ,a,b,c,d);

return 0;

}

4-12

#include

int main()

{

int h=10;

float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4; printf("请输入一个点(x,y):");

scanf("%f,%f",&x,&y);

d1=(x-x4)*(x-x4)+(y-y4)*(y-y4); /*求该点到各中心点距离*/ d2=(x-x1)*(x-x1)+(y-y1)*(y-y1);

d3=(x-x2)*(x-x2)+(y-y2)*(y-y2);

d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);

if (d1>1 && d2>1 && d3>1 && d4>1) h=0; /*判断该点是否在塔外*/ printf("该点高度为%d\n",h);

return 0;

}

5-3

#include

int main()

{

int p,r,n,m,temp;

printf("请输入两个正整数n,m:");

scanf("%d,%d,",&n,&m);

if (n

{

temp=n;

n=m;

m=temp;

}

p=n*m;

while(m!=0)

{

r=n%m;

n=m;

}

printf("它们的最大公约数为:%d\n",n);

printf("它们的最小公约数为:%d\n",p/n);

return 0;

}

5-4

#include

int main()

{

char c;

int letters=0,space=0,digit=0,other=0;

printf("请输入一行字符:\n");

while((c=getchar())!='\n')

{

if (c>='a' && c<='z' || c>='A' && c<='Z')

letters++;

else if (c==' ')

space++;

else if (c>='0' && c<='9')

digit++;

else

other++;

}

printf("字母数:%d\n空格数:%d\n数字数:%d\n其它字符数:%d\n",letters,space,digit,other);

return 0;

}

5-5

#include

int main()

{

int a,n,i=1,sn=0,tn=0;

printf("a,n=:");

scanf("%d,%d",&a,&n);

while (i<=n)

{

tn=tn+a; /*赋值后的tn为i个a组成数的值*/

sn=sn+tn; /*赋值后的sn为多项式前i项之和*/

a=a*10;

++i;

}

printf("a+aa+aaa+...=%d\n",sn);

return 0;

}

5-6

#include

int main()

{double s=0,t=1;

int n;

for (n=1;n<=20;n++)

{

t=t*n;

s=s+t;

}

printf("1!+2!+...+20!=%22.15e\n",s);

return 0;

}

5-7

#include

int main()

{

int n1=100,n2=50,n3=10;

double k,s1=0,s2=0,s3=0;

for (k=1;k<=n1;k++) /*计算1到100的和*/

{s1=s1+k;}

for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/ {s2=s2+k*k;}

for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/

{s3=s3+1/k;}

printf("sum=%15.6f\n",s1+s2+s3);

return 0;

}

5-8

#include

int main()

{

int i,j,k,n;

printf("parcissus numbers are ");

for (n=100;n<1000;n++)

{

i=n/100;

j=n/10-i*10;

k=n%10;

if (n==i*i*i + j*j*j + k*k*k)

printf("%d ",n);

}

printf("\n");

return 0;

}

5-10

#include

int main()

{

int i,n=20;

double a=2,b=1,s=0,t;

for (i=1;i<=n;i++)

{

s=s+a/b;

t=a,

a=a+b,

b=t;

}

printf("sum=%16.10f\n",s);

return 0;

}

5-11

#include

int main()

{

double sn=100,hn=sn/2;

int n;

for (n=2;n<=10;n++)

{

sn=sn+2*hn; /*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/

}

printf("第10次落地时共经过%f米\n",sn);

printf("第10次反弹%f米\n",hn);

return 0;

}

5-12

#include

int main()

{

int day,x1,x2;

day=9;

while(day>0)

{x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1;

day--;

}

printf("total=%d\n",x1);

return 0;

}

5-13

#include

#include

int main()

{

float a,x0,x1;

printf("enter a positive number:");

scanf("%f",&a);

x0=a/2;

x1=(x0+a/x0)/2;

do

{x0=x1;

x1=(x0+a/x0)/2;

}while(fabs(x0-x1)>=1e-5);

printf("The square root of %5.2f is %8.5f\n",a,x1);

return 0;

}

5-15

#include

#include

int main()

{float x0,x1,x2,fx0,fx1,fx2;

do

{printf("enter x1 & x2:");

scanf("%f,%f",&x1,&x2);

fx1=x1*((2*x1-4)*x1+3)-6;

fx2=x2*((2*x2-4)*x2+3)-6;

}while(fx1*fx2>0);

do

{x0=(x1+x2)/2;

fx0=x0*((2*x0-4)*x0+3)-6;

if ((fx0*fx1)<0)

{x2=x0;

fx2=fx0;

}

{x1=x0;

fx1=fx0;

}

}while(fabs (fx0)>=1e-5); printf("x=%6.2f\n",x0); return 0;

}

5-16

#include

int main()

{int i,j,k;

for (i=0;i<=3;i++)

{for (j=0;j<=2-i;j++)

printf(" ");

for (k=0;k<=2*i;k++)

printf("*");

printf("\n");

}

for (i=0;i<=2;i++)

{for (j=0;j<=i;j++)

printf(" ");

for (k=0;k<=4-2*i;k++)

printf("*");

printf("\n");

}

return 0;

}

6-1

#include

#include

int main()

{int i,j,n,a[101];

for (i=1;i<=100;i++)

a[i]=i;

a[1]=0;

for (i=2;i

for (j=i+1;j<=100;j++)

{if(a[i]!=0 && a[j]!=0)

if (a[j]%a[i]==0)

a[j]=0;

}

printf("\n");

for (i=2,n=0;i<=100;i++)

{ if(a[i]!=0)

{printf("%5d",a[i]);

n++;

}

if(n==10)

{printf("\n");

n=0;

}

}

printf("\n");

return 0;

}

6-2

#include

int main()

{int i,j,min,temp,a[11];

printf("enter data:\n");

for (i=1;i<=10;i++)

{printf("a[%d]=",i);

scanf("%d",&a[i]);

}

printf("\n");

printf("The orginal numbers:\n"); for (i=1;i<=10;i++)

printf("%5d",a[i]);

printf("\n");

for (i=1;i<=9;i++)

{min=i;

for (j=i+1;j<=10;j++)

if (a[min]>a[j]) min=j;

temp=a[i];

a[i]=a[min];

a[min]=temp;

}

printf("\nThe sorted numbers:\n"); for (i=1;i<=10;i++)

printf("%5d",a[i]);

printf("\n");

return 0;

}

6-3

#include

int main()

{

int a[3][3],sum=0;

int i,j;

printf("enter data:\n");

for (i=0;i<3;i++)

for (j=0;j<3;j++)

scanf("%3d",&a[i][j]);

for (i=0;i<3;i++)

sum=sum+a[i][i];

printf("sum=%6d\n",sum);

return 0;

}

6-4

#include

int main()

{ int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j;

printf("array a:\n");

for (i=0;i<10;i++)

printf("%5d",a[i]);

printf("\n");

printf("insert data:");

scanf("%d",&number);

end=a[9];

if (number>end)

a[10]=number;

else

{for (i=0;i<10;i++)

{if (a[i]>number)

{temp1=a[i];

a[i]=number;

for (j=i+1;j<11;j++)

{temp2=a[j];

a[j]=temp1;

temp1=temp2;

}

break;

}

}

}

printf("Now array a:\n");

for (i=0;i<11;i++)

printf("%5d",a[i]);

printf("\n");

return 0;

}

6-5

#include

#define N 5

int main()

{ int a[N],i,temp;

printf("enter array a:\n");

for (i=0;i

scanf("%d",&a[i]);

printf("array a:\n");

for (i=0;i

printf("%4d",a[i]);

for (i=0;i

a[i]=a[N-i-1];

a[N-i-1]=temp;

}

printf("\nNow,array a:\n");

for (i=0;i

printf("%4d",a[i]);

printf("\n");

return 0;

}

6-6

#include

#define N 10

int main()

{ int i,j,a[N][N];

for (i=0;i

{a[i][i]=1;

a[i][0]=1;

}

for (i=2;i

for (j=1;j<=i-1;j++)

a[i][j]=a[i-1][j-1]+a[i-1][j];

for (i=0;i

{for (j=0;j<=i;j++)

printf("%6d",a[i][j]);

printf("\n");

}

printf("\n");

return 0;

6-7

#include

int main()

{ int a[15][15],i,j,k,p,n;

p=1;

while(p==1)

{printf("enter n(n=1--15):");

scanf("%d",&n);

if ((n!=0) && (n<=15) && (n%2!=0))

p=0;

}

for (i=1;i<=n;i++)

for (j=1;j<=n;j++)

a[i][j]=0;

j=n/2+1;

a[1][j]=1;

for (k=2;k<=n*n;k++)

{i=i-1;

j=j+1;

if ((i<1) && (j>n))

{i=i+2;

j=j-1;

}

else

{if (i<1) i=n;

if (j>n) j=1;

}

if (a[i][j]==0)

a[i][j]=k;

else

{i=i+2;

j=j-1;

a[i][j]=k;

}

}

for (i=1;i<=n;i++)

{for (j=1;j<=n;j++)

printf("%5d",a[i][j]);

printf("\n");

}

return 0;

}

6-8

#include

#define N 4

#define M 5 /* 数组为4行5列*/

int main()

{

int i,j,k,a[N][M],max,maxj,flag;

printf("please input matrix:\n");

for (i=0;i

for (j=0;j

scanf("%d",&a[i][j]);

for (i=0;i

{max=a[i][0]; /* 开始时假设a[i][0]最大*/

maxj=0; /* 将列号0赋给maxj保存*/

for (j=0;j

if (a[i][j]>max)

{max=a[i][j]; /* 将本行的最大数存放在max中*/

maxj=j; /* 将最大数所在的列号存放在maxj中*/

}

flag=1; /* 先假设是鞍点,以flag为1代表*/

for (k=0;k

if (max>a[k][maxj]) /* 将最大数和其同列元素相比*/

{flag=0; /* 如果max不是同列最小,表示不是鞍点令flag1为0 */

continue;}

if(flag) /* 如果flag1为1表示是鞍点*/

{printf("a[%d][%d]=%d\n",i,maxj,max); /* 输出鞍点的值和所在行列号*/

break;

}

}

if(!flag) /* 如果flag为0表示鞍点不存在*/

printf("It is not exist!\n");

return 0;

}

6-9

#include

#define N 15

int main()

{ int i,number,top,bott,mid,loca,a[N],flag=1,sign;

char c;

printf("enter data:\n");

scanf("%d",&a[0]);

i=1;

while(i

{scanf("%d",&a[i]);

if (a[i]>=a[i-1])

i++;

else

printf("enter this data again:\n");

}

printf("\n");

for (i=0;i

printf("%5d",a[i]);

printf("\n");

while(flag)

{printf("input number to look for:");

scanf("%d",&number);

sign=0;

top=0; //top是查找区间的起始位置

bott=N-1; //bott是查找区间的最末位置

if ((numbera[N-1])) //要查的数不在查找区间内loca=-1; // 表示找不到

while ((!sign) && (top<=bott))

{mid=(bott+top)/2;

if (number==a[mid])

{loca=mid;

printf("Has found %d, its position is %d\n",number,loca+1);

sign=1;

}

else if (number

bott=mid-1;

else

top=mid+1;

}

if(!sign||loca==-1)

printf("cannot find %d.\n",number);;

printf("continu or not(Y/N)?");

scanf(" %c",&c);

if (c=='N'||c=='n')

flag=0;

}

return 0;

}

6-10

#include

int main()

{int i,j,upp,low,dig,spa,oth;

char text[3][80];

upp=low=dig=spa=oth=0;

for (i=0;i<3;i++)

{ printf("please input line %d:\n",i+1);

gets(text[i]);

for (j=0;j<80 && text[i][j]!='\0';j++)

{if (text[i][j]>='A'&& text[i][j]<='Z')

upp++;

else if (text[i][j]>='a' && text[i][j]<='z')

low++;

else if (text[i][j]>='0' && text[i][j]<='9')

dig++;

else if (text[i][j]==' ')

spa++;

else

oth++;

}

}

printf("\nupper case: %d\n",upp);

printf("lower case: %d\n",low);

printf("digit : %d\n",dig);

printf("space : %d\n",spa);

printf("other : %d\n",oth);

return 0;

}

6-12a-c

#include

int main()

{ int j,n;

char ch[80],tran[80];

printf("input cipher code:");

gets(ch);

printf("\ncipher code :%s",ch);

j=0;

while (ch[j]!='\0')

{ if ((ch[j]>='A') && (ch[j]<='Z'))

tran[j]=155-ch[j];

else if ((ch[j]>='a') && (ch[j]<='z'))

tran[j]=219-ch[j];

else

tran[j]=ch[j];

j++;

}

n=j;

printf("\noriginal text:");

for (j=0;j

putchar(tran[j]);

printf("\n");

return 0;

}

6-12b

#include

int main()

{int j,n;

char ch[80];

printf("input cipher code:\n");

gets(ch);

printf("\ncipher code:%s\n",ch);

j=0;

while (ch[j]!='\0')

{ if ((ch[j]>='A') && (ch[j]<='Z'))

ch[j]=155-ch[j];

else if ((ch[j]>='a') && (ch[j]<='z'))

ch[j]=219-ch[j];

else

ch[j]=ch[j];

j++;

}

n=j;

printf("original text:");

for (j=0;j

putchar(ch[j]);

printf("\n");

return 0;

}

6-13

#include

int main()

{ char s1[80],s2[40];

int i=0,j=0;

printf("input string1:");

scanf("%s",s1);

printf("input string2:");

scanf("%s",s2);

while (s1[i]!='\0')

i++;

while(s2[j]!='\0')

s1[i++]=s2[j++];

s1[i]='\0';

printf("\nThe new string is:%s\n",s1); return 0;

}

7-1-1

#include

int main()

{int hcf(int,int);

int lcd(int,int,int);

int u,v,h,l;

scanf("%d,%d",&u,&v);

h=hcf(u,v);

printf("H.C.F=%d\n",h);

l=lcd(u,v,h);

printf("L.C.D=%d\n",l);

return 0;

}

int hcf(int u,int v)

{int t,r;

if (v>u)

{t=u;u=v;v=t;}

while ((r=u%v)!=0)

{u=v;

v=r;}

return(v);

}

int lcd(int u,int v,int h)

{

return(u*v/h);

}

7-3

#include

int main()

{int prime(int);

int n;

printf("input an integer:");

scanf("%d",&n);

if (prime(n))

printf("%d is a prime.\n",n); else

printf("%d is not a prime.\n",n); return 0;

}

int prime(int n)

{int flag=1,i;

for (i=2;i

if (n%i==0)

flag=0;

return(flag);

}

7-4

#include

#define N 3

int array[N][N];

int main()

{ void convert(int array[][3]);

int i,j;

printf("input array:\n");

for (i=0;i

for (j=0;j

scanf("%d",&array[i][j]); printf("\noriginal array :\n");

for (i=0;i

{for (j=0;j

printf("%5d",array[i][j]);

printf("\n");

}

convert(array);

printf("convert array:\n");

for (i=0;i

{for (j=0;j

printf("%5d",array[i][j]);

printf("\n");

}

return 0;

}

void convert(int array[][3])

{int i,j,t;

for (i=0;i

for (j=i+1;j

{t=array[i][j];

array[i][j]=array[j][i];

array[j][i]=t;

}

}

#include

#include

int main()

{void inverse(char str[]);

char str[100];

printf("input string:");

scanf("%s",str);

inverse(str);

printf("inverse string:%s\n",str);

return 0;

}

void inverse(char str[])

{char t;

int i,j;

for (i=0,j=strlen(str);i<(strlen(str)/2);i++,j--)

{t=str[i];

str[i]=str[j-1];

str[j-1]=t;

}

}

7-6

#include

int main()

{void concatenate(char string1[],char string2[],char string[]); char s1[100],s2[100],s[100];

printf("input string1:");

scanf("%s",s1);

printf("input string2:");

scanf("%s",s2);

concatenate(s1,s2,s);

printf("\nThe new string is %s\n",s);

return 0;

《c++程序设计》谭浩强课后习题答案

第一章 1.5题 #include using namespace std; int main() { cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0; 1.6题 #include using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; cout<<"a+b="; cout< using namespace std; int main() { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout< using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< using namespace std; int main() { int a,b,c; int add(int x,int y); cin>>a>>b; c=add(a,b);

(完整版)谭浩强c程序设计课后习题答案

谭浩强c++程序设计课后答案 娄警卫

第一章1.5题 #include using namespace std; int main() { cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0; 1.6题 #include using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; cout<<"a+b="; cout< using namespace std; int main() { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout< using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< using namespace std; int main() { int a,b,c; int add(int x,int y); cin>>a>>b; c=add(a,b); cout<<"a+b="<

C语言程序设计第三版谭浩强课后习题答案完整版

1.5 #include void main() { printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"); printf(" Very good! \n"); printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"); } 1.6 #include void main() { float max(float x,float y,float z); float a,b,c; printf("请分别输入a,b,c:\n"); scanf("%f,%f,%f",&a,&b,&c); printf("max=%f",max(a,b,c)); } float max(float x,float y,float z) { float m,n; m=x>y? x: y; n=m>z? m: z; return(n); } 3.6 #include void main() { char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’; printf(“a%c b%c\tc%c\tabc\n”,c1,c2,c3); printf(“\t\b%c %c\n”,c4,c5);

} 3.9.1 #include void main() { double x=2.5,y=4.7,z; int a=7; z=x+a%3*(int)(x+y)%2/4; printf("该表达式的值为:%f",z); } 3.9.2 #include void main() { int a=2,b=3; float x=3.5,y=2.5,z; z=(float)(a+b)/2+(int)x%(int)y; printf("该表达式的值为:%f",z); } 4.5 #include void main() { int a=5,b=7; double x=67.8564,y=-789.124; char c='A'; long n=1234567; unsigned u=65535; printf("%d%d\n",a,b); printf("%3d%3d\n",a,b); printf("%f,%f\n",x,y); printf("%-10f,%-10f\n",x,y); printf("%8.2f,%8.2f,%.4f,%.4f,%3f,%3f\n",x,y,x,y,x,y); printf("%e,%10.2e\n",x,y); printf("%c,%d,%o,%x\n",n,n,n); printf("%ld,%lo,%x\n",n,n,n); printf("%u,%o,%x,%d\n",u,u,u,u);

C语言程序设计第三版谭浩强课后习题答案完整版

1.6 编写一个程序,输入a、b、c 三个值,输出其中最大值。 课后习题答案完整版 第一章 1.5 请参照本章例题,编写一个C 程序,输出以下信息: ************************** Very Good! ************************** 解:mian() {int a,b,c,max; printf( “请输入三个数a,b,c:\n ” ); scanf( “%d,%d,%”d ,&a,&b,&c); C语言程序设计第三版谭浩强 解: mian() {printf( ”); “************************** printf( “”X “ n” ); printf( “Very Good!” \ n”); printf( “”X “ n” ); printf( “************************** ); max=a; if(max

{char #include c1='a',c2='b',c3= 'c',c4= ' \101 ',c5= ' 116'; printf( “a%cb%c n”,c1,c2,c 3); printf( “ b%c %c” ,c4,c5); } 解: aa 口bb 口口口cc 口口口口口口abc A 口N 3.7 要将"China" 译成密码,译码规律是:用原来字母后面的第 4 个字母代替原来的字母.例如,字母"A" 后面第 4 个字母是"E" . "E"代替"A"。因此,"China"应译为"Glmre" 。请编一程序,用赋初值的方法使cl 、c2、c3、c4、c5 五个变量的值分别为, ' C'、h'、i '、n'、a'经过运算,使cl、c2、c3、c4、c5分别变为'G'、' I '、' m >' r'、’ e',并输出。main() { char c1=' C' ,c2=' h' ,c3= ' i ' ,c4= ' n' ,c 5=' a' ; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf(" 密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 运行结果: 密码是GImre 3.9 求下面算术表达式的值。 解: 1 )x+a%3*(int)(x+y)%2/4

C语言程序设计第三版谭浩强课后习题答案完整版

C语言程序设计第三版谭浩强课后习题答案完整版Last revision on 21 December 2020

C语言程序设计第三版谭浩强 课后习题答案完整版 第一章 请参照本章例题,编写一个C程序,输出以下信息: ************************** Very Good! ************************** 解: mian() {printf(“**************************”); printf(“\n”); printf(“Very Good!\n”); printf(“\n”); printf(“**************************”); } 编写一个程序,输入a、b、c三个值,输出其中最大值。 解: mian() {int a,b,c,max; printf(“请输入三个数a,b,c:\n”); scanf(“%d,%d,%d”,&a,&b,&c); max=a; if(max main() { char c1=’C’,c2=’h’,c3=’i’,c4=’n’,c5=’a’; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf("密码 是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 运行结果: 密码是Glmre 求下面算术表达式的值。

C程序设计第四版谭浩强完整版课后习题答案

C程序设计第四版谭浩强完整版课后习题答案集团标准化办公室:[VV986T-J682P28-JP266L8-68PNN]

C程序设计(第四版)(谭浩强)第一章课后习题答案 P006 向屏幕输出文字. #include<>代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to \n"); return 0; } P008 求两个数的和. #include<> int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum);

return 0; } P008 调用函数比较两个数的大小. #include<> int main() { int max(int x,int y); int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c); return 0; } int max(int x,int y) { int z; if (x>y) z=x; else z=y; return(z); }

P015 三个数的大小.(数字0表示课后练习题) #include<> int main() { int a,b,c,d; int max(int x , int y , int z); printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); printf("The max is :%d .\n",d); } int max(int x , int y , int z) { int m; if (x>y && x>z) m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); }

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言1 1.1什么是计算机程序1 1.2什么是计算机语言1 1.3C语言的发展及其特点3 1.4最简单的C语言程序5 1.4.1最简单的C语言程序举例6 1.4.2C语言程序的结构10 1.5运行C程序的步骤与方法12 1.6程序设计的任务14 1-5 #include int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; } 1-6#include int main() {int a,b,c,max; printf("please input a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if (max

2.5结构化程序设计方法34 习题36

C语言程序设计第三版谭浩强课后习题答案完整版

C语言程序设计第三版谭浩强 课后习题答案完整版 第一章 1.5请参照本章例题,编写一个C程序,输出以下信息:************************** V ery Good! ************************** 解: mian() {printf(“**************************”); printf(“\n”); printf(“V ery Good!\n”); printf(“\n”); printf(“**************************”); } 1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。解: mian() {int a,b,c,max; printf(“请输入三个数a,b,c:\n”); scanf(“%d,%d,%d”,&a,&b,&c); max=a; if(max main() { char c1=?C?,c2=?h?,c3=?i?,c4=?n?,c5=?a?; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf("密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 运行结果: 密码是Glmre 3.9求下面算术表达式的值。 (1)x+a%3*(int)(x+y)%2/4 设x=2.5,a=7,y=4.7 (2)(float)(a+b)/2+(int)x%(int)y 设a=2,b=3,x=3.5,y=2.5 (1)2.5 (2)3.5 3.10写出程序运行的结果。 main() {int i,j,m,n; i=8; j=10; m=++i; n=j++; printf(“%d,%d,%d,%d”,i,j,m,n); } 解: 9,11,9,10 3.12 写出下面表达式运算后a的值,设原来a=12。设a和n都已定义为整型变量。 (1)a+=a (2)a-=2 (3)a*=2+3 (4)a/=a+a (5)a%=(n%=2),n的值等于5 (6)a+=a-=a*=a 解: (1) 24 (2) 10 (3) 60 (4) 0 (5) 0 (6) 0 第四章 4.4若a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1=’a’,c2=’b’。想得到以下输出格式和结果,请写出程序(包括定义变量类型和设计输出)。 a=_3_ _b=_4_ _c=_5 x=1.200000,y=2.400000,z=-3.600000 x+y=_3.600_ _y+z=-1.20_ _z+x=-2.40 c1=ˊaˊ_or_97(ASCII)

C程序设计第四版谭浩强完整版课后习题答案

C程序设计第四版谭浩强完整版课后习题答案 Document serial number【LGGKGB-LGG98YT-LGGT8CB-LGUT-

C程序设计(第四版)(谭浩强)第一章课后习题答案 #include<>代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to \n"); return 0; } #include<> int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum); return 0; } P008 调用函数比较两个数的大小. #include<> int main() { int max(int x,int y); int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c); return 0; } int max(int x,int y) { int z; if (x>y) z=x; else z=y; return(z); }

P015 三个数的大小.(数字0表示课后练习题) #include<> int main() { int a,b,c,d; int max(int x , int y , int z); printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); printf("The max is :%d .\n",d); } int max(int x , int y , int z) { int m; if (x>y && x>z) m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); } C程序设计(第四版)(谭浩强)第2章课后 习题答案 算法——程序的灵魂 P017 计算机1-5相乘的积. #include<> int main() { int i,s=1; for(i=1;i<6;i++) { s=s*i; n",s); return 0; } #include<> int main() { int i,s=1; for(i=1;i<12;i++) 可以是i=i+2 { if(i%2!=0) s=s*i; else continue; }

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言1 什么是计算机程序1 什么是计算机语言1 语言的发展及其特点3 最简单的C语言程序5 最简单的C语言程序举例6 语言程序的结构10 运行C程序的步骤与方法12 程序设计的任务14 1-5 #include <> int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; } 1-6#include <> int main() {int a,b,c,max; printf("please input a,b,c:\n");

scanf("%d,%d,%d",&a,&b,&c); max=a; if (max

数据类型42 整型数据44 字符型数据47 浮点型数据49 怎样确定常量的类型51 运算符和表达式52 语句57 语句的作用和分类57 最基本的语句——赋值语句59 数据的输入输出65 输入输出举例65 有关数据输入输出的概念67 用printf函数输出数据68 用scanf函数输入数据75 字符数据的输入输出78 习题82 3-1 #include <> #include <> int main() {float p,r,n; r=; n=10; p=pow(1+r,n); printf("p=%f\n",p); return 0;

c面向对象程序设计课后习题答案(谭浩强版)

第一章5: #include using namespace std; int main() { cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."< using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; cout<<"a+b="; cout< using namespace std; int main() { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout<

else m=y; if (z using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< using namespace std; int main() {int add(int x,int y); int a,b,c; cin>>a>>b; c=add(a,b); cout<<"a+b="< using namespace std; int main() {void sort(int x,int y,int z); int x,y,z; cin>>x>>y>>z;

谭浩强C++课后习题答案

第1章 谭浩强C++课后习题答案 1.请根据你的了解,叙述C++ 的特点。C++ 对C有哪些发展? 【解】略。 2.一个C++的程序是由哪几部分构成的?其中的每一部分起什么作用? 【解】略。 3.从拿到一个任务到得到最终结果,一般要经过几个步骤? 【解】略。 4.请说明编辑、编译、连接的作用。在编译后得到的目标文件为什么不能直接运行? 【解】编译是以源程序文件为单位进行的,而一个完整的程序可能包含若干个程序文件,在分别对它们编译之后,得到若干个目标文件(后缀一般为.obj),然后要将它们连接为一个整体。此外,还需要与编译系统提供的标准库相连接,才能生成一个可执行文件(后缀为.exe)。不能直接运行后缀为.obj的目标文件,只能运行后缀为.exe的可执行文件。 5.分析下面程序运行的结果。 #include using namespace std; int main( ) { cout<<" This "<<" is "; cout<<" a "<<" C++ "; cout<<"program. " << endl; return 0; } 【解】输出的结果为 ThisisaC++program.

C++面向对象程序设计题解与上机指导 4 6.分析下面程序运行的结果。 #include using namespace std; int main( ) { int a,b,c; a=10; b=23; c=a+b; cout<<" a+b="; cout< using namespace std; int main( ) { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout<

C程序设计(第四版)(谭浩强)完整版 课后习题答案

C程序设计(第四版)(谭浩强)第一章课后习题答案 P006 1.1 向屏幕输出文字. #include//预编译. 代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to https://www.360docs.net/doc/743386816.html,\n"); return 0; //与int main对应,为了程序可移植性,建议全用int main + return 0;. } P008 1.2 求两个数的和. #include int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum); return 0; } P008 1.3 调用函数比较两个数的大小. #include int main() { int max(int x,int y); //被调用函数在主函数后面,用前先声明. int a,b,c; scanf("%d,%d",&a,&b); //输入时要按格式来,此处的逗号,用空格会发生错误. c=max(a,b); //a,b作为实参传入被调用函数中. printf("The max is %d .\n",c); return 0;

} int max(int x,int y) //定义了两个形参. { int z; //z属于局部变量,可与主函数中相同名字. if (x>y) z=x; else z=y; return(z); //z作为整个程序的出口值,赋给主函数中的c. } P015 0.6 三个数的大小.(数字0表示课后练习题) #include int main() { int a,b,c,d; //d是用于存储最大值的. int max(int x , int y , int z); //测试可知,在VS2008中,可以不预先声明. printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); //调用函数中有三个形参,这里需要传入三个实参,才可运算. printf("The max is :%d .\n",d); // d可以换成max(a,b,c). } int max(int x , int y , int z) { int m; if (x>y && x>z) //求三者之大的一种方法. m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); //返回值m给主函数中的d. } C程序设计(第四版)(谭浩强)第2章课 后习题答案 算法——程序的灵魂

c语言程序设计第三版谭浩强课后习题答案.doc

c 语言程序设计第三版谭浩强课后习题 答案 【篇一:c 语言程序设计第三版谭浩强课后习题答案完 整版】 ude stdio.h void main() { printf(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n); printf(very good! \n); printf(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n); } 1.6 #include stdio.h void main() { float max(float x,float y,float z); float a,b,c; printf( 请分别输入a,b,c:\n); scanf(%f,%f,%f,a,b,c); printf(max=%f,max(a,b,c)); } float max(float x,float y,float z) { float m,n; m=xy? x: y; n=mz? m: z; return(n); } 3.6 #include stdio.h void main() { char c1= ’a’,c2= ’b’,c3= ’1c0’1’,c4,c=5’= 1’16’; printf( “a%c b%c n”,c1,c2,c3); printf( “n”,c4,c5); }

3.9.1 #include stdio.h void main() { double x=2.5,y=4.7,z; int a=7; z=x+a%3*(int)(x+y)%2/4; printf( 该表达式的值为:%f,z); } 3.9.2 #include stdio.h void main() { int a=2,b=3; float x=3.5,y=2.5,z; z=(float)(a+b)/2+(int)x%(int)y; printf( 该表达式的值为:%f,z); } 4.5 #include stdio.h void main() { int a=5,b=7; double x=67.8564,y=-789.124; char c=a; long n=1234567; unsigned u=65535; printf(%d%d\n,a,b); printf(%3d%3d\n,a,b); printf(%f,%f\n,x,y); printf(%-10f,%- 10f\n,x,y); printf(%8.2f,%8.2f,%.4f,%.4f,%3f,%3f\n,x,y,x,y,x,y); printf(%e,%10.2e\n,x,y); printf(%c,%d,%o,%x\n,n,n,n); printf(%ld,%lo,%x\n,n,n,n); printf(%u,%o,%x,%d\n,u,u,u,u); printf(%s,%5.3s\n,computer,computer); } 4.6 #include stdio.h

C程序设计(第四版)_谭浩强_第四章_课后习题答案

选择结构程序设计 P086 4.1 一无二次方程求根的二分支. #include #include int main() { double a,b,c,disc,x1,x2,p,q; scanf("%lf %lf %lf",&a,&b,&c); disc=b*b-4*a*c; if(disc<0) //这是选择结构和其判断条件的示例. printf("This equation hasn't real roots\n"); else { p=-b/(2.0*a); q=sqrt(disc)/(2.0*a); x1=p+q; x2=p-q; printf("x1=%7.2f\nx2=%7.2f",x1,x2); } return 0; } P087 4.2 二个数按大小输出. #include int main() //此程序代表按大小顺序输出. { float a,b,t; scanf("%f %f",&a,&b); //出错时,注意检查这里是否按格式输入了.比如有个逗号. if(a>b) { t=a; a=b; b=t; } printf("%5.2f,%5.2f\n",a,b);

return 0; } P088 4.3 三个数按大小输出. #include int main() //此程序代表按大小顺序输出. { float a,b,c,t; scanf("%f %f %f",&a,&b,&c); if(a>b) //此处执行后,a为小者. { t=a; a=b; b=t; } if(a>c) //此处执行后,a为小者. { t=a; a=c; c=t; } if(b>c) //上面已经搞定a是最小者,现在对比得出次小者,并且已经归到变量中. { t=b; b=c; c=t; } printf("%5.2f,%5.2f%5.2f\n",a,b,c); return 0; } P099 4.4 判断输入字符,并最终按小写输出. #include int main() { char ch; scanf("%c",&ch);

语言程序设计第二版谭浩强章课后习题答案清华大学出版社

原题:打印出下题的结果 main() { int a=5,b=7; float x=,y=; char c='A'; long n=1234567; unsigned u=65535; printf("%d%d\n",a,b); printf("%3d%3d\n",a,b); printf("%f,%f\n",x,y); printf("%-10f,%-10f\n",x,y); printf("%,%,%4f,%4f,%3f,%3f\n",x,y,x,y,x,y); printf("%e,%\n",x,y); printf("%c,%d,%o,%x\n",c,c,c,c); printf("%ld,%lo,%x\n",n,n,n); printf("%u,%o,%x,%d\n",u,u,u,u); printf("%s,%\n","COMPUTER","COMPUTER"); } 结果: 57 5 7 , , , ,,,, +01, +02 A,65,101,41 1234567,4553207,d687 65535,177777,ffff,-1 COMPUTER, COM 4-6 原题: 用下面的scanf函数输入数据,使a=3,b=7,x=,y=,c1='A',c2='a'。问在键盘上如何输入main() { int a,b; float x,y; char c1,c2; scanf("a=%d,_b=%d",&a,&b); scanf("_%f_%e",&x,7y); scanf("_%c_%c",&c1,&c2);

答案: 输入格式为: a=3,b=7 A a 4-7 原题:用下面的scanf函数输入数据使a=10,b=20,c1='A',c2='a',x=,y= z=,请问在键盘上如何输入数据 scanf("%5d%5d%c%c%f%f%*f,%f",&a,&b,&c1,&c2,&x,&y,&z); 答案: 输入格式为: 10 , 友情提示:10与20之间是3个空格,而那个是随便一个浮点数即可。 4-8 原题: 设圆半径r=,圆柱高h=3,求圆周长,圆面积,圆球表面积,圆球体积,圆柱体积,用scanf输入数据,输出结果,输出时要求有文字说明,取小数点后2位数字,请编写程序。 答案: #define PI main() { int h; float r; float cubage1,cubage2,per,area,facearea; clrscr(); printf("please input the circle's radii\n"); scanf("%f",&r); clrscr(); printf("please input the cylinder's height\n"); scanf("%d",&h); per=2*PI*r; area=PI*r*r; facearea=4*PI*r*r; cubage1=4/3*PI*r*r*r; cubage2=area*h; clrscr();

c++程序设计谭浩强课后习题答案(完整版)

c++程序设计谭浩强课后习题答案(完整版) -CAL-FENGHAI-(2020YEAR-YICAI)_JINGBIAN

第八章 #include using namespace std; class Time {public: //成员改为公用的 int hour; int minute; int sec; }; Time t; void set_time(void) //在 main 函数之前定义 { cin>>t.hour; cin>>t.minute; cin>>t.sec; } void show_time(void) //在 main 函数之前定义 { cout< using namespace std; class Time {public: void set_time(void) {cin>>hour; cin>>minute; cin>>sec; } void show_time(void)

{cout< using namespace std; class Time {public: void set_time(void); void show_time(void); private: int hour; int minute; int sec; }; void Time::set_time(void) {cin>>hour; cin>>minute; cin>>sec; } void Time::show_time(void) {cout<

相关文档
最新文档