版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
word文档可自由复制编辑习题参考答案第1章C/C++语言简单程序的编写和调试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语言程序设计基础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+年利率×存款年数]#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章控制结构3-1参考程序if语句参考程序:#include<stdio.h>intmain(void){floatresult;printf("Entergrade=");scanf("%f",&result);if(result>=0&&result<=100){ if(result>=90)printf("优\n");elseif(result>=80)printf("良\n");elseif(result>=70)printf("中\n");elseif(result>=60)printf("及格\n");elseprintf("不及格\n");printf("Result=%.2f",result);}elseprintf("ERROR");return(0);}switch语句参考程序:#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("中\n");break;case8:printf("良\n");break;case9:case10:printf("优\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("最大公约数是:%d\n",a);printf("最小公倍数是:%d\n",c*d/a);return(0);}3-6参考程序#include<stdio.h>#include<math.h>intmain(void){floatx1,x2,x,err,root;printf("请输入精度:");scanf("%f",&err);printf("请输入上界:");scanf("%f",&x1);printf("请输入下界:");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整除时余数为1的最小10个自然数是:\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不是一个完全平方数!\n",s); } else { printf("%d=%d*%d*%d*%d\n",s,n,n+1,n+2,n+3); printf("%d是一个完全平方数!\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是一个完全平方数!\n",s); } elseprintf("%d不是一个完全平方数!\n",s);return(0);}第4章数组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++) //长度为2的子串在主串中出现一次,则j+1,如此循环if(str[i]==substr[0]&&str[i+1]==substr[1])j++; //子串在主串中出现的次数printf("\n子串在主串中出现的次数=%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请输入你想要转换的十进制数:");scanf("%d",&num);printf("\n请输入要转换的进制(2<=n<=16):");scanf("%d",&n);while(num){temp=num%n;num=num/n;output[i++]=ch[temp];}output[i]='\0';printf("转换为%d进制后为:",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章函数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的素数,循环k次,即找出紧靠m的k个素数{for(j=2;j<i;j++)//判断一个数是否为素数,如果不是,跳出此循环,判断下一个数if(i%j==0)break;if(j>=i)//如果是素数,放入数组xx中xx[n++]=i;}}6-12参考程序#include"stdio.h"#defineN50intmain(void){inta[N],k,i;intfun(int*);k=fun(a); //调用函数printf("numbersis%d\n",k); //输出满足条件的数据的个数for(i=0;i<k;i++)printf("%3d",a[i]); //输出满足条件的数据return(0);}intfun(int*p){inti=0,k,a1,a2;for(k=10;k<=99;k++) {a2=k/10; //取出十位数a1=k-a2*10; //取出个位数if((k%3==0&&a1==5)||(k%3==0&&a2==5))//是否满足能被3整除、且有一位上的数是5{p[i]=k; //记下满足条件的数 i++; //满足条件的数的个数}}return(i); //返回满足条件的数据的个数}第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]={"学号","姓名","数学","物理","英语","计算机","平均成绩"};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))=='*') //则判断下一个字符是否为'*'/ fun1(); //调用函数处理(删除)注释/ else //否则原样输出读入的两个字符 { putchar(c); putchar(d); } else if(c=='\''||c=='\"') //判断是否是字符'或" fun2(c); //调用函数处理字符'或"包含的字符 else putchar(c);return(0);}voidfun1(){intc,d;c=fgetc(fp);d=fgetc(fp);while(c!='*'||d!='/'){ //连续的两个字符不是*和/则继续处理注释c=d;d=fgetc(fp);}}voidfun2(intc) //c中存放的是定界符'或"{intd; putchar(c);while((d=fgetc(fp))!=c) //读入下一个字符判断是否是定界符c{ putchar(c); //当不是定界符c时继续循环if(d=='\\') //若出现转义字符\putchar(fgetc(fp)); //则下一个字符不论是何均原样输出}putchar(d);}第8章结构体与共用体8-1参考程序#include<stdio.h>typedefstructcomplex{intreal; //real为复数的实部intim; //im为复数的虚部}COMPLEX;intmain(void){COMPLEXza={2,3}; //说明结构静态变量并初始化COMPLEXzb={4,5};COMPLEXz;voidprint(COMPLEX,COMPLEX,COMPLEX);z=product(za,zb); //调用product函数,返回值赋给结构变量zprint(za,zb,z); //调用print函数,输出计算结果return(0);}COMPLEXproduct(COMPLEXza,COMPLEXzb)//计算复数za×zb,函数的返回值为结构类型{COMPLEXw;w.real=za.real*zb.real-za.im*zb.im;w.im=za.real*zb.im+za.im*zb.real;return(w); //返回计算结果,返回值的类型为结构}voidprint(COMPLEXza,COMPLEXzb,COMPLEXz) //输出复数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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 污水处理瓦工施工合同篇
- 墙板施工合同商场内部装修
- 高铁维护合同执行台账
- 旅游度假区建设项目土地租赁合同
- 主题公园内部墙面翻新刮瓷合同
- 医疗中心空调系统安装合同
- 居民小区地坪施工承包合同
- 咨询项目部顾问聘用合同
- 2025工程建设项目招标投标合同书
- 2025危险物品承包运输合同范本
- 2024-2025学年高一【数学(人教A版)】函数的零点与方程的解-教学设计
- 中建型钢混凝土结构施工方案
- 【课件】程式与意蕴-中国传统绘画+课件-2024-2025学年高中美术人美版(2019)美术鉴赏
- 社会学概论-终结性考核-国开(SC)-参考资料
- 2022年全国应急普法知识竞赛试题库大全-上(单选题库-共4部分-1)
- 2024年厂长岗位聘用合同范本版B版
- 船用动力系统电气化改造实践
- 2024河北石家庄市辛集市大学生乡村医生专项计划招聘5人笔试备考试题及答案解析
- 木制品加工销售承包协议
- 实+用法律基础-形成性考核任务三-国开(ZJ)-参考资料
- 汽车修理厂喷漆合作合同
评论
0/150
提交评论