《C语言程序设计第二版第四章第七章课后答案.doc》由会员分享,可在线阅读,更多相关《C语言程序设计第二版第四章第七章课后答案.doc(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1、输入函数scanf的参数错误,应该为:scanf(%f,&k); 2、|1234 1234 | 3、ff10 4、1,3,1 5、原字符串左边加空格再加字符串本省,字符个数总和为5个6、scanf(%d,%d,%c,%c,&a1,&a2,&c1,&c2); 7、printf(a+b=%dn,a+b); printf(a-b=%dn,a-b); printf(a*b=%dn,a*b); printf(a/b=%dn,a/b); printf(float)a/b=%fn,(float)a/b); printf(a%b=%dn,a%b); 8、void main() float r; float
2、 s,c; printf(please input the number:); scanf(%f,&r); if(r=0) s = 3.14*r*r; c = 2*3.14*r; printf(s = %f, c = %fn,s,c); else printf(you input number is error!); 9void main() int n; printf(please input the number:); scanf(%d,&n); if(n=100 & n c | a+c b | b+c a) s = (a+b+c)/2; Area = sqrt(s*(s-a)*(s-b)
3、*(s-c); printf(%fn,Area); else printf(you input the number is error!n); 习题4 1:0 2:20 3: (x20)|(x-100) 4: *a=25,b=14,c=16* 5: 37 6: if(a=b) printf(1); else printf(2); 7、#include void main() char a,b,t1,t2; scanf(%c,%c,&a,&b); t1=ab?a:b; t2=ab?a:b; if(t1-t2)%2=0)printf(%c,%c,a+1,b+1); else printf(%c,%
4、c,a-1,b-1); getch(); 8、#include void main() int temp1=0,temp2=0,x,y,i=1; printf(Please input (x,y): ); scanf(%d,%d,&x,&y); while(i*y) shang=%d,yushu=%d,x,y,temp2,x-y*temp2); getch(); 9、#include void main() float x,y,m=0,n=0; scanf(%f,%f,&x,&y); n=(x-2)*(x-2); m=(y-2)*(y-2); if(m+n)=1) printf(%.3f,%.
5、3f)In the yuan,x,y); else printf(%.3f,%.3f)out of the yuan,x,y); getch(); 10、#include void main() int temp=0,month,year; printf(Please input (year,month): ); scanf(%d,%d,&year,&month); if(year%400=0)|(year%4=0&year%100!=0) temp=1; if(month=2) if(temp)printf(%d year %d month have 29 ,year,month); els
6、e printf(%d year %d month have 28 ,year,month); else if(month%2=0) printf(%d year %d month have 30 ,year,month); else printf(%d year %d month have 31 ,year,month); getch(); 11、switch(a/10) case 5:m=4;break; case 4:m=3;break; case 3:m=2;break; case 2:m=1;break; default:m=5; 12、方法一:#include void main(
7、) int x,y; scanf(%d,&x); if(x-5) y=x-1; else if(x=0) y=x; else if(x0&x8) y=x+1; printf(%d,y); getch(); 方法二:#include void main() int x,y; scanf(%d,&x); if(x-5) if(x=0)y=x; float x,y,t;printf(Enter two number (x,y): );scanf(%f,%f,&x,&y);SWAP(x,y);printf(nnExchanged:x=%f,y=%f,x,y);getch();5、#includeint
8、 fib(int n)int p;if(n=0) p=0;else if(n=1) p=1;else p=fib(n-1)+fib(n-2);return p;void main()int n;printf(Please input fib: );scanf(%d,&n);printf(nnfibonacci=%d,fib(n);6、#includelong fac(int n)long f;if(n=0)f=1;elsef=n*fac(n-1);return f;void main()int m,n;long f;printf(Please input (m,n): );scanf(%ld,
9、%ld,&m,&n);f=fac(n+m)+fac(n);printf(nnfibonacci=%ld,f);7、#includevoid list()int i,j;for(i=1;i=9;i+) for(j=1;j=i;j+)printf(%d*%d=%d ,i,j,i*j);printf(n);void main()list();8、#includeint he(int n)int i,s=0;for(i=1;in;i+)if(n%i=0)s+=i;return s;void main()int i,j;for(i=1;i30000;i+)for(j=1;j30000;j+)if(he(
10、i)=j)&(he(j)=i)&(i!=j)printf(%d%d ,i,j);9、#include#define max 100struct worklong sid;char name15;workermax;int size=0;struct work *set_list()doprintf(nnPlease input (sid,name): );scanf(%ld,%s,&workersize.sid,workersize.name);size+;while(workersize-1.sid!=0);return worker;void paixu(struct work a)int
11、 i,j,k;long t;char v15;for(i=0;isize-1-2;i+)k=i;for(j=i;jaj.sid)k=j;if(k!=i)t=ai.sid; ai.sid=aj.sid; aj.sid=t;strcpy(v,ai.name);strcpy(ai.name,aj.name);strcpy(aj.name,v);for(i=0;i=size-2;i+)printf(sid: %ldtname:%snn,ai.sid,ai.name);getch();void select(struct work a)int i=0,found=1;long num;printf(In
12、put select sid: );scanf(%ld,&num);for(i=0;i=size-2;i+)if(ai.sid=num) found=0;printf(%ld: %s,num,ai.name);break;if(found)printf(nnNO this sid!);getch();void main()struct work *p;p=set_list();paixu(p);select(p);10、#includefloat jiecheng(int n)long s=1;int i;for(i=1;i=n;i+)s=s*i;return s;float chengfan
13、g(float x,int n)float s=1;int i;for(i=1;i=n;i+)s=s*x;return s;float f(float x,int n)float s=1,t=0;int i=1,j=-1;for(i=1;i=0 ak=temp break k=8(2) %s stri=Z cstri+26-a+ i+a4、/*题目:求一组成绩的平均分数以及高于平均分的成绩。*/#include#define max 100void main()float amax,s=0,average=0;int i,n;printf(Please input N: ); /*输入要处理元
14、素的个数*/scanf(%d,&n);printf(nPlease input %d ge shu: ,n); /*输入90,85,92,77,80,62*/for(i=0;in;i+)scanf(%f,&ai);s+=ai;average = s / n;printf(naverage = %.2fn,average); /*输出81.00*/for(i=0;iaverage)printf(%.2f ,ai); /*输出90.00,85.00,92.00*/5、/*题目:编写程序,输入一组整数,将他们排序后由小到大输出。*/#include#define max 100void main()
15、int amax,j,i,k=0,t=0,n=0;printf(Please input N: ); /*输入要处理元素的个数*/scanf(%d,&n);for(i=0;in;i+)scanf(%d,&ai);for(i=0;in-1;i+) /*比较n-1遍*/k=i;for(j=i+1;jaj)k=j;if(k!=i) /*元素排序前下标与排序后下标不符,则交换其值*/ t=ai;ai=ak;ak=t;printf(nn);for(i=0;in;i+)printf(%d ,ai);6、/*题目:从键盘输入一个4x4的整数矩阵,以主对角线()为对称轴*/*将左下角元素中较大者替换右上角元素
16、,并将右上角含对称轴输出。*/#include#includevoid main()int d44=0,0,0,0,i,j;clrscr();printf(nPlease input 16 num: );for(i=0;i4;i+)for(j=0;j4;j+)scanf(%d,&dij);for(i=0;i4;i+)for(j=0;jdji) /*左下角元素大于右上角元素则交换*/dji=dij;for(i=0;i4;i+)printf(nnnnn);/*为了保持每行的间隔与每列的相同*/for(j=0;j=i)printf(t%d,dij);/*为了使位数不同的数输出依然保持三角型*/els
17、eprintf(t); 7、*题目:输入一个3x4的二维数组,然后分别按行和按列输出。*/ #include #include void main() int d34=0,0,0,0,i,j; printf(Please input 12 num: ); for(i=0;i3;i+) for(j=0;j4;j+) scanf(%d,&dij); printf(nnAn hang: ); for(i=0;i3;i+) for(j=0;j4;j+) printf(%d ,dij); printf(nnAn lie: ); for(i=0;i4;i+) for(j=0;j3;j+) printf(%
18、d ,dji); 8、/*题目:编写程序,将两个字符串连接起来,不用strcat函数。*/ #include #include void main() char s150,s250; int i=0,j=0,t=0; printf(Input one: ); gets(s1); while(s1i!=0) i+; /*统计s1中字符个数*/ printf(nnInput second: ); gets(s2); while(s2j!=0) j+; /*统计s2中字符个数*/ for(t=0;t=j;t+) /*把s2中字符连接到s1后面*/ s1i=s2t; i+; printf(nnone
19、+ second: ); puts(s1); 9、/*输入一行字符串,统计字符对ab的个数。*/ #include #include #define MAX 100 void main() char strMAX; int i=0,flage=0; int count=0; printf(nPlease input a string: ); gets(str); while(stri) if(stri=a) flage=1; else if(stri=b) if(flage=1) count+;flage=0; else flage=0; i+; printf(the double char
20、ab count is %dn,count); 10、#include /*相对于第一种方法,这种方法能把同为最大的字符串全部打印出来。*/ #include int paixu(int a,int n) /*构造对长度为n的数组排序的函数*/ int i,j,t,k,temp; for(i=0;in-1;i+) k=i; for(j=i+1;jaj) k=j; if(k!=i) t=ai; ai=ak; ak=t; temp=an-1; return temp; /*返回最大字符串的长度的值*/ void main() char str150,str250,str350,str450,str
21、550,str650; char str750,str850,str950,str1050; int count10=0,temp10=0,i,j; int paixu(int a,int n); for(i=1;i=10;i+) /*输入10个字符串*/ printf(nPlease input %d string: ,i); if(i=1) gets(str1);while(str1count0)count0+;temp0+; if(i=2) gets(str2);while(str2count1)count1+;temp1+; if(i=3) gets(str3);while(str3c
22、ount2)count2+;temp2+; if(i=4) gets(str4);while(str4count3)count3+; temp3+; if(i=5) gets(str5);while(str5count4)count4+; temp4+; if(i=6) gets(str6);while(str6count5)count5+; temp5+; if(i=7) gets(str7);while(str7count6)count6+; temp6+; if(i=8) gets(str8);while(str8count7)count7+; temp7+; if(i=9) gets(
23、str9);while(str9count8)count8+; temp8+; if(i=10) gets(str10);while(str10count9)count9+;temp9+; j=paixu(temp,10); for(i=1;i=10;i+) if(counti-1=j) /*只要字符串长度与最大长度相等就打印出来*/ switch(i) case 1:puts(str1); printf(n); break; case 2:puts(str2); printf(n); break; case 3:puts(str3); printf(n); break; case 4:put
24、s(str4); printf(n); break; case 5:puts(str5); printf(n); break; case 6:puts(str6); printf(n); break; case 7:puts(str7); printf(n); break; case 8:puts(str8); printf(n); break; case 9:puts(str9); printf(n); break; case 10:puts(str10); printf(n); break; 11、/*给数组a输入m个按升序排列的数,给数组b输入n个按降序排列的数*/ /*将a与b中的元素
25、按降序排列存在数组c中。*/ #include void main() int a100,b100,c100,i,m,n,j,k,temp=0,s; printf(nm = );scanf(%d,&m); /*确定M的个数*/ printf(nn = );scanf(%d,&n); /*确定N的个数*/ printf(nPlease input m ge shu: ); for(s=0;sm;s+) scanf(%d,&as); printf(nPlease input n ge shu: ); for(s=0;sn;s+) scanf(%d,&bs); i=m-1;j=0;k=0; /*i,
26、j,分别标记a与b且都是从最大元素开始做标记*/ for(;) if(i=0) /*a中只有一个数或者a处理到了最后一个元素*/ if(ai=bj) /*如果a的最后一个元素=b中当前元素*/ ck=bj; k+; j+; else ck=ai; k+; for(;) /*把b数组接到c后面*/ ck=bj; j+; k+; if(j=n) /*如果b处理完,结束。并做标记temp=1*/ temp=1;break; if(temp=1)break; /*标记temp=1,全部处理结束*/ if(aibj)&(i!=0)&(j!=n) ck=ai; i-; k+; if(j=n) /*b处理到完*/ for(;) /*把b数组接到c后面*/ if(i=0) /*如果a处理完,结束。并做标记temp=1*/ ck=ai;temp=1;break; ck=ai; i-; k+; if(temp=1) break;/*标记temp=1,全部处理结束*/ for(s=0;sm+n;s+) printf(%d ,cs); getch(); /21