C程序设计(第三版,C语言)课后习题答案

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

文档介绍

C程序设计(第三版,C语言)课后习题答案

C程序设计(第三版)习题答案(1-6章)谭浩强-著习题答案第一章1.5#includevoidmain(){printf("*********n  Verygood!n*********n");}1.6(法一)#includemain(){inta,b,c,max;scanf("%d,%d,%d",&a,&b,&c);max=(a>b)?a:b;max=(max>c)?max:c;printf("max=%dn",max);}(法二)main(){inta,b,c,max;scanf("%d,%d,%d",&a,&b,&c);if(a>b&&a>c)  max=a;elseif(b>c)  max=b;else  max=c;printf("max=%dn",max);}(法三)#includevoidmain(){intmax(intx,inty,intz);inta,b,c,d;scanf("%d,%d,%d",&a,&b,&c);d=max(a,b,c);printf("max=%dn",d);}intmax(intx,inty,intz){intA,B;if(x>y)A=x;elsenA=y;if(z>A)B=z;elseB=A;return(B);}第三章3.3(1)(10)10=(12)8=(a)16(2)(32)10=(40)8=(20)16(3)(75)10=(113)8=(4b)16(4)(-617)10=(176627)8=(fd97)16(5)(-111)10=(177621)8=(ff91)16(6)(2483)10=(4663)8=(963)16(7)(-28654)10=(110022)8=(9012)16(8)(21003)10=(51013)8=(520b)163.6aabb       (8)cc       (8)abc      (7)AN3.7main(){charc1='C',c2='h',c3='i',c4='n',c5='a';c1+=4,c2+=4,c3+=4,c4+=4,c5+=4;printf("%c%c%c%c%cn",c1,c2,c3,c4,c5);}3.8main(){intc1,c2;c1=97;c2=98;printf("%c%c",c1,c2);}3.9(1)=2.5(2)=3.53.109,11,9,103.12(1)24(2)10(3)60(4)0(5)0(6)0第四章4.4main(){inta,b,c;longintu,n;floatx,y,z;charc1,c2n;a=3;b=4;c=5;x=1.2;y=2.4;z=-3.6;u=51274;n=128765;c1='a';c2='b';printf("n");printf("a=%2db=%2dc=%2dn",a,b,c);printf("x=%8.6f,y=%8.6f,z=%9.6fn",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("c1='%c'or%d(ASCII)n",c1,c1);printf("c2='%c'or%d(ASCII)n",c2,c2);}4.5575767.856400,-789.12396267.856400,-789.123962  67.86-789.12,67.856400,-789.123962,67.856400,-789.1239626.785640e+001,-7.89e+002A,65,101,411234567,4553207,d68765535,177777,ffff,-1COMPUTER,COM4.6a=3b=7/x=8.5y=71.82/c1=Ac2=a/4.7  10  20Aa1.5-3.75+1.4,67.8/(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车4.8main(){floatpi,h,r,l,s,sq,sv,sz;pi=3.1415926;printf("inputr,hn");scanf("%f,%f",&r,&h);l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;sv=4.0/3.0*pi*r*r*r;sz=pi*r*r*h;printf("l=%6.2fn",l);printf("s=%6.2fn",s);printf("sq=%6.2fn",sq);printf("vq=%6.2fn",sv);printf("vz=%6.2fn",sz);}4.9main(){floatc,f;scanf("%f",&f);c=(5.0/9.0)*(f-32);printf("c=%5.2fn",c);}n4.10#include"stdio.h"main(){charc1,c2;scanf("%c,%c",&c1,&c2);putchar(c1);putchar(c2);printf("n");printf("%c%cn",c1,c2);}第五章5.3(1)0(2)1(3)1(4)0(5)15.4main(){inta,b,c;scanf("%d,%d,%d",&a,&b,&c);if(ab)?a:b;max=(c>temp)?c:temp;printf("max=%d",max);}5.5main(){intx,y;scanf("%d",&x);if(x<1)y=x;elseif(x<10)y=2*x-1;elsey=3*x-11;printf("y=%d",y);}5.6main(){intscore,temp,logic;chargrade;logic=1;while(logic)  {scanf("%d",&score);   if(score>=0&&score<=100)logic=0;  n}if(score==100)  temp=9;else  temp=(score-score%10)/10;switch(temp)  {case9:grade='A';break;   case8:grade='B';break;   case7:grade='C';break;   case6:grade='D';break;   case5:   case4:   case3:   case2:   case1:   case0:grade='E';  }printf"score=%d,grade=%c",score,grade);}5.7main(){longintnum;intindiv,ten,hundred,thousand,ten_thousand,place;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-ten_thousand*10000)/1000;hundred=(num-ten_thousand*10000-thousand*1000)/100;ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10;switch(place)  {case5:printf("%d,%d,%d,%d,%dn",ten_thousand,thousand,hundred,ten,indiv);          printf("%d,%d,%d,%d,%dn",indiv,ten,hundred,thousand,ten_thousand);          break;   case4:printf("%d,%d,%d,%dn",thousand,hundred,ten,indiv);          printf("%d,%d,%d,%dn",indiv,ten,hundred,thousand);          break;   case3:printf("%d,%d,%dn",hundred,ten,indiv);          printf("%d,%d,%dn",indiv,ten,hundred);          break;   case2:printf("%d,%dn",ten,indiv);          printf("%d,%dn",indiv,ten);          break;   case1:printf("%dn",indiv);          printf("%dn",indiv);   }}n5.8main(){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;scanf("%ld",&i);if(i<=1e5)bonus=i*0.1;elseif(i<=2e5)bonus=bon1+(i-100000)*0.075;elseif(i<=4e5)bonus=bon2+(i-200000)*0.05;elseif(i<=6e5)bonus=bon4+(i-400000)*0.03;elseif(i<=1e6)bonus=bon6+(i-600000)*0.015;elsebonus=bon10+(i-1000000)*0.01;printf("bonus=%10.2f",bonus);}main(){longi;floatbonus,bon1,bon2,bon4,bon6,bon10;intbranch;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf("%ld",&i);branch=i/100000;if(branch>10)branch=10;switch(branch){case0:bonus=i*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:case7case8:case9:bonus=bon6+(i-600000)*0.015;break;case10:bonus=bon10+(i-1000000)*0.01;}printf("bonus=%10.2f",bonus);}4.9main(){intt,a,b,c,d;scanf("%d,%d,%d,%d",&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=b;b=c;c=t;}if(b>d){t=b;b=d;d=t;}if(c>d){t=c;c=d;d=t;}printf("%d%d%d%dn",a,b,c,d);}n5.10main(){inth=10;floatx,y,x0=2,y0=2,d1,d2,d3,d4;scanf("%f,%f",&x,&y);d1=(x-x0)*(x-x0)+(y-y0)*(y-y0);d2=(x-x0)*(x-x0)+(y+y0)*(y+y0);d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);if(d1>1&&d2>1&&d3>1&&d4>1)h=0;printf("h=%d",h);}第六章循环控制6.1main(){inta,b,num1,num2,temp;scanf("%d,%d",&num1,&num2);if(num1='a'&&c<='z'||c>='A'&&c<='Z')letters++;   elseif(c=='')space++;   elseif(c>='0'&&c<='9')digit++;   elseother++;  }printf("letters=%dnspace=%dndigit=%dnother=%dn",letters,space,digit,other);}6.3main(){inta,n,count=1,sn=0,tn=0;scanf("%d,%d",&a,&n);while(count<=n)  {tn+=a;   sn+=tn;   a*=10;   ++count;  }printf("a+aa+aaa+...=%dn",sn);}n6.4main(){floatn,s=0,t=1;for(n=1;n<=20;n++)  {t*=n;   s+=t;  }printf("s=%en",s);}6.5main(){intN1=100,N2=50,N3=10;floatk;floats1=0,s2=0,s3=0;for(k=1;k<=N1;k++)s1+=k;for(k=1;k<=N2;k++)s2+=k*k;for(k=1;k<=N3;k++)s3+=1/k;printf("s=%8.2fn",s1+s2+s3);}6.6main(){inti,j,k,n;for(n=100;n<1000;n++)  {i=n/100;   j=n/10-i*10;   k=n%10;   if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)        printf("n=%dn",n);   }}6.7#defineM1000main(){intk0,k1,k2,k3,k4,k5,k6,k7,k8,k9;inti,j,n,s;for(j=2;j<=M;j++)  {n=0;   s=j;   for(i=1;i1)printf("%d,%d",k0,k1);        if(n>2)printf(",%d",k2);        if(n>3)printf(",%d",k3);        if(n>4)printf(",%d",k4);        if(n>5)printf(",%d",k5);        if(n>6)printf(",%d",k6);        if(n>7)printf(",%d",k7);        if(n>8)printf(",%d",k8);        if(n>9)printf(",%dn",k9);        }      }}main(){staticintk[10];inti,j,n,s;for(j=2;j<=1000;j++)  {n=-1;   s=j;   for(i=1;i0)  {x1=(x2+1)*2;   x2=x1;   day--;   }printf("x1=%dn",x1);}6.11#include"math.h"main(){floata,xn0,xn1;scanf("%f",&a);xn0=a/2;xn1=(xn0+a/xn0)/2;do  {xn0=xn1;   xn1=(xn0+a/xn0)/2;  }while(fabs(xn0-xn1)>=1e-5);printf("a=%5.2fn,xn1=%8.2fn",a,xn1);}6.12#include"math.h"main(){floatx,x0,f,f1;x=1.5;do  {x0=x;   f=((2*x0-4)*x0+3)*x0-6;   f1=(6*x0-8)*x0+3;   x=x0-f/f1;  }while(fabs(x-x0)>=1e-5);printf("x=%6.2fn",x);}6.13#include"math.h"main(){floatx0,x1,x2,fx0,fx1,fx2;do  {scanf("%f,%f",&x1,&x2);   fx1=x1*((2*x1-4)*x1+3)-6;   fx2=x2*((2*x2-4)*x2+3)-6;  n}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;     }   else     {x1=x0;      fx1=fx0;     }   }while(fabs(fx0)>=1e-5);printf("x0=%6.2fn",x0);}6.14main(){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("");   for(k=0;k<=4-2*i;k++)     printf("*");   printf("n");   }}6.15main(){chari,j,k;for(i='x';i<='z';i++)  for(j='x';j<='z';j++)    {if(i!=j)      for(k='x';k<='z';k++)        {if(i!=k&&j!=k)          {if(i!='x'&&k!='x'&&k!='z')             printf("na--%ctb--%ctc--%cn",i,j,k);          }         }     }}第七章数组7.1#include#defineN101main(){inti,j,line,a[N];for(i=2;ia[j])min=j;   temp=a[i];   a[i]=a[min];   a[min]=temp;   }for(i=0;iend)a[10]=number;else  {for(i=0;i<10;i++)     n{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;        }      }    }for(i=0;i<11;i++)  printf("%6d",a[i]);}7.5#defineN5main(){inta[N]={8,6,5,4,1},i,temp;for(i=0;in))     {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("%3d",a[i][j]);   printf("n");  }}7.8#defineN10#defineM10main(){inti,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj;scanf("%d,%d",&n,&m);for(i=0;ia[k][maxj])flag1=0;   if(flag1)     {printf("na[%d][%d]=%dn",i,maxj,max);       flag2=1;     }   }if(!flag2)printf("NOT");}7.9#include#defineNn15main(){inti,j,number,top,bott,min,loca,a[N],flag;charc;for(i=0;i<=N;i++)  scanf("%d",&a[i]);flag=1;while(flag)  {scanf("%d",&number);   loca=0;   top=0;   bott=N-1;   if((numbera[N-1]))     loca=-1;   while((loca==0)&&(top<=bott))     {min=(bott+top)/2;      if(number==a[min])        {loca=min;         printf("number=%d,loca=%dn",number,loca+1);        }      elseif(number='A'&&text[i][j]<='Z')        uppn++;      elseif(text[i][j]>='a'&&text[i][j]<='z')        lown++;      elseif(text[i][j]>='0'&&text[i][j]<='9')        dign++;      elseif(text[i][j]=='')        span++;      else        othn++;      }   }for(i=0;i<3;i++)  nprintf("%sn",text[i]);printf("uppn=%dn",uppn);printf("lown=%dn",lown);printf("dign=%dn",dign);printf("span=%dn",span);printf("othn=%dn",othn);}7.11main(){staticchara[5]={'*','*','*','*','*'};inti,j,k;charspace='';for(i=0;i<=5;i++)  {printf("n");   for(j=1;j<=3*i;j++)     printf("%1c",space);   for(k=0;k<=5;k++)     printf("%3c",a[k]);   }}7.12#includemain(){inti,n;charch[80],tran[80];gets(ch);i=0;while(ch[i]!='')  {if((ch[i]>='A')&&(ch[i]<='Z'))     tran[i]=26+64-ch[i]+1+64;   elseif((ch[i]>='a')&&(ch[i]<='z'))     tran[i]=26+96-ch[i]+1+96;   else     tran[i]=ch[i];   i++;   }n=i;for(i=0;imain(){inti,resu;charns1[100],s2[100];gets(s1);gets(s2);i=0;while((s1[i]==s2[i])&&(s1[i]!=''))i++;if(s1[i]==''&&s2[i]=='')resu=0;else  resu=s1[i]-s2[i];printf("s1=%s,s2=%s,resu=%dn",s1,s2,resu);}7.15#include"stdio.h"main(){charfrom[80],to[80];;inti;scanf("%s",from);for(i=0;i<=strlen(from);i++)  to[i]=from[i];printf("%sn",to);}第八章8.1hcf(u,v)intu,v;{inta,b,t,r;if(u>v){t=u;u=v;v=t;}a=u;b=v;while((r=b%a)!=0)  {b=a;a=r;}return(a);}lcd(u,v,h)intu,v,h;{return(u*v/h);}main(){intu,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%dn",h);l=lcd(u,v,h);printf("L.C.D=%dn",l);}8.2#include"math.h"floatx1,x2,disc,p,q;greater_than_zero(a,b)floata,b;{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}equal_to_zero(a,b)flaota,b;{x1=x2=-b/(2*a);}smaller_than_zero(a,b)floatna,b;{p=-b/(2*a);q=sqrt(-disc)/(2*a);}main(){floata,b,c;scanf("%f,%f,%f",&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)<=1e-5)  {equal_to_zero(a,b);   printf("x1=%5.2ftx2=%5.2fn",x1,x2);  }elseif(disc>0)  {greater_than_zero(a,b);   printf("x1=%5.2ftx2=%5.2fn",x1,x2);  }else  {smaller_than_zero(a,b);   printf("x1=%5.2f+%5.2fitx2=%5.2f-%5.2fin",p,q,p,q);  }}8.3main(){intnumber;scanf("%d",&number);if(prime(number))  printf("yes");else  printf("no");}intprime(number)intnumber;{intflag=1,n;for(n=2;n0;i--)  {str[i*2]=str[i];   str[i*2-1]='';  }printf("%sn",str);}8.9intalph,digit,space,others;main(){chartext[80];gets(text);alph=0,digit=0,space=0,others=0;count(text);printf("nalph=%d,digit=%d,space=%d,others=%dn",alph,digit,space,others);}count(str)charstr[];{inti;for(i=0;str[i]!='';i++)  if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))    alph++;  elseif(str[i]>='0'&&str[i]<='9')    digit++;  elseif(strcmp(str[i],'')==0)    space++;  else    others++;}8.10intalph(c)charc;{if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))  return(1);else  return(0);}intlongest(string)charstring[];{intlen=0,i,length=0,flag=1,place,point;for(i=0;i<=strlen(string);i++)  nif(alph(string[i]))    if(flag)      {point=i;       flag=0;      }    else      len++;   else     {flag=1;      if(len>length)        {length=len;         place=point;         len=0;         }      }return(place);}main(){inti;charline[100];gets(line);for(i=longest(line);alph(line[i]);i++)  printf("%c",line[i]);printf("n");}8.11#defineN10charstr[N];main(){inti,flag;for(flag=1;flag==1;)  {scanf("%s",str);   if(strlen(str)>N)     printf("inputerror");   else     flag=0;   }sort(str);for(i=0;istr[i+1])      {t=str[i];       str[i]=str[i+1];       str[i+1]=t;      }}8.12#includefloatnsolut(a,b,c,d)floata,b,c,d;{floatx=1,x0,f,f1;do  {x0=x;   f=((a*x0+b)*x0+c)*x0+d;   f1=(3*a*x0+2*b)*x0+c;   x=x0-f/f1;   }while(fabs(x-x0)>=1e-5);return(x);}main(){floata,b,c,d;scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%10.7fn",solut(a,b,c,d));}8.13main(){intx,n;floatp();scanf("%d,%d",&n,&x);printf("P%d(%d)=%10.2fn",n,x,p(n,x));}floatp(tn,tx)inttn,tx;{if(tn==0)  return(1);elseif(tn==1)  return(tx);else  return(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn);}8.14#defineN10#defineM5floatscore[N][M];floata_stu[N],a_cor[M];main(){inti,j,r,c;floath;floats_diff();floathighest();r=0;c=1;input_stu();avr_stu();avr_cor();printf("nnumberclass12345avr");for(i=0;ihigh)      {high=score[i][j];       *r=i+1;       *c=j+1;       }return(high);}floats_diff(){inti,j;floatsumx=0.0,sumxn=0.0;for(i=0;i#defineN10voidinput_e(num,name)intnum[];charname[N][8];{inti;for(i=0;inum[j])min=j;   temp1=num[i];   num[i]=num[min];   num[min]=temp1;   strcpy(temp2,name[i]);   strcpy(name[i],name[min]);   strcpy(name[min],temp2);  }for(i=0;inum[N-1]))  loca=-1;while((loca==0)&&(top<=bott))  {min=(bott+top)/2;   if(n==num[min])     {loca=min;      printf("number=%d,name=%sn",n,name[loca]);     }   elseif(n#defineMAX1000main(){intc,i,flag,flag1;chart[MAX];i=0;flag=0;flag1=1;while((c=getchar())!=''&&i='0'&&c<='9'||c>='A'&&c<='F'||c>='a'&&c<='f')     {flag=1;      t[i++]=c;     }   elseif(flag)     {t[i]='';      printf("nnumber=%dn",htoi(t));      printf("continue?");      c=getchar();      if(c=='n'||c=='N')        flag1=0;      else        {flag=0;         i=0;         }      }    }}htoi(s)chars[];{inti,n;n=0;for(i=0;s[i]!='';i++)  {if(s[i]>='0'&&s[i]<='9')     n=n*16+s[i]-'0';   if(s[i]>='a'&&s[i]<='f')     n=n*16+s[i]-'a'+10;   if(s[i]>='A'&&s[i]<='F')     n=n*16+s[i]-'A'+10;   }return(n);}8.17#includevoidnconvert(n)intn;{inti;if((i=n/10)!=0)  convert(i);putchar(n%10+'0');}main(){intnumber;scanf("%d",&number);if(number<0)  {putchar('-');   number=-number;  }convert(number);}8.18main(){intyear,month,day;intdays;scanf("n%d,%d,%d",&year,&month,&day);days=sum_day(month,day);if(leap(year)&&(month>=3))  days+=1;printf("days=%dn",days);}staticintday_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}intsum_day(month,day)intmonth,day;{inti;for(i=1;ic&&b+c>a&&a+c>b)  printf("area=%8.2fn",AREA(a,b,c));else  printf("inputerror");}9.4#defineLEAP(y)(y%4==0)&&(y%100!=0)||(y%400==0)main(){intyear;scanf("%d",&year);if(LEAP(year))  printf("yes");else  printf("no");}8.5printf("value=%formatt",x);printf("value=%formatt",x);putchar('n');printf("value=%formatt",x1);printf("value=%formatt",x2);putchar('n');输出结果:value=5.000000ormat       value=5.000000ormatvalue=3.000000ormat       value=8.000000ormat9.6#definePRprintf#defineNL"n"#defineFs"%f"#defineF"%6.2f"#defineF1FNL#defineF2F"t"FNL#defineF3F"t"F"t"FNLmain(){floatna,b,c;scanf(Fs,&a);scanf(Fs,&b);scanf(Fs,&c);PR(NL);PR(F1,a);PR(F1,b);PR(F1,c);PR(NL);PR(F2,a,b);PR(F1,c);PR(NL);PR(F3,a,b,c);}9.7/*format.h文件*/#defineDIGIT(d)printf("%dn",d)#defineFLOAT(f)printf("%8.2fn",f)#defineSTRING(s)printf("%sn",s)/*用户文件*/#include"format.h"main(){intd,num;floatf;chars[80];scanf("%d",&num);switch(num)  {case1:scanf("%d",&d);          DIGIT(d);          break;   case2:scanf("%f",&f);          FLOAT(f);          break;   case3:scanf("%s",s);          STRING(s);          break;   default:printf("error");  }}9.8main(){inta,b,c;scanf("%d,%d,%d",&a,&b,&c);printf("max=%dn",max(a,b,c));}max(a,b,c)inta,b,c;{intt;t=x>y?x:y;return(t>z?t:z);}#defineMAX(a,b)((a)>(b)?(a):(b))main(){inta,b,c;scanf("%d,%d,%d",&a,&b,&c);printf("max=%dn",MAX(MAX(a,b),c));}9.10#include"stdio.h"#defineMAX80#defineCHANGE1main(){charstr[MAX];intni;scanf("%s",str);#if(CHANGE)  {for(i=0;i='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z')          str[i]++;        elseif(str[i]=='z'||str[i]=='Z')          str[i]-=25;     }   }#endifprintf("n%sn",str);}第十章指针10.1main(){intn1,n2,n3;int*p1,*p2,*p3;scanf("%d,%d,%d",&n1,&n2,&n3);p1=&n1;p2=&n2;p3=&n3;if(n1>n2)swap(p1,p2);if(n1>n3)swap(p1,p3);if(n2>n3)swap(p2,p3);printf("%d,%d,%dn",n1,n2,n3);}swap(p1,p2)int*p1,*p2;{intp;p=*p1;*p1=*p2;*p2=p;}10.2main(){char*str1[20],*str2[20],*str3[20];charswap();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("%sn%sn%sn",str1,str2,str3);}charswap(p1,p2)char*p1,*p2;{char*p[20];strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);}10.3main(){intnnumber[10];input(number);max_min_value(number);output(number);}input(number)intnumber[10];{inti;for(i=0;i<10;i++)  scanf("%d",&number[i]);}max_min_value(number)intnumber[10];{int*max,*min;int*p,*end;end=number+10;max=min=number;for(p=number+1;p*max)max=p;  elseif(*p<*min)min=p;*p=number[0];number[0]=*min;*min=*p;*p=number[9];number[9]=*max;*max=*p;return;}output(number)intnumber[10];{int*p;for(p=number;parray;p--)  *p=*(p-1);*array=end;m--;if(m>0)move(array,n,m);}n10.5#definenmax50main(){inti,k,m,n,num[nmax],*p;scanf("%d",&n);p=num;for(i=0;i='a'&&*p<='z')     ++sle;   elseif(*p>='A'&&*p<='Z')     ++cle;   elseif(*p=='')     ++wsp;   elseif(*p>='0'&&*p<='9')     ++di;   else     ++ot;   p++;  }printf("sle=%d,cle=%d,wsp=%d,di=%d,ot=%dn",sle,cle,wsp,di,ot);}10.9main(){inta[3][3],*p,i;for(i=0;i<3;i++)  scanf("%d,%d,%d",a[i][0],a[i][1],a[i][2]);p=a;move(p);for(i=0;i<3;i++)  printf("%d%d%dn",a[i][0],a[i][1],a[i][2]);}move(pointer)int*pointer;{inti,j,t;for(i=0;i<2;i++)for(j=i+1;j<3;j++)  {t=*(pointer+3*i+j);   *(pointer+3*i+j)=*(pointer+3*j+i);   *(pointer+3*j+i)=t;  }}n10.10main(){inta[5][5],*p,i,j;for(i=0;i<5;i++)for(j=0;j<5;j++)  scanf("%d",&a[i][j]);p=a;change(p);for(i=0;i<5;i++)  {printf("n");   for(j=0;j<5;j++)     printf("%8d",a[i][j]);   }}change(p)int*p;{inti,j,change;int*pmax,*pmin;pmax=p;pmin=p;for(i=0;i<5;i++)  for(j=0;j<5;j++)    {if(*pmax<*(p+5*i+j))pmax=p+5*i+j;     if(*pmin>*(p+5*i+j))pmin=p+5*i+j;    }change=*(p+12);*(p+12)=*pmax;*pmax=change;change=*p;*p=*pmin;*pmin=change;pmin=p+1;for(i=0;i<5;i++)  for(j=0;j<5;j++)    if(((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;change=*(p+4);*(p+4)=*pmin;*pmin=change;pmin=p+1;for(i=0;i<5;i++)  for(j=0;j<5;j++)    if(((p+5*i+j)!=(p+4))&&((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))       pmin=p+5*i+j;change=*(p+20);*(p+20)=*pmin;*pmin=change;pmin=p+1;for(i=0;i<5;i++)  for(j=0;j<5;j++)    if(((p+5*i+j)!=p)&&((p+5*i+j)!=(p+4))&&((p+5*i+j)!=(p+20))           &&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;change=*(p+24);*(p+24)=*pmin;*pmin=change;}10.11main(){intni;char*p,str[10][10];for(i=0;i<10;i++)  scanf("%s",str[i]);p=str;sort(p);for(i=0;i<10;i++)  printf("%sn",str[i]);}sort(p)char*p;{inti,j;chars[10],*smax,*smin;for(i=0;i<10;i++)  {smax=p+10*i;   for(j=i+1;j<10;j++)     {smin=p+10*j;      if(strcmp(smax,smin)>0)        {strcpy(s,smin);         strcpy(smin,smax);         strcpy(smax,s);        }      }   }}10.12#defineMAX20main(){inti;char*pstr[10],str[10][MAX];for(i=0;i<10;i++)  pstr[i]=str[i];for(i=0;i<10;i++)  scanf("%s",pstr[i]);sort(pstr);for(i=0;i<10;i++)  printf("%sn",pstr[i]);}sort(pstr)char*pstr[10];{inti,j;char*p;for(i=0;i<10;i++)  {for(j=i+1;j<10;j++)     {if(strcmp(*(pstr+i),*(pstr+j))>0)        {p=*(pstr+i);         *(pstr+i)=*(pstr+j);         *(pstr+j)=p;         }      }   }}10.13#include"math.h"main(){intnn=20;floata,b,a1,b1,a2,b2,c,(*p)(),jiff();scanf("%f,%f",&a,&b);scanf("%f,%f",&a1,&b1);scanf("%f,%f",&a2,&b2);p=sin;c=jiff(a,b,n,p);printf("sin=%fn",c);p=cos;c=jiff(a1,b1,n,p);printf("cos=%fn",c);p=exp;c=jiff(a2,b2,n,p);printf("exp=%fn",c);}floatjiff(a,b,n,p)floata,b,(*p)();intn;{inti;floatx,f,h,area;h=(b-a)/n;x=a;area=0;for(i=1;i<=n;i++)  {x=x+h;   area=area+(*p)(x)*h;   }return(area);}10.14main(){inti,n,num[20];char*p;scanf("%d",&n);for(i=0;i=2)     {printf("%-8d",*(pnum+i));      for(k=0;k<5;k++)        printf("%-8.2f",*(psco+5*i+k));      printf("%-8.2f",*(pave+i));     }   }}good(pcou,pnum,psco,pave)char*pcou;int*pnum;float*psco,*pave;{inti,j,k,label;printf("number");for(i=0;i<5;i++)  printf("%-8s",pcou+10*i);printf("average");for(i=0;i<4;i++)  {label=0;   for(j=0;j<5;j++)     if(*(psco+5*i+j)>=85.0)label++;   if((label>=5)||(*(pave+i)>=90))     {printf("%-8d",*(pnum+i));      for(k=0;k<5;k++)        printf("%-8.2f",*(psco+5*i+k));      printf("%-8.2f",*(pave+i));     }   }}10.16#include"stdio.h"main(){charstr[50],*pstr;inti,j,k,m,e10,digit,ndigit,a[10],*pa;gets(str);pstr=str;pa=a;ndigit=0;i=j=0;while(*(pstr+i)!='')  {if((*(pstr+i)>='0')&&(*(pstr+i)<='9'))     j++;   else     {if(j>0)        {digit=*(pstr+i-1)-48;         k=1;         while(k0)  {digit=*(pstr+i-1)-48;   k=1;   while(k=1)&&(n<=12))  printf("%sn",*(mname+n));else  printf("error");}10.20main(){inti;char**p,*pstr[5],str[5][10];for(i=0;i<5;i++)  pstr[i]=str[i];for(i=0;i<5;i++)  scanf("%s",pstr[i]);p=pstr;sort(p);for(i=0;i<5;i++)  printf("%sn",pstr[i]);}sort(p)char**P;{inti,j;char*pchange;for(i=0;i<5;i++)  {for(j=i+1;j<5;j++)     {if(strcmp(*(p+i),*(p+j))>0)        {pchange=*(p+i);         *(p+i)=*(p+j);         *(p+j)=pchange;        }      }   }}10.21main(){inti,n,digit[20],**p,*pstr[20];scanf("%d",&n);for(i=0;i**(p+j))        {pchange=*(p+i);         *(p+i)=*(p+j);         *(p+j)=pchange;        n}     }   }}第十一章结构体与共用体11.1struct{intyear;  intmonth;  intday;}date;main(){intdays;scanf("%d,%d,%d",&date.year,&date.month,&date.day);switch(date.month)  {case1:days=date.day;break;   case2:days=date.day+31;break;   case3:days=date.day+59;break;   case4:days=date.day+90;break;   case5:days=date.day+120;break;   case6:days=date.day+151;break;   case7:days=date.day+181;break;   case8:days=date.day+212;break;   case9:days=date.day+243;break;   case10:days=date.day+273;break;   case11:days=date.day+304;break;   case12:days=date.day+334;break;   }if((date.year%4==0&&date.year%100!=0||date.year%400==0)                         &&date.month>=3)  days+=1;printf("days=%dn",days);}11.2structdt{intyear;  intmonth;  intday;}date;main(){scanf("%d,%d,%d",&date.year,&date.month,&date.day);printf("n%dn",days(date.year,date.month,date.day));}days(year,month,day)intyear,month,day;{intdaysum=0,i;staticintdaytab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}for(i=1;i=3)  daysum+=1;return(daysum);}n11.311.4#defineN5structstudent{charnum[6];  charname[8];  intscore[4];  }stu[N];main(){input(stu);print(stu);}input(stu)structstudentstu[];{inti,j;for(i=0;imax)     {max=sum;      maxi=i;     }   }average/=5;printf("number  namescore1score2score3averagen");for(i=0;i<5;i++)  {printf("%8s%10s",stu[i].num,stu[i].name);   for(j=0;j<3;j++)     printf("%7d",stu[i].score[j]);   printf("%6.2fn",stu[i].avr);  }printf("average=%5.2fn",average);printf("Thebeststudentis%s,sum=%dn",stu[maxi].name,max);}
查看更多

相关文章

您可能关注的文档