C语言程序设计_谭浩强_第三版习题答案

申明敬告: 本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。

文档介绍

C语言程序设计_谭浩强_第三版习题答案

1.5请参照本章例题,编写一个C程序,输出以下信息:**************************VeryGood!**************************解:mian(){printf(“**************************”);printf(“n”);printf(“VeryGood!n”);printf(“n”);printf(“**************************”);}1.6编写一个程序,输入a、b、c三个值,输出其中最大值。解:mian(){inta,b,c,max;printf(“请输入三个数a,b,c:n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(maxmain(){charc1=’C’,c2=’h’,c3=’i’,c4=’n’,c5=’a’;nc1+=4;c2+=4;c3+=4;c4+=4;c5+=4;printf("密码是%c%c%c%c%cn",c1,c2,c3,c4,c5);}运行结果:密码是Glmre3.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.53.10写出程序运行的结果。main(){inti,j,m,n;i=8;j=10;m=++i;n=j++;printf(“%d,%d,%d,%d”,i,j,m,n);}解:9,11,9,103.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=_5x=1.200000,y=2.400000,z=-3.600000x+y=_3.600__y+z=-1.20__z+x=-2.40c1=ˊaˊ_or_97(ASCII)c2=ˊbˊ_or_98(ASCII)main()n{inta=3,b=4,c=5;longintu=51274,n=128765;floatx=1.2,y=2.4,z=3.6;charc1=’a’,c2=’b’;printf("a=%2db=%2dc=%2dn",a,b,c);printf("x=%f,y=%f,z=%fn",x,y,z);printf("x+y=%5.2fy+z=%5.2fz+x=%5.2fn",x+y,y+z,z+x);printf("u=%6ldn=%9ldn",u,n);printf("%s%s%d%sn","c1=’a’","or",c1,"(ASCII)");printf("%s%s%d%sn","c2=’a’","or",c2,"(ASCII)");}4.7用scanf下面的函数输入数据,使a=3,b=7,x=8.5,y=71.82,c1=ˊAˊ,c2=ˊaˊ,问在键盘上如何输入?main(){inta,b;floatx,y;charc1c2;scanf("a=%d_b=%d",&a,&b);scanf("_x=%f_y=%e",&x,&y);scanf("_c1=%c_c2=%c",&c1,&c2);}a=3_b=7_x=8.5_y=71.82_c1=A_c2=a4.8设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用scanf输入数据,输出计算结果,输出时要求文字说明,取小数点后两位数字。请编程序。main(){floatr,h,C1,Sa,Sb,Va,Vb;scanf("%f,%f",&r,&h);C1=2*3.14*r;Sa=3.14*r*r;Sb=4*Sa;Va=4*3.14*r*r*r/3;Vb=Sa*h;printf("C1=%.2fn",C1);printf("Sa=%.2fnSb=%.2fnVa=%.2fnVb=%.2fn",Sa,Sb,Va,Vb);}4.9输入一个华氏温度,要求输出摄氏温度。公式为c=5(F-32)/9输出要求有文字说明,取位2小数。main(){floatF,c;scanf("%f",&F);c=5*(F-32)/9;nprintf("c=%.2f",c);}4.10编程序,用getchar函数读入两个字符给c1、c2,然后分别用函数和函数输出这两个字符。并思考以下问题:(1)变量c1、c2应定义为字符型或整形?抑二者皆可?(2)要求输出c1和c2值的ASCII码,应如何处理?用putchar函数还是printf函数?(3)整形变量与字符变量是否在任何情况下都可以互相代替?如:charc1,c2;与intc1,c2;是否无条件的等价?#include"stdio.h"main(){charc1,c2;c1=getchar();c2=getchar();putchar(c1);putchar(’n’);putchar(c2);putchar(’n’);}#include"stdio.h"main(){charc1,c2;c1=getchar();c2=getchar();printf("c1=%dc2=%dn",c1,c2);printf("c1=%cc2=%cn",c1,c2);}第五章5.1什么是算术运算?什么是关系运算?什么是逻辑运算?解:略。5.2C语言中如何表示“真”和“假”?系统如何判断一个量的“真”和“假”?解:设有一个逻辑表达式,若其结果为“真”,则以1表示;若其结果为“假”,则以0表示。但是判断一个逻辑量的值时,以0代表“真”,以非0代表“假”。例如3&&5的值为“真”,系统给出3&&5的值为1。5.3写出下面各逻辑表达式的值。设a=3,b=4,c=5。(1)a+b>c&&b==c(2)a||b+c&&b-c(3)!(a>b)&&!c||1(4)!(x=a)&&(y=b)&&0(5)!(a+b)+c-1&&b+c/2解:(1)0(2)1(3)1n(4)0(5)15.4有3个整数a、b、c,由键盘输入,输出其中最大的数。解:方法一#includemain(){inta,b,c;printf("请输入3个整数:");scanf("%d,%d,%d",&a,&b,&c);if(amain(){inta,b,c,temp,max;printf("请输入3个整数:");scanf("%d,%d,%d",&a,&b,&c);temp=(a>b)?a:b;max=(temp>c)?temp:c;printf("3个整数的最大数是%dn”,max);}5.5有一函数:写一程序,输入x值,输出y值。解:#includemain(){intx,y;printf("输入x:");scanf("%d",&x);if(x<1){y=x;nprintf("x=%3d,y=x=%dn",x,y);}elseif(x<10){y=2*x-1;printf("x=%3d,y=2*x-1=%dn",x,y);}else{y=3*x-11;printf("x=%3d,y=3*x-11=%dn",x,y);}}5.6给一个百分制成绩,要求输出等级’A’、’B’、’C’、’D’、’E’。90分以上为’A’,80~90分为’B’,70~79分为’C’,60分以下为’D’。解:程序如下:#includemain(){floatscore;chargrade;case2:printf("请输入学生成绩:");scanf("%f",&score);while(score>100||(score<0){printf("n输入有误,请重新输入:");scanf("%f",&score);}switch((int)(score/10)){case10:case9:grade=’A’;break;case8:grade=’B’;break;case7:grade=’C’;break;case6:grade=’D’;break;case5:case4:case3:case1:case0:grade=’E’;}printf("成绩是%5.1f,相应的等级是%c。n",score,grade);}说明:对输入的数据进行检查,如小于0或大于100,要求重新输入。(int)(score/10)的作用是将(score/10)的值进行强制类型转换,得到一个整型值。5.7给定一个不多于5位的正整数,要求:①求它是几位数;②n分别打印出每一位数字;③按逆序打印出各位数字。例如原数为321,应输出123。解:#includemain(){longintnum;intindiv,ten,hundred,thousand,ten_thousand,place;printf("请输入一个整数(0~99999):");scanf("%ld",&num);if(num>9999)place=5;elseif(num>999)place=4;elseif(num>99)place=3;elseif(num>9)place=2;elseplace=1;printf("place=%dn",place);ten_thousand=num/10000;thousand=num/1000%10;hundred=num/100%10;ten=num%100/10;indiv=num%10;switch(place){case5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv);printf("n反序数字为;");printf("%d%d%d%d%dn",indiv,ten,hundred,thousand,ten_thousand);break;case4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);printf("n反序数字为:");printf("%d%d%d%dn",indiv,ten,hundred,thousand);break;case3:printf("%d,%d,%d",hundred,ten,indiv);printf("n反序数字为:");printf("%d%d%dn",indiv,ten,hundred);break;case2:printf("%d,%d",ten,indiv);printf("n反序数字为:");printf("%d%dn",indiv,ten);break;case1:printf("%d",indiv);printf("n反序数字为:");printf("%dn",indiv);break;}}n5.8企业发放的奖金根据利润提成。利润I低于或等于10万元时,奖金可提成10%;利润高于10万元,低于20万元(1000001000000时,超过100万元的部分按1%提成。从键盘输入当月利润I,求应发放奖金总数。要求:(1)用if语句编程序;(2)用switch语句编程序。解:计算利润时,要特别注意不同利润的不同提成比例。例如,利润为15万元,其中有10万元按10%的比例提成,另外5万元则按7.5%提成。(1)用if语句编程序。#includemain(){longi;floatbonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;printf("请输入利润i:");scanf("%ld",&i);if(i<=100000)bonus=i*0.1;elseif(i<=200000)bonus=bon1+(i-100000)*0.075;elseif(i<=400000)bonus=bon2+(i-200000)*0.05;elseif(i<=600000)bonus=bon4+(i-400000)*0.03;elseif(i<=1000000)bonus=bon6+(i-600000)*0.015;elsebonus=bon10+(i-1000000)*0.01;printf(”奖金是%10.2fn",bonus);}此题的关键在于正确写出每一区间的奖金计算公式。例如利润在10万元至20万时,奖金应由两部分组成:①利润为10万元时应得的奖金。即100000ⅹ0.1;②10万元以上部分应得的奖金。即(num-100000)ⅹ0.075。同理,20万~40万这个区间的奖金也应由两部分组成:①利润为20万元时应得的奖金,即100000ⅹ0.1ⅹ10万ⅹ0.075;②20万元以上部分应得的奖金,即(num-200000)ⅹ0.05。程序中先把10万、20万、40万、60万、100万各关键点的奖金计算出来,即bon1、nbon2、bon4、bon6、hon10;然后再加上各区间附加部分的奖金。(2)用switch语句编程序。输入利润i,确定相应的提成等级branch根据branch确定奖金值0奖金=i*0.11奖金=bon1+(i-105)*0.0752奖金=bon2+(i-2*105)*0.0534奖金=bon4+(i-4*105)*0.0356奖金=bon6+(i-6*105)*0.01578910奖金=bon10+(i-106)*0.01输出奖金#includemain(){longi;floatbonus,bon1,bon2,bon4,bon6,bon10;intc;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;printf("请输入利润i:");scanf("%ld",&i);c=i/100000;if(c>10)c=10;switch(c){case0:bonus=1*0.1;break;case1:bonus=bon1+(i-100000)*0.075;break;case2:case3:bonus=bon2+(i-200000)*0.05;break;case4:case5:bonus=bon4+(i-400000)*0.03;break;case6:case7:case8:case9:bonus=bon6+(i-600000)*0.015;break;case10:bonus=bon10+(i-1000000)*0.01;n}printf("奖金是%10.2f",bonus);}5.9输入4个整数,要求按由大到小的顺序输出。解:此题采用依次比较的方法排出其大小顺序。在学习了循环和数组以后,可以有更多的排序方法。#includemain(){intt,a,b,c,d;printf("请输入4个整数:");scanf("%d,%d,%d,%d",&a,&b,&c,&d);printf("na=%d,b=%d,c=%d,d=%dn",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;}if(b>c){t=a;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,%dn",a,b,c,d);}5.10有4个圆塔,圆心分别为(2,2)、(-2,2)、(2,-2)、(-2,-2),圆半径为1。这4个塔的高度分别为10m。塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的高度为零)。程序如下:#includemain(){inth=10;floatx1=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-x1)*(x-x1)+(y-y1)*(y-y1);d2=(x-x2)*(x-x2)+(y+y2)*(y+y2);d3=(x+x3)*(x+x3)+(y-y3)*(y-y3);d4=(x+x4)*(x-x4)*(y+y4)*(y+y4);nif(d1>1&&d2>1&&d3>1&&d4>1)h=0;printf("该点高度为%dn",h);}第六章第六章循环控制6.1输入两个正整数m和n,求其最大公约数和最小公倍数。main(){longm,n,i=1,j,s;scanf("%ld,%ld",&m,&n);for(;i<=m&&i<=n;i++){if(m%i==0&&n%i==0)s=i;}if(m>=n)j=m;elsej=n;for(;!(j%m==0&&j%n==0);j++);printf("s=%ld,j=%ldn",s,j);}6.2输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。#include"stdio.h"main(){charc;inti=0,j=0,k=0,l=0;while((c=getchar())!=’n’){if(c>=65&&c<=90||c>=97&&c<=122)i++;elseif(c>=48&&c<=57)j++;elseif(c==32)k++;elsel++;}printf("i=%d,j=%d,k=%d,l=%dn",i,j,k,l);}6.3求Sn=a+aa+aaa+…+aa…aaa(有n个a)之值,其中a是一个数字。例如:2+22+222+2222+22222(n=5),n由键盘输入。#include"math.h"main(){intn,sum=0,i=1,s=2;scanf("%d",&n);while(i<=n){sum=sum+s;s=s+2*pow(10,i);i++;}printf("sum=%dn",sum);}6.4求,(即求1!+2!+3!+4!+5!+…+20!)main(){intn,i=1;longsum=0,s=1;scanf("%d",&n);nwhile(i<=n){s=s*i;sum=sum+s;i++;}printf("sum=%ldn",sum);}6.5求main(){doublei=1,j=1,k=1,s1=0,s2=0,s3=0,sum;for(;i<=100;i++)s1=s1+i;for(;j<=50;j++)s2=s2+j*j;for(;k<=10;k++)s3=s3+1/k;sum=s1+s2+s3;printf("sum=%fn",sum);}6.6打印出所有"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该本身。例如:153是一个水仙花数,因为153=1^3+5^3+3^3。#include"math.h"main(){intx=100,a,b,c;while(x>=100&&x<1000){a=0.01*x;b=10*(0.01*x-a);c=x-100*a-10*b;if(x==(pow(a,3)+pow(b,3)+pow(c,3)))printf("%5d",x);x++;}}6.7一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。编程序找出1000之内的所有完数,并按下面格式输出其因子:6itsfactorsare1、2、3main(){intm,i,j,s;for(m=6;m<10000;m++){s=1;for(i=2;i=0.00001);printf("%.3fn",x1);}6.12用牛顿迭代法求方程在1.5附近的根。nmain(){doublex,y;x=1.5;do{y=2*x*x*x-4*x*x+3*x-6;x=x-y/(6*x*x-8*x+3);}while(y!=0);printf("x=%.3fn",x);}6.13用二分法求方程在(-10,10)之间的根main(){doublex1,x2,y1,y2;x1=-10;x2=10;do{y1=2*x1*x1*x1-4*x1*x1+3*x1-6;x1=x1-y1/(6*x1*x1-8*x1+3);}while(y1!=0);do{y2=2*x2*x2*x2-4*x2*x2+3*x2-6;x2=x2-y2/(6*x2*x2-8*x2+3);}while(y2!=0);printf("x1=%.3f,x2=%.3fn",x1,x2);}6.14打印以下图案*************************#include"math.h"main(){inti,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("");nfor(k=0;k<=4-2*i;k++)printf("*");printf("n");}}第七章第七章数组7.1用筛法求之内的素数。main(){inti,j,a[100];for(i=2;i<100;i++){a[i]=i;for(j=2;j<=i;j++){if(j=k+1)printf("%5d",a[i]);}printf("n");}7.2用选择法对10个整数从小到大排序。main(){inti,j,a[10],t;for(i=0;i<10;i++)scanf("%d",&a[i]);for(j=1;j<10;j++)for(i=0;i<=9-j;i++)nif(a[i]>a[i+1]){t=a[i+1];a[i+1]=a[i];a[i]=t;}for(i=0;i<10;i++)printf("%5d",a[i]);}或main(){staticinta[10],i,j,k,t;for(i=1;i<11;i++)scanf("%d",&a[i]);for(j=1;j<10;j++)for(i=1;i<=10-j;j++)if(a[i]>a[i+1]){t=a[i+1];a[i+1]=a[i];a[i]=t;}for(i=1;i<11;i++)printf("%d",a[i]);printf("n");}7.3求一个3×3矩阵对角线元素之和。main(){inti=0,j=0,a[3][3],s1,s2;for(i=0;i<3;i++)for(j=0;j<3;j++)scanf("%d",&a[i][j]);s1=a[0][0]+a[1][1]+a[2][2];s2=a[0][2]+a[1][1]+a[2][0];printf("s1=%d,s2=%dn",s1,s2);}或main(){staticinti,j,s1,s2,a[3][3];for(i=1;i<=3;i++)for(j=1;j<=3;j++)scanf("%d",&a[i][j]);s1=a[1][1]+a[2][2]+a[3][3];s2=a[1][3]+a[2][2]+a[3][1];printf("%d,%dn",s1,s2);}7.4已有一个已排好的数组今输入一个数要求按原来排序的规律将它插入数组中。main(){staticinta[10]={1,7,8,17,23,24,59,62,101};inti,j,t;scanf("%d",&a[9]);for(i=9;i>0;i--)nif(a[i]-1;i--)printf("%5d",b[i]);printf("n");}7.6打印出以下杨辉三角形(要求打印出10行)。11112113311464115101051∶∶main(){staticintm,n,k,b[15][15];b[0][1]=1;for(m=1;m<15;m++){for(n=1;n<=m;n++){b[m][n]=b[m-1][n-1]+b[m-1][n];nprintf("%-5d",b[m][n]);}printf("n");}}}或main(){inti,j,n,k,a[10][10];statica[][1]={{1},{1},{1},{1},{1},{1},{1},{1},{1},{1}};a[1][1]=1;for(k=2,k<11;k++)for(i=2;i<=k;i++)for(j=2;j<=i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(k=1;k<11;k++)for(i=1;i<=k;i++)for(j=1;j<=i;j++)printf("%d",a[i][j]);}7.7打印“魔方阵”,所谓魔方阵是指这样的方阵,它的每一行、每一列和对角线之和均相等。例如,三阶魔方阵为816357492要求打印出由1~n2的自然数构成的魔方阵。解:#includemain(){inta[16][16],i,i,k,p,m,n;p=1;while(p==1){printf("Entern(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]=07-8#includevoidmain(){n inta[4][4],i,j,max,min,k1,k2,flag; printf("输入一个4*4的数组nn"); for(i=0;i<4;i++) {  for(j=0;j<4;j++)  {   scanf("%d",&a[i][j]);  } } printf("鞍点为:nn"); for(i=0;i<4;i++) {  max=a[i][0];  for(j=0;j<4;j++)  {   if(max<=a[i][j])   {    max=a[i][j];    k1=j;   }  }  min=a[0][k1];  for(j=0;j<4;j++)  {   if(min>=a[j][k1])   {    min=a[j][k1];    k2=j;   }  }  if((max!=min))flag=0;  else  {   flag=1;   printf("%dn",max);   break;  } } if(flag==0)printf("不存在nn");}  7-9#includenvoidmain(){ floata[15]={100,98,65,55,34,23,23,12,11,9,7,6,4,3,2},n; inti,low,high,mid; printf("原数组为:nn"); for(i=0;i<15;i++) {  printf("%6.2f",a[i]); } printf("nn输入一个数nnn"); scanf("%f",&n); low=0; high=14; i=0; while(low<=high) {  mid=(low+high)/2;  if(a[mid]==n)  {   printf("该数是数组中第%d个元素nn",mid+1);   i++;   break;  }  elseif(a[mid]voidmain(){ chara[3][80]; inti,j,upon=0,low=0,num=0,spa=0,oth=0; for(i=0;i<3;i++) {  printf("输入第%d行文字nn",i);  gets(a[i]);  for(j=0;j<80&&a[i][j]!='';j++)  {   if(a[i][j]>='A'&&a[i][j]<='Z')upon+=1;   elseif(a[i][j]>='a'&&a[i][j]<='z')low+=1;n   elseif(a[i][j]>=48&&a[i][j]<=57)num+=1;   elseif(a[i][j]=='')spa+=1;   elseoth+=1;  } } printf("英文大写字母:%d个nn",upon); printf("英文小写字母:%d个nn",low); printf("数字:       %d个nn",num); printf("空格:       %d个nn",spa); printf("其他字符:   %d个nn",oth);}  7-11#includevoidmain(){ inti,j; for(i=0;i<5;i++) {  for(j=0;j<=i;j++)  {   printf(" ");  }  for(j=i;jvoidmain(){ chara[80],b[80]; inti,j; printf("输入密码nn"); gets(a); for(i=0;i<80&&a[i]!='';i++) {  if((a[i]>='A')&&(a[i]<='Z'))n  {   b[i]=155-a[i];  }  elseif((a[i]>='a')&&(a[i]<='z'))  {   b[i]=219-a[i];  }  elseb[i]=a[i]; } b[i]=''; printf("密文为:nn"); puts(a); printf("原文为:nn"); puts(b);}  7-13#includevoidmain(){ chara[80],b[80]; inti,j,k; printf("输入第1个字符串nn"); gets(a); printf("输入第2个字符串nn"); gets(b); for(i=0;i<80;i++) {  if(a[i]=='')  {   k=i;   for(j=0;j<80&&b[j]!='';j++)   {    a[k]=b[j];    k++;   }   a[k]='';   break;  } } printf("连接后字符串为:nn"); puts(a);}n  7-14#includevoidmain(){ chara[80],b[80]; inti,j,k; printf("输入第1个字符串nn"); gets(a); printf("输入第2个字符串nn"); gets(b); for(i=0;(i<80)&&(a[i]!='')&&(b[i]!='');i++) {  k=a[i]-b[i];  if(k>0)  {   printf("nn字符串a大于字符串b:%4dnn",k);   break;  }  elseif(k<0)  {   printf("nn字符串a小于字符串b:%4dnn",k);   break;  }  elsek=0; } if(k==0)printf("nn字符串a等于字符串b:%4dnn",k);}  7-15#includevoidmain(){ chars1[80],s2[80]; inti,j; printf("输入数组s1nn"); gets(s1); printf("输入数组s2nn"); gets(s2); for(i=0;i<80&&s2[i]!='';i++) {  s1[i]=s2[i];n } s1[i]=''; printf("nn复制后的数组为:nn"); puts(s1);}第八章 8-1#includevoidmain(){ intmax_div(int,int); intmin_mul(int,int); intm,n; printf("输入两个整数nn"); scanf("%d%d",&m,&n); if(max_div(m,n)==0)printf("nn无最大公约数为nn"); elseprintf("nn最大公倍数为:%dnn",max_div(m,n)); printf("nn最大公倍数为:%dnn",min_mul(m,n));}intmax_div(intk1,intk2){ intmin(int,int); inti,k,flag; k=min(k1,k2); for(i=k;i>1;i--) {  if((k1%i==0)&&(k2%i==0))  {   return(i);   flag=1;   break;  }  elseflag=0; } if(flag==0)return(0);}intmin_mul(intk1,intk2){ intmax(int,int); inti,k; k=max(k1,k2); for(i=k;;i++) {n  if((i%k1==0)&&(i%k2==0))  {   return(i);   break;  } }}intmin(intk1,intk2){ intmin; min=k1; if(k1>k2)min=k2; return(min);}intmax(intk1,intk2){ intmax; max=k1; if(k1#includevoidmain(){ voidroot1(float,float,float); voidroot2(float,float,float); voidroot3(float,float,float); floata,b,c,k; printf("输入a,b,c的值nn"); scanf("%f%f%f",&a,&b,&c); k=b*b-4*a*c; if(k>0)root1(a,b,c); elseif(k==0)root2(a,b,c); elseroot3(a,b,c);}voidroot1(floata,floatb,floatc){ floatp,q; p=-b/(2*a); q=sqrt(b*b-4*a*c)/(2*a); printf("nn方程的两个根为:x1=%6.2f,x2=%6.2fnn",p+q,p-q);n}voidroot2(floata,floatb,floatc){ floatp; p=-b/(2*a); printf("nn方程的两个根为:x1=x2=%6.2fnn",p);}voidroot3(floata,floatb,floatc){ floatp,q; p=-b/(2*a); q=sqrt(-b*b+4*a*c)/(2*a); printf("nn方程的两个根为:x1=%6.2f+%6.2fi,x2=%6.2f-%6.2finn",p,q,p,q);} 8-3#includevoidmain(){ intprime(int); intx; printf("输入一个正整数nn"); scanf("%d",&x); if(prime(x)==0)printf("nn此数为素数nn"); elseprintf("nn此数不是素数nn"); }intprime(intx){ inti,flag; for(i=2;ivoidmain()n{ voidexchange(inta[3][3]); inta[3][3],i,j; printf("输入一个3*3的矩阵nn"); for(i=0;i<3;i++) {  for(j=0;j<3;j++)  {   scanf("%d",&a[i][j]);  } } exchange(a); printf("nn转置后的矩阵为:nn"); for(i=0;i<3;i++) {  for(j=0;j<3;j++)  {   printf("%d",a[i][j]);  }  printf("n"); }} 8-5#includevoidmain(){ voidexchange(charstr[]); charstr[80]; printf("输入一个字符串nn"); gets(str); exchange(str); printf("nn反序字符串为:nn"); puts(str);}voidexchange(charstr[]){ inti,j,k; chartemp; for(i=0;;i++) {  if(str[i]=='')break; } k=i/2;n for(j=0;j<=k;j++) {  temp=str[j];  str[j]=str[i-1-j];  str[i-1-j]=temp; } str[i]='';} 8-6#includevoidmain(){ voidstrcat(charstr1[],charstr2[]); charstr1[80],str2[80]; printf("输入str1nn"); gets(str1); printf("nn输入str2nn"); gets(str2); strcat(str1,str2); printf("nn连接后的字符串为:nn"); puts(str1);}voidstrcat(charstr1[],charstr2[]){ inti,j; for(i=0;;i++) {  if(str1[i]=='')  {   for(j=0;str2[j]!='';j++)   {    str1[i]=str2[j];    i+=1;   }   break;  } } str1[i]='';} 8-7#includevoidmain()n{ voidstrcpy(charstr1[],charstr2[]); charstr1[80],str2[80]; printf("输入str2nn"); gets(str2); strcpy(str1,str2); printf("nn复制后的字符串为:nn"); puts(str1);}voidstrcpy(charstr1[],charstr2[]){ inti,j=0; for(i=0;str2[i]!='';i++) {  if((str2[i]=='a')||(str2[i]=='e')||(str2[i]=='i')||(str2[i]=='o')||(str2[i]=='u'))  {   str1[j]=str2[i];   j++;  } } str1[j]='';} 8-8#includevoidmain(){ voidstradd(charstr[]); charstr[80]; printf("输入一串数字nn"); gets(str); stradd(str); printf("nn加空格后的字符串nn"); puts(str);}voidstradd(charstr[]){ chara[80]; inti=0,j;   for(j=0;str[j]!='';j++) {  a[i]=str[j];  a[i+1]='';  i+=2;n } a[i]=''; for(i=0;a[i]!='';i++) {  str[i]=a[i]; } str[i]='';} 8-9#includevoidmain(){ voidnum(charstr[],inta[]); charstr[80]; inta[4]={0}; printf("输入一个字符串nn"); gets(str); num(str,a); printf("字母个数为:   %dn",a[0]); printf("数字个数为:   %dn",a[1]); printf("空格个数为:   %dn",a[2]); printf("其它字符个数为:%dn",a[3]);}voidnum(charstr[],inta[]){ inti,word=0,num=0,span=0,oth=0; for(i=0;str[i]!='';i++) {  if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))word+=1;  elseif(str[i]>=48&&str[i]<=57)num+=1;  elseif(str[i]=='')span+=1;  elseoth+=1; } a[0]=word; a[1]=num; a[2]=span; a[3]=oth;} 8-10#includevoidmain(){n intword(charc); intlongest(charstr[]); inti; charstr[80]; printf("输入一行字符串nn"); gets(str); printf("nn最长的单词为:nn"); for(i=longest(str);word(str[i]);i++)  printf("%c",str[i]); printf("n");}intword(charc){ if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))return1; elsereturn0;} intlongest(charstr[]){ inti,flag=1,len=0,point,max_len=0,max_point; for(i=0;i<=strlen(str);i++) {  if(word(str[i]))  {   if(flag==1)   {    point=i;    flag=0;   }   elselen++;  }  else  {   flag=1;   if(len>max_len)   {    max_len=len;    max_point=point;   }   len=0;  } } return(max_point);}n 8-11#includevoidmain(){ charstr[80]; voidsort(charstr[]); printf("输入一个字符串nn"); gets(str); sort(str); printf("nn字符由小到大排序为:nn"); puts(str);}voidsort(charstr[]){ inti,j; chartemp; for(i=0;istr[j+1])   {    temp=str[j];    str[j]=str[j+1];    str[j+1]=temp;   }  } }}  8-12#include#includevoidmain(){ inta,b,c,d; floatroot(inta,intb,intc,intd); printf("输入a,b,c,dnn"); scanf("%d%d%d%d",&a,&b,&c,&d); printf("nn在1附近的根为:%fnn",root(a,b,c,d));}floatroot(inta,intb,intc,intd)n{ floatx=1,x1,f,f1; do {  x1=x;  f=((a*x1+b)*x1+c)*x1+d;  f1=(a*x1+b)*x1+c;  x=x1-f/f1; }while(fabs(x-x1)>=1e-5); return(x);} 8-13#includevoidmain(){ floatp(int,float); intn; floatx; printf("输入n和xnn"); scanf("%d%f",&n,&x); printf("nn%d阶勒德让多项式的值为:%fnn",n,p(n,x));}floatp(intn,floatx){ if(n==0)return1; elseif(n==1)returnx; elsereturn((2*n-1)*x-p(n-1,x)-(n-1)*p(n-2,x))/n;}  8-14#include#definen12#definen25floata_stu[n1],a_cou[n2];voidmain(){ voidinput(floata[][n2]); voidstu_aver(floata[][n2]); voidcou_aver(floata[][n2]); inthightest(int*s,int*c,floata[][n2]); floatd(floata[][n2]); inti,j,s=0,c=0;n floatscore[n1][n2],h; input(score); stu_aver(score); cou_aver(score); printf("nn 序号   课程1    2    3    4    5    平均成绩nn"); for(i=0;ihightest)   {    hightest=a[i][j];    *s=i+1;    *c=j+1;   }  } } return(hightest);}floatd(floata[][n2]){ inti; floatx=0,y=0; for(i=0;i#defineN3voidmain(){ voidinput(intnum[],charname[N][8]); voidsort(intnum[],charname[N][8]); voidsearch(intnum[],charname[N][8],intn); intflag,n,num[N]; charc,name[N][8]; input(num,name); sort(num,name);  for(flag=1;flag;) {  printf("输入要查找的职工号nn");  scanf("%d",&n);  search(num,name,n);  printf("是否继续查找?Y/N?nn");  c=getchar();  if(c=='N'||c=='n')flag=0; }}voidinput(intnum[],charname[N][8]){ inti; for(i=0;inum[j])min=j;  }  temp1=num[i];  strcpy(temp2,name[i]);  num[i]=num[min];  strcpy(name[i],name[min]);  num[min]=temp1;  strcpy(name[min],temp2); } printf("排序结果如下:nn"); for(i=0;inum[high]))flag=-1; while((low<=high)&&(flag==0)) {  mid=(low+high)/2;  if(n==num[mid])  {   flag=mid+1;   printf("第%4d号职工的名字为:%8snn",n,name[mid]);   break;  }  elseif(n#defineN11000voidmain(){ intexchange(chara[]); inti=0,flag=0,flag1=1; charc,a[N1]; printf("输入一个十六进制数nn"); while(((c=getchar())!='')&&(i='0')&&(c<='9'))||((c>='a')&&(c<='f'))||((c>='A')&&(c<='F')))  {   a[i++]=c;   flag=1;  }       elseif(flag==1)  {   a[i]='';   printf("对应的十进制数为:%dnn",exchange(a));   printf("是否继续?Y/N?nn");   c=getchar();   if(c=='N'||c=='n')flag1=0;   else   {    flag=0;    i=0;    printf("输入一个十六进制数nn");   }  } }}intexchange(chara[]){ inti,n=0; for(i=0;a[i]!='';i++) {  if((a[i]>='0')&&(a[i]<='9'))n=n*16+a[i]-'0';  if((a[i]>='a')&&(a[i]<='f'))n=n*16+a[i]-'a'+10;  if((a[i]>='A')&&(a[i]<='F'))n=n*16+a[i]-'A'+10; } return(n);} 8-17n#includevoidmain(){ voidexchange(intn); intn; printf("输入一个整数nn"); scanf("%d",&n); printf("输出是:nn"); if(n<0) {  putchar('-');  n=-n; } exchange(n); printf("nn");}voidexchange(intn){ inti; if((i=n/10)!=0)exchange(i); putchar(n%10+'0');} 8-18#includevoidmain(){ intday_num(intyear,intmon,intday); intyear,mon,day; printf("输入年、月、日nn"); scanf("%d%d%d",&year,&mon,&day); printf("nn输入的日期是当年的第%d天nn",day_num(year,mon,day));}intdate[]={31,28,31,30,31,30,31,31,30,31,30,31};intday_num(intyear,intmon,intday){ inti,days=0; for(i=0;i#defineexchange(x,y)t=x;x=y;y=tvoidmain(){ intx,y,t; printf("输入x,ynn"); scanf("%d%d",&x,&y); exchange(x,y); printf("nn交换后x=%d,y=%dnn",x,y);} 9-2#include#defineacr(x,y)((x)%(y))voidmain(){ intx,y; printf("输入x,ynn"); scanf("%d%d",&x,&y); printf("nn余数为%dnn",acr(x,y));} 9-3#include#include#defines(a,b,c)((a+b+c)/2)#definearea(a,b,c,s)sqrt(s*(s-a)*(s-b)*(s-c))voidmain(){ floata,b,c,s,area; charm; intflag=1; while(flag==1) {  printf("输入a,b,cnn");  scanf("%f%f%f",&a,&b,&c);  if(a>0&&b>0&&c>0&&(a+b>c||a+c>b||b+c>a)&&(fabs(a-b)#defineLEAP_YEAR(y)((y%4==0)&&(y%100)!=0||(y%400)==0)来源:(http://blog.sina.com.cn/s/blog_48b65ecd0100cu2x.html)-谭浩强 C语言程序设计(第三版)课后习题答案_水若寒冰_新浪博客voidmain(){ inty; printf("输入年份nn"); scanf("%d",&y); if(LEAP_YEAR(y))printf("nn%d年是闰年nn",y); elseprintf("nn%d年不是闰年nn",y);} 9-6#include#definePRprintf#defineNL"nn"#defineD"%f"#defineD1DNL#defineD2DDNL#defineD3DDDNLvoidmain(){ floatx=1; PR(D1,x); PR(D2,x,x); PR(D3,x,x,x);}n 9-7#include#include"format.h"voidmain(){ intx=1; floaty=2; charz[]="Iloveyou"; PR(D1,x); PR(F1,y); PR(S1,z);} 9-8#includevoidmain(){ floatx,y,z,t; printf("输入三个数x,y,znn"); scanf("%f%f%f",&x,&y,&z); t=(x>y)?x:y; t=(t>z)?t:z; printf("nn三个数中最大的为:%fnn",t);} #include#defineMAX(a,b)((a)>(b)?(a):(b))voidmain(){ floatx,y,z; printf("输入三个数x,y,z:nn"); scanf("%f%f%f",&x,&y,&z); printf("nn其中最大的数为:%fnn",MAX(MAX(x,y),z));} 9-10#include#defineN80#defineCHANGE1voidmain(){ charstr[N]; inti;n printf("输入一行字符串nn"); gets(str);#ifCHANGE for(i=0;i='a')&&(str[i]<='z'))||((str[i]>='A')&&(str[i]<='Z')))  {   if((str[i]=='z')||(str[i]=='Z'))str[i]=str[i]-25;   elsestr[i]=str[i]+1;  } } str[i]=''; puts(str);#elseputs(str);#endif}第十章 10.1#includevoidmain(){ voidswap(int*p,int*q); intx,y,z; int*p1,*p2,*p3; printf("输入三个整数nn"); scanf("%d%d%d",&x,&y,&z); p1=&x; p2=&y; p3=&z; if(x>y)swap(p1,p2); if(x>z)swap(p1,p3); if(y>z)swap(p2,p3); printf("nn三个数由小到达输出为:%d%d%dnn",x,y,z);}voidswap(int*p,int*q){ inttemp; temp=*p; *p=*q; *q=temp;}  n10.2#include#include#defineN80voidmain(){ voidswap(char*p,char*q); charstr1[N],str2[N],str3[N]; printf("请输入三个字符串nn"); scanf("%s",str1); scanf("%s",str2); scanf("%s",str3); if(strcmp(str1,str2)>0)swap(str1,str2); if(strcmp(str1,str3)>0)swap(str1,str3); if(strcmp(str2,str3)>0)swap(str2,str3); printf("nn三个字符串由小到大排列,分别为:nn%sn%sn%sn",str1,str2,str3);}voidswap(char*p,char*q){ charstr[N]; strcpy(str,p); strcpy(p,q); strcpy(q,str);}  10.3#include#defineN10voidmain(){ voidinput(intnum[N]); voidswap(int*p,int*q); voidexchange(intnum[N]); voidoutput(intnum[N]); intnum[N]; input(num); exchange(num); output(num);}voidinput(intnum[N]){ inti;n printf("输入十个整数nn"); for(i=0;i#defineN10#defineM3voidmain(){ voidmove(intnum[N],intn,intm); intnum[N]; inti; printf("输入%d个整数nn",N); for(i=0;i=num;p--)n  *p=*(p-1); *num=temp; m--; if(m>0)move(num,n,m);} 10.5#include#defineN8voidmain(){ voidleave(intnum[N]); intnum[N]; inti,*p; p=num; for(i=0;i#defineN10nvoidmain(){ intcount(charstr[N]); inti; charstr[N]; printf("输入一个字符串nn"); gets(str); printf("nn字符串长度为:%dnn",count(str));}intcount(charstr[N]){ char*p; inti=0; p=str; while(*p!='') {  i++;  p++; } return(i);}  10.7#include#defineN80#defineM4voidmain(){ voidcopy(charstr1[N],charstr2[N]); charstr1[N],str2[N]; printf("输入字符串str1nn"); gets(str1); printf("nn输入字符串str2nn"); gets(str2); copy(str1,str2); printf("nn整合之后的字符串为:nn"); puts(str1);}voidcopy(charstr1[N],charstr2[N]){ char*p1,*p2; inti; p1=str1;n p2=str2; for(i=0;i#defineN80voidmain(){ voidcount(charstr[N],int*cap,int*low,int*space,int*num,int*oth); charstr[N]; inta=0,b=0,c=0,d=0,e=0; int*cap,*low,*space,*num,*oth; cap=&a; low=&b; space=&c; num=&d; oth=&e; printf("输入一个字符串nn"); gets(str); count(str,cap,low,space,num,oth); printf("nn大写字母个数为:%dnn小写字母个数为:%dnn空格个数位:%dnn数字个数为:%dnn其他字符个数为:%dnn",*cap,*low,*space,*num,*oth);}voidcount(charstr[N],int*cap,int*low,int*space,int*num,int*oth){ inti; for(i=0;str[i]!='';i++) {  if(str[i]>='A'&&str[i]<='Z')(*cap)++;  elseif(str[i]>='a'&&str[i]<='z')(*low)++;  elseif(str[i]=='')(*space)++;  elseif(str[i]>='0'&&str[i]<='9')(*num)++;  else(*oth)++; }}  10.9#includen#defineN3voidmain(){ voidmove(int*p); inta[N][N]; int*p,i,j; printf("输入一个%d*%d的矩阵nn",N,N); for(i=0;i#defineN3来源:(http://blog.sina.com.cn/s/blog_48b65ecd0100dco7.html)-谭浩强 C语言程序设计(第三版)课后习题答案_水若寒冰_新浪博客voidmain(){ voidswap(int*p,int*q); voidsort(int*p); intarr[N][N];n inti,j,*p; printf("输入一个%d*%d的矩阵nn",N,N); for(i=0;i*max)max=p+i*N+j;   if(*(p+i*N+j)<*min)min=p+i*N+j;  } } swap(max,(p+(N-1)*(N+1)/2)); swap(min,p);n min=p+1; for(i=0;i#defineN5voidmain(){ voidswap(char*p,char*q); voidsort(char*p); charstr[N][N],*p; inti; printf("输入%d个字符串nn",N); for(i=0;i0)swap((p+i*N),(p+j*N));  } }}  10.12#include#defineN5#defineM10voidmain(){ voidsort(char*p[N]); charstr[N][M],*p[N]; inti; printf("输入%d个字符串nn",N); for(i=0;i0)   {    q=p[i];    p[i]=p[j];    p[j]=q;   }  } }}  10.13#include#includevoidmain(){ floatintegral(floata,floatb,intn,double(*fun)(double)); intn=100; floata,b,c; double(*fun)(double); printf("输入积分范围:(a,b)nn"); scanf("%f%f",&a,&b); fun=sin; c=integral(a,b,n,fun); printf("nnsin积分结果为:%fnn",c); fun=cos; c=integral(a,b,n,fun); printf("nncos积分结果为:%fnn",c); fun=exp; c=integral(a,b,n,fun); printf("nnexp积分结果为:%fnn",c);}floatintegral(floata,floatb,intn,double(*fun)(double)){ inti;n floatx,h,area; h=(b-a)/n; x=a; area=0; for(i=0;i#defineN20voidmain(){ voidswap(int*p,int*q); voidback(int*p,intn); inta[N],i,n; printf("输入个数nnn"); scanf("%d",&n); printf("nn输入%d个整数nn",n); for(i=0;i#defineM4#defineN5voidmain(){ voidaver_count1(char*cou,float*sco); voidaver_stu(float*sco,float*ave); voidfail2(char*cou,int*num,float*sco,float*ave); voidgood(char*cou,int*num,float*sco,float*ave); charcourse[N][10],*cou; floatscore[M][N],aver[M],*sco,*ave; intnumber[M],*num,i,j; cou=course[0]; printf("输入%d门课程名nn",N); for(i=0;i2)  {   printf("%-8d",*(num+i));   for(k=0;k=85)flag+=1;  }  if(flag==5||(*(ave+i)>=90))  {   printf("%-8d",*(num+i));   for(k=0;k#defineN50voidmain(){ charstr[N],*p; inti,j,temp; intnumber[N],*num; printf("输入一个字符串nn"); gets(str); p=str;n temp=0; j=0; num=number; for(i=0;(*(p+i))!=''&&i='0'&&(*(p+i))<='9')  {   if((*(p+i+1))>='0'&&(*(p+i+1))<='9')    temp=temp*10+*(p+i)-48;   else   {    temp=temp*10+*(p+i)-48;    *(num+j)=temp;    temp=0;    j++;   }  } } printf("nn字符串中共有%d个整数,它们分别为:nn",j); for(i=0;i#defineN10voidmain(){ intstrcmp(char*p1,char*p2); charstr1[N],str2[N]; char*p1,*p2; printf("输入字符串str1nn"); gets(str1); printf("nn输入字符串str2nn"); gets(str2); p1=str1; p2=str2; printf("nn%dnn",strcmp(p1,p2));}intstrcmp(char*p1,char*p2){n inti,flag=0; for(i=0;*(p1+i)!=''&&*(p2+i)!='';i++) {  if(*(p1+i)==*(p2+i))flag=0;  else  {   flag=*(p1+i)-*(p2+i);   break;  } } returnflag;} 10.18#includevoidmain(){ char*mon[]={"一月份","二月份","三月份","四月份","五月份","六月份","七月份","八月份","九月份","十月份","十一月份","十二月份"}; intn; printf("输入一个月份号nn"); scanf("%d",&n); if(n>=1&&n<=12)printf("nn%snn",mon[n-1]); elseprintf("nn%d月份不存在nn",n);} 10.20#include#defineN5#defineMAX100voidmain(){ voidsort(char**p); char*pstr[N],**p,str[N][MAX]; inti; for(i=0;i0)   {    temp=*(p+i);    *(p+i)=*(p+j);    *(p+j)=temp;   }  } }}  10.21#include#defineN100voidmain(){ voidsort(int**p,intn); inti,n; int*pnum[N],num[N],**p; printf("输入整数个数nn"); scanf("%d",&n); for(i=0;i**(p+j))   {    temp=*(p+i);    *(p+i)=*(p+j);    *(p+j)=temp;   }  } }}第十一章 11.1#includestructdate{ intyear; intmonth; intday;}date;voidmain(){ staticintday_table[]={31,28,31,30,31,30,31,31,30,31,30,31}; inti,day_sum; printf("输入年,月,日nn"); scanf("%d%d%d",&date.year,&date.month,&date.day); day_sum=0; for(i=0;i2))  day_sum+=1;n printf("nn该日在本年中是第%d天nn",day_sum);}  11.2#includestructdate{ intyear; intmonth; intday;}date;voidmain(){ intdays(inty,intm,intd); printf("输入年,月,日nn"); scanf("%d%d%d",&date.year,&date.month,&date.day); printf("nn该日在本年中是第%d天nn",days(date.year,date.month,date.day));}intdays(inty,intm,intd){ staticintday_table[]={31,28,31,30,31,30,31,31,30,31,30,31}; inti,day_sum; day_sum=0; for(i=0;i2))  day_sum+=1; returnday_sum;} 11.3#include#defineN2structstu{ intnum; charname[10]; floatscore[3];}stu[N];voidmain(){n voidprint(structstu*p); inti,j; structstu*p; p=stu; for(i=0;inum));  printf("n输入姓名:");  scanf("%s",(p+i)->name);  for(j=0;j<3;j++)  {   printf("n输入成绩%d:",j+1);   scanf("%f",&((p+i)->score[j]));  } } print(stu);}voidprint(structstu*p){ inti,j; printf("学号  姓名  成绩1  成绩2  成绩3nn"); for(i=0;inum,(p+i)->name);  for(j=0;j<3;j++)  {   printf("%-8f",(p+i)->score[j]);  }  printf("nn"); }}  11.4#include#defineN2structstu{ intnum; charname[10]; floatscore[3];}stu[N];nvoidmain(){ voidinput(structstu*p); voidprint(structstu*p); structstu*p; p=stu; input(p); print(p);}voidinput(structstu*p){ inti,j; for(i=0;inum));  printf("n输入姓名:");  scanf("%s",(p+i)->name);  for(j=0;j<3;j++)  {   printf("n输入成绩%d:",j+1);   scanf("%f",&((p+i)->score[j]));  } }}voidprint(structstu*p){ inti,j; printf("学号  姓名  成绩1  成绩2  成绩3nn"); for(i=0;inum,(p+i)->name);  for(j=0;j<3;j++)  {   printf("%-9.2f",(p+i)->score[j]);  }  printf("nn"); }}  11.5#includen来源:(http://blog.sina.com.cn/s/blog_48b65ecd0100dfur.html)-谭浩强 C语言程序设计(第三版)课后习题答案_水若寒冰_新浪博客#defineN3structstu{ charnum[10]; charname[10]; floatscore[3]; floataverage;}stu[N];voidmain(){ voidinput(structstu*p); voidaver(structstu*p); floataver_total(structstu*p); voidprint(structstu*p); structstu*p; p=stu; input(p); aver(p); print(p);}voidinput(structstu*p){ inti,j; printf("按顺序依次输入学生的学号,姓名,成绩1,成绩2,成绩3nn"); for(i=0;inum,(p+i)->name);  for(j=0;j<3;j++)   scanf("%f",&((p+i)->score[j])); }}voidaver(structstu*p){ inti,j; floatsum; for(i=0;iscore[j];  }n  (p+i)->average=sum/3; }}floataver_total(structstu*p){ floatsum=0; for(;paverage; return(sum/N);}voidprint(structstu*p){ inti,j; structstu*q; floatmax=0; printf("三门课的总平均成绩为:%fnn",aver_total(p)); for(i=0;iaverage>max)  {   max=(p+i)->average;   q=p+i;  } } printf("最高分学生的数据为:nn"); printf("学号  姓名  成绩1  成绩2  成绩3  平均成绩nn"); printf("%-7s%-7s",q->num,q->name); for(j=0;j<3;j++) {  printf("%-8.2f",q->score[j]); } printf("%-8.2f",q->average); printf("nn");}  11.8#include#include#defineNULL0#defineLENsizeof(structstudent)structstudent{ longintnum;n floatscore; structstudent*next;}list_a,list_b;intn,sum;voidmain(){ structstudent*creat(void); structstudent*insert(structstudent*ha,structstudent*hb); voidprint(structstudent*head); structstudent*ahead,*bhead,*abh; ahead=creat(); sum=n; bhead=creat(); sum=sum+n; abh=insert(ahead,bhead); print(abh);}structstudent*creat(void){ structstudent*head; structstudent*p1,*p2; n=0; p1=p2=(structstudent*)malloc(LEN); printf("输入学生的学号和成绩nn"); printf("如果输入的学号为0,就停止输入nn"); scanf("%ld%f",&p1->num,&p1->score); head=NULL; while(p1->num!=0) {  n=n+1;  if(n==1)head=p1;  elsep2->next=p1;  p2=p1;  p1=(structstudent*)malloc(LEN);  scanf("%ld%f",&p1->num,&p1->score); } p2->next=NULL; return(head);}structstudent*insert(structstudent*ha,structstudent*hb){ structstudent*pa1,*pa2,*pb1,*pb2; pa1=pa2=ha;n pb1=pb2=hb; if(ha==NULL&&hb!=NULL)ha=hb; else {  while((pb1->num>pa1->num)&&(pa1->next!=NULL))  {   pa2=pa1;   pa1=pa1->next;   if(pb1->num<=pa1->num)   {    if(ha==pa1)ha=pb1;    elsepa2->next=pb1;    pb2=pb1;    pb1=pb1->next;    pa2=pa1;    pa1=pb2;    pb2->next=pa2;    }  }  if((pb1->num>pa1->num)&&(pa1->next==NULL))pa1->next=pb1; } return(ha);}voidprint(structstudent*head){ structstudent*p; printf("nn共有%d条记录,分别为:nn",sum); p=head; if(head!=NULL)  do  {   printf("%ld%8.2fn",p->num,p->score);   p=p->next;  }while(p!=NULL);}  11.9#include#include#defineN13structperson{ intnum;n intnext;}link[N+1];voidmain(){ inti,count,h; for(i=1;i<=N;i++) {  if(i==N)link[i].next=1;  elselink[i].next=i+1;  link[i].num=i; } printf("nn"); count=0; h=N; printf("退出者依次是:nn"); while(count#include#defineLENsizeof(structstudent)#defineNULL0structstudent{ longintnum;n charname[20]; structstudent*next;}a,b;intn;voidmain(){ structstudent*creat(void); structstudent*dele(structstudent*ahead,structstudent*bhead); voidprint(structstudent*head); structstudent*p1,*p2; printf("输入a中同学的学号,姓名nn"); p1=creat(); printf("输入b中同学的学号,姓名nn"); p2=creat(); print(dele(p1,p2));}structstudent*creat(void){ structstudent*head; structstudent*p1,*p2; n=0; p1=p2=(structstudent*)malloc(LEN); scanf("%d%s",&p1->num,p1->name); head=NULL; while(p1->num!=0) {  n+=1;  if(n==1)head=p1;  elsep2->next=p1;  p2=p1;  p1=(structstudent*)malloc(LEN);  scanf("%d%s",&p1->num,p1->name); } p2->next=NULL; return(head);}structstudent*dele(structstudent*ahead,structstudent*bhead){ structstudent*ah,*bh; structstudent*ap1,*ap2,*bp1,*bp2; ap1=ap2=ahead; bp1=bp2=bhead; while(ap1->next) {n  while(bp1->next)  {   if(ap1->num!=bp1->num)   {    bp2=bp1;    bp1=bp1->next;    }   else   {    if(ap1==ahead)ahead=ap1->next;    elseap2->next=ap1;    ap2=ap1;    ap1=ap1->next;    break;   }  } } return(ahead);}voidprint(structstudent*head){ structstudent*p; printf("删除后a中学生学号及成绩分别为:nn"); p=head; if(head!=NULL) {  do  {   printf("%ld%s",p->num,p->name);   p=p->next;  }while(p!=NULL); }}
查看更多

相关文章

您可能关注的文档