收藏 分销(赏)

C实验参考答案.doc

上传人:xrp****65 文档编号:7022026 上传时间:2024-12-25 格式:DOC 页数:27 大小:124.50KB
下载 相关 举报
C实验参考答案.doc_第1页
第1页 / 共27页
C实验参考答案.doc_第2页
第2页 / 共27页
点击查看更多>>
资源描述
实验二 基本数据类型、运算符及表达式 1. 分析以下程序,判断输出结果,然后上机运行程序验证结果。 (1) # include <stdio.h> main( ) { int sum = 1 ; sum = sum +100 ; printf("SUM = %d\n", sum); } 输出结果为:101 (2) 改正后的程序为: # include <stdio.h> main( ) { char ch1 , ch2 ; ch1 = 'b' ;//不能把字符串"b"赋给字符变量 ch2 = ch1 - 32 ; printf("%c %d %c %d\n", ch2,ch2,ch1,ch1); } (3) # include <stdio.h> main( ) { int a = 10 , b =3 ; printf("%d\n", (a = a - 1 , b + a , b + 2)); } 输出结果为5,(a = a - 1 , b + a , b + 2)作为一个逗号表达式。 (4) # include <stdio.h> main( ) { int a = 5 , b = 3 , c = 4 ; a *= a += 3; b = ++c ; c = b++ ; a = b +++ c ; printf("\n%d,%d,%d", a , b , c); } 输出结果为:11,7,5 (5)程序改正为: # include <stdio.h> main( ) { int a = 1 , b = 2 , c = 3 , logic ; logic = a + b > c && b <= c ; printf("logic = %d\n", logic); logic = a >= b + c || b == c; printf("logic = %d\n", logic); logic =!(a < c) +b!=1 && (a + c)/2 ; printf("logic = %d\n", logic); } 输出结果为: logic = 0 logic = 0 logic = 1 2. 以下程序可能有多处错误,请改正并调试运行程序。 (1) # include <stdio.h> # include HIGH 10//改为:#include HIGH 10 main( ) { int a = 5 ; //可再定义一个变量b HIGH *= HIGH ; //HIGH是宏名,不能被赋值,可改为:b=HIGH*HIGH; printf("\n%d,%d", a , HIGH); //输出a,b的值 } 改正后的程序为: # include <stdio.h> # define HIGH 10 main( ) { int a = 5 ,b; b = HIGH*HIGH ; printf("\n%d,%d", a , b); } 输出结果为:5,100 (2)此程序功能为将输入的华氏温度转换成摄氏温度,公式为c = 5 / 9 ·(f–32 )。 # include <stdio.h> main( ) { float c , f ; scanf("%f", f) ;//输入时,变量要用&取地址 c = (5 / 9)· (F – 32) ; //5/9是为0的,要变成实数。F没有定义,定义的是f printf((" F= %f , C = % f\n", f , c) ;//多了一个左括号 } 改正后的程序为: # include <stdio.h> void main( ) { float c,f; scanf("%f", &f); c=(5.0/9)*(f-32); printf("F= %f,C = %f\n",f,c); } (3)此程序功能是求:y = 。 # include <stdio.h> # include <math.h> main( ) { int a ; double x , y ; Scanf("%d%f", &a , &x) ;//scanf不能大写,x的类型是double,格式化字符应该用%ld y = SIN(sqr(ax) + LN(a + x) ;//sin函数不能大写,sqr应该为sqrt,ax应为a*a,LN不是有效的函数名,要求自然对数用的是log(),另外括号不匹配 printf(" Y= %f \n", y) ;//y的类型是double,格式化字符应该用%ld } 程序改正后为: # include <stdio.h> # include <math.h> main( ) { int a ; double x , y ; scanf("%d%lf", &a , &x);//scanf不能大写 y = sin(sqrt(a*x) + log(a + x));//sin函数不能大写,ax应为a*a,LN不是有效的函数名,要求自然对数用的是log(),另外括号不匹配 printf(" Y= %lf \n", y); } 3. 此程序用于说明数据类型转换,调试运行该程序,然后回答下列问题。 ①此程序中定义了哪几种数据类型的变量? ②指出程序中哪处为自动类型转换,哪处为强制类型转换。 ③变量c按字符形式输出后,该字符是什么?它是ASCII码字符吗? ④对变量x的输出结果给予解释。 #include <stdio.h> main() { int w,x; float f1,f2=1.23456789E9; char c=227; unsigned int y=65535; w=23.4567; f1=(float)w; x=y; printf("w=%d,f1=%f,f2=%f\n",w,f1,f2); printf("c=%c,y=%u,x=%d\n",c,y,x); } 分析:(1)程序中定义了int ,float ,char unsigned 四种数据类型的变量。 (2)f1=(float)w;这一句是强制类型转换,w=23.4567;x=y;用的是自动类型转换 (3)变量C按字符输出的是?,不是ASCII字符。 (4)结果的解释略 实验三 顺序结构程序设计 1.分析以下程序,判断输出结果,然后上机调试验证结果。 (1) main() { int a , b ; char c , d ; scanf ( "%d%d" , &a , &b ) ; scanf ( "%c%c" , &c , &d ) ; printf("%d,%d,%c,%c\n",a,b,c,d); } 运行时输入:34 56ab<回车> 再次运行时,输入:34,56,a,b<回车> 看一看运行结果。 程序改正为: #include <stdio.h> //头文件一定要包含 void main() { int a , b ; char c , d ; scanf ( "%d%d" , &a , &b ) ; scanf ( "%c%c" , &c , &d ) ; printf("%d,%d,%c,%c\n",a,b,c,d); } 分析:当输出为34 56ab时,第一个scanf语句中的a,b变量接收了34和56两个数值,字符ab由第二个scanf语句的变量c,d接收。 (2) 改正后的程序为: #include <stdio.h> void main( ) { int x=34 ; float y=45.98 ; printf ("%6d,%6.2f" , x , y ); } (3) #include <stdio.h> void main() { printf("\n"); printf("%10s%10s\n","china","Beijing"); printf("%-10s%-10s\n","china","Beijing"); printf("%10.3s\n","china"); } 分析:%10s 输出时右对齐,%-10s输出时左对齐,%10.3s表示输出时只输出3个字符 (4) #include <stdio.h> void main() { int k=-1; printf("k=%d\n" , k); printf("k=%o\n" , k); printf("k=%x\n" , k); printf("k=%u\n" , k); } 分析:计算机在做运算的时候,是用补码参与运算的。int 型的数据在VC里是用四个字节表示的。变成补码后再做相应格式的输出。 (5) main() { int a , c; unsigned d; float b , e; a = 3.5 +3/2; b = 23 ; c = '\xe0'; d = - 1; e = 2.555555555; printf (" \n %d , %f , %x , %u , %f " , a , b , c , d , e ); } 分析:和上题一样,是个补码的应用。 (6) #include < stdio.h > main() {int i=97; char ch='a'; putchar(i) ; putchar('\n') ; putchar(ch) ; } 分析:输出 a a (7) #include<stdio.h> main() { int ch; ch=getchar(); putchar(ch); } 分析:从键盘上接收一个字符,显示一个。 2.以下程序可能有多处错误,请改正并上机调试。 (1)输入三角形的三条边的长,求三角形的面积(假设输入的三边长能构成一个三角形)。 main( ) { float a,b,c,s,area;//应该定义成double型 scanf("%f,%f,%f",&a,&b,&c); s=1/2*(a+b+c);//1/2 的值是0,要变成实数 area=sqrt(s*(s-a)*(s-b)*s-c));//括号不匹配。用了数据函数sqrt要包含math.h头文件 printf("a=%7.2f\nb=%7.2f\nc=%7.2f\narea=%7.2f\n",a,b,c,area); } 改正后的程序为: #include<stdio.h> #include<math.h> void main() { double a,b,c,s,area; scanf("%lf,%lf,%lf",&a,&b,&c); s=1.0/2*(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("a=%7.2lf\nb=%7.2lf\nc=%7.2lf\narea=%7.2lf\n",a,b,c,area); } (2)从键盘输入圆的半径的值,计算圆的周长和面积。 main() { float r,c,area; scanf("%f",&r) c=2r*PI;//2*r area=r*r*PI;//PI要定义成宏再用。 printf("r=%f\n",r); printf("c=%.2f\n",c); printf("area=%10.2f\n",area); } 改正后的程序为: #define PI 3.14159 #include<stdio.h> void main() { float r,c,area; scanf("%f",&r); c=2*r*PI; area=r*r*PI; printf("r=%f\n",r); printf("c=%.2f\n",c); printf("area=%10.2f\n",area); } (3) 略 (4) 略 3.编程题 (1)编程序计算平面上任意两点之间的距离。 #include<stdio.h> #include<math.h> void main() { float x1,y1,x2,y2,c; printf("请输入第一个点的座标:"); scanf("%f,%f",&x1,&y1); printf("请输入第二个点的座标:"); scanf("%f,%f",&x2,&y2); c=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); printf("两点距离为:%f",c); } (2)从键盘上输入一个小写字母,用大写字母输出。 #include<stdio.h> void main() { char c; scanf("%c",&c); c-=32; printf("%c",c); } #include<stdio.h> void main() { char c; getchar(c); c-=32; putchar(c); } (分别用getchar()和scanf() 输入,用putchar()和printf()输出) 实验四:选择结构程序设计 参考答案 1.分析以下程序,判断输出结果,然后上机调试验证结果. (1)输出结果为:2,2,2 分析:因为j++||k++的左边为真,所以不管右边的表达式的值为真还是为假,结果都是为真,所以不会执行 k++这个表达式.k的值不会加1.同理,i++会执行.所以i的值会加1. (2)输出结果为:1,0,7 分析:同上,还是考查的逻辑运算符的运算规则的知识点. (3)输出结果为:3 分析:ok1的值是1,非0即为真.!ok1即为假.所以x--就不会执行,会执行else下面的语句,再按照同样的分析 进行. (4)输出结果为:@#& 分析:a的值为2,a>0为真,真值即为1,所以从case 1:入口进行,同理b<10也为真,即为1.也从下面的case 1: 进入.就先输出@,然后碰到break,退出这个switch后,返回上一个switch.但是没有break,所以执行case 0: 后面的语句,同理会输出#,完了后再输出& (5)输出结果为:PassFail 分析:当x值为3的时候,从case 3:入口进行,输出Pass 后没有break语句,继续执行case 2:后面的语句输出 Fail .到了最后就退出了switch结构.不要认为还会回去执行default语句.所以我们一般把default语句放 在最后的. 2.以下程序可能有多处错误,请改正并上机调试. 第一处错误:scanf("%d",year);->scanf("%d",&year); 第二处错误:if((year%4=0&year%100!=0)OR(year%400==0))->if((year%4==0&&year%100!=0)||(year%400==0)) 第三处错误:if(leap=1)->if(leap==1) 3.在横线处给程序填空 (1)输入4个整数,按大小顺序输出 #include<stdio.h> void main() { int a,b,c,d,t; scanf("%d,%d,%d,%d",&a,&b,&c,&d); if(a<b) { t=a;a=b;b=t;} if(b<c) { t=b;b=c;c=t;} if(c<d) { t=c;c=d;d=t;} if(a<b) { t=a;a=b;b=t;} if(b<c) { t=b;b=c;c=t;} if(a<b) { t=a;a=b;b=t;} printf("%d,%d,%d,%d",a,b,c,d); } (2)将字母换成小写 #include<stdio.h> void main() { char c; scanf("%c",&c); if(c>='A'&&c<='Z') c=c+32; printf("%c",c); } 4.(1)编写一个程序,求一元二次方程ax2+bx+c=0的根 #include<math.h> void main() { float a,b,c,x1,x2,disc; printf("please input a,b,c:"); scanf("%f,%f,%f",&a,&b,&c); disc=sqrt(b*b-4*a*c); if(disc<0) printf("this not a gen please input again!"); else if(disc==0) printf("this is two same gen:x1=x2=%f",(-b)/(2*a)); else if(disc>0) printf("this is two diffent gen:x1=%f,x2=%f",(-b+sqrt(disc))/(2*a),(-b-sqrt(disc))/(2*a)); } (2)给出一个百分制的成绩,要求输出成绩等级"A","B","C","D","E".90分以上的为"A",80~89分的为"B",70~79分的为"C",60~69分的为"D",60分以下的为"E". #include<stdio.h> void main() { int score; printf("请输入百分制成绩:"); scanf("%d",&score); if(score>=90) printf("等级:A\n"); else if(score>=80) printf("等级:B\n"); else if(score>=70) printf("等级:C\n"); else if(score>=60) printf("等级:D\n"); else printf("等级:E\n"); } (3)编程序:用scanf()函数给x赋值,分别为x<-1,-1<=x<=1,x>1三种情况,求y的值. #include<stdio.h> void main() { float x,y; printf("请输入x的值:"); scanf("%f",&x); if(x<-1) y=1; else if(x<1) y=2*x+9; else y=5*x*x-3; printf("x=%f,y=%f\n",x,y); } 实验五:循环结构程序设计 参考答案 1.分析以下程序,画出控制流程图,判断输出结果,然后上机调试结果并回答问题. (1)程序改正后为: #include<stdio.h> void main() { int x=567; do { printf("%c",x%10+'0'); }while(x/=10); } 以上程序输出结果为:765,实现的功能为:把一个整数反序输出. (2) #include<stdio.h> void main() { int i; for(i=0;i<5;++i) { if(i==3) continue; printf("%d",i); } } 以上程序输出结果为:0124,循环执行了5次.但只输出四次i的值,原因是当i==3的时候,执行continue语句,跳过下面的语句,转而去执行下一次循环. (3) 程序输出结果为: 10 012345678910 分析:10的输出是由于第一个for循环后面有个分号,下面的printf语句就不再是它的循环体了.所以等到10次循环完了后,再执行printf语句,这时的i已经变成了10. 而第二行的输出是由于第二个for的循环体是printf语句.所以printf语句执行了10次,每次输出一个值. (4)程序输出结果为:6 分析:for(;;) for 语句里面的三个表达式都缺省,只要第二个表达式缺省就构成了死循环.这是个知识点. (5)程序输出结果省略. 2.以下程序可能有多处错误,请改正并上机调试. 改正后的程序如下: #include<stdio.h> void main() { int i=0,j=10,k=2,s=0; for(;;)//只能有两个分号,表示三个表达式都缺省. { i+=k; if(i>j)//后面不能有分号. { printf("%d\n",s); break; }//少了一个花括号. s+=i;//应该在if语句的外面. } } 3.在横线处给程序填空. (1)求1!+2!+3!+...+10! #include<stdio.h> void main() { int i=1; long int t=1,sum=0; do { t=t*i; sum=sum+t; i++; }while(i<=10);//循环10次.每次循环sum累加i! printf("sum=%d",sum); } (2)打印九九表 #include<stdio.h> void main() { int i,j; for(i=1;i<=9;i++) printf("%4d",i); printf("\n"); for(i=1;i<=36;i++) printf("%c",'-'); printf("\n"); for(i=1;i<=9;i++) { for(j=1;j<=9;j++) if(j<i) printf("%8c",' '); else printf("%d*%d=%-4d",i,j,i*j); printf("\n"); } } (3)求Fibonacci数列1,1,2,3,5,8...的前N(N为偶数)项. #include<stdio.h> void main() { long int f1,f2; int i,n; scanf("%d",&n); f1=f2=1; printf("\n");//先换行 for(i=1;i<=n;i++) { printf("%12ld%12ld",f1,f2); if(!(i%2))//每输出两次换行,每次输出2项,也就是每次输出四项了再换行. printf("\n"); f1=f2+f1;//让下一次for循环输出的f1的值是前两个数的和 f2=f2+f1;//让下一次for循环输出的f2的值是前两个数的和 } } 4.编程题 (1)输入一行字符,分别统计出其中的英文字母,空格,数字和其他字符的个数. #include<stdio.h> #include<string.h> void main() { char str[100]; static int i,sum[4]; printf("请从键盘输入一行字符:"); gets(str); for(i=0;i<strlen(str);i++) { if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z') sum[0]++; else if(str[i]==' ') sum[1]++; else if(str[i]>='1'&&str[i]<='9') sum[2]++; else sum[3]++; } printf("英文字母个数为:%d,空格为:%d,数字为:%d,其他字符为:%d",sum[0],sum[1],sum[2],sum[3]); } (2)求100~300之间的全部素数,并且每行输出5个数. #include<stdio.h> #include<math.h> void main() { int i,j,n,flag=1,sum=0; for(i=100;i<=300;i++) { for(j=2;j<=sqrt(i);j++) { if(i%j==0) { flag=0; break; } else flag=1; } if(flag==1) { if(++sum%5==0) printf("%4d\n",i); else printf("%4d",i); } } } (3)分别用三种循环语句完成计算10! //用for循环 #include<stdio.h> void main() { long int i,t=1,sum=0; for(i=1;i<=10;i++) { sum+=t*=i; } printf("10!=%ld",sum); } //用while循环 #include<stdio.h> void main() { long int i=1,t=1,sum=0; while(i<=10) { sum+=t*=i++; } printf("10!=%ld",sum); } //用do...while循环 #include<stdio.h> void main() { long int i=1,t=1,sum=0; do { sum+=t*=i++; }while(i<=10); printf("10!=%ld",sum); } (4).打印所有的"水仙花数".所谓"水仙花数"指一个三位数,其各位数字的立方和等于该数本身.例如:153=1*1*1+5*5*5+3*3*3.153即是一个水仙花数. #include<stdio.h> void main() { int i; for(i=100;i<=999;i++) if(i==(i/100)*(i/100)*(i/100)+(i/10%10)*(i/10%10)*(i/10%10)+(i%10)*(i%10)*(i%10)) printf("%4d",i); } (5).求序列2/1+3/2+5/3+8/5+...的前20项之和. #include<stdio.h> void main() { float m=2,n,t,sum=0; for(n=1;n<=20;n++) { t=n; sum+=m/n; n=m; m=m+t; } printf("sum=%f",sum); } 实验六 函数与编译预处理 1. void main() { float a,b,c; scanf("%f%f",&a,&b); c=add(a,b); printf("sum is %f\n",c); } float add(float x,float y) { float z; z=x+y; return(z); } //错误的地方,(1)没有包含头文件<stdio.h> (2)函数定义应该在函数调用的前面. 更正后的程序: #include<stdio.h> float add(float x,float y) { float z; z=x+y; return(z); } void main() { float a,b,c; scanf("%f%f",&a,&b); c=add(a,b); printf("sum is %f\n",c); } 2. void main() { int a=3,b=6; printf("a=%d,b=%d\n",a,b); exchange1(a,b); printf(:a=%d,b=%d\n",a,b); } void exchange1(int x,int y) { int t; t=x;x=y;y=t; printf("x=%d,y=%d\n",x,y); } //错误的地方,(1)没有包含头文件<stdio.h> (2)函数定义应该在函数调用的前面. 更正后的程序: #include<stdio.h> void exchange1(int x,int y) { int t; t=x;x=y;y=t; printf("x=%d,y=%d\n",x,y); } void main() { int a=3,b=6; printf("a=%d,b=%d\n",a,b); exchange1(a,b); printf("a=%d,b=%d\n",a,b); } 3. long int fac(int n) { long int p; int i; p=1; for(i=1;i<=n;i++) p=p*i; return(p); } int cmn(int m,int n) { int x; x=fac(m)/(fac(n)*fac(m-n)); return(x); } void main() { int m,n,c; scanf("%d%d",&m,&n); c=cmn(m,n); printf("c=%d\n",c); } //应该加个#include<stdio.h> 头文件 4. int a=7,b=9; void main() { int i,a=5,x=80,y=60; for(i=1;i<4;i++) { printf("a=%d,b=5d,max=%d\n",a,b,max(a,b)); b+=6; } printf("result_sum=%d\n",sum(x,y)); } int sum(int x,int y) { extern int m,n; int temp; temp=x+y+m+n+a+b; return(temp); } int m=12,n=25; int max(int 1,int b) { return(a>b?a:b); } 程序更正: #include<stdio.h> int a=7,b=9; void main() { int i,a=5,x=80,y=60; int max(int a,int b); int sum(int x,int y); for(i=1;i<4;i++) { printf("a=%d,b=5d,max=%d\n",a,b,max(a,b)); b+=6; } printf("result_sum=%d\n",sum(x,y)); } int sum(int x,int y) { extern int m,n; int temp; temp=x+y+m+n+a+b; return(temp); } int m=12,n=25; int max(int a,int b) { return(a>b?a:b); } 5.调试运行以下程序. #include<stdio.h> int fun(int a) { int b=0; static int c=2; b+=c; c+=1; return(a+b-c); } void main() { int a=3,i; for(i=0;i<3;i++) printf("%4d",fun(a)); } 6.略 7.略 8. #include<stdio.h> #define DEBUG void main() { int a=14,b=15,temp; temp=a/b; #ifdef DEBUG printf("a=%d,b=%d",a,b); #endif printf("temp=%d\n",temp); } 程序结果为:a=14,b=15,temp=0 (1)第二行中,宏定义是可以没有具体的值的,在这里只是定义了一个宏名.DEBUG没有值. (2)第7~9行为: #ifdef DEBUG printf("a=%d,b=%d",a,b); #endif 会被编译,因为种种原因DEBUG已经被定义过. (3)如将第2行删除,程序运行结果为:temp=0 因为没有定义DEBUG,所以原来第7~9行的程序没有执行. (4)第9行不能删除,因为#ifdef 和 #endif必须成对出现. 9.编程题 #include<stdio.h> long int fac(int n) { long int p; int i; p=1; for(i=1;i<=n;i++) p=p*i; return(p); } void main() { int m,n,c; scanf("%d%d",&m,&n); c=fac(n)/(fac(n-m)*fac(m)); printf("c=%d",c); } 实验七 数组 2 (1)求最大偏差,填空如下: average = sum/count; n-2; date[count]+data[count+1]+data[count+2]; max=sum; highiff=max-average; highiff
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服