c语言练习题详解.doc

上传人:仙人指路1688 文档编号:2384752 上传时间:2023-02-17 格式:DOC 页数:49 大小:141.50KB
返回 下载 相关 举报
c语言练习题详解.doc_第1页
第1页 / 共49页
c语言练习题详解.doc_第2页
第2页 / 共49页
c语言练习题详解.doc_第3页
第3页 / 共49页
c语言练习题详解.doc_第4页
第4页 / 共49页
c语言练习题详解.doc_第5页
第5页 / 共49页
点击查看更多>>
资源描述

《c语言练习题详解.doc》由会员分享,可在线阅读,更多相关《c语言练习题详解.doc(49页珍藏版)》请在三一办公上搜索。

1、一、程序改错/*- 1【程序改错】-题目:改正以下程序的函数体中不正确的语句。-注意:不可以增加或删除程序行,也不可以更改程序的结构。-*/#include stdio.hmain() char s210; /*ERROR*/ scanf (%s,&s2); /*ERROR*/ printf(%c,s2); /*- 2【程序改错】-题目:-注意:不可以增加或删除程序行,也不可以更改程序的结构。-*/main() unsigned k; /*ERROR*/ scanf(%d,&k); k+=5; /*ERROR*/ printf(k=%d,k); /*- 3.【程序改错】-题目:打印50以内所有

2、能被3整除的偶数。-注意:不可以增加或删除程序行,也不可以更改程序的结构。-*/main() /*ERROR*/ int t; do if(t%3=0&t%2=0) printf( %d,t); t-; /*ERROR*/ while(!t); /*- 4.【程序改错】-功能:先从键盘上输入一个3行3列矩阵的各个元素的值,然后输 出主对角线上的元素之和sum。-*/#include stdio.hint fun() int a33,sum; int i,j; /*ERROR*/ a=0; for(i=0;i3;i+) for(j=0;j3;j+) /*ERROR*/ scanf(%d,aij)

3、; for(i=0;i3;i+) /*ERROR*/ sum=sum+aij; /*ERROR*/ printf(sum=%fn,sum);main() fun(); /*- 5.【程序改错】-题目:改正以下程序的函数体中不正确的语句。-注意:不可以增加或删除程序行,也不可以更改程序的结构。-*/#include stdio.hmain() int a110,i; for(i=0;i=9;i+) /*ERROR*/ scanf(%d ,a1i); /*ERROR*/ printf(%fn,a10+a19); /*- 6.【程序改错】-题目:从键盘输入35,判断这个数是否能被3和5整除。-注意:

4、不可以增加或删除程序行,也不可以更改程序的结构。-*/#include stdio.hmain() int n,flag; scanf(%d,&n); flag=0; /*ERROR*/ if(n/3!=0) flag=-1; if(n%5!=0) flag=-1; /*ERROR*/ if(flag!=0) printf(能被3,5整除); else printf(不能被3,5整除); /*- 7.【程序改错】-题目:改正以下程序中的错误。-注意:不可以增加或删除程序行,也不可以更改程序的结构。-*/#include stdio.h/*ERROR*/#include maths.h main

5、() int a,b; double c,d; a=b=2; c=4; d=sqrt(a*b+c); /*ERROR*/ printf(d=%dn,d); /*- 8.【程序改错】-题目:改正以下程序的函数体中不正确的语句。-注意:不可以增加或删除程序行,也不可以更改程序的结构。-*/#include stdio.h/*ERROR*/void mian() int a=b=5; int k; char c; double x=1.42; x=x/(-3); c=b; /*ERROR*/ k=a+b; a+=a-=(b=4)*(a=3); /*- 9.【程序改错】-功能:编写函数fun求1000

6、以内(不包括1000)所有m的倍数之和。-*/#include stdio.h#define N 1000int fun(int m) /*ERROR*/ int s=0;i; for(i=1;iN;i+) /*ERROR*/ if(i%m=0) s+=i; return s; main() int sum; sum=fun(8); printf(%d以内所有%d的倍数之和为:%dn,N,8,sum); /*- 10.【程序改错】-功能:利用公式法,求方程ax2+bx+c=0的解。-*/#include #include int main()double a,b,c,disc,x1,x2,re

