C语言程序设计习题与上机实验全部答案_第1页
C语言程序设计习题与上机实验全部答案_第2页
C语言程序设计习题与上机实验全部答案_第3页
C语言程序设计习题与上机实验全部答案_第4页
C语言程序设计习题与上机实验全部答案_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

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

文档简介

1、C语言程序设计习题与上机实验(参考答案)2008年3月30日第一部分 同步习题集第1章 C语言概述一、判断题1、正确2、正确3、错误4、错误二、单项选择题 1、B2、B3、A4、A5、D6、C7、D8、C三、填空题1、 /* */2、函数首部、函数体第2章 算法一、 填空题1、算法2、算法、数据结构3、自然语言、N-S结构图、伪代码4、顺序结构、选择结构、循环结构 5、程序设计风格、程序结构、清晰的结构第3章 数据类型运算符与表达式一、单项选择题1、D2、D3、D4、D5、B6、AB7、D8、D9、C10、A11、D12、C13、B14、C15、C16、B17、C二、填空题1、2;-32768

2、72767;4;42、1.0/2*(a*x+(a+x)/(4.0*a)第4章 最简单的C程序设计一、单项选择题1、B2、B3、D4、C5、A6、D7、D二、填空题1、;2、3、%4、输入输出函数三、写结果题1、a2、6,53、-6 4、11空格105、ch:dec=97;oct=141,hex=61,ASCII=a6、修改题printf(“%d,%d,%d,%dn”,+i, j-, i+, -j); 结果为:7,4,5,4四、程序填空题1、%c2、%.2f3、(1) &x,&y (2) printf(“%d,%d”,x,y); 第5章 逻辑运算和判断选择结构一、单项选择题1、C

3、2、A3、D4、D5、B6、D7、B8、B9、C10、D11、C12、C二、写运行结果题1、1002、63、24、a=2,b=1三、程序填空题1、(1) x>y (2) u>z 2、(1)m= =c (2) m= =b 3、(1)ch>=A &&ch<=Z (2) ch=ch-32 4、(1)x>2 && x<=10(2) x>-1 && x<=2四、编程题1、#include <stdio.h>main( ) int a, b, t; printf(“Please input a,b:

4、”); scanf(“%d,%d”, &a, &b); t = a; a = b; b = t; printf(“a=%d,b=%d”, a, b);2、编程实现以下功能:读入两个运算数(data1和data2)及一个运算符(op),计算表达式data1 op data2的值,其中op可为+,-,*,/(用switch语句实现)。#include <stdio.h>int data1,data2,y; char op;给data1, op ,data2赋值op= =?y=data1+data2输出结果y=data1-data2y=data1*data2y=data1

5、/data2*/main( ) float data1, data2, result;char op;printf(“Please input express (data1 op data2) : ”);scanf(“%f%c%f”, &data1, &op, &data2);switch (op) case +: result = data1 + data2; break; case -: result = data1 - data2; break; case *: result = data1 * data2; break; case /: result = dat

6、a1 / data2; break; default: printf(“Input error!”); exit(0);Printf(“%g%c%g=%g”,data1, op, data2, result);3、试编程判断输入的正数是否既是5又是7的倍数。若是,则输出yes,否则输出no。#include <stdio.h>main( ) int x ; printf(“Please input x”);scanf(“%d”, &x);if(x >=0)if(x %5= =0&& x %7= =0) printf("yes");

7、else printf("no"); else printf(“Input data error!”);4、#include <stdio.h>main( ) float x, y ; printf(“Please input x:”);scanf(“%f”, &x);if (x < 1)y = x;else if (x < 10) y = 2 * x - 11; else y = 3 * x -11;printf (“y=%g”, y);5、#include <stdio.h>main( ) char grade ;float

8、score;printf(“POlease input grade:”);scanf(“%c”, &grade);switch (grade)case A: case a: score = 95;break;case B: case b: score = 85;break;case C: case c: score = 75;break;case D: case d: score = 65;break;case E: case e: score = 50;break;default: printf(“Input error!”); exit(0);printf (“score=%f”,

9、 score);第6章 循环结构一、判断题1、错误2、正确3、正确4、错误5、正确二、单项选择题1、A2、C3、A4、D5、B6、B7、A8、D9、A10、D11、C12、B13、C14、AC三、写结果题1、8,202、25811143、84、85、a=16 y=606、74*#7、68、a=419、10、2,3 四、程序填空题1、c!=#c>=0 && c<=92、t=t*it=-t/i3、i<1010*i+54、i<xx%i=05、inword=0inword=1五、编程题1、#include <stdio.h>main( )float

