• 63.00 KB
  • 2022-04-22 11:48:24 发布

《C程序设计》课后习题答案(第8章).doc

  • 9页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'(1)#includevoidmain(){inthcf(int,int);/*zuidagongyueshu*/intlcd(int,int,int);/*zuixiaogongbeishu*/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);}inthcf(intu,intv){intt,r;if(u#includefloatx1,x2,disc,p,q;voidmain(){voidgreater_than_zero(float,float);voidequal_to_zero(float,float);voidsmaller_than_zero(float,float);floata,b,c;printf("ninputa,b,c:");scanf("%f,%f,%f",&a,&b,&c);printf("equation:%5.2f*x*x+%5.2f*x+%5.2f=0n",a,b,c);disc=b*b-4*a*c;printf("root:n");if(disc>0){greater_than_zero(a,b);printf("x1=%fnx2=%fn",x1,x2);} elseif(disc==0){equal_to_zero(a,b);printf("x1=x2=%fn",x1);}else{smaller_than_zero(a,b);printf("x1=%f+%finx2=%f-%fin",p,q,p,q);}}voidgreater_than_zero(floata,floatb){x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}voidequal_to_zero(floata,floatb){x1=x2=(-b)/(2*a);}voidsmaller_than_zero(floata,floatb){p=(-b)/(2*a);q=sqrt(-disc)/(2*a);}(3)#includevoidmain(){intprime(int);intn;printf("ninputaninteger:");scanf("%d",&n);if(prime(n))printf("n%disaprime.n",n);elseprintf("n%disnotaprime.n",n);}intprime(intn){intflag=1,i;for(i=2;i#defineN3intarray[N][N];voidmain() {voidconvert(intarray[][3]);inti,j;printf("inputarray:n");for(i=0;i#includevoidmain(){voidinverse(charstr[]);charstr[100];printf("inputstring:");scanf("%s",str);inverse(str);printf("inversestring:%sn",str);}voidinverse(charstr[]) {chart;inti,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;}}(6)#includevoidmain(){voidconcatenate(charstring1[],charstring2[],charstring[]);chars1[100],s2[100],s[100];printf("ninputstring1:");scanf("%s",s1);printf("inputstring2:");scanf("%s",s2);concatenate(s1,s2,s);printf("Thenewstringis%sn",s);}voidconcatenate(charstring1[],charstring2[],charstring[]){inti,j;for(i=0;string1[i]!="";i++)string[i]=string1[i];for(j=0;string2[j]!="";j++)string[i+j]=string2[j];string[i+j]="";}(7)#includevoidmain(){voidcpy(chars[],charc[]);charsty[80],c[80];printf("ninputstring:");gets(sty);cpy(sty,c);printf("nThevowellettersare:%sn",c);} voidcpy(chars[],charc[]){inti,j;for(i=0,j=0;s[i]!="";i++)if(s[i]=="a"||s[i]=="A"||s[i]=="e"||s[i]=="E"||s[i]=="i"||s[i]=="I"||s[i]=="o"||s[i]=="O"||s[i]=="u"||s[i]=="U"){c[j]=s[i];j++;}c[j]="";}(8)#include#includevoidmain(){charstr[80];voidinsert(charstr[]);printf("inputfourdigits:");scanf("%s",str);insert(str);}voidinsert(charstr[]){inti;for(i=strlen(str);i>0;i--){str[2*i]=str[i];str[2*i-1]="";}printf("output:n%sn",str);}(9)#includeintletter,digit,space,others;voidmain(){voidcount(charstr[]);chartext[80];printf("npleaseinputstring:n");gets(text);printf("string:");puts(text);letter=0; digit=0;space=0;others=0;count(text);printf("letter:%d,digit:%d,space:%d,others:%dn",letter,digit,space,others);}voidcount(charstr[]){inti;for(i=0;str[i]!="";i++)if((str[i]>="A"&&str[i]<="Z")||(str[i]>="a"&&str[i]<="z"))letter++;elseif(str[i]>="0"&&str[i]<="9")digit++;elseif(str[i]=="")space++;elseothers++;}(10)#include#includevoidmain(){intalphabetic(charc);intlongest(charstring[]);inti;charline[100];printf("inputoneline:n");gets(line);printf("Thelongestwordis:");for(i=longest(line);alphabetic(line[i]);i++)printf("%c",line[i]);printf("n");}intalphabetic(charc){if((c>="a"&&c<="z")||(c>="A"&&c<="Z"))return1;elsereturn0;}intlongest(charstring[]){intlen=0,i,length=0,flag=1,place=0,point; for(i=0;i<=strlen(string);i++)if(alphabetic(string[i]))if(flag){point=i;flag=0;}elselen++;else{flag=1;if(len>=length){length=len;place=point;len=0;}}returnplace;}(11)#include#include#defineN10charstr[N];voidmain(){voidsort(charstr[]);inti,flag;for(flag=1;flag==1;){printf("inputstring:n");scanf("%s",&str);if(strlen(str)>N)printf("stringtoolong,inputagain!");elseflag=0;}sort(str);printf("stringsorted:n");for(i=0;istr[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}(12)#include#includevoidmain(){floatsolut(floata,floatb,floatc,floatd);floata,b,c,d;printf("inputa,b,c,d:");scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%fn",solut(a,b,c,d));}floatsolut(floata,floatb,floatc,floatd){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-3);returnx;}(13)#includevoidmain(){intx,n;floatp(int,int);printf("ninputn&x:");scanf("%d,%d",&n,&x);printf("n=%d,x=%dn",n,x);printf("P%d(%d)=%fn",n,x,p(n,x));}floatp(intn,intx) {if(n==0)return1;elseif(n==1)returnx;elsereturn((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/n;}'