C语言程序设计课后答案罗朝盛_第1页
C语言程序设计课后答案罗朝盛_第2页
C语言程序设计课后答案罗朝盛_第3页
C语言程序设计课后答案罗朝盛_第4页
C语言程序设计课后答案罗朝盛_第5页
已阅读5页,还剩34页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第1章习题参照答案(略)判断题1

2

3

4

5√

×

×

√填空题1)main2)函数的首部、函数体3){、}4)main、main5)语句、分号(;)选择题1

2

3

4

5C

C

C

A

B选择题(1)#include<>main(){inta,b,s;floatt;printf("EnterA,B=?\n");scanf("%d%d",&a,&b);s=a+b;t=a*b;printf("s=%d,t=%f\n",s,t);}2)#include<>main(){printf("=================\n");printf("Hello!\n");printf("Howdoyoudo\n");printf("=================\n");}3)#include<>main(){intr,h;floatv;printf("EnterR,H=?\n");scanf("%d%d",&r,&h);v=*r*r*h;printf("v=%f\n",v);}第2章习题参照答案一.(略)二.填空题b(1)acbed2ab2)3)164)105)16、16、116)4、10、67)18、3、38)sin(65*180)+2*(+exp(y))/(x-y)9)25、2510)2711)412)0三、选择题1

2

3

4

5

6

7

8

9

1011

12D

D

C

B

D

A

D

B

C

D

A

B四、编程题1.#include<>main(){intx,a,b,c;printf("Enteraintgernumberx=?\n");scanf("%d",&x);a=x/100;b=x%100/10;c=x%10;printf("%d%d%d\n",a,b,c);}2.#include<>main(){unsigneda,b,r1,r2,r3;scanf("%u%u",&a,&b);r1=a&b;r2=a|b;r3=a^b;printf("r1=%u,r2=%u,r3=%u\n",r1,r2,r3);getch();}3.#include<>main(){floata,b,h,s;scanf("%f%f?%f",&a,&b,&h);s=(a+b)*h/2;printf("s=%f\n",s);}第3章习题参照答案一、判断题1

2

3

4

5

×

×

×填空题语句表达式语句5{}函数由“%”和格式字符&getchar()1027,33,1b11x(1)=x(2)=125+6程序阅读题1、p=p=p=p=p=p=2、a----ASCII:97a----ASCII:97a----ASCII:97编程题1、#include<>main(){inta,b;floatr1,r2;scanf("%d%d",&a,&b);r2=a+b;r1=a*b/r2;printf("r1=%0.2f,r2=%0.2f\n",r1,r2);}2、#include<>main(){floatc,f;printf("InputC=");scanf("%f",&c);f=5*c+32;printf("F=%f\n",f);}3、#include<>voidmain(){floatc1,c2,c3,ver,s;printf("Inputa,bandc:");scanf("%f%f%f",&c1,&c2,&c3);s=c1+c2+c3;ver=s/3;printf("totals=%f,ver=%f\n",s,ver);}}第4章习题参照答案判断题1

2

3

4

5

×

×

√选择题1B

2B

3B

4B

5D

6A

7B

8B程序阅读题1)1052)a=83)abc123DEF4)程序填空题(1)ch(2)div=n(3)min=mark

ch==')'div—min=mark

count--max=mark

count==0sum+=mark

