C语言-第4章 选择结构程序设计(2)

2019-08-31 21:24

12.

#include main( ) { char ch;

ch=getchar( ); switch(ch)

{ case ?A? : printf(“%c”,?A?); case ?B? : printf(“%c”,?B?);

break;

default: printf(“%s\\n”,”other”); } }

当从键盘输入字母A时,运行结果为: AB 14.

#include main ( )

{ int i=0,j=0,k=6; if((++i>0)||(++j>0)) k++;

printf(\}

运行的结果为: 1,0,7

13.

#include main( )

{ int a=1,b=0;

scanf(“%d”,&a); switch(a)

{ case 1: b=1;break; case 2: b=2;break; default : b=10;} printf(\}

若键盘输入5,运行结果为:10

三、编程题

1、输入两个数,输出较大的数。 #include int main() {

int a,b;

printf(“Please input two numbers :”) ;

scanf(“%d%d”,&a,&b); if(a>b)

printf(“%d\\n”,a) ; else

printf(“%d\\n”,b);

return 0 ;

}

2、输入一个整数,判断其奇偶性。 #include void main(){ int m; scanf(\ if (m%2 == 0) printf(\ else printf(\ } 输出。

#include int main ( ) { char c1; c1=getchar(); if(c1>=?A?&&c1<=?Z?) c1=c1+32; putchar(c1); return 0;

}

4、输入三个数,按照从小到大的顺序输出。 #include int main(){ float a,b,c,t;

scanf(“%f%f%f”,&a,&b,&c); if(a>b)

{t=a;a=b;b=t;}

3、输入一个字符,如果是大写英文字母,将其转换为小写字母并输出,如果不是,则原样

if(a>c)

{t=a;a=c;c=t;} if(b>c)

{t=b;b=c;c=t;}

printf(\}

5、函数y=f(x)表示如下,编程实现输入一个x值,输出y值。 2x+1 (x<0) y= 0 (x=0) 2x-1 (x>0)

#include void main() { int x,y;

scanf(“%d”,&x); if(x<0)

y=2*x+1; if(x>0)

y=2*x-1; if(x==0)

y=0; printf(“%d”,y); }

6、编一程序每个月根据每个月上网时间计算上网费用,计算方法如下:

30元??费用??每小时3元?每小时2.5元?

?10小时10?50小时?50小时要求当输入每月上网小时数,显示该月总的上网费用。 #include void main() { int hour; float fee;

printf(“please input hour:\\n”); scanf(“%d”,&hour); if(hour<=10) fee=30; else if(hour<=50) fee=3*hour;

else

fee=hour*2.5;

printf(“The total fee is %f”,fee); }

7、神州行用户无月租费,话费每分钟0.6元,全球通用户月租费50元,话费每分钟0. 4元。输入一个月的通话时间,分别计算出两种方式的费用,判断哪一种合适。 #include

void main() { float t,szx,qqt;

printf(“请输入您的通话时间:”); scanf(“%f,”,&t); szx= 0.6*t; qqt=50+0.4*t; if (szx>qqt)

printf(“建议使用全球通”); else

printf(“建议使用神州行);

}

8、运输公司对用户计算运输费用。路程(s km)越远,每吨·千米运费越低。标准如下: s < 250 没有折扣 250≤s < 500 2%折扣 500≤s < 1000 5%折扣 1000≤s < 2000 8%折扣 2000≤s < 3000 10%折扣

3000≤s 15%折扣

#include int main() {

double s,w,p,cost,t; //s、w、p、t、cost分别表示路程、重量、单价、折扣、费用 printf(\ scanf(\ if(s<0||w<0||p<0)

printf(“输入错误!”);

else {

if(s>=3000) t=0.15;

else if(s>=2000)

t=0.10;

else if(s>=2000)

t=0.10;

else if(s>=1000)

t=0.08;

else if(s>=500)

t=0.05; else if(s>=250)

t=0.08;

else

t=0;

cost=p*w*s*(1-t);

printf(“cost=.2f\\n”,cost); }

return 0;

}

9、要求按照考试成绩的等级输出百分制分数段,A等为85分以上,B等为70~84分,C等为60~69分 ,D等为 60分以下 。成绩的等级由键盘输入。

#include int main(){ char grade; scanf(\ printf(\ switch(grade) { case 'A': printf(\~100\\n\ case 'B': printf(\~84\\n\ case 'C': printf(\~69\\n\ case 'D': printf(\ default: printf(\ }

return 0; }


C语言-第4章 选择结构程序设计(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:语法

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: