当前位置:文档之家› 济南大学C语言上机实验答案

济南大学C语言上机实验答案


2、编写程序,能显示如下图形:
* * * * * * * * * *
printf(“*\n” “**\n” “***\n” “****\n”);
#include<stdio.h> void main() { printf(“*\n”); printf(“**\n”); printf(“***\n”); printf(“****\n”); }
printf("a=%d,b=%d\nc1=%c,c2=%c\nd=%15.6f,e=%15.12f\n",a,b,c1,c2,d,e); printf("f=%f,g=%f\n",f,g);
??:d的取值为什么是3157.890137 而不是3157.890121呢?
2_9. 分析以下程序的应得结果,并与上机运行结果进行比较。
#include <stdio.h> void main( ) scanf(“%d%d,&a,&b”); { int a,b,sum; scanf(“%d%d”,&a,&b); cj=f(a,b); printf(“cj is %d\n", cj); } int f(int x,int y) { int z; z=x*y; return z; }
切记:不要同时在VC中打开两个C源程序,做完一个程序记得 要先关闭其工作空间(不是仅关闭打开的程序文件),才能新建 或打开第二个C源程序。
2_2.编写程序,输入圆半径r,求圆周长、圆面积、圆球表面积、圆球体积。 #include<stdio.h> #define PI=3.1416 #define PI 3.1416 void main() 此处,float可以替换为double,格式 { 控制符"f"相应的替换为"lf" float r,zc,mj,bmj,tj; printf("please input radius:\n"); scanf("%f",&r); zc=2*PI*r; mj=PI*r*r; tj=4.0/3*PI*r*r*r tj=4/3*PI*r*r*r bmj=4*PI*r*r; tj=4*PI*r*r*r/3; printf("r=%.2f,zc=%.2f,mj=%.2f,bmj=%.2f,tj=%.2f\n",r,zc,mj,bmj,tj); }
2_1. 编写程序,从键盘输入一个大写字符,将它转换为对应的小写字母后输出。
#include<stdio.h> void main() { char x,y; printf("please input a capital:\n"); scanf("%c",&x); y=x+32; printf("it's lowercase is %c\n",y); }
数据类型所占存储空间的大小 #include<stdio.h> void main() { int x,y; float a; scanf(“%d,%d”,&x,&y); a=(float)(x+y)/2; printf("The average is:%f",a); }
(9)2.3,5.4,运行结果-429496729,出错!
3_1. 编写程序,输入一个字符存入变量ch中,根据该字符的ASCII码值判断并输出 字符的类型,即字母(alpha)、数字(numeric)或其他字符(other)。
#include<stdio.h> void main() if(表达式);× { char ch; printf("please input a char:"); if语句括号后面不要加分号 scanf("%c",&ch); if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') 'ch' '65' × printf("%c is alpha\n",ch); 'a':表示字符常量a,单引号内 else if(ch>='0'&&ch<='9') 部只能有一个字符,字符变量 printf("%c is numeric\n",ch); 的外面不能加单引号。 else 例如:a, 'a' 是完全不同的,a printf("%c is other\n",ch); 表示变量,而'a'表示字符常量 } 如何判断ch是字母? if (ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') 其中‘A‟表示A的ASCII码。 为什么不能直接写if (ch>=„A‟&&ch<=„z‟) ?
printf("a=%d,b=%d\nc1=%c,c2=%c\nd=%15.6f,e=%15.12f\n",a,b,c1,c2,d,e); printf("f=%f,g=%f\n",f,g);
??:e的取值为什么是-6.869999885559?
2_10. 以下程序的功能为计算由键盘输入的任意两个整数的平均值
#include<stdio.h> void main() { float x,y,a; scanf("%f,%f",&x,&y); a=(x+y)/2; printf("The average is:%f",a); }
2_10. 补充内容:测试各基本数据类型所占用的存储空间大小(VC环境中)。 课本P266第8题 #include<stdio.h> void main( ) { int c,f,i,d,s,l,ld; sizeof( ):用以测试一个数据或数据类型所占 c=sizeof(char); 存储空间字节数。 i=sizeof(int); 格式: sizeof(数据类型)或 sizeof(表达式) s=sizeof(short); l=sizeof(long); f=sizeof(float); d=sizeof(double); ld=sizeof(long double); printf("char=%d, int=%d, short int=%d, long int=%d,float=%d, double=%d, long double=%d\n",c,i,s,l,f,d,ld); }
(7)33000,31542,运行结果-497,出错!
#include<stdio.h> void main() { short x,y; float a; scanf("%hd,%hd",&x,&y); a=(float)(x+y)/2; printf("The average is:%f",a); }
#include<stdio.h> void main() { short x,y,a; scanf("%x,%"y,&x,&y); a=(x+y)/2; printf("The average is:"a); }
(6)1,0,运行结果为0,出错!
#include<stdio.h> void main() { short x,y,a; scanf("%hd,%hd",&x,&y); scanf( "%hd,%hd",&x,&y) a=(x+y)/2; printf("The average is:%hd",a); printf("The average is:%hd",a); }
2_3.输入一个华氏温度,要求输出摄氏温度。公式为: c
5 F 32 9
#include <stdio.h> void main() { float c,f; printf("please input f:\n"); scanf("%f",&f); c=5.0/9*(f-32) 或 c=5*(f-32)/9 c=1.0*5/9*(f-32); printf("c=%.2f\n",c); }
或: #include<stdio.h> void main() { printf(“*\n**\n***\n****\n”); }
切记:不要同时在VC中打开两个C源程序,做完一个程序记得 要先关闭其工作区,才能新建或打开第二个C源程序。
3、编写程序,从键盘输入两个整数,输出两个整数的积。
#include <stdio.h> void main( ) inta,b,cj; { int a,b,cj; scanf(“%d%d”,&a,&b); cj=a*b; printf(“cj is %d\n", cj); }
#include<stdio.h> void main() { 未作修改前运行结果 int a,b; float d,e; char c1,c2; double f,g; a=61;b=62; c1='a';c2='b'; f=3157.890121;g=0.123456789; d=3.56;e=-6.87; printf("a=%d,b=%d\nc1=%c,c2=%c\nd=%6.2f,e=%6.2f\n",a,b,c1,c2,d,e); printf("f=%15.6f,g=%15.12f\n",f,g); }
相关主题