10、max, min, score, average; int i; printf(“Please input score:”); scanf(“%f”, &score);max = min = average = score;for ( i=1; i <= 34; i+) printf(“Please input score:”); scanf(“%f”, &score);if (score < min ) min =score;if (score > max ) max =score;average + = score;average /= 35.0;prin

11、tf(“max=%g,min=%g,average=%g”, max,. min, average);2、#include <stdio.h>main( ) int a, b, r, temp;printf(“Please input a,b”);scanf(“%d,%d”, &a, &b);while ( a<=0 | b<=0)printf(“Try again: ”);scanf(“%d,%d”, &a, &b);if (a < b) r = a; a = b; b = r;temp = a * b;while (b != 0

12、)r = a % b; a = b ; b = r;printf(“最大公约数=%d, 最小公倍数=%d”, a , temp / a);第7章 数组一、单项选择题1、C2、D3、C4、C5、C6、C7、D8、D9、C10、B11、C12、B13、C14、D15、AFGHKL二、写结果题1、32、he3、AHaMA4、How does she5、三、程序填空题1、a9=x i<92、k=i k != i3、 x !=0 t+04、strlen(t) tk = = c5、bj!=0 ai=0四、编程题1、#include <stdio.h>#define N 10main( )

13、 int aN, temp, i;for (i = 0 ; i < N; i+) printf(“Please input NO %d:”, i+1);scanf(“%d”, &ai);for (i = 0 ; i <= N/2 -1; i+) temp = ai;ai=aN-i-1;aN-i-1=temp; for (i = 0 ; i < N; i+) printf(“%3d”, ai);2、输出杨辉三角形前十行。#include <stdio.h>#define N 10main( )int aNN;int i, j;for (i = 0 ; i &

14、lt; N; i+)aii = 1;ai0 = 1;for (i = 1 ; i < N; i+) for (j = 1 ; j <= i; j+)aij=ai-1j-1+ai-1j;for (i = 0 ; i < N; i+) for (j = 0 ; j <= i; j+) printf(“%5d”, aij); printf(”n”); 第8章 函数一、单项选择题1、D中加一句z=x+y;2、选D(题不严密)3、D4、A5、B6、D7、B8、A9、B10、D11、A12、A13、B14、CD15、Ai=5i=2i=2i=4i=2二、写结果题1、82、43、4、5

15、三、程序填空题1、float fun(float a, float b); 或float fun(float , float ); x+y, x-yz+y, z-y2、i <= 10 arrayi-1 return avgr 3、j=num-1 arrayi< arrayi+14、t*i s+factor(k)5、 l>h s, l+1, h-1四、编程题1、int prime(int x) int i; int flag=1; for(i=2;i<x;i+) if (x%i =0) flag=0; break; return(flag);main() int x, y

16、; printf(“Please input x:”); scanf(“%d”, &x); y=prime(x); if (y=1) printf("%d is prime!", x); else printf("%d is not prime!", x);2、#include <stdio.h>long fun(char str);main() char datastring10; long data;printf(“Please input data string:”);scanf(“%s”, datastring); data=

17、fun(datastring); printf("output numbern"); printf("%ld", data); long fun(char str) int i; long data; data=0; for(i=0;stri!='0'i+) data=data*10+(stri-'0'); return data;第9章 编译预处理一、单项选择题1、C2、B3、D4、D5、B6、B7、D8、D二、判断题1、错误2、正确3、正确4、正确5、错误6、正确三、编程题1、求面积#include <stdio

18、.h>#include <math.h>#define S(a,b,c) ( (a)+(b)+(c) ) /2.0 )#define AREA(a,b,c) sqrt(S(a,b,c)*( S(a,b,c)-a) *( S(a,b,c)-b) *( S(a,b,c)-c)main( )double a, b, c, area;printf(“Please input a,b,c:”);scanf(“%lf, %lf, %lf”, &a, &b, &c);area=AREA(a,b,c);printf(“area=%lg”,area);第10章 指针一、

19、单项选择题1、B2、B3、D(?)4、D5、B6、C7、D8、B9、CD10、D 11、B12、C13、D14、D15、C16、B17、A18、D3 55 3二、写结果题1、192、603、4、495、GOOGLE6、-27、Excute right!8、xyzabc9、4321三、程序填空题1、*max=i a,7, &max, &min 2、*p!=0 *p-0 j->03、s+n-1或 p1+n-1 p2-4、s1+*s2s1=p或puts(p)或return5、*qstrk或*(str+k)四、编程题1、#include <stdio.h>void s