count>0编程题(1)#include<>voidmain(){intx,y;printf("EntertheX,Y=?\n");scanf("%d,%d",&x,&y);if(x>0&&y>0)printf("(%d,%d)是第一像限\n",x,y);elseif(x>0&&y<0)printf("(%d,%d)是第四像限\n",x,y);elseif(x<0&&y>0)printf("(%d,%d)是第三像限\n",x,y);elseif(x<0&&y<0)printf("(%d,%d)是第二像限\n",x,y);elseif(x>0&&y==0)printf("(%d,%d)在X轴正方向\n",x,y);elseif(x<0&&y==0)printf("(%d,%d)在X轴负方向\n",x,y);elseif(x==0&&y>0)printf("(%d,%d)在Y轴正方向\n",x,y);elseif(x==0&&y<0)printf("(%d,%d)在Y轴负方向\n",x,y);elseprintf("(%d,%d)为坐标原点\n",x,y);}2)#include<>#include<>main(){floata,b,c,x,s;printf("Inputa,bandc:");scanf("%f%f%f",&a,&b,&c);if(a+b<=c||a+c<=b||b+c<=a)printf("Error!\n");else{x=(a+b+c)/2;s=sqrt(x*(x-a)*(x-b)*(x-c));printf("area=%f\n",s);}}(3)程序代码以下:#include<>voidmain(){floatsalary,tax;intntax;printf("\nPleaseenterasalary=");scanf("%f",&salary);ntax=salary/500;if(ntax>=10)ntax=10;switch(ntax){case0:case1:tax=0;break;case2:tax=(salary-1000)*;break;case3:case4:tax=500*+(salary-1500)*;break;case5:case6:tax=500*+1000*+(salary-2500)*;break;case7:case8:case9:tax=500*+1000*+1000*+(salary-3500)*;break;case10:tax=500*+1000*+1000*+1500*+(salary-5000)*;}printf("\nThetax=%10.2f\n",tax);}4)方法一#include<>#include<>main(){inti=1;floatt=-1,s=0;do{t=-t/i;s=s+t;i=i+1;}while(fabs(t)>1e-6);printf("s=%f\n",s);}方法二#include<>voidmain(){inti=1,sign=1;floatt=,s=;whilet>1e-6){t=t*i;s=s+sign/t;i=i+1;sign=-sign;};printf("s=%f\n",s);}(5)方法一:#include<>main(){inti,a,b,c;for(i=100;i<=999;i++){a=i/100;b=i/10%10;c=i%10;if(i==a*a*a+b*b*b+c*c*c)printf("%d\n",i);}}方法二:#include<>voidmain(){inti,a,b,c;for(a=1;a<=9;a++)for(b=0;b<=9;b++)for(c=0;c<=9;c++){i=a*100+b*10+c;if(i==a*a*a+b*b*b+c*c*c)printf("%d\n",i);}}6)#include<>main(){inti,j;for(i=1;i<=4;i++){for(j=1;j<=6-i;j++)putchar('');for(j=1;j<=i*2-1;j++)printf("%c",65+i-1);printf("\n");}for(i=3;i>=1;i--){for(j=1;j<=6-i;j++)putchar('');for(j=1;j<=i*2-1;j++)printf("%c",65+i-1);printf("\n");}}7)#include<>main(){inta,b,c;for(a=0;a<=20;a++)for(b=0;b<=33;b++){c=100-a-b;if(a*5+b*3+c/==100)printf("%d,%d,%d\n",a,b,c);}}8)#include<>#include<>main(){floatx,x0,a;scanf("%f",&a);x=a;do{x0=x;x=2/*x0+a/(3*x0*x0);}while(fabs(x-x0)>1e-6);printf("x=%f\n",x);}9)#include<>#include<>main(){floatpi,pi0;inti=1pi=2;do{pi0=pi;pi=pi**i/*i-1))**i/*i+1));i=i+1;}while(fabs(pi-pi0)>1e-6);printf("pi=%f\n",pi);}(10)#include<>voidmain(){inti,j,s;for(i=6;i<=1000;i++){s=1;for(j=2;j<=i/2;j++)if(i%j==0)s+=j;if(s==i)/*判断是否是合数*/{printf("%d=1",i);/*打印输出合数及for(j=2;j<=i/2;j++)/*打印输出各因子

=1*/*/if(i%j==0)printf("+%d",j);printf("\n");}}}(11)#include<>#include<>voidmain(){intx,y,z,k;k=sqrt(2000);for(x=0;x<=k;x++)for(y=0;y<=k;y++)for(z=0;z<=k;z++)if(x*x+y*y+z*z==2000)printf("%d,%d,%d\n",x,y,z);}第5章习题参照答案判断题1

2

3

4

5

6

7

8

9

10111213

1415√×

√×

×√×

××

××

××

××填空题1)322)43)str1[7]='\0';4)for(i=1;i<=6;i++)gets(a[i]);5)strcpy(web,"");6)int*p=a;7)58)19)*(p+6)或*(s+6)选择题1B

2C

3B

4A

5B

6C

7B

8A

9C

10D

11B

12D

13B

14A

15D程序阅读题1)62)str3)604)bcdefgacdefgabdefgabc5)a1e25程序填空题(1)#include<>scanf("%f",&a[i])(2)scanf("%d",&n)a[i][i]=1(3)b[i]!='\0'strcpyputs(b)(4)p=a[0];或p=&a[0][0];*p六、编程题1、使用数组和指针2种方法来办理。

s=0;a[i-1][j-1]

x=a[0]printf("\n")

fabs(a[i]-s)方法一(使用数组)#include<>voidmain(){inti=0,a[10],t;printf("Enterthe10number=\n");for(i=0;i<10;i++)scanf("%d",&a[i]);for(i=0;i<5;i++){t=a[i];a[i]=a[9-i];a[9-i]=t;}for(i=0;i<10;i++)printf("%d",a[i]);printf("\n");}方法二(使用指针)#include<>voidmain(){inti=0,a[10],t,*p,*q;printf("Enterthe10number=\n");for(p=a;p<a+10;p++)scanf("%d",p);p=a;q=a+9;for(;p<q;p++,q--){t=*p;*p=*q;*q=t;}for(p=a;p<a+10;p++)printf("%d",*p);printf("\n");}2、#include<>main(){inti,j,t,n,a[11]={0,1,2,3,4,5,6,7,8,9,10};for(i=0;i<10;i++)printf("%d",a[i]);printf("\n");printf("EntertheMoven=?");scanf("%d",&n);for(i=1;i<=n;i++){t=a[10];for(j=9;j>=0;j--)a[j+1]=a[j];a[0]=t;}for(i=0;i<=10;i++)printf("%d",a[i]);getch();}3、#include<>#include<>main(){inta[100],b[100],i,j,p,n=0,t;randomize();for(i=0;i<100;i++){a[i]=random(100);if(a[i]%2==0)b[n++]=a[i];}for(i=0;i<n-1;i++){p=i;for(j=i+1;j<n;j++)if(b[j]>b[p])p=j;t=b[p];b[p]=b[i];b[i]=t;}for(i=0;i<n;i++){printf("%d",b[i]);if(i%10==9)printf("\n");}getch();}4、#include<>#include<>#defineN50main(){inta[N],i,j,p,t;randomize();for(i=0;i<N;i++){a[i]=random(100);for(j=0;j<i;j++)if(a[i]==a[j]){i--;break;}}for(i=0;i<N-1;i++){p=i;for(j=i+1;j<N;j++)if(a[j]>a[p])p=j;t=a[p];a[p]=a[i];a[i]=t;}for(i=0;i<N;i++){printf("%d",a[i]);if(i%10==9)printf("\n");}}4,#include<>#defineN10voidmain(){inti;floata[N],s=,x0=;for(i=0;i<N;i++){scanf("%f",&a[i]);x0+=a[i];}x0/=N;for(i=0;i<N;i++)s+=(a[i]-x0)*(a[i]-x0);printf("%10.6f",s);}5,#defineN4#defineM3voidmain(){inti,j,sum,a[N][M+1],max,p;printf("inputscore\n");for(i=0;i<N;i++){sum=0;for(j=0;j<M;j++){scanf("%d",&a[i][j]);sum=sum+a[i][j];

/*i代表学生序号*//*每位学生成绩输入前,其总成绩赋初值/*j代表课程代号*//*输入第i号学生第j门课程的成绩*//*累加求总成绩*/

0*/}a[i][M]=sum;

/*求第

i个学生的总成绩放到

a[i][M]*/}max=a[0][M];p=0;for(i=1;i<N;i++)

/*查找总成绩最高的学生

*/if(a[i][M]>max)p=i;printf("No.%dTotalscore:%d\n",p+1,a[p][M]);}7、#include<>voidmain(){inta[5][6];inti,j,mi,mj;for(i=0;i<5;i++)for(j=0;j<6;j++){printf("inputa[%d][%d]:",i,j);scanf("%d",&a[i][j]);}mi=0;mj=0;for(i=0;i<5;i++){for(j=0;j<6;j++){if(a[i][j]<a[mi][mj]){mi=i;mj=j;}printf("%d",a[i][j]);}printf("\n");}printf("min=%d",a[mi][mj]);}6,#include<>#include<>voidmain(){floata[5][6];inti,j,max;printf("Enterarraya=?\n");for(i=0;i<5;i++)for(j=0;j<6;j++)scanf("%f",&a[i][j]);for(i=0;i<5;i++){max=a[i][0];for(j=0;j<6;j++)/*查找第i行中绝对值最大的元素*/if(fabs(a[i][j])>fabs(max))max=a[i][j];for(j=0;j<6;j++)/*第i行中所有元素除以绝对值最大的元素

*/{a[i][j]=a[i][j]/max;printf("%",a[i][j]);}printf("-----max=%\n",max);}}7,#include<>#include<>#defineN20main(){charstr[N][15],st[15],*p=st;inti,j;for(i=0;i<N;i++)scanf("%s",str[i]);for(i=0;i<N;i++)for(j=0;j<N-i-1;j++)if(strcmp(str[j],str[j+1])>0){strcpy(p,str[j]);strcpy(str[j],str[j+1]);strcpy(str[j+1],p);}for(i=0;i<N;i++)printf("%s\n",str[i]);}10、程序以下:#include<>main(){inta[10],b[10],t;intn=0,m=0,i,j,f;printf("InputArraya,endwith–1:");scanf("%d",&t);while(n<10&&t!=-1){a[n++]=t;scanf("%d",&t);}printf("InputArrayb,endwith–1:");scanf("%d",&t);while(m<10&&t!=-1){b[m++]=t;scanf("%d",&t);}for(i=0;i<n;i++)/*打印只在数组{

a中出现的元素

*/f=1;for(j=0;j<m;j++)if(a[i]==b[j]){f=0;break;}if(f)printf("%d",a[i]);}for(i=0;i<m;i++)/*打印只在数组b中出现的元素*/{f=1;for(j=0;j<n;j++)if(b[i]==a[j]){f=0;break;}if(f)printf("%d",b[i]);}}11、#include<>main(){charstr[81];inti=-1;printf("Input:");scanf("%s",str);while(++i<80&&str[i]!='\0')if(str[i]=='$')str[i]='S';puts(str);}12、#include<>voidmain(){charx[80],y[26];inti,j,ny=0;gets(x);for(i=0;x[i]!='\0';i++)if(x[i]>='A'&&x[i]<='Z'){for(j=0;j<ny;j++)if(y[j]==x[i])break;if(j==ny){y[ny]=x[i];ny++;}}for(i=0;i<ny;i++)printf("%c",y[i]);printf("\n");}13、#include<>voidmain(){intm,bin[32],j;scanf("%d",&m);for(j=0;m!=0;j++){bin[j]=m%2;m=m/2;}for(;j!=0;j--)printf("%d",bin[j-1]);}14、#include<>#include<>#include<>voidmain(){chars[80];inti=0;gets(s);while(s[i]!='\0')if(isdigit(s[i]))strcpy(s+i,s+i+1);elsei++;puts(s);}15、voidmain(){inta,b,c;int*p1,*p2,*p3,*t;printf("\nPleaseinputthreeintegers:\n");scanf("%d,%d,%d",&a,&b,&c);p1=&a,p2=&b,p3=&c;if(*p1>*p2)t=p1,p1=p2,p2=t;if(*p1>*p3)t=p1,p1=p3,p3=t;if(*p2>*p3)t=p2,p2=p3,p3=t;printf("neworder:\n");printf("%d,%d,%d",*p1,*p2,*p3);}16、voidmain(){charst[80],ss[20],sp[20],*a=st,*b=ss,*t=sp;inti,n,m,p=-1;printf("\nPleaseinputtwostring:\n");gets(a);gets(b);n=strlen(a);m=strlen(b);for(i=0;i<=n-m+1;i++){a=st+i;strncpy(t,a,m);

/*将字符指针

a指向的字符串取前

m个字符复制到

t所指的字符串中*/if(strcmp(b,t)==0){p=i;break;}}if(p==-1)printf("%d",p);elseprintf("%d",p+1);

/*因数组下标从

0开始,所以输出

p+1*/getch();}17、#include<>voidmain(){charst[80],*p=st,ch;printf("Enterastring\n");gets(p);while(*p!='\0'){ch=*p;if(ch>='A'&&ch<='Z')ch='A'+('Z'-ch);elseif(ch>='a'&&ch<='z')ch='a'+('z'-ch);*p=ch;/*变换后的字符代替原字符p++;/*指针指向下一个字符*/}/*指针指向字符串首字符

*/

*/p=st;puts(p);getch();}18、#include<>voidmain(){charst[80],*p=st,ch;longk=0;printf("Enterastring\n");gets(p);strupr(p);while(*p!='\0'){ch=*p;if(ch>='0'&&ch<='9'||ch>='A'&&ch<='F')p++;elsestrcpy(p,p+1);/*滤去所有的非十六进制字符*/}p=st;while(*p!='\0')/*将十六制数变换成十进数*/{ch=*p;if(ch>='0'&&ch<='9')k=16*k+(ch-'0');elseif(ch>='A'&&ch<='F')k=16*k+(10+ch-'A');p++;}printf("%ld\n",k);}第6章习题参照答案判断题1

2

3

4

5

6

7

8

9

10×

×

×

×

×

×

×选择题1C

2B

3A

4C

5D填空题1)char*printch(int,char);2)intstrlen(char*);3)intmin(int*);4)intrandom(void);5)intdigits(double,int);程序阅读题1)(127)10=(7F)16(2)303)a=64,b=64(4)(3,6)程序填空题(1)random(10)+’0’s,N,c(2)int*mx<v[k]v[k+1](3)sign=-1digit=0digit=digit*10+(s[k]-(4){,,,,}floatx,float*a,intnreturny

a[I]-

‘0’

‘0’)编程题(1)#include<>intjfnum(intn);main(){inti;for(i=100;i<=999;i++)if(judge(i))printf("%d\n",i);}intjudge(intn){intn1,n2,n3,flag;if(n<100||n>999)printf("error!");else{n1=n%10;n2=(n/10)%10;n3=n/100;if(n==n1*n1*n1+n2*n2*n2+n3*n3*n3)flag=1;elseflag=0;}return(flag);}2)floatmysqr(floata,floateps){floatx0,x1,t;if(a<0)printf("error!");else{x0=a/;do{x1=(x0+a/x0)/;t=fabs(x1-x0);x0=x1;}while(t>eps);}return(x1);}3)voidcode(chars[],intn){inti;for(i=0;i<n;i++)if('A'<=s[i]&&s[i]<='Z'||'a'<=s[i]&&s[i]<='z'){s[i]=s[i]+5;if(s[i]>'z'||s[i]>'Z'&&s[i]<'a')s[i]=s[i]-26;}}voidrecode(chars[],intn){inti;for(i=0;i<n;i++)if('A'<=s[i]&&s[i]<='Z'||'a'<=s[i]&&s[i]<='z'){s[i]=s[i]-5;if(s[i]<'A'||s[i]<'a'&&s[i]>'Z')s[i]=s[i]+26;}}4)intleapyear(intyear);main(){intyear;printf("Inputyearnumber:");scanf("%d",&year);if(leapyear(year)==1)printf("\n%disleapyear\n",year);elseprintf("\n%disnotleapyear\n",year);}intleapyear(intyear){if(year%4==0&&year%100!=0||year%400==0)return(1);elsereturn(0);}5)#include<>char*search(char*s,charc){intflag;flag=0;while(*s!='\0'){if(*s==c){flag=1;break;}s++;}if(flag==1)return(s);elsereturn(NULL);}6)#include<>voidmaxmin(inta,intb,intc,int*max,int*min);main(){inta,b,c,max,min;printf("\nPleaseinputthreeintegers:\n");scanf("%d,%d,%d",&a,&b,&c);maxmin(a,b,c,&max,&min);printf("max=%d,min=%d",max,min);}voidmaxmin(inta,intb,intc,int*max,int*min){if(a>b){*max=a;*min=b;}if(c>*max)*max=c;if(c<*min)*min=c;}(7)#include<>#include<>voidgetdata(inta[],intn);/*随机生成数据函数的原型声明*/voidprintdata(inta[],intn);/*输出数据函数原型声明*/voidsort(inta[],intn);/*排序函数原型声明*/floataverage(inta[],intn);/*求平均值函数原型声明*/voidcount(inta[],intn,floatx,intb[]);

