c语言练习题详解_第1页
c语言练习题详解_第2页
c语言练习题详解_第3页
c语言练习题详解_第4页
c语言练习题详解_第5页
已阅读5页,还剩44页未读 继续免费阅读

下载本文档

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

文档简介

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(

19、%d,&num); /*fill*/ if(_) m+; /*fill*/ else if(_) n+; else z+; printf(m,n,z=%d %d %dn,m,n,z); /*-11.【程序填空】-题目:根据下面程序的输出结果, 完善程序。程序执行结果:a=1.382,b=9.163,i=20-*/ #include stdio.hmain() float a=1.382,b=9.163; int i=20; /*fill*/ printf (_, a,b,i); /*-12.【程序填空】-题目:从键盘输入一个小写字母,改用大写字母输出(不用scanf函数)。-*/*fill*/

20、#include_main( ) char c; /*fill*/ c=_; printf(%c,%dn,c,c); c=c-32; printf(%c,%dn,c,c); /*-13.【程序填空】-题目:下面程序的功能是打印图案* # * # * #,请填空。-*/#include stdio.hmain() int i; for(i=1;ic) t=b;b=c;c=t; printf(%5.2f,%5.2f,%5.2f,a,b,c); /*-15.【程序填空】-题目:利用scanf函数,依次为变量i、j、x、y 输入数据 。-*/#include stdio.hmain() int i,j

21、; float x,y; /*fill*/ scanf(%d,%d,_); /*fill*/ scanf(_, &x,&y); /*-16.【程序填空】-题目:本程序用getchar函数输入一个字符给变量s,完善程序。-*/ #include“stdio.h”main() char s; /*fill*/ _ putchar(s); /*-17.【程序填空】-题目:用do while语句求 n=1100的和(包含100)。-*/#include stdio.hmain() int i,sum=0; i=1; do sum=sum+i; /*fill*/ i_; /*fill*/ while(i

22、_100); printf (%d,sum ); 三、程序设计/*-1.【程序设计】-功能:编写函数求150(包括50)中奇数的平方和。结果为20825.000000。-*/#include stdio.hfloat sum(int n) /*begin*/ /* end */void testfunc() file *in,*out; int i; float o; in=fopen(in.dat,r); if(in=null) printf(read file error); out=fopen(out.dat,w); if(out=null) printf(write file error); fscanf(in,%d,&i); o=sum(i); fprintf(out

温馨提示

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

评论

0/150

提交评论