c语言选择结构设计(练习课)

上传人:san****019 文档编号:70865964 上传时间:2019-01-18 格式:PPT 页数:25 大小:415.31KB
返回 下载 相关 举报
c语言选择结构设计(练习课)_第1页
第1页 / 共25页
c语言选择结构设计(练习课)_第2页
第2页 / 共25页
c语言选择结构设计(练习课)_第3页
第3页 / 共25页
c语言选择结构设计(练习课)_第4页
第4页 / 共25页
c语言选择结构设计(练习课)_第5页
第5页 / 共25页
点击查看更多>>
资源描述

《c语言选择结构设计(练习课)》由会员分享,可在线阅读,更多相关《c语言选择结构设计(练习课)(25页珍藏版)》请在金锄头文库上搜索。

1、第四章 选择结构程序设计 练习课,若变量已正确定义,在if (W)printf(“%dn ”,k);中,以下不可替代W的是 A)ab+c B)ch=getchar() C)a=b+c D)a+,答案: A (不能用符号表示不等于),有以下程序 include main() int a=1,b=0; if(!a) b+; else if(a=0)if(a)b+=2; else b+=3; printf(”%dn”,b); 程序运行后的输出结果是 A)0 B)1 C)2 D)3,答案: A (if与else的配对)问题,若有定义语句int a, b;double x;则下列选项中没有错误的是 A)

2、switch(x%2) B)switch(int)x/2.0) case 0: a+; break; case 0: a+; break; case 1: b+; break; case 1: b+; break; default : a+; b+; default : a+; b+; C)switch(int)x%2) D)switch(int)(x)%2) case 0: a+; break; case 0.0: a+; break; case 1: b+; break; case 1.0: b+; break; default : a+; b+; default : a+; b+; ,答

3、案: C A: x不能进行%运算 B: switch的表达式不是整数 D:case后面只能是整数常量,有以下程序 #include main() int x=1, y=0; if ( !x ) y+; else if( x=0 ) if ( x ) y+=2; else y+=3; printf ( “%dn “, y); 程序运行后的输出结果是 A) 3 B) 2 C) 1 D) 0,答案: D,有以下程序 #include main() int x; scanf(“%d“, 若程序运行时从键盘输入12,则输出结果为 _ 。,答案: 1217,有以下程序(说明:字符0的ASCII码值为48)

4、 #include main() char c1,c2; scanf(“%d“, 若程序运行时从键盘输入48,则输出结果为 _ 。,答案: 09,以下程序运行后的输出结果是 _ 。 #include main() int x=10,y=20,t=0; if(x=y) t=x ; x=y ; y=t ; printf(“%d %dn“, x , y); ,答案: 20 0,练习1,1.当从键盘输入字符a或A时,输出American; 当从键盘输入字符b或B时,输出Britain;当从键盘输入字符c或C时,输出China;若输入其他字符则输出France。 2. 输入a、b、c的值,求ax2+bx

5、+c=0的根。,练习2 switch,根据输入的数字1-7,输出相对应星期单词。 Example : What day is it? 7 The day is Sunday.,#include void main() int day; printf(“what day is it? n“); scanf(“%d“, ,#include void main() char day; printf(“what day is it? n“); day=getchar(); printf(“The day is “); switch(day) case 1: printf(“Monday!“); bre

6、ak; case 2: printf(“Tuesday!“); break; case 3: printf(“Wednesday!“); break; case 4: printf(“Thurday!“); break; case 5: printf(“Friday!“); break; case 6: printf(“Saturday!“); break; case 7: printf(“Sunday!“); printf(“n“); ,练习3 number of days in a month,输入一个整数(1-12),显示该整数相对应月份的天数。 Example : Give a mon

7、th number : 5 This month has 31 days.,#include void main() int month, days; printf(“Give a month number: n“); scanf(“%d“, ,#include void main() int month, days; printf(“Give a month number: n“); scanf(“%d“, ,练习4 : the clocks hand,分别输入三个整数 hour, minute, second,表示一个有效的时间,要求在屏幕上显示下一秒的时间。 Example : What

8、 time is it ? 15, 59, 59 One second later, it will be 16 :00 : 00,#include void main() int hour,minute, second; printf(“What time is it ? n“); scanf(“ %d:%d:%d“, ,#include void main() int hour,minute, second, s; printf(“What time is it ? n“); scanf(“ %d:%d:%d“, ,练习5 : 计算器,输入两个实数和一个运算符(+,-,*,/),计算这两个

9、数计算后的结果。 Example : Give the first real 2.6 Give the second real 3 Give a character among +, -, *, / * The result of 2.6 * 3 is 7.8,#include void main() double a,b; char op; printf(“Give the first realn“); scanf(“%lf“, ,输入可如下表示 printf(“ The expression is “); scanf(“%lf%c%lf“,练习6:日期的比较,输入两个日期(每个日期包括年、

10、月、日),判断哪个日期在前,哪个日期在后。,#include void main() int year1,month1,day1; int year2,month2,day2; int flag; printf(“the first date(yyyy-mm-dd): “); scanf(“%d-%d-%d“, ,if(year1!=year2) if(year1year2) flag=1; else flag=0; else if(month1!=month2) if(month1month2) flag=1; else flag=0; else if(day1day2) flag=1; e

11、lse flag=0;,printf(“ %04d-%02d-%02d 在 %04d-%02d-%02d“, year1, month1, day1, year2, month2, day2); if(flag) printf(“之前n”); else printf(“之后n”); ,思考题: 自动售货机,一个自动售货机提供几种商品,它能接受20, 10, 5 和1 硬币。 现在输入一种商品的价格,售货机内可用的20, 10, 5和1 硬币数量,以及用户塞入的 20, 10, 5 和 1 硬币数。 要求输出:应找回的钱数及20, 10, 5 和 1 硬币数。 其中要求 找回的硬币数要尽可能的最少 如果售货机内没有零钱的话,应该给出提示信息。,Example : Product cost : 4 Inserted 20-coins: 0 Available 20-coins : 7 Inserted 10-coins: 2 Available 10-coins : 0 Inserted 5-coins: 0 Available 5-coins : 2 Inserted 1-coins: 0 Available 1-coins: 121 Returned coins: 0 20-coin, 1 10-coin, 1 5-coin and 1 1-coin.,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号