实验4 选择结构程序设计一、实验目的(1)了解C 语言表示逻辑量的方法(以0代表“假”,以非0代表“真”)。
(2)学会正确使用逻辑运算符和逻辑表达式。
(3)熟练掌握if 语句和switch 语句。
(4)结合程序掌握一些简单的算法。
(5)学习调试程序。
一、实验内容题目1 有3个整数a 、b 、c ,由键盘输入,输出其中最大的数。
方法1:main( ){int a ,b ,c ;printf("input three integer :");scanf("%d ,%d ,%d",&a ,&b ,&c);if (a<b)if (b<c) /*a 小于b 且b 小于c ,则c 最大*/printf("max =%d\n",c);else /*a 小于b 且b 大于c ,则b 最大*/printf("max =%d\n",b);else if(a<c) /*a 大于b 且a 小于c ,则c 最大*/printf("max =%d\n",c);else /*a 大于b 且a 大于c ,则a 最大*/printf("max =%d\n",a);}方法2:main( ){int a ,b ,c ,temp ,max ;printf("input three integer :");scanf("%d ,%d ,%d",&a ,&b ,&c);temp =(a>b)?a :b ; /*temp 为a 、b 中较大值*/max =(temp>c)?temp :c ; /*在temp 和c 中比较出最大值*/printf("max =%d\n",max);}运行结果:input three integer :12,34,9↙max = 34题目2 有一函数:y =⎪⎩⎪⎨⎧≥-<≤-<)10(113)101(12)1(x x x x x x,写一程序,输入x ,输出y 值。
main( ){int x ,y ;printf("input x :");scanf("%d",&x);if(x<1) /*当x <1时,求对应y 值*/{y=x;printf("x=%3d,y=x=%d\n",x,y);}else if(x<10) /*当1≤x<10时,求对应y值*/{y=2*x-1;printf("x=%3d,y=2*x-1=%d\n",x,y);}else /*当x≥10时,求对应y值*/{y=3*x-11;printf("x=%3d,y=3*x-11=%d\n",x,y);}}运行结果:input x:20↙x= 20,y=3*x-11=49题目3 给出一百分制成绩,要求输出成绩等级‘A’、‘B’、‘C’、‘D’、‘E’。
90分以上为‘A’,80~89分为‘B’,70~79分为‘C’,60~69分为‘D’,60分以下为‘E’。
main( ){float score;char grade;printf("input student score:");scanf("%f",&score);while (score>100||score<0) /*当输入错误时,提示用户并允许重新输入*/{printf("error\n.");scanf("%f",&score);}switch((int)(score/10)) /*将score/10的值转换成整型以便于判断*/{case 10:case 9: grade=’A’;break;case 8: grade=’B’;break;case 7: grade=’C’;break;case 6: grade=’D’;break;case 5:case 4:case 3:case 2:case 1:case 0: grade=’E’;}printf("score: %5.1f,grade: %c\n",score,grade);}运行结果:input student score:90.5↙score: 90.5,grade:A题目4给一个不多于5位的正整数,要求:①求出它是几位数;②分别打印出每一位数字;③按逆序打印出各位数字,例如原数为321,应输出123。
1main( ){long int num;int indiv,ten,hundred,thousand,ten_thousand,place;printf("input 0-99999:");scanf("%ld",&num);if(num>9999)place=5;else if(num>999)place=4;else if(num>99)place=3;else if(num>9)place=2;else place=1;printf("place=%d\n",place);/*输出位数*/printf("shuzi is:");ten_thousand=num/10000;/*以下五行分别求万位、千位、百位、十位、个位数字 */thousand=(int)(num-ten_thousand*10000)/1000;hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);switch(place) /*根据位数判断应该输出哪几位数字*/{case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv);/*正序输出*/printf("\ninvert is:");printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);/*逆序输出*/break;case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);printf("\ninvert is:");printf("%d%d%d%d\n",indiv,ten,hundred,thousand);break;case 3:printf("%d,%d,%d",hundred,ten,indiv);printf("\ninvert is:");printf("%d%d%d\n",indiv,ten,hundred);break;case 2:printf("%d,%d",ten,indiv);printf("\ninvert is:");printf("%d%d\n",indiv,ten);break;case 1:printf("%d",indiv);printf("\ninvert is:");printf("%d\n",indiv);break;}}运行结果:input 0-99999:98765↙place=5shuzi is:9,8,7,6,5invert is: 5678923 题目5 试编程判断输入的正整数是否既是5又是7的整数倍。
若是,则输出yes ;否则输出no 。
main( ){int x ;printf("input x :");scanf("%d",&x);if(x%5==0&&x%7==0)printf("yes");elseprintf("no");}运行情况:inupt x :35↙yes思考:if 中的条件表达式可否改成 !x%5&&!x%7 ?题目6 编制程序要求输入正整数a 和b ,若a 2+b 2大于100,则输出a 2+b 2百位以上的数字,否则输出两数之和。
main( ){int a ,b ,x ,y ;printf("input a 、b :");scanf("%d%d",&a ,&b);x =a*a +b*b ;if(x>100) /*若a 2+b 2>100*/{y =x/100; /*y 为a 2+b 2 百位以上的数字*/printf("%d",y);}else printf("%d",a +b);}运行情况:input a 、b : 10 8↙1题目7 已知银行整存整取存款不同期限的月息利率分别为:月息利率=⎪⎪⎪⎩⎪⎪⎪⎨⎧=====年期限年期限年期限年期限年期限8%84.05%75.03%69.02%66.01%63.0输入存款的本金和年限,求到期时能从银行得到的利息与本金的合计。
(利息的计算公式为:利息=本金×月息利率×12 ×存款年限。
#include "stdio.h"main( ){int year ;float money ,rate ,total ; /* money :本金 rate :月利率 total :本利合计 */printf("Enter money and year =?");scanf("%f%d",&money ,&year); /* 输入本金和存款年限 */if (year ==1) rate =0.0063; /* 根据年限确定利率 */else if(year ==2) rate =0.0066;else if(year==3) rate=0.0069;else if(year==5) rate=0.0075;else if(year==8) rate=0.0084;else rate=0.0;total=money+money*rate*12*year; /* 计算到期的本利合计 */printf("Total= %.2f\n",total);}题目8输入年份和月份,求该月有多少天。