20、wap(int *p1,int *p2) int t; t= *p1;*p1=*p2;*p2=t;main()int a, b;printf(“Please input a,b:”);scanf(“%d,%d”, &a, &b);swap(&a,&b);printf(“a=%d,b=%d”,a,b);2、#include <stdio.h>int strlength(char *p) char *s=p;while(*p) p+;return p-s;main()char str81;printf(“Please input string:”);ge

21、ts(str);printf(“length=%d”,strlength(str);第11章 结构体和共用体一、单项选择题1、A2、A3、D4、D5、A6、D7、D8、D9、C10、B11、B12、C13、A(不严密)二、写结果题1、Zhao2、7,33、9三、程序填空题1、p->link p!=NULL或p四、编程题1、#include <stdio.h>#define N 5struct student long num; char name20; float score3;void input(struct student w, int n);void output(s

22、truct student w, int n);main() struct student stN; int n; printf("Please input the number of worker(<10):"); scanf("%d", &n); input(st, n); output(st, n);void input(struct student w, int n)int i, j;float temp; for(i=0;i<n;i+) printf("Please input NO %dn", i+1)

23、; printf("Num:"); scanf("%ld",&wi.num); getchar(); printf("name:"); gets(); for (j=0; j < 3; j+) printf(“Score%d=”, j); scanf("%f",& temp );wi.scorej = temp; void output(struct student w, int n) int i, j; for(i=0;i<n;i+) printf("nOutp

24、ut NO %dn",i+1); printf("Num:"); printf("%ld",wi.num); printf("nname:"); puts(); printf("score:"); for (j=0; j < 3; j+) printf("%g ", wi.scorej); 第12章 位运算一、单项选择题1、C2、B3、D4、B5、0x13(无答案)6、A7、A8、Bx=11y=17z=11二、写结果题1、108,-22、03、a=894、第13章

25、文件一、单项选择题1、A2、D3、B4、B5、A6、A7、C8、C9、A10、D二、程序填空题1、fgetc(fp) count+2、fname3、EOF ch, stdout4、f2=fopen(argv2 fgetc(f1),f25、 ”stu_list”, ”wb” &studi,sizeof(struct student_type),1三、编程题1、#include <stdio.h>main()FILE *fp;char str255;int i ;if (fp=fopen(“d:test.txt”,”r”)=NULL)printf(“Open error!”);

26、exit(0);for (i=1;i<=7;i+)fgets(str,255,fp);printf(“%s”, str);fclose(fp);2、#include <stdio.h>main(int argc , char *argv)FILE *fp1,*fp2;if(argc!=3) printf("you forgot to enter a filenamen"); exit(0);if (fp1=fopen(argv1,”r”)=NULL)printf(“Open argument1 file error!”);exit(0);if (fp2=f

27、open(argv2,”a”)=NULL)printf(“Open argument2 file error!”);exit(0);while (!feof(fp1)fputc(fgetc(fp1),fp2);fclose(fp1);fclose(fp2);3、将指定的文本文件中的小写字母改为大写字母。#include <stdio.h>main()FILE *fp;char filename81;char ch;long pos;printf("Please input filename:");gets(filename);if (fp=fopen(filen

28、ame,"r+")=NULL)printf("Open error!");exit(0);pos=0;while (!feof(fp)fseek(fp,pos,0);ch=fgetc(fp);if (ch>='a' && ch<='z')ch = ch -32;fseek(fp,pos,0);fputc(ch,fp);pos+;fclose(fp); 4、将100200之间的素数保存到A盘根目录的“test.txt”文本文件中。(每行存5个数,每个数占10列)include <stdio.

29、h>main()FILE *fp;char str255;int i, j, count; ;if (fp=fopen("a:test.txt","w")=NULL)printf("Open error!");exit(0);count = 0;for ( i = 100;i<=200; i+)for(j = 2; j <= i-1; j+)if (i % j = 0)break;if (j = i ) count+;fprintf(fp,"%10d", i);if (count % 5 =0 )

30、fprintf(fp,"%c", 'n');fclose(fp);5、将平均成绩不及格的学生数据保存到A盘的根目录的文件“stud.dat”中。#include <stdio.h>#define N 50struct student long num; char name20; float score3; float average;void input(struct student w, int n);void save(struct student w, int n);main() struct student stN; int n; pri

31、ntf("Please input the number of worker(<10):"); scanf("%d", &n); input(st, n); save(st, n);void input(struct student w, int n) int i, j; float temp; for(i=0;i<n;i+) printf("Please input NO %dn", i+1); printf("Num:"); scanf("%ld",&wi.num

32、); getchar(); printf("name:"); gets(); wi.average = 0; for (j=0; j < 3; j+) printf("Score%d=", j); scanf("%f",& temp ); wi.scorej = temp; wi.average= wi.average+ wi.scorej; wi.average = wi.average / 3.0; void save(struct student w, int n) FILE *fp; int i;

33、if (fp=fopen("a:stud.dat","wb")=NULL) printf("Open error!"); exit(0); for(i=0;i<n;i+) if (wi.average < 60) fwrite(&wi,sizeof(struct student),1,fp); fclose(fp);第二部分 综合练习与提高第3章 数据类型、运算符与表达式一、单项选择题1-5 CABCD6-10 BDBBC11 B二、填空题1、(3*a*e)/(b*c)2、3.5 3、x*x*x*x*x*x-2*x

34、*x*x*x*x+3*x*x*x*x+4*x*x*x-5*x*x+6*x+7第4章 最简单的C程序设计一、单项选择题1-4 BCDB二、填空题1、a=3 b=78.5 71.82A a2、a=12345,b=-198.34,c=6.5 (表示空格) 3、a=2, b=-1 4、3 3第5章 逻辑运算和判断选择结构一、单项选择题1-5 DACBA 6-10 DBBBA11-14 DBCA二、读程序写结果1、02、1 03、24、a=-15、*&三、程序填空题1、x <z x<y 2、c<d b<c 3、x>2&&x<=10 x>-

35、1&&x<=24、x= =a | x= =-a x>-a && x<a 5、a= =0 disc<06、(y%4= =0)&&(y%100!=0) f=07、x<0 x/10 x<40四、编程题1、 # include<stdio.h>void main() int n=0; float score=0, max, min, sum=0, average=0; printf("input NO.%d score: ", +n); scanf("%f",&

36、;score); max=min=score; while( n<=3 ) if(score>max) max=score; if(score<min) min=score; sum+=score; printf("input NO.%d score: ", n+); scanf("%f", &score); average=sum/3; printf("max=%.1f, min=%.1f, average=%.1fn", max, min, average);2、教材P98 例5.23、4 教材P1111