/*统计函数原型声明

*/main(){inta[20],b[3];floatx;getdata(a,20);printf("\nInitialize:\n");printdata(a,20);sort(a,20);printf("\nSort:\n");printdata(a,20);x=average(a,20);printf("\nAverage:%f\n",x);count(a,20,x,b);printf("\n>=<:\n");printdata(b,3);}voidgetdata(inta[],intn){inti;randomize();for(i=0;i<n;i++)a[i]=(rand()%90)+10;}voidprintdata(inta[],intn){inti;for(i=0;i<n;i++){if(i%5==0)printf("\n");printf("%d",a[i]);}}voidsort(inta[],intn){inti,j,k,t;for(i=0;i<n-1;i++){k=i;for(j=i+1;j<n;j++)if(a[k]>a[j])k=j;t=a[i];a[i]=a[k];a[k]=t;}}floataverage(inta[],intn){floats;inti;s=;for(i=0;i<n;i++)s=s+a[i];return(s/n);}voidcount(inta[],intn,floatx,intb[]){inti;b[0]=b[1]=b[2]=0;for(i=0;i<n;i++)if(a[i]>x)b[0]++;elseif(a[i]==x)b[1]++;elseb[2]++;}8)#include""voidinvert(char*string){intn;char*h,*t,c;n=strlen(string);h=string;t=h+n-1;while(h<t){c=*h;*h=*t;*t=c;h++;t--;}}9)voidmystrcpy(char*s1,char*s2,intm){s2=s2+m-1;while(*s2!='\0'){*s1=*s2;s1++;s2++;}*s1='\0';}10)intf(intn){if(n<0)printf("error");else{if(n==0)return0;elseif(n==1)return1;elsereturnf(n-2)+f(n-1);}}11)方法一、使用循环#include""#include""floatyx(floatx){floaty0,y1;if(x<0)printf("error");else{y0=sqrt(x);y1=sqrt(x+y0);while(fabs(y1-y0)>{y0=y1;y1=sqrt(x+y0);}}returny1;}方法二:使用递归方法#include<>#include<>doublefunc(intxcount,doublex)/*xcount是根号的个数*/{doubley=0;if(xcount==1)y=sqrt(x);elsey=sqrt(func(xcount-1,x)+x);returny;}voidmain(){intxcount;doubleresult,x;scanf("%d%lf",&xcount,&x);if(x<=0)/*x小于0出错*/printf("DateError!");elseprintf("%lf",func(xcount,x));getch();}12)#include""voidmaxa(inta[3][4]);voidverse(inta[3][4]);voidmain(){inti,j,a[3][4];for(i=0;i<3;i++)for(j=0;j<4;j++)scanf("%d",&a[i][j]);maxa(a);verse(a);getch();}voidmaxa(inta[3][4]){inti,j,imax=0,jmax=0;for(i=0;i<3;i++)for(j=0;j<4;j++)if(a[i][j]>a[imax][jmax]){imax=i;jmax=j;}printf("Themaxnumberisa[%d][%d]=%d\n",imax,jmax,a[imax][jmax]);}voidverse(inta[3][4]){inti,j,imax=0,jmax=0;for(j=0;j<4;j++){for(i=0;i<3;i++)printf("%d",a[i][j]);printf("\n");}}第7章习题参照答案判断题1

2

3

4

5

6√

×

×

×

×选择题1B

2B

3C

4D

5B

