选择作业:
1、输入两个运算量和一个运算符,完成加、减、乘、除、取余运算,输出运算结果。 #include\ main( ) {
int x,y,z; char ch;
printf(\ch=getchar();
printf(\scanf(\switch(ch) {
case '+':z=x+y;printf(\ case '-':z=x-y;printf(\,z);break; case '*':z=x*y;printf(\,z);break; case '/': { if (y==0)
printf(\ else {z=x/y; printf(\ case '%': {if (y==0)
printf(\
else {z=x%y; printf(\
default:printf(\ } } 2.
用公式f=p*w*s*(1-d)计算运输费。要求运费和路程从
# include
printf(\printf(\printf(\if(s>=3000) c=12; else c=s/250; switch( c){
case 0: d=0;break; case 1: d=0.02;break; case 2:
case 3: d=0.05;break;
case 4: case 5: case 6: case 7: d=0.08;break; case 8: case 9: case 10: case 11: d=0.10;break; case 12: d=0.15; break; } f=p*w*s*(1-d); printf(\}
3.区分键盘上的字母、数字、空格和回车字符。 # include
{ char c; printf(\ c = getchar(); if(c==' ' || c=='\\n')
printf(\ else if(c>='0' && c <= '9')
printf(\
else if(c>='a' && c <= 'z' ) printf(\大写 letter.\\n\ else if(c>='A' && c <= 'Z')
printf(\小写 letter.\\n\ else
printf(\}
实验三
一、编程,输入任意三个数n1,n2,n3,求其中最大的一个数。 输入输出示例: input n1,n2,n3: 2 10.5 max=10.50 源程序: #include
{float n1,n2,n3,max;
printf(\scanf(\max=n1;
if(max printf(\} 运行结果: please input n1,n2,n3:9 10 3 8 the max is:10.0 Press any key to continue 二、编程,输入x,计算并输出下列分段函数f(x)的值(保留2位小数)。 x x<1 y= 2x-1 1<=x<10 3x-1 1 x>=10 输入输出示例: input x: -2.5 f(-2.500000)=-2.5 源程序: #include {float x,y; printf(\scanf(\if(x<1) y=x; else if(x>=1&&x<10) y=2*x-1; else y=3*x-1; printf(\} 运行结果; 第一次运行: please input x:0.5 f(0.5)=0.5 Press any key to continue 第二次运行; please input x:8 f(8.0)=15.0 Press any key to continue 第三次运行: please input x:10 f(10.0)=29.0 Press any key to continue 三、编程,把百分制成绩转换成5级记分制,要求用switch语句。 90分以上(包括90): A 80至90分(包括80):B 70至80分(包括70):C 60至70分(包括60):D 60分以下:E 输入输出示例: input Score: 86 86的等级为B 源程序: #include printf(\scanf(\y=(int)(score/10); switch(y) {case 0: case 1: case 2: case 3: case 4: case 5: printf(\的等级为E\\n\,score); break; case 6: printf(\的等级为D\\n\,score); break; case 7: printf(\的等级为C\\n\,score); break; case 8: printf(\的等级为B\\n\,score); break; case 9: case 10: printf(\的等级为A\\n\,score); break;} } 第一次运行结果: please input score:100 A Press any key to continue 第二次运行结果: please input score:86 86.0的等级为B Press any key to continue 第三次运行结果: please input score:50 50的等级为E Press any key to continue 四、编程,输入2005年的任一个月,输出这个月的天数,要求使用switch语句。 输入输出示例: Input month of 2005: 10 2005年10月有31天 源程序: #include void main( ) {int month; printf(\scanf(\switch(month) {case 1:case 3:case 5:case 7: case 8:case 10:case 12: printf(\年%d月有31天\\n\case 2: printf(\年%d月有28天\\n\case 4:case 6:case 9:case 11: printf(\年%d月有30天\\n\default: } printf(\ } 运行结果: please input a month;1 2005年1月有31天 Press any key to continue 五、改错,对2个整数进行乘、除和求余运算。 源程序(有错误的程序) #include prnitf(“输入x 运算符 y:”); scanf(“%d%c%d”,&x,&sign,&y); if(sign==’*’) printf(“%d * %d = %d\\n”,x,y,x*y); else if(sign==’/’) printf(“%d / %d = %d\\n”,x,y,x/y); else if(sign==’%’) printf(“%d %% %d = %d\\n”,x,y,x%y); else printf(“运算符输入错误”); }