37、12 习题5.6 5.7第6章 循环控制一、判断题1-5 ×××二、单项选择题1-5 DCCDD 6-10 AABBA11-13 CCB三、读程序写结果1、y=-12、m=13、364、a=-55、1.6000006、87、*#*#*#$8、89、8,-210、411、1四、程序填空题1、a!=0或a 2、i=1, m=0 a%i= =03、100-5*i-j*2 l%5= =04、i%3= =2&& i%5= =3&& i%7= =2 j=j+15、n/10-i*10 n= =i*i*i+j*j*j+k*k*k 6、m%n n w

38、五、编程题1、 # include<stdio.h>void main( ) int i, a=0, b=0, c=0, d=0, e=0; char grade;for(i=1; i<=30; i+) printf(“Input NO.%d grade: ”, i);scanf("%c ",&grade); switch(grade) case 'A': a+;break; case 'B': b+;break; case 'C': c+;break; case 'D': d+;br

39、eak;case 'E': e+; printf("A=%c B=%c C=%c D=%c E=%c n", a, b, c, d, e);2、 #include<stdio.h>#include<math.h>void main() int m,k,i,n=0; for(m=1;m<=100;m+) k=sqrt(m); for(i=2;i<=k;i+) if(m%i= =0) break; if(i>=k+1) printf("%6d",m); /*每个数占6个字符的宽度*/n=n+1; if

40、(n%5= =0) printf("n"); /*每5个数就换一行*/ printf("n");第7章 数组一、单项选择题1-5 BDBDB 6-10 DCADB11-15 CDDD二、读程序写结果1、aabcd2、fi3、count0=6, count1=114、GDABC5、LBLMNP6、7078 9198三、程序填空题1、 m<9 i = 0 2、&numijnumij!= numji3、完整程序如下#include<stdio.h> void main() int a32=2,10,9,4,5,119,b22=-1,-

