logo资料库

C语言上机实验答案.doc

第1页 / 共31页
第2页 / 共31页
第3页 / 共31页
第4页 / 共31页
第5页 / 共31页
第6页 / 共31页
第7页 / 共31页
第8页 / 共31页
资料共31页,剩余部分请下载后查看
实验一 上机操作初步(2 学时) 一、实验方式:一人一机 二、实验目的: 1、熟悉 VC++语言的上机环境及上机操作过程。 2、了解如何编辑、编译、连接和运行一个 C 程序。 3、初步了解 C 程序的特点。 三、实验内容: 说明:前三题为必做题目,后两题为选做题目。 1、输出入下信息:(实验指导书 P79) ************************* Very Good ************************* 2、计算两个整数的和与积。(实验指导书 P81) 3、从键盘输入一个角度的弧度值 x,计算该角度的余弦值,将计算结果输出到屏幕。(书 P3) 4、在屏幕上显示一个文字菜单模样的图案: ================================= 1 输入数据 2 修改数据 3 查询数据 4 打印数据 ================================= 5、从键盘上输入两个整数,交换这两个整数。 四、实验步骤与过程: 五、实验调试记录: 六、参考答案: 1、#include void main( ) { printf(“********************\n”); printf(“ Very Good\n”); printf(“********************\n”); } 2、#include void main( ) { int a,b,c,d;
printf(“Please enter a,b:”); scanf(“%d,%d”,&a,&b); c=a+b; d=a*b; printf(“%d+%d=%d\n”,a,b,c); printf(“%d*%d=%d\n”,a,b,d); } 3、#include #include void main( ) { double x,s; printf(“Please input value of x:”); scanf(“%lf”,&x); s=cos(x); printf(“cos(%lf)=%lf\n”,x,s); } 4、#include void main( ) { printf(“==================================\n”); printf(“ printf(“ 1 输入数据 2 修改数据\n”); 3 查询数据 4 打印数据\n”); printf(“===================================\n”); } 5、#include void main( ) { int x,y,t; printf(“Please enter x and y:”); scanf(“%d%d”,&x,&y); t=x; x=y; y=t; printf(“After swap:x=%d,y=%d\n”,x,y); } 实验二 简单的 C 程序设计(4 学时) 一、实验方式:一人一机 二、实验目的: 1、掌握 C 语言的数据类型。 2、学会使用 C 语言的运算符及表达式。 3、掌握不同数据类型的输入输出方法。 三、实验内容:
说明:前四题为必做题目,后两题为选做题目。 1、输入 r1、r2,求出圆形垫片面积。(实验指导书 P84) 2、输入华氏温度 h,输出摄氏温度 c。(实验指导书 P85) 3、从键盘输入一个 3 位整数,将输出该数的逆序数。(实验指导书 P89) 4、输入并运行以下程序,分析运行结果。 #include void main( ) { int i,j; i=8; j=10; printf(“%d,%d\n”,++i,++j); i=8; j=10; printf(“%d,%d\n”,i++,j++); i=8; j=10; printf(“%d,%d\n”,++i,i); i=8; j=10; printf(“%d,%d\n”,i++,i); } 5、输入三角形三条边的边长,求三角形的面积。(书 P55) 6、输入 3 个字符型数据,将其转换成相应的整数后,求它们的平均值并输出。(书 P55) 四、实验步骤与过程: 五、实验调试记录: 六、参考答案: 1、#include #define PI 3.14 void main( ) { float r1,r2; double s1,s2,s; printf(“Please enter r1,r2:\n”); scanf(“%f%f”,&r1,&r2); s2=r2*r2*PI; s1=r1*r1*PI; s=s2-s1; printf(“s=%lf\n”,s); } 2、#include void main( ) { float h,c;
printf(“请输入华氏温度:”); scanf(“%f”,&h); c=5.0/9*(h-32); printf(“\n 摄氏温度:%f\n”,c); } 3、#include void main( ) { int a,b,c,x,y; printf(“请输入一个 3 位的正整数:\n”); scanf(“%d”,&x); a=x/100; /*求 x 的百位数*/ b=(x-a*100)/10; /*求 x 的十位数*/ c=x-a*100-b*10; /*求 x 的个位数*/ y=c*100+b*10+a; printf(“%d:%d\n”,x,y); } 4、运行结果:9,11 8,10 9,8 8,8 5、#include #include void main( ) { int a,b,c; double area,s; printf(“Please enter a,b,c:”); scanf(“%d,%d,%d”,&a,&b,&c); s=(double)(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf(“area=%.2lf\n”,area); } 6、#include void main( ) { char a,b,c; float x; printf(“Please enter:\n”); a=getchar(); b=getchar(); c=getchar(); x=(a+b+c)/3.0; printf(“(a+b+c)/3=%.2f\n”,x);
} 实验三 选择结构程序设计(2 学时) 一、实验方式:一人一机 二、实验目的: 1、熟练掌握 if 语句和 switch 语句。 2、练习并掌握多分支选择结构的编程方法。 3、学习调试和修改程序的步骤。 三、实验内容: 说明:前三题为必做题目,后两题为选做题目。 1、读入 3 个分别表示箱子长、宽、高的整数值,判断并输出该箱子是立方体还是长方体。 (实验指导书 P104) 2、输入某一年月,输出该月的天数。(实验指导书 P105) 3、有一函数: y= (x<1) (1≤x<10) x 2x-1 3x-11 (x≥10) 编写程序,输入 x 值,输出 y 值。 4、从键盘输入一个字符,如果该字符为小写字母,则转换为大写字母输出;如果该字符 为大写字母,则转换为小写字母输出;如果为其他字符,原样输出。(书 P94) 5、输入 4 个整数,要求按由小到大的顺序输出。 四、实验步骤与过程: 五、实验调试记录: 六、参考答案: 1、#include void main( ) { int l,w,h; printf(“请输入箱子的长、宽、高:\n”); scanf(“%d%d%d”,&l,&w,&h); if(l==w&&w==h) /*如果长、宽、高相等,则为立方体*/ printf(“该箱子是立方体。”); else printf(“该箱子是长方体。”); } 2、#include void main( ) { int year,month,days;
printf(“Please enter year and month:\n”); switch(month) { case 2: if(year%4==0&&year%100!=0||year%400==0) days=29; else days=28; break; case 1:case 3:case 5:case 7:case 8:case 10:case 12: days=31; break; case 4: case 6: case 9: case 11: days=30; } printf(“%d 年%d 月有%d 天\n”,year,month,days); } 3、#include void main( ) { float x,y; printf(“Please input x:\n”); scanf(“%f”,&x); if(x<1) y=x; else if(x<10) y=2*x-1; else y=3*x-11; printf(“y=%f\n”,y); } 4、#include void main( ) { char x; printf(“Enter a char:\n”); x=getchar(); if(x>=‘a’&&x<=‘z’) x=x-32; else if(x>=‘A’&&x<=‘Z’) x=x+32; putchar(x); } 5、#include void main( ) { int a,b,c,d,t;
printf(“请输入 4 个整数:”); scanf(“%d%d%d%d”,&a,&b,&c,&d); if(a>b) { t=a; a=b; b=t; } if(a>c) { t=a; a=c; c=t; } if(a>d) { t=a; a=d; d=t; } if(b>c) { t=b; b=c; c=t; } if(b>d) { t=b; b=d; d=t; } if(c>d) { t=c; c=d; d=t; } printf(“排序结果如下:\n”); printf(“%d,%d,%d,%d\n”,a,b,c,d); } 实验四 循环结构程序设计(4 学时) 一、实验方式:一人一机 二、实验目的: 1、熟练掌握 while 语句、do-while 语句和 for 语句。 2、练习并掌握循环结构的嵌套形式。 3、掌握循环结构的程序设计方法。 三、实验内容: 说明:前四题为必做题目,后两题为选做题目。 1、从键盘上输入若干个学生的成绩,统计并输出最高成绩和最低成绩,当输入负数时结 束输入。(实验指导书 P117) 2、求所有的水仙花数。水仙花数是一个 3 位数的自然数,该数各位数的立方和等于该数 本身。(实验指导书 P118) 3、判断输入的某个数是否为素数。若是,输出 YES,否则输出 NO。(书 P123) 4、计算π的近似值。公式如下:π/4=1-1/3+1/5-1/7+……,直到最后一项的绝对值小 于 10-6 为止。(书 P123) 5、计算 s=1!+2!+……+20! 的值并输出。 6、输入 10 个整数,统计并输出其中正数、负数和零的个数。 四、实验步骤与过程: 五、实验调试记录: 六、参考答案: 1、#include void main( ) { float x,max,min;
printf(“Please input scores:”); scanf(“%f”,&x); max=min=x; while(x>=0) { if(x>max) max=x; if(x void main( ) { int x,y,z; int k=100; while(k<=999) { x=k/100; y=(k-x*100)/10; z=k-x*100-y*10; if(k==x*x*x+y*y*y+z*z*z) printf(“%d\n”,k); k++; } } 3、#include #include void main( ) { int i,x,yes,a; printf(“Enter integer number:”); scanf(“%d”,&x); yes=1; i=2; a=(int)sqrt((double)x); while(yes&&i<=a) if(x%i==0) yes=0; i++; { } printf(“%d”,x); if(yes) printf(“YES\n”);
分享到:
收藏