6A填空题1)程序编译从前2)#include“”3)c=c-324)25)5316)z=0编程题1)#include<>#defineRN(x)if(x%4==0&&x%100!=100||x%400==0)printf("YES");elseprintf("NO");voidmain(){intn;scanf("%d",&n);RN(n)}2)#include<>#defineMIN(x,y,z)printf("%d",(x<y?x:y)<z?(x<y?x:y):z);voidmain(){inta,b,c;scanf("%d%d%d",&a,&b,&c);MIN(a,b,c)}3)#include<>#defineINPUT(a,n)for(i=0;i<n;i++)scanf("%f",&a[i]);#defineAVERAGE(a,n,ave)for(i=0;i<n;i++)ave+=a[i];voidmain(){floata[5],ave=0;inti;INPUT(a,5)AVERAGE(a,5,ave)printf("%f",ave/10);}第8章习题参照答案1.思虑题(略)2.判断题:123456√×××√√3.选择题12345678ADDBDADD4.程序阅读1)2,4,6,8,10,。2)(3)Shen。(4)18。(5)1-575.程序填空(1)p->next!=NULLinfop=p->next;(2)p1->next!=NULLp0->next=p1;p0->next=NULL;(3)s[k].avers[k].aver>s[maxsub].avermaxsub=k(4)max=i;min=i;stud[max].name,stud[max].scorestud[min].name,stud[min].score5)headn++p->next6.编程题1)structcomplx{intreal;intim;};main(){structcomplxc1,c2,c3;scanf("%d+%di",&,&;scanf("%d+%di",&,&;=*=*+*;printf("(%d+%di)*(%d+%di)=%d+%di",,,,,,;getch();}(2)#include<>#include<>main()#defineN10structstudent{longnum;charname[10];floatscore[3];floatave;};main(){voidinput(structstudents[],intn);/*输入n个学生数据*/floataverage(structstudents[],intn);/*计算第n个学生的平均成绩*/intmax(structstudents[],intn);/*计算n个学生中哪个学生的平均成绩最高structstudents[N];