41、2,-3,-4; int i,j,k,s,c32; for(i=0;i<3;i+) for(j=0;j<2;j+) s=0; for(k=0;k<2;k+) s+=aik*bkj; cij=s; for(i=0;i<3;i+) for(j=0;j<2;j+) printf("%6d",cij); printf("n"); 4、完整程序如下main() int a10=191,3,6,4,11,7,25,13,89,10;int i,j,k;for(i=0;i<10;i+) k=ai;j=i-1;while(j>=

42、0&&k<aj) aj+1=aj;j-;aj+1=k;for(i=0;i<10;i+) printf("%d ",ai);5、50 c= = tk四、编程题1、#include<stdio.h>void main() char s180,s240; int i=0,j=0,n=-1,result; char c; printf("input s1:"); gets(s1); printf("以下为求s1的长度n"); while(s1i!='0') i+; printf(&quo

43、t;s1长度为:%dnn",i); printf("以下为将s1赋值给s2n"); i=0; while(s1i!='0') s2i=s1i; i+; s2i='0' puts(s2); printf("n"); printf("以下为连接s1和s2n"); i=0; printf("input s1:"); gets(s1); printf("input s2:"); gets(s2); while(s1i!='0') i+; whil

44、e(s2j!='0') s1i+=s2j+; s1i='0' puts(s1); printf("n"); printf("以下为在s1中搜索字符n"); i=0; printf("input s1:"); gets(s1); printf("input a charactor:"); c=getchar(); getchar(); while(s1i!='0') if(s1i=c) n=i; break; i+; printf("字符在字符串中的下标位置为

45、:%dnn",n); printf("以下为比较两个字符串n"); i=0; printf("input s1:"); gets(s1); printf("input s2:"); gets(s2); while(s1i=s2i)&&(s1i!='0') i+; if(s1i='0'&&s2i='0') result=0; else result=s1i-s2i; printf("result=%dn",result);第8章

46、 函数一、BBABA CBCDA D二、1. 0 2 4 6 8 10 12 14 16 180 2 4 6 8 10 12 14 16 182. -125=-5*5*53. 4 34. 3 5 7 1 5 95. 8,17三、1. p+ ai-1=ai;2.(a%i=0)&&(b%i=0) (m*n)/max3. n+ flag=-14. x2=mid x1=mid5.ch=# c=#第9章 编译预处理一、ABAAB BBACB二、××第10章 指针一、ADDBD ACBDD DCDBB DCDCB DCDAD ABCBD ACDBCC二、1. edcba

47、 2. 7 3. 5, 4 4, 54. 80,-205. BAAAAA6. c7. 38. 9,69. This is A Book10. 98765432111. TCILISILICT12. bad三、1. stri!=0 j=i k+12. a i%10*10+i3. a*pk=ai4. while(*s+=*t+) *t=05. str sp=sp+i 第11章 结构体与共用体一、 CDDDB CC二、 name=zhangping,old=20第12章 位运算一、 CBCBB第13章 文件一、 CADA二、1. fname fp2. if(strcmp(str,string)=0)

48、 fopen(argv1,”r”)3. *+argv,”r”fgets(s,BUFFSIZE,fg)4. char *argvc=fgets(fp)5. char *argv!feof(fp1)6. hello7. fp=fopen(“data.txt”,”r”)=NULL ch-A 第三部分 上机实验与指导(答案)实验三 选择结构程序设计1、main()float score;char grade;printf(“Please input a student score:”);scanf(“%f”, &score);while(score>100 | score<0)pr

49、intf(“nInput error, try again!”);scanf(“%f”, &score);switch ( (int)(score/10) )case 10: case 9: grade=A; break;case 8: grade=B; break;case 7: grade=C; break;case 6: grade=D; break;case 5:case 4:case 3:case 2:case 1:case 0: grade=E;printf(“score is %5.1f, score grade is %c.n”, score, grade);2、#in

50、clude <stdio.h>main( ) float x, y ; printf(“Please input x:”);scanf(“%f”, &x);if (x < 0)y = 0;else if (x < 10) y = x ; else if ( x < 20 )y = 10;else if ( x < 40)y = 0.5 * x + 20;else printf(“Input Error!”); exit (0);printf (“y=%g”, y);3、include <stdio.h>void main(void)int choice;/* 显示菜单及输入提示信息 */printf("-n") ;printf(" The Menu of Data Managementn") ;printf("1. Input Datan") ;printf("2. Output Datan") ;printf("3. Insert Datan"

温馨提示

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

评论

0/150

提交评论