1、C语言程序设计实验设计习题答案实验三3.4编写程序,用getchar函数读入两个字符c1,c2,然后分别用putchar函数输出void main() char c1,c2; c1=getchar(); c2=getchar(); putchar(c1); putchar(c2); putchar(n); printf(%c %cn,c1,c2);3.5把790分钟换成用小时和分钟表示,然后输出。void main() int s=790; printf(%d小时,s/60); printf(%d分钟,s%60);3.6将读入三个数a,b,c,然后交换它们的值,把a的值给b,把b的值给c,把c
2、的值给a.main() int a,b,c,t; scanf(%d,%d,%d,&a,&b,&c); t=a; a=b; b=c; c=t; printf(%d,%d,%d,a,b,c); 3.7输入一个double类型的数,使该数保留小数点后两位,对第三位小数进行四舍五入处理,然后输出此数,验证处理是否真确。#includevoid main()float x,s,a; int z; scanf(%f,&x); a=100*x+0.5; z=(int)(a); s=z/100.0; printf(%.2fn,s);main() int a,b,t; double c; scanf(%lf,&
3、c); b=100*c; t=1000*c; a=t%10; if (a=5) b=b+1; else b=b+0; c=1.0*b/100; printf(%7.2lf/n,c); return 0;void main() int x,y; scanf(%d,&x); if(x-5) y = x ; if(x=0) y = x-1; if(x0) y = x+1; printf(%d,y); 实验四4.2不嵌套if语句void main() int x,y; scanf(%d,&x); if(x-5) y = x ; if(x=0) y = x-1; if(x0) y = x+1; prin
4、tf(%d,y); 法二:嵌套if语句void main() int x,y; scanf(%d,&x); if(x-5)&(x0) y=x; else if(x=0) y=x-1; else if(x0) y=x+1; printf(%d,y);4.3如下程序运用条件运算符求变量的绝对值?void main() int a,M; scanf(%d,&a); M=a0?a:-a; printf(abs(a)=%d,M);4.4某服装店经营套服,也可单件出售,若买的不少于50套,每套80元,不足50套的每套90元,只买上衣每件60元,只买裤子每条45元,(读入所买上衣C和裤子t的件数,计算应付款
5、m。void main() int c,t,m; printf(input the number of coat and trousers your want buy:n); scanf(%d%d,&c,&t); if(t=c) if(c=50)m=c*80; else m=c*90; else if(ct) if(t=50) m=t*80+(c-t)*60; else m=t*90+(c-t)*60; else if(c=50) m=c*80+(t-c)*45; else m=c*90+(t-c)*45; printf(%d,m );4.5:从键盘中输入三个数,分别让他们代表三条线段的长度,
6、请编写程序,判断由这三条线段组成的三角形将是什么三角形(不等边,等腰,等边或不构成三角形)?Includemain() int a,b,c; scanf(%d %d %d,&a,&b,&c); if(a+bc&b+ca&c+ab) if(a=b)&(b=c) printf(等边三角形);else if(a=b&b!=c)|(b=c&c!=a) printf(等腰三角形); else printf(不等边三角形); else printf(不构成三角形);4.6:判断输入的整数是否既是5又是7的倍数,若是,则输出yes,否则输出novoid main() int x; scanf(%d,&x);
7、 if(x%5=0&x%7=0) printf(yes); else printf(no);实验五5.2输入一行字母,将字母加密输出(如“a变成c,b变成d,.,z变成b)main() int c; while ( (c=getchar () ) !=n) if (c=A|c=a) c += 2; else if (c= y|c= Y|c= z|c= Z) c=c-24; printf(%c,c); putchar (n) ;5.3利用for循环编写程序法一:#includestdio.hmain() int i,j; for(i=1;i=4;i+) for(j=1;j=6;j+) if(i=
8、2|i=3) if(j=1|j=6) printf(* ); else printf( ); else printf(* ); printf(n); 法二:#includestdio.hmain() int i,j; for(i=1;i=4;i+) if(i=1|i=4) for(j=1;j=6;j+) printf(* ); printf(n); else printf(* ); for(j=1;j=4;j+) printf( ); printf(* ); printf(n); 5.4利用循环语句编程,输出三角形状的九九乘法表#includestdio.hmain() int i,j,k;
9、for(i=0;i=9;i+) if(i=0) printf(*); else printf(%4d,i); printf(n); for(i=1;i=19;i+) printf(_ ); printf(n); for(i=1;i=9;i+) printf(%d,i); for(j=1;j=i;j+) k=i*j; printf(%4d,k); printf(n); 5.5:用while语句或do-while语句编写程序,计算:e=1+1/1!+1/2!+.+1/n!.#include #include math.hmain()float s=1.0,e=1.0,t;int i=1; whil
10、e(fabs(t)=1e-5) s=s*i; t=1/s; e=e+1.0/s; i+; printf(%fn,e); printf(%d,i);5.6:如果一个数恰好等于它的所有因子(包括1但不包括自身)之和,则称之为“完数”,例如:6的因子为1,2,3且6=1+2+3,所以6是完数,输出完数1+完数2+.=和值。void main() int i,j,sum,k; for(i=2;i=1000;i+) sum=0; for(j=1;ji;j+) if(i%j=0) sum+=j; if(sum=i) printf(%d的因子:n,i); k=0; for(j=1;ji;j+) if(i%j
11、=0&k=0) printf(%d,j); else if(i%j=0) printf(+%d,j); k+; printf(=%d,sum); printf(n); 实验六6.2:将数组xx(有n个元素)的前K个元素(kn=移到数组的尾部,变为后K个元素,但是数组的这两段(原前K个元素为一段,另外的n-k为一段)例如:若输入7和1 2 3 4 5 6 7,再输入5,则输出6 7 1 2 3 4 5.#includestdio.hmain() int n,k,xx20; int i,j,t; printf(nPlease enter a number); scanf(%d,&n); print
12、f(nPlease enter %d numbers:,n); for(i=0;in;i+) scanf(%d,&xxi); printf(%nPlease enter another number:); scanf(%d,&k); for(i=0;ik;i+) t=xx0; for(j=1;jn;j+) xxj-1=xxj; xxn-1=t; printf(nAfter moving:n); for(i=0;in;i+) printf(%3d,xxi); printf(n);6.3对a数组中n种商品价格进行分段统计,商品各段价格存到b数组中,其中:价格为100到199元的个数存到b1,价格为
13、200到299元的个数存到b2,价格为300到399的个数存到b3,价格为400到499元的个数存到b4,价格为500以上的个数存到b5中,例如:a的数组为:90,180,270,380,590,530,140,750,380,调用函数后,b中存放的数据为:1,2,1,2,0,3#include main() int i,a100=90,180,270,380,590,530,140,750,380,b6; for(i=0;i6;i+) bi=0; for(i=0;i9;i+) switch(ai/100) case 0:b0+;break; case 1:b1+;break; case 2:
14、b2+;break; case 3:b3+;break; case 4:b4+;break; default :b5+;break; printf(The result is:); for(i=0;i6;i+) printf(%d,bi); printf(n);6.4编写程序,先读入10个整数,再把他们按从小到大的次序排列起来;最后再读入一个整数K,并将K插入到该数列中,插入后数列仍然从小到大排列。例如:若输入:6,3,42,23,35,71,98,67,56,38,最后再输入待插入数59,则输出为:6,23,35,38,42,56,59,67,98#include main() int i,
15、j,t,p,q,s,n,a11=6,3,42,23,35,71,98,67,56,38; for(i=0;i10;i+) for(j=i+1;jaj) t=ai; ai=aj; aj=t; printf(%d ,ai); printf(ninput number:n); scanf(%d,&n); for(i=0;i10;i+) if(n=i;j-) aj+1=aj; break;ai=n;for(i=0;i=10;i+) printf(%d ,ai); printf(n); 6.5统计字符串s在字符串str中出现的次数。main() char str1255,str2255;int sum=
16、0;printf(请输入两个字符串!n);gets(str1);gets(str2);int i=0,j=0;while(str1i!=0) while(str2j=str1i+j&str2j!=0) j+; if(str2j=0) sum+; j=0; i+;6.7:a是一个M行N列的整数矩阵,编写程序求出此矩阵周边元素之和。#define M 4#define N 5#include stdio.hmain() int aaMN=1,3,5,7,9,2,9,9,9,4,6,9,9,9,8,1,3,5,7,0; int i,j,y,sum=0; printf(The original dat
17、a is :n); for(i=0;iM;i+) for(j=0;jN;j+) printf(%6d,aaij); printf(n); for(i=0;iN;i+) sum+=aa0i+aaM-1i; for(i=1;iM-1;i+) sum+=aai0+aaiN-1; printf(nThe sum: %dn,sum); printf(n); 实验七7.2:编写一个程序,将两个字符串连接起来,不要用strcat.方法一#include void main() const char str1=Hello ; const char str2=every one!; char str255; f
18、or(int i=0;str1i!=0;i+) stri=str1i; for(int j=0;str2j!=0;j+) stri+j=str2j; stri+j=0; printf(%s,str); getchar(); 方法二#include string.h#include stdio.hmain()char str150=every one!;char str250=hello ;strcpy(str2+strlen(str2),str1);printf(%s,str2);7.3:写一个程序,能分别统计出从键盘上输入的字符串中小写字符的个数和数字字符的个数和其它字符的个数,输入字符串以
19、!作为结束标志#include stdio.hmain()int lower=0,digit=0,other=0,i=0;char s255;while(si=getchar()!=!) i+;for(int j=0;sj!=!;j+) if(sj=a) +lower; else if(sj=0)+digit; else +other;printf(小写字符:%dn数字字符:%dn其它字符:%dn,lower,digit,other);getchar();7.4:设有一个字符串“This is a program”,请编程求字符“a”首次出现的位置#include stdio.hmain()i
20、nt i,pos;char str=This is a program;for(i=0;stri!=0;i+) if (stri=a) pos=i;break;printf(%sn,str);printf(The position is %dn,pos);7.5:把一串密码译成明文,密码以字符表示结束。#include stdio.hmain()int i,j,k=0;char s255;while(sk=getchar()!=) k+;for(j=0;sj!=;j+) if(sj=A) sj+=32; if(sj=a) sj+=3; else if(sj=x|sj=y|sj=z) sj-=2
21、3;printf(译码后结果:);for(i=0;si!=;i+)printf(%c,si);printf(n);getchar();7.6:编写一个程序,实现对键盘输入的两个字符串进行比较,然后输出两个字符串中第一个不同ASC|码之差,例如:输入的两个字符串分别为“computer”和“compare”,则输出为-20#include stdio.hmain()int i=0;char str1255,str2255;printf(n Input string 1:);scanf(%s,str1);printf(n Input string 2:);scanf(%s,str2);while(1) if(str1i=str2i) i+; continue; else break; printf(n%dn,str1i-str2i);