C教材第4版習(xí)題參考答案(1-8章)_第1頁(yè)
C教材第4版習(xí)題參考答案(1-8章)_第2頁(yè)
C教材第4版習(xí)題參考答案(1-8章)_第3頁(yè)
C教材第4版習(xí)題參考答案(1-8章)_第4頁(yè)
C教材第4版習(xí)題參考答案(1-8章)_第5頁(yè)
已閱讀5頁(yè),還剩34頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

word文檔可自由復(fù)制編輯習(xí)題參考答案第1章C/C++語(yǔ)言簡(jiǎn)單程序的編寫(xiě)和調(diào)試1-1參考程序#include<stdio.h>intmain(void){floatl,w,h,cubage;printf("Pleaseinputrectangle'slength,width,high:");scanf("%f,%f,%f",&l,&w,&h);cubage=l*w*h;printf("\ncubage=%f\n",cubage);return(0);}1-2參考程序#include<stdio.h>intmain(void){printf("Helooksveryhealthy.");return(0);}第2章C語(yǔ)言程序設(shè)計(jì)基礎(chǔ)2-1參考程序#include<stdio.h>intmain(void){printf("*\n");printf("***\n");printf("*****\n");printf("*******\n");printf("*********\n");printf("***********\n");printf("*********\n");printf("*******\n");printf("*****\n");printf("***\n");printf("*\n");return(0);}2-2參考程序本利和=本金+利息=本金×[1+年利率×存款年數(shù)]#include<stdio.h>intmain(void){ floatsum;sum=20*(1+3.15*2);printf("sum=%f",sum);return(0);}2-3參考程序#include<stdio.h>#include<math.h>#definepi3.141592intmain(void){floatr,v;printf("PleaseInputR=");scanf("%f",&r);v=4./3*pi*pow(r,3);printf("V=%f",v);return(0);}2-4參考程序#include"stdio.h"intmain(void){floatc,f;printf("PleaseInputF=");scanf("%f",&f);c=5./9*(f-32);printf("C=%.2f",c);return(0);}第3章控制結(jié)構(gòu)3-1參考程序if語(yǔ)句參考程序:#include<stdio.h>intmain(void){floatresult;printf("Entergrade=");scanf("%f",&result);if(result>=0&&result<=100){ if(result>=90)printf("優(yōu)\n");elseif(result>=80)printf("良\n");elseif(result>=70)printf("中\(zhòng)n");elseif(result>=60)printf("及格\n");elseprintf("不及格\n");printf("Result=%.2f",result);}elseprintf("ERROR");return(0);}switch語(yǔ)句參考程序:#include<stdio.h>intmain(void){inti;floatresult;printf("Result=");scanf("%f",&result);if(result<0&&result>100){printf("ERROR!!");exit(0);}i=result/10;switch(i){case0:case1:case2:case3:case4:case5:printf("不及格\n");break;case6:printf("及格\n");break;case7:printf("中\(zhòng)n");break;case8:printf("良\n");break;case9:case10:printf("優(yōu)\n");}printf("Result=%.2f\n",result);}3-2參考程序#include<stdio.h>intmain(void){inty,m,d,day;printf("YYYY-M-R:");scanf("%d-%d-%d",&y,&m,&d);if(m>=3)if(y%4==0&&(y%100!=0)‖(y%400==0)) d=d+1;printf("DD=%d",d);switch(m){case1:day=d;break;case2:day=d+31;break;case3:day=d+31+28;break;case4:day=d+31+28+31;break;case5:day=d+31+28+31+30;break;case6:day=d+31+28+31+30+31;break;case7:day=d+31+28+31+30+31+30;break;case8:day=d+31+28+31+30+31+30+31;break;case9:day=d+31+28+31+30+31+30+31+31;break;case10:day=d+31+28+31+30+31+30+31+31+30;break;case11:day=d+31+28+31+30+31+30+31+31+30+31;break;case12:day=d+31+28+31+30+31+30+31+31+30+31+30;}printf("DAY=%d",day);return(0);}3-3參考程序參考程序1#include<stdio.h>intmain(void){inta,b,c;printf("abc:");scanf("%d%d%d",&a,&b,&c);printf("Max=%d",(a>b?a:b)>c?(a>b?a:b):c);return(0);}參考程序2#include<stdio.h>intmain(void){inta,b,c,max;printf("abc:");scanf("%d%d%d",&a,&b,&c);if(a>b)max=a;elsemax=b;if(max<c)max=c;printf("MAX=%d",max);return(0);}3-4參考程序#include<stdio.h>intmain(void){floata=2,b=3,c=1,sum=0;inti=1;for(;i<=9;i++){sum+=a/c+b/a;printf("%.0f/%.0f+%.0f/%.0f+\n",a,c,b,a);c=a+c;a=a+b;b=a+b;}printf("%.0f/%.0f+%.0f/%.0f",a,c,b,a);printf("\nSUM=%f",sum);return(0);}3-5參考程序#include"stdio.h"intmain(void){inta,b,c,d,m;printf("PleaseInputtwointeger:");scanf("%d%d",&c,&d);if(c>d){m=d;d=c;c=m;}a=c;b=d;while(b!=0){m=a%b;a=b;b=m;}printf("最大公約數(shù)是:%d\n",a);printf("最小公倍數(shù)是:%d\n",c*d/a);return(0);}3-6參考程序#include<stdio.h>#include<math.h>intmain(void){floatx1,x2,x,err,root;printf("請(qǐng)輸入精度:");scanf("%f",&err);printf("請(qǐng)輸入上界:");scanf("%f",&x1);printf("請(qǐng)輸入下界:");scanf("%f",&x2);while(fabs(x1-x2)>err){x=(x1+x2)/2; if((2*pow(x,3)-4*pow(x,2)+3*x-6)*(2*pow(x1,3)-4*pow(x1,2)+3*x1-6)<0) x2=x; else x1=x;}root=(x1+x2)/2;printf("Therootis:%.2f",root);return(0);}3-7參考程序#include"stdio.h"intmain(void){doublesum=0,s=1;inti=1;for(i=1;i<=18;i++){s*=i;sum+=s;}printf("SUM=%.0lf",sum);return(0);}3-8參考程序#include"stdio.h"intmain(void){inti,j,k;for(i=6;i>0;i--){for(j=0;j<6-i;j++) printf("%3c",'');for(k=2*i-1;k>0;k--) printf("%3c",'*');printf("\n");}return(0);}3-9參考程序#include"stdio.h"intmain(void){intj;floati=2000,k;for(j=1;;j++){if(i/2-1<0){i=i/2;break;}i=i/2-1;}printf("J=%d,I=%f",j,i);return(0);}3-10參考程序#include<stdio.h>intmain(void){ inti=1; intcount=0; printf("\n能被2,3,5整除時(shí)余數(shù)為1的最小10個(gè)自然數(shù)是:\n"); while(count<=10) { if((i%2==1)&&(i%3==1)&&(i%5==1)) { count++; printf("%d",i); } i++;}return(0);}3-11參考程序#include<stdio.h>intmain(void){longintn,s;s=n=0;while(s<=100000){++n;s+=n*n*n;}printf("n=%d\n",n-1);return(0);}3-12參考程序#include<stdio.h>#include<math.h>intmain(void){ intn,s; doubler; printf("inputn:"); scanf("%d",&n); s=n*(n+1)*(n+2)*(n+3); r=sqrt(s); if(s!=r*r) { printf("n=%d\ts=%d\tsqrt(%d)=%f\n",n,s,s,r); printf("%d不是一個(gè)完全平方數(shù)!\n",s); } else { printf("%d=%d*%d*%d*%d\n",s,n,n+1,n+2,n+3); printf("%d是一個(gè)完全平方數(shù)!\n",s); return; } s++; r=sqrt(s); if(s==r*r) { printf("%d+1=%d=%d*%d\n",s-1,s,(int)r,(int)r); printf("%d是一個(gè)完全平方數(shù)!\n",s); } elseprintf("%d不是一個(gè)完全平方數(shù)!\n",s);return(0);}第4章數(shù)組4-1參考程序#defineN10#include<stdio.h>intmain(void){inti,a[N],t;for(i=0;i<N;i++)scanf("%d",&a[i]);for(i=0;i<N/2;i++){t=a[i];a[i]=a[N-1-i];a[N-1-i]=t;}for(i=0;i<N;i++)printf("%d",a[i]);return(0);}4-2參考程序源程序XT4-2.C#include<stdio.h>#defineN10intmain(void){charstr[N],t;inta,b;printf("\nEnterNcharacter:");for(a=0;a<N;a++)scanf("%c",&str[a]);for(a=1;a<N;++a)for(b=0;b<N-a;++b) if(str[b]>str[b+1]) { t=str[b]; str[b]=str[b+1]; str[b+1]=t;}printf("\n");for(a=0;a<N;a++)printf("%c",str[a]);return(0);}4-3參考程序源程序XT4-3.C#include<stdio.h>#defineN10intmain(void){charstr[N][20],t[20];inta,b;printf("\nEnterNstrings:");for(a=0;a<N;a++)gets(str[a]);for(a=1;a<N;++a)for(b=0;b<N-a;++b) if(strcmp(str[b],str[b+1])>0) { strcpy(t,str[b]); strcpy(str[b],str[b+1]); strcpy(str[b+1],t);}printf("\n");for(a=0;a<N;a++)puts(str[a]);return(0);}4-4參考程序#include<stdio.h>intmain(void){inta[5][5],i,j;chark='';for(i=0;i<5;i++){for(j=0;j<5;j++){a[i][j]=j+i*5+1;printf("%3d",a[i][j]);}printf("\n");}printf("\n");for(i=0;i<5;i++)printf("%3d",a[0][i]);printf("\n");for(i=1;i<4;i++){printf("%3d%9c%3d",a[i][0],k,a[i][4]);printf("\n");}for(i=0;i<5;i++)printf("%3d",a[4][i]);printf("\n\n");for(i=1;i<4;i++){for(j=1;j<4;j++)printf("%5d",a[i][j]);printf("\n");}printf("\n");return(0);}4-5參考程序#defineN10#include<stdio.h>intmain(void){inta[N],max,min,i,j,k;for(i=0;i<N;i++)scanf("%d",&a[i]);min=a[0];for(i=1;i<N;i++)if(a[i]<min){min=a[i];j=i;}max=a[0];for(i=1;i<N;i++)if(a[i]>max){max=a[i]; k=i;}a[j]=a[0];a[0]=min;a[k]=a[N-1];a[N-1]=max;printf("\nThepositionofminis:%3d\n",j);printf("Thepositionofmaxis:%3d\n",k);for(i=0;i<N;i++)printf("%5d",a[i]);return(0);}4-6參考程序源程序XT4-7.C#include<stdio.h>intmain(void){chars[]="abcdfghijkmnoprs",c;inti,top,bot,mid;printf("inputacharacter\n");scanf("%c",&c);for(top=0,bot=10;top<=bot;){mid=(top+bot)/2;if(c==s[mid]){printf("Thepositionis%d\n",mid+1);break;}elseif(c>s[mid])top=mid+1;elsebot=mid-1;}if(top>bot)printf("Fail!\n");return(0);}4-7參考程序#include<stdio.h>intmain(void){chars[]="labchmfye",t;inti,j,k;k=strlen(s);for(i=0;i<k;i+=2)for(j=i+2;j<k;j++)if(s[i]>s[j]) {t=s[i];s[i]=s[j];s[j]=t;j++;}puts(s);printf("\n");return(0);}4-8參考程序#include<string.h>#include<stdio.h>#defineN81intmain(void){charstr[N],ch;inti,m,j;printf("Entertheoriginalstring:\n");gets(str);m=strlen(str);for(i=1;i<m;i++){ch=str[i];j=i-1;while((j>=0)&&(ch<str[j])){str[j+1]=str[j];j--;}str[j+1]=ch;}printf("Thestringaftersorting:%s\n",str);return(0);}4-9參考程序#include<stdio.h>#defineN7intmain(void){inti,j,a[N][N];for(i=0;i<N;i++){a[i][i]=1;a[i][0]=1;}for(i=2;i<N;i++)for(j=1;j<=i-1;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=0;i<N;i++){for(j=0;j<=i;j++)printf("%6d",a[i][j]);printf("\n");}printf("\n");return(0);}4-10參考程序#include<stdio.h>#include<math.h>#defineN10intmain(void){ floatsum=0,avg,abs=0,fc,x[N]; inti; printf("\nEntertheoriginaldatais:\n"); for(i=0;i<N;i++) scanf("%f",&x[i]); for(i=0;i<10;i++) sum+=x[i]; avg=sum/N; for(i=0;i<10;i++) abs+=(x[i]-avg)*(x[i]-avg); fc=sqrt(abs/N); printf("fc=%f\n",fc); return(0);}4-11參考程序#include<stdio.h>#defineM10inta[M][M]={0};intmain(void){inti,j,k,n;printf("Entern:\n");scanf("%d",&n);for(j=0;j<M;j++)for(k=0;k<M;k++)a[j][k]=(k+1)*(j+1);printf("\n");for(i=0;i<n;i++){for(j=0;j<n;j++)printf("%4d",a[i][j]);printf("\n");}return(0);}4-12參考程序#include<stdio.h>#include<string.h>intmain(void){charstr[81],substr[2];inti,j=0;printf("輸入主串:");gets(str);printf("輸入子串:");gets(substr);printf("\n主串為%s",str);printf("\n子串為%s",substr);for(i=0;str[i+1]!='\0';i++) //長(zhǎng)度為2的子串在主串中出現(xiàn)一次,則j+1,如此循環(huán)if(str[i]==substr[0]&&str[i+1]==substr[1])j++; //子串在主串中出現(xiàn)的次數(shù)printf("\n子串在主串中出現(xiàn)的次數(shù)=%d\n",j);return(0);}4-13參考程序#include<stdio.h>intmain(void){chara[20];longx=0L;inti=0;scanf("%s",a);while(a[i]!='\0'){x=x*10+(a[i]–'0');i++;}printf("%ld",x);return(0);}4-14參考程序#include<stdio.h>intmain(void){charch[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'},output[20];intnum,temp,n,i=0;printf("\n請(qǐng)輸入你想要轉(zhuǎn)換的十進(jìn)制數(shù):");scanf("%d",&num);printf("\n請(qǐng)輸入要轉(zhuǎn)換的進(jìn)制(2<=n<=16):");scanf("%d",&n);while(num){temp=num%n;num=num/n;output[i++]=ch[temp];}output[i]='\0';printf("轉(zhuǎn)換為%d進(jìn)制后為:",n);for(;i>=0;i--)printf("%c",output[i]);printf("\n");return(0);}第5章指針5-1參考程序#include<stdio.h>intmain(void){charstr1[50],str2[50],*p1,*p2;printf("\nEnterstring1:");gets(str1);printf("\nEnterstring2:");gets(str2);p1=str1;p2=str2;if(*p1!=*p2)printf("\nstring1!=string2");else{while((*p1==*p2)&&(*p1!='\0')&&(*p2!='\0')){p1++;p2++;}if((*p1=='\0')&&(*p2=='\0'))printf("\nstring1=string2");elseprintf("\nstring1!=string2");}return(0);}5-2參考程序#include<stdio.h>#include<string.h>intmain(void){chars[81],*p1,*p2;intn;printf("\nEnterstring:");gets(s);n=strlen(s);p1=s;p2=s+n-1;while(p1<p2){if(*p1!=*p2)break;else{p1++;p2--;}}if(p1<p2)printf("No\n");elseprintf("Yes\n");return(0);}5-3參考程序#include<stdio.h>intmain(void){inti;chars[20],*p;printf("\nEnterstring:");gets(s);p=s;for(i=0;i<strlen(s);i+=2)printf("%c",*(p+i));return(0);}5-4參考程序#include<stdio.h>intmain(void){chars[50],*q1,*q2,t;intp,num,k;printf("Inputstring:\n");gets(s);printf("\nInputtheposition:\n");scanf("%d",&p);printf("Inputthenumberofdatathatbesortedagain:\n");scanf("%d",&num);printf("Theoldstringis:\n");puts(s);printf("\n");q1=&s[p-1];q2=&s[p-2+num];for(;q1<&s[p-1]+num/2;q1++,q2--){t=*q1;*q1=*q2;*q2=t;}printf("Thenewarraybis:\n");puts(s);return(0);}5-5參考程序#include<stdio.h>#include<string.h>intmain(void){intm,n=0;charstr[80],str1[80],*p1=str,*p2=str1;printf("Inputastring:\n");gets(p2);printf("Inputm:\n");scanf("%d",&m);if(strlen(p2)<m)printf("Errorinput!\n");while(n<m-1){p2++,n++;}while(*p2!='\0'){*p1=*p2;p1++;p2++;}*p1='\0';printf("Resultis:%s\n",str);return(0);}5-6參考程序#include<stdio.h>intmain(void){charstr1[80],str2[80];char*p,*q;intlocat[10];intj=0,len2,i=0,posit=0;printf("Enterstring1:\n");gets(str1);printf("Enterstring2:\n");gets(str2);q=str2;for(j=0;*q!='\0';j++,q++)len2=j+1;p=str1;q=str2;do{if(*p!=*q){p++;posit++;}else{while((*q!='\0')&&(*q==*p)){q++;p++;posit++;}if(*q=='\0'){locat[i]=posit-len2;i++;}}q=str2;}while(*p!='\0');printf("string1=%s\nstring2=%s\n",str1,str2);printf("num=%d\nposit=:\n",i);for(j=0;j<i;j++)printf("%d",locat[j]);printf("\n");return(0);}5-7參考程序#include<stdio.h>intmain(void){char*str;char*p;intnl=0,cl=0,ol=0;printf("\nEnterstring:");gets(str);p=str;do{if((*p>='0')&&(*p<='9'))nl++;elseif((*p>='a')&&(*p<='z')‖(*p>='A')&&(*p<='Z'))cl++;elseol++;p++;}while(*p!='\0');printf("Stringis%s\n",str);printf("nl=%d,cl=%d,ol=%d\n",nl,cl,ol);return(0);}5-8參考程序#include<stdio.h>#include<string.h>intmain(void){ char*str; inti,k; unsignedlongt=0; printf("Enterastringmadeupof'0'to'9'digitalcharacter:\n"); gets(str); printf("Thestringis:%s\n",str); if(strlen(str)>8) printf("Thestringistoolong!"); else { i=strlen(str); for(--i;i>=0;i--) { k=str[i]-'0'; t=t*10+k; }printf("Theresult:%lu\n",t); }return(0);}第6章函數(shù)6-1參考程序#include<stdio.h>intmain(void){charstr1[50],str2[50];intstrcomp(char*,char*);printf("\nEnterstring1:");gets(str1);printf("\nEnterstring2:");gets(str2);if(strcomp(str1,str2))printf("\nstring1=string2");elseprintf("\nstring1!=string2");return(0);}intstrcomp(char*p1,char*p2){intflag;if(*p1!=*p2)flag=0;else{while((*p1==*p2)&&(*p1!='\0')&&(*p2!='\0')){p1++;p2++;}if((*p1=='\0')&&(*p2=='\0'))flag=1;elseflag=0;}return(flag);}6-2參考程序#include<stdio.h>#include<string.h>intmain(void){chars[81];intpanduan(char*);printf("\nEnterstring:");gets(s);if(panduan(s))printf("Yes\n");elseprintf("No\n");return(0);}intpanduan(char*p1){intn,flag;char*p2;n=strlen(p1);p2=p1+n-1;while(p1<p2){if(*p1!=*p2)break;else{p1++;p2--;}}if(p1<p2)flag=0;elseflag=1;return(flag);}6-3參考程序#include<stdio.h>intmain(void){intn;doublex;doublep(int,double);printf("Entern,x:");scanf("%d,%lf",&n,&x); printf("%3c%6cp(x)\n",'x',''); printf("%.3lf",x);printf("%lf\n",p(n,x));return(0);}doublep(intn,doublex) {if(n==0)return(1);elseif(n==1)return(x);else return(((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n);}6-4參考程序#include<stdio.h>intmain(void){chars[50];intp,num,k;voidsort(chars[],intnum,intp)printf("Inputstring:\n");gets(s);printf("\nInputtheposition:\n");scanf("%d",&p);printf("Inputthenumberofdatathatbesortedagain:\n");scanf("%d",&num);printf("Theoldstringis:\n");puts(s);printf("\n");sort(s,num,p);printf("Thenewarraybis:\n");puts(s);return(0);}voidsort(chars[],intnum,intp){char*q1,*q2,t;q1=&s[p-1];q2=&s[p-2+num];for(;q1<&s[p-1]+num/2;q1++,q2--){t=*q1;*q1=*q2;*q2=t;}}6-5參考程序#include<stdio.h>#include<string.h>intmain(void){intm;charstr1[80],str2[80],*p1=str1,*p2=str2;voidstrcopy(char*,char*,int);printf("Inputastring:\n");gets(p2);printf("Inputm:\n");scanf("%d",&m);if(strlen(p2)<m)printf("Errorinput!\n");strcopy(p1,p2,m);printf("Resultis:%s\n",str1);return(0);}voidstrcopy(char*p1,char*p2,intm){intn=0;while(n<m-1){p2++,n++;}while(*p2!='\0'){*p1=*p2;p1++;p2++;}*p1='\0';}6-6參考程序#include<stdio.h>#include<math.h>#defineN100intmain(void){inta[N],i,n;intfun(int*);n=fun(a);printf("n=%d\n",n); for(i=0;i<n;i++) printf("%d",a[i]);return0;}fun(inta[]){intm,i,k,n=0;for(m=101;m<=200;m=m+2) {k=(int)sqrt(m); for(i=2;i<=k;i++)if(m%i==0)break; if(i>=k+1) {a[n]=m; n=n+1; }}return(n);}6-7參考程序#include<math.h>#include<stdio.h>intmain(void){doublex;doublepai(double);printf("\nPleaseenteraprecision:");scanf("%lf",&x);printf("\neps=%1f,pi=%lf",x,pai(x));return(0);}doublepai(doubleeps){doublex,y;intn;for(n=1,x=0,y=1;y>eps;n++){x+=y;y=n*y/(2*n+1);}return(2*x);}6-8參考程序#defineM10#defineN5#include<stdio.h>#include<string.h>intmain(void){inti;char**q,*p[N],str[N][M];voidsort(char**);for(i=0;i<N;i++)p[i]=str[i];printf("\nEnter%dstrings:\n",N);for(i=0;i<N;i++)scanf("%s",p[i]);q=p;sort(q);printf("\nResultis:");for(i=0;i<N;i++)printf("%s\n",p[i]);return(0);}voidsort(char**p){inti,j;char*t;for(i=0;i<N;i++)for(j=i+1;j<N;j++){if(strcmp(*(p+i),*(p+j))>0) {t=*(p+i); *(p+i)=*(p+j); *(p+j)=t; }}}6-9參考程序#include<stdio.h>intmain(void){intn;intsub(int);printf("\nEntern:");scanf("%d",&n);printf("%d\n",sub(n));return(0);}intsub(intn){inta;if(n==1)return1;a=n+sub(n-1);return(a);}6-10參考程序#include<string.h>#include<stdio.h>#defineN81intmain(void){charstr[N];voidinsert(char*);printf("Entertheoriginalstring:\n");gets(str);insert(str);printf("Thestringaftersorting:%s\n",str);return(0);}voidinsert(char*str){inti,m,j;charch;m=strlen(str);for(i=1;i<m;i++){ch=str[i];j=i-1;while((j>=0)&&(ch<str[j])){str[j+1]=str[j];j--;}str[j+1]=ch;}}6-11參考程序#include<conio.h>#include<stdio.h>intmain(void){intm,k,zz[1000];voidfun(int,int,int*);printf("\nPleaseentertwointegersn,k:");scanf("%d%d",&m,&k);fun(m,k,zz);for(m=0;m<k;m++)printf("%d",zz[m]);printf("\n");return(0);}voidfun(intm,intk,intxx[]){inti,j,n;for(i=m+1,n=0;n<k;i++)//找大于m的素?cái)?shù),循環(huán)k次,即找出緊靠m的k個(gè)素?cái)?shù){for(j=2;j<i;j++)//判斷一個(gè)數(shù)是否為素?cái)?shù),如果不是,跳出此循環(huán),判斷下一個(gè)數(shù)if(i%j==0)break;if(j>=i)//如果是素?cái)?shù),放入數(shù)組xx中xx[n++]=i;}}6-12參考程序#include"stdio.h"#defineN50intmain(void){inta[N],k,i;intfun(int*);k=fun(a); //調(diào)用函數(shù)printf("numbersis%d\n",k); //輸出滿(mǎn)足條件的數(shù)據(jù)的個(gè)數(shù)for(i=0;i<k;i++)printf("%3d",a[i]); //輸出滿(mǎn)足條件的數(shù)據(jù)return(0);}intfun(int*p){inti=0,k,a1,a2;for(k=10;k<=99;k++) {a2=k/10; //取出十位數(shù)a1=k-a2*10; //取出個(gè)位數(shù)if((k%3==0&&a1==5)||(k%3==0&&a2==5))//是否滿(mǎn)足能被3整除、且有一位上的數(shù)是5{p[i]=k; //記下滿(mǎn)足條件的數(shù) i++; //滿(mǎn)足條件的數(shù)的個(gè)數(shù)}}return(i); //返回滿(mǎn)足條件的數(shù)據(jù)的個(gè)數(shù)}第7章文件7-1參考程序#include<stdio.h>#include<stdlib.h>#defineN8intmain(void){charxuehao[N][12],name[N][12];intscore[N][4];doubleaverage[N];voidpaverage(int(*)[4],double*); voidfileread(char(*)[12],char(*)[12],int(*)[4]);voidfilewrite(char(*)[12],char(*)[12],int(*)[4],double*);fileread(xuehao,name,score); paverage(score,average);return(0);filewrite(xuehao,name,score,average);return(0);}voidfileread(char(*fxuehao)[12],char(*fname)[12],int(*fscore)[4]){FILE*fp;chart[5];inti,j;fp=fopen("stuscore.in","r"); if(fp==NULL){printf("cannotopenstuscore.in\n");exit(0);}for(i=0;i<N;i++) {fscanf(fp,"%s",fxuehao[i]); fscanf(fp,"%s",fname[i]); for(j=0;j<4;j++){ fscanf(fp,"%s",t); fscore[i][j]=atoi(t); }}fclose(fp);}voidpaverage(int(*fscore)[4],double*average){inti;for(i=0;i<N;i++)average[i]=(fscore[i][0]+fscore[i][1]+fscore[i][2]+fscore[i][3])/4.0;}voidfilewrite(char(*fxuehao)[12],char(*fname)[12],int(*fscore)[4],double*average){chartit[][10]={"學(xué)號(hào)","姓名","數(shù)學(xué)","物理","英語(yǔ)","計(jì)算機(jī)","平均成績(jī)"};FILE*fp;inti,j,k;fp=fopen("stuscore.out","w"); if(fp==NULL){printf("cannotopenstuscore.out\n");exit(0);}printf("\n%12s%12s%8s%8s%8s%8s%10s",tit[0],tit[1],tit[2],tit[3],tit[4],tit[5],tit[6]);printf("\n------------------------------------------------------------------");for(i=0;i<N;i++){fprintf(fp,"%12s",fxuehao[i]); fprintf(fp,"%12s",fname[i]); for(j=0;j<4;j++) fprintf(fp,"%4d",fscore[i][j]); fprintf(fp,"%5.1f",*(average+i)); fprintf(fp,"\n");}for(k=0;k<N;k++) printf("\n%12s%12s%8d%8d%8d%8d%7.1f",fxuehao[k],fname[k],fscore[k][0],fscore[k][1], fscore[k][2],fscore[k][3],average[k]);printf("\n");fclose(fp); }7-2參考程序#defineN15#include<stdio.h>#include<stdlib.h>intmain(void){inta[N];voidmyread(int*);voidsort(int*);voidmywrite(int*);myread(a);sort(a);mywrite(a);}voidmyread(int*a){FILE*fp;intk;fp=fopen("7-2.dat","r");if(fp==NULL){printf("cannotopen8-11.dat\n");exit(0);}for(k=0;k<N;k++)fscanf(fp,"%d",a+k);fclose(fp);}voidsort(int*a){inti,j,t;for(i=1;i<N;i++)for(j=0;j<N-i;j++) if(*(a+j)>*(a+j+1)) { t=*(a+j);*(a+j)=*(a+j+1);*(a+j+1)=t; }}voidmywrite(int*a){FILE*fp;intk;fp=fopen("7-2.out","w");if(fp==NULL) {printf("cannotopen8-11.out\n");exit(0);}for(k=0;k<N;k++){fprintf(fp,"%d",*(a+k)); printf("%d",*(a+k)); }fclose(fp);}7-3參考程序#include<stdio.h>#include<stdlib.h>intmain(void){inti,j,n;FILE*fp;printf("\nPleaseEntern:");scanf("%d",&n);fp=fopen("8-5.out","w");if(fp==NULL){ printf("Filecannotopen!\n"); exit(0);}for(i=1;i<=n;i++){for(j=1;j<=n;j++)if(j==1||j==i||j==n) { printf("*"); fprintf(fp,"*"); }else { printf(""); fprintf(fp,""); }printf("\n");fprintf(fp,"\n");}fclose(fp);return(0);}7-4參考程序#include"stdio.h"#include"stdlib.h"FILE*fp;intmain(void){intc,d;voidfun1();voidfun2(int);fp=fopen("EX7-4.C","r");if(fp==NULL){ printf("Filecannotopen!"); exit(0);}while((c=fgetc(fp))!=EOF) if(c=='/') //如果是字符注釋的起始字符'/'/ if((d=fgetc(fp))=='*') //則判斷下一個(gè)字符是否為'*'/ fun1(); //調(diào)用函數(shù)處理(刪除)注釋/ else //否則原樣輸出讀入的兩個(gè)字符 { putchar(c); putchar(d); } else if(c=='\''||c=='\"') //判斷是否是字符'或" fun2(c); //調(diào)用函數(shù)處理字符'或"包含的字符 else putchar(c);return(0);}voidfun1(){intc,d;c=fgetc(fp);d=fgetc(fp);while(c!='*'||d!='/'){ //連續(xù)的兩個(gè)字符不是*和/則繼續(xù)處理注釋c=d;d=fgetc(fp);}}voidfun2(intc) //c中存放的是定界符'或"{intd; putchar(c);while((d=fgetc(fp))!=c) //讀入下一個(gè)字符判斷是否是定界符c{ putchar(c); //當(dāng)不是定界符c時(shí)繼續(xù)循環(huán)if(d=='\\') //若出現(xiàn)轉(zhuǎn)義字符\putchar(fgetc(fp)); //則下一個(gè)字符不論是何均原樣輸出}putchar(d);}第8章結(jié)構(gòu)體與共用體8-1參考程序#include<stdio.h>typedefstructcomplex{intreal; //real為復(fù)數(shù)的實(shí)部intim; //im為復(fù)數(shù)的虛部}COMPLEX;intmain(void){COMPLEXza={2,3}; //說(shuō)明結(jié)構(gòu)靜態(tài)變量并初始化COMPLEXzb={4,5};COMPLEXz;voidprint(COMPLEX,COMPLEX,COMPLEX);z=product(za,zb); //調(diào)用product函數(shù),返回值賦給結(jié)構(gòu)變量zprint(za,zb,z); //調(diào)用print函數(shù),輸出計(jì)算結(jié)果return(0);}COMPLEXproduct(COMPLEXza,COMPLEXzb)//計(jì)算復(fù)數(shù)za×zb,函數(shù)的返回值為結(jié)構(gòu)類(lèi)型{COMPLEXw;w.real=za.real*zb.real-za.im*zb.im;w.im=za.real*zb.im+za.im*zb.real;return(w); //返回計(jì)算結(jié)果,返回值的類(lèi)型為結(jié)構(gòu)}voidprint(COMPLEXza,COMPLEXzb,COMPLEXz) //輸出復(fù)數(shù)za×zb=z{printf("(%d+%di)*(%d+%di)=",za.real,za.im,zb.real,zb.im);printf("(%d+%di)\n",z.real,z.im);}8-2參考程序#include"stdio.h"structstudent{intnum;charname[20];intscore;};FILE*fp;intmain(void){charc;inti;structstudenta[5];voidprint(structstudent*);voidoutput(structstudent*);print(a);output(a);return(0);}voidprint(structstudenta[]){inti;if((fp=fopen("8-2.dat","r"))==NULL){printf("Cannotopenfile");exit(0);}for(i=0;i<5;i++){fscanf(fp,"%d%s%d",&a[i].num,a[i].name,&a[i].score);printf("\n%d%s%d",a[i].num,a[i].name,a[i].score);}fclose(fp);}voidoutput(structstudenta[]){inti;fp=fopen("8-2.out","w");for(i=0;i<5;i++){if(a[i].score>60){ fprintf(fp,"%d%s%d",a[i].num,a[i].name,a[i].score); printf("\n%d%s%d",a[i].num,a[i].name,a[i].score);}}}8-3參考程序#include<stdio.h>#include<string.h>#defineMAX10structwork{charname[10];chartel[10];};intmain(void){structworks[MAX];intnum;charname[5];voidreadin(structwork*,int*);voidsearch(structwork*,char*,int);readin(s,&num);printf("Entername:");gets(name);search(s,name,num);return(0);}voidreadin(structwork*a,int*n){inti=0;FILE*fp;if((fp=fopen("9-3.dat","r"))==NULL){printf("\nFilenotfound");exit(0);}while(!feof(fp)){fscanf(fp,"%s%s",a[i].name,a[i].tel);i++;}*n=--i;}voidsearch(structwork*a,char*name,intn){inti=0,flag=0;while(i<=n){if(strcmp(a[i].name,name)!=0) i++;else {flag=1;break;}}if(flag)

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論