*/inti,m;floata=0;/*总平均成绩*/input(s,N);for(i=0;i<N;i++){s[i].ave=average(s,i);a=a+s[i].ave;}a=a/N;printf("NumberNameScore-1Score-2Score-3Average\n");for(i=0;i<N;i++)printf("%ld%10s%f%f%f%f\n",s[i].num,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave);printf("Thetotalaveragescoreis%f\n",a);m=max(s,N);printf("Themaxscorestudent:\n");printf("Number:%ld\nName:%s\nScore1:%f\nScore2:%f\nScore3:%f\nAverageScore:%f\n",s[m].num,s[m].name,s[m].score[0],s[m].score[1],s[m].score[2],s[m].ave);}voidinput(structstudents[],intn){inti;for(i=0;i<n;i++){printf("Pleaseinputstudentnumber:");scanf("%ld",&s[i].num);printf("Pleaseinputstudentname:");scanf("%s",s[i].name);printf("Pleaseinputstudentscore1:");scanf("%f",&s[i].score[0]);printf("Pleaseinputstudentscore2:");scanf("%f",&s[i].score[1]);printf("Pleaseinputstudentscore3:");scanf("%f",&s[i].score[2]);}}floataverage(structstudents[],intn){inti;floata=0;for(i=0;i<3;i++)a=a+s[n].score[i];a=a/3;return(a);}intmax(structstudents[],intn){inti,m=0;for(i=1;i<n;i++)if(s[i].ave>s[m].ave)m=i;return(m);}3)structdate{intyear;intmonth;intday;};intdays(structdated){intn,t;if%4==0&&%100!=0||%400==0)t=29;elset=28;switch{case1:n=;break;case2:n=+31;break;case3:n=+31+t;break;case4:n=+31+t+31;break;case5:n=+31+t+31+30;break;case6:n=+31+t+31+30+31;break;case7:n=+31+t+31+30+31+30;break;case8:n=+31+t+31+30+31+30+31;break;case9:n=+31+t+31+30+31+30+31+31;break;case10:n=+31+t+31+30+31+30+31+31+30;break;case11:n=+31+t+31+30+31+30+31+31+30+31;break;case12:n=+31+t+31+30+31+30+31+31+30+31+30;}return(n);}main(){structdated={2006,3,15};printf("%d",days(d));getch();}4)structks{intreg;char*name;charsex;char*birthday;intscore[6];};structksinput(){structkss;inti,sum=0;printf("register:");scanf("%d",&;printf("name:");scanf("%s",;printf("sex:");scanf("%c",&;printf("birthday:");scanf("%s",;for(i=0;i<5;i++){printf("score-%d:",i+1);scanf("%d",&[i]);sum=sum+[i];}[5]=sum;return(s);}voidprint(structkss){inti;printf("register:%d\n",;printf("name:%s\n",;printf("sex:%c\n",;printf("birthday:%s\n",;for(i=0;i<5;i++)printf("score-%d:%d\n",i+1,[i]);printf("sum:%d\n",[5]);getch();}structkssearch(structkss[],intn){inti,p=0;for(i=1;i<n;i++)if(s[i].score[5]>s[p].score[5])p=i;return(s[p]);}voidsort(structkss[],intn){inti,j,p;structkst;for(i=0;i<n-1;i++){p=i;for(j=i+1;j<n;j++)if(s[j].reg<s[p].reg)p=j;t=s[i];s[i]=s[p];s[p]=t;print(s[i]);}print(s[n-1]);}5)#defineNULL0#defineLENsizeof(structstudent)structstudent{longnum;charname[10];charsex;/*'F'表示女,'M'表示男*/intage;structstudent*next;}main(){structstudent*creat(intn);voidprint(structstudent*head);structstudent*delete(structstudent*head,intage);structstudent*head;intn,age;printf("\nPleaseinputthelengthoflist:");scanf("%d",&n);head=creat(n);print(head);}structstudent*creat(intn){structstudent*head,*p1,*p2;inti;head=NULL;for(i=1;i<=n;i++){p1=(structstudent*)malloc(LEN);printf("Number:");scanf("%ld",&p1->num);printf("Name:");scanf("%s",p1->name);printf("Sex:");scanf("%c",&p1->sex);printf("Age:");scanf("%d",&p1->age);if(i==1)head=p1;elsep2->next=p1;p2=p1;}p2->next=NULL;return(head);}voidprint(structstudent*head){structstudent*p;p=head;printf("NumberNameSexAge\n");while(p!=NULL){printf("%ld%10s%c%d",p->num,p->name,p->sex,p->age);p=p->next;}}6)structstudent*delete(structstudent*head,intage){structstudent*p1,*p2;p1=head;while(p1!=NULL){if(p1->age==age){if(p1==head)head=p1->next;elsep2->next=p1->next;free(p1);}p2=p1;p1=p1->next;}return(head);}第9章习题参照答案填空题7)文本文件/二进制文件8)创办a文件9)打开a文件,并且文件读写地址指针在文件尾(10)将字符变量ch中的字符写入fp所指向的文件/从fp所指向的文件中读取n-1个字符,并且写入数组s中(11)0(12)for(i=0;i<30;i++)fread(&stud[i],sizeof(structstudent_type),1,fp)13)fseek(fp,30L,SEEK_SET)14)-1选择题1

2

3

4

5

6

7

8A

B

C

D

C

C

D

C程序阅读题-5,-4,-3,-2,total=-14Language!123453678910程序填空题1(fp=fopen("c:\\","w"))(ch=getchar())fputc(ch,fp)stu[i].name,stu[i].sno,stu[i].age,stu[i].address2"w"&stu[i],sizeof(structstudent),1,fpr3w编程题(1)#include<>voidmain(){FILE*fp1,*fp2;charch;if((fp1=fopen("C:\\Mydir\\","r"))==NULL){printf("Cannotopenfilestrikeanykeyexit!");exit(0);}if((fp2=fopen("C:\\Mydir\\","w"))==NULL){printf("Cannotopenfilestrikeanykeyexit!");exit(0);}while((ch=fgetc(fp1))!=EOF){if(ch>='A'&&ch<='Z')ch+=32;elseif

(ch>='a'&&ch<='z')ch-=32;fputc(ch,fp2);}

;fclose(fp1);fclose(fp2);remove("C:\\Mydir\\");rename("C:\\Mydir\\","C:\\Mydir\\");}(2)#include<>intsearch(FILE*fp,charch);voidmain(){FILE*fp;charfilename[30];charch;intcount;printf("Pleseinputfilename:\n");gets(filename);printf("Pleseinputthecharacteryouwanttosearch:\n");scanf("%c",&ch);if((fp=fopen(filen

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论