7、alpart,imagpart;/*ERROR*/ scanf(%lf%lf%lf,a,b,c); /*ERROR*/ disc=b*b-4ac; if(fabs(disc)1e-6) x1=(-b+sqrt(disc)/(2*a); x2=(-b-sqrt(disc)/(2*a); printf(has distinct real roots:%8.4lf and %8.4lfn,x1,x2); else realpart= -b/(2*a); imagpart=sqrt(-disc)/(2*a); printf(has complex roots:n); printf(%8.4lf+%8.

8、4lf i n,realpart, imagpart); printf(%8.4lf%8.4lf i n,realpart, imagpart); return 0; /*- 11.【程序改错】-题目:求8!的结果。-注意:不可以增加或删除程序行,也不可以更改程序的结构。-*/#include stdio.hmain( ) int i=8; long s1=1,j; /*ERROR*/ for(j=1;jy?x: y; return z;main( ) int a,b,c; /*ERROR*/ scanf(%d%d, a, b); c=max(a, b); printf(c=%dn, c);

9、/*- 13.【程序改错】-功能:根据整型形参m,计算如下公式的值: y=11/31/51/7.1/(2m+1)-*/#include stdio.hdouble fun(int m) /*ERROR*/ double y=1 int i; for(i=1; i=m; i+) /*ERROR*/ y+=1.0/(2i+1); return(y);main() int n; printf(Enter n: ); scanf(%d, &n); printf(nThe result is %1fn, fun(n); /*- 14.【程序改错】-功能:编写函数fun计算下列分段函数的值: x*20 x

10、0且x-3 f(x)= sin(x) 0x10且x2及x3 x*x+x-1 其它-*/#include math.h#include stdio.hfloat fun(float x) /*ERROR*/ float y /*ERROR*/ if (x=0 & x10.0 & x!=2.0 & x!=3.0) y=sin(x); else y=x*x+x-1; /*ERROR*/ return x;main() float x,f; printf(Input x=); scanf(%f,&x); f=fun(x); printf(x=%f,f(x)=%fn,x,f);/*- 15.【程序改错】

11、-题目:下面程序用来计算10个数的平均数,试改正程序中的错误之处.-注意:不可以增加或删除程序行,也不可以更改程序的结构。-*/*ERROR*/#include stdio.hmain() int i; float s=0,a; /*ERROR*/ for(i=1;i=0 & b=0); if(a=b) /*ERROR*/ t=a;a=b;b=t; printf(a=%d,b=%d,a,b); /*- 17.【程序改错】-功能:在fun函数中求出以下分数序列的前n项之和。和值通过函数值返回到main 函数。 2/1+3/2+5/3+8/5+13/8+21/13 例如:若n = 5,则应输出:8

12、.391667。-*/#include conio.h#include stdio.h double fun ( int n ) int a, b, c, k; double s; s = 0.0; a = 2; b = 1; for ( k = 1; k = n; k+ ) /*ERROR*/ s = (double)a / b; c = a; a = a + b; b = c; /*ERROR*/ return c;main( ) int n = 5; printf( nThe value of function is: %lfn, fun ( n ) ); /*- 18.【程序改错】-功

13、能:求100以内(包括100)的偶数之和.-*/#include stdio.hmain() /*ERROR*/ int i,sum=1; /*ERROR*/ for(i=2;i=100;i+=1) sum+=i; /*ERROR*/ printf(Sum=%d n;sum); 二、程序填空/*-1【程序填空】-题目:输入三角形三边a,b,c,求面积area,area为s(s-a)(s-b)(s-c)平方根, 其中s=(a+b+c)/2 。-*/#include stdio.h/*FILL*/#include _ main( ) float a,b,c,s,area; scanf(%f,%f,

14、%f,&a,&b,&c); /*FILL*/ s= _ /2*(a+b+c); area = sqrt (s*(s-a)*(s-b)*(s-c); printf(a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2fn,a,b,c,s); printf(area=%7.2f,area); /*-2.【程序填空】-题目:根据下面程序的输出结果, 完善程序。程序执行结果:a=45,b=16 -*/ #include stdio.hmain() int a=45,b=16; /*FILL*/ printf (_, a,b); /*-3.【程序填空】-题目:根据下面程序的输出结果, 完善程序(

15、大写字母U代表空格)。程序执行结果:a=UUU15.38c=UUU20.21-*/#include stdio.hmain() float a=15.38,c=20.21; /*FILL*/ printf (_, a,c); /*-4.【程序填空】-题目:打印100以内,个位数为6且能被3整除的所有数。-*/#include stdio.hmain() int i,j; /*FILL*/ for(i=0;_;i+) j=i*10+6 ; /*FILL*/ if (_) continue; printf(%d,j); /*-5.【程序填空】-题目:根据下面程序的输出结果, 完善程序(大写字母U代

16、表空格)。程序执行结果:a=UUU15.38c=UUU20.21-*/#include stdio.hmain() float a=15.38,c=20.21; /*FILL*/ printf (_, a,c);/*-6.【程序填空】-题目:根据下面程序的输出结果, 完善程序(大写字母U代表空格)。程序执行结果:a=-15.38UUUU,b=9.16UUUfirst-*/#include stdio.hmain() float a=-15.38,b=9.16,c=2.26389,d=-0.343689; /*FILL*/ printf (_, a,b); /*-7.【程序填空】-题目:根据下面

17、程序的输出结果, 完善程序(大写字母U代表空格)。程序执行结果:PI=3.14UUUUr=UUU25.33Area=2015.61-*/#include stdio.hmain() float pi=3.1415,r=25.33,area; area=pi*r*r; /*FILL*/ printf (_, pi,r); /*FILL*/ printf (_, area); /*-8.【程序填空】-题目:根据下面程序的输出结果, 完善程序。程序执行结果:a=1.382,b=9.163,i=20-*/ #include stdio.hmain() float a=1.382,b=9.163; in

18、t i=20; /*FILL*/ printf (_, a,b,i); /*-9.【程序填空】-功能:要求输出结果为: A,B 65,66-*/#include stdio.hmain() /*FILL*/ char a,_; /*FILL*/ a=_; b=b; a=a-32; /*FILL*/ b=b-_; printf(%c, %cn%d,%dn,a,b,a,b); /*-10.【程序填空】-题目:求20个数中的正整m、负数n和零z的个数。请填空。-*/#include stdio.hmain() int m=0,n=0,z=0,i,num; for(i=0;i20;i+) scanf(%d,&num); /*FILL*/ if(_) m+; /*FILL*/ else if(_) n+; else z+; printf(m,n,z=%d %d %dn,m,n,z); /*-11.【程序填空】-题目:根据下面

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号