第四章 选择结构程序设计
例题部分
1、输入两个学生a和b成绩,输出其中高的成绩。
#include <stdio.h>
int main()
{
float a,b,max;
printf("Please input the score of ab: \n");
scanf("%f,%f",&a,&b);
if(a>b){
max=a;
}else{
max=b;
}
printf("Max=%.2f\n",max);
}
2、输入三个学生的成绩,输出其中高的成绩。
#include <stdio.h>
int main()
{
float a,b,c,t;
printf("Please input the score of abc: \n");
scanf("%f,%f,%f",&a,&b,&c);
if(a>b){ // 假设a最大
t=a; // 实现a和b的交换
a=b;
b=t; // 此时b最大
}
if(a>c){
t=a;
a=c;
c=t; // 此时c最大
}
if(b>c) // b和c再进行比较
{
t=b;
b=c;
c=b; // 注意结尾
}
printf("a=%.2f\nb=%.2f\nc=%.2f\n",a,b,c);
}
3、给出三角形的三个边长,求三角形的面积
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,s,area;
printf("Please input the a b c: \n");
scanf("%f,%f,%f",&a,&b,&c);
if(a+b>c&&a+c>b&&b+c>a)
{
s=0.5*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area=%6.2f\n",area);
}
printf("It is not a trilateral.\n");
return 0;
}
4、为促销,对购买货物多的顾客有优惠,凡买50件以上(含50)的优惠5%,买100件以上(含50)的优惠7.5%,买300件以上(含300)的优惠10%,买500件以上(含500)的优惠15%。要求编写程序,用户输入购买的数量和单价,程序输出应付货款。
#include <stdio.h> int main() { double number,price,discount,sum; printf("Please enter number and price: \n"); scanf("%lf,%lf",&number,&price); if(number>=500) { discount=0.15; }else { if(number>=300) { discount=0.10; }else { if(number>=100) { discount=0.075; }else { if(number>50) { discount=0.05; }else { discount=0; printf("You enter the number is not discount!\n"); } } } } sum=number*price*(1-discount); printf("The discount is %10.2lf\nTotal price is :%2.2lf\n",discount,sum); return 0; }
#include <stdio.h> int main() { double number,price,discount,sum; printf("Please enter number and price: \n"); scanf("%lf,%lf",&number,&price); if(number>=500) { discount=0.15; }else if(number>=300) { discount=0.10; }else if(number>=100) { discount=0.075; }else if(number>=50) { discount=0.05; }else { discount=0; printf("You enter the number is not discount!\n"); } sum=number*price*(1-discount); printf("The discount is %10.2lf\nTotal price is :%10.2lf\n",discount,sum); return 0; }
5、switch语句实现输入数字显示对应星期几
#include <stdio.h>
void main()
{
int a;
printf("input intger numbers: " );
scanf("%d",&a);
switch(a) // 判断a的值,选择对应事件
{
case 1: printf("Monday\n");break;
case 2: printf("Tuesday\n");break;
case 3: printf("Wednesday\n");break;
case 4: printf("Thursday\n");break;
case 5: printf("Friday\n");break;
case 6: printf("Saturday\n");break;
case 7: printf("Sunday\n");break;
default: printf("error\n");break;
}
}
6、判断是否为闰年
(1)普通if-else语句实现
#include <stdio.h> int main() { int year,leap; printf("Please enter the year: \n"); scanf("%d",&year); if(year%4==0) { if(year%100==0) { leap=0; if(year%400==0) { leap=1; // 如果能被一百整除不是闰年,但是能被400整除,则是闰年 }else { leap=0; } }else { leap=1; } }else { leap=0; } // 判断leap的值,为1是闰年,为0不是 if(leap) { printf("This is a year!\n"); }else { printf("This is not a year!\n"); } return 0; }
(2)优化实现
#include <stdio.h> int main() { int year,leap; printf("Please enter the year: \n"); scanf("%d",&year); if(year%4!=0) { leap=0; //如果不能被4整除,则不是; }else if(year%100!=0) { leap=1; //如果不能被100整除,则是; }else if(year%400!=0) { leap=0; //如果不能被400整除,则不是; } else { leap=1; } // 判断leap的值,为1是闰年,为0不是 if(leap) { printf("This is a year!\n"); }else { printf("This is not a year!\n"); } return 0; }
// 也可以用下面更简洁的 // 能被4整除且不能被100整除或能被400整除,则是闰年 if((year%4==0 && year!=0)||year%400==0) { leap=1; } else { leap=0; }
7、运输公司对用户计算运费。运输距离(运输距离以distance表示)越远,单位运费(以每吨.千米为单位)越低。
#include <stdio.h>
int main()
{
int distance,c;
double weight,price,discount,cost;
printf("Please enter the Price Weight Distance: \n");
scanf("%lf,%lf,%d",&price,&weight,&distance);
if(distance>=3000)
{
c=12;
}
else
{
c=distance/250;
}
switch(c)
{
case 0:discount=0;break;
case 1:discount=2;break;
case 2:
case 3:discount=5;break;
case 4:
case 5:
case 6:
case 7:discount=8;break;
case 8:
case 9:
case 10:
case 11:discount=10;break;
case 12:discount=15;break;
}
cost=price*weight*distance*(1-discount/100);
printf("Totally cost is : %6.2lf\n",cost);
return 0;
}
提高部分
1、用三目运算符实现大小写字母转换
#include <stdio.h>
int main()
{
char ch;
scanf("%c",&ch);
ch=(ch>='A'&&ch<='Z')?(ch+32):(ch>='a'&&ch<='z')?(ch-32):printf("this is no character!");
printf("%c\n",ch);
return 0;
}
习题部分
1、由键盘输入3个整数a、b、c要求输出其中最大的数
(1)if 语句实现
#include <stdio.h> int main() { int a,b,c,max; printf("Please enter a b c:\n"); scanf("%d,%d,%d",&a,&b,&c); if(a>b) { max=a; // 此时a大 a=b; // 此时a小 b=max; // 此时b大 } if(a>c) { max=c; a=c; c=max; } if(b>c) { max=b; b=c; c=b; } printf("Max=%d\n",c); return 0; }
(2)三目运算符实现
#include <stdio.h>
int main()
{
int a,b,c,max;
printf("Please enter a b c:\n");
scanf("%d,%d,%d",&a,&b,&c);
max=(a>b&&a>c)?a:(b>a&&b>c)?b:(c>a&&c>a)?c:printf("This is not number\n");
printf("Max=%d\n",c);
return 0;
}
#include <stdio.h>
int main()
{ int a,b,c,temp,max;
printf("请输入三个整数:");
scanf("%d,%d,%d",&a,&b,&c);
temp=(a>b)?a:b; /*将a和b中的大者存入temp中*/
max=(temp>c)?temp:c; /*将a和b中的大者与c比较,取最大者*/
printf("三个整数的最大数是%d\n",max);
return 0;
}
2、输入一个分数对应返回等级
#include <stdio.h>
int main()
{
int t;
char grade;
float score;
printf("Please enter score:\n");
scanf("%f",&score);
while (score>100||score<0)
{printf("ERROR Number,Please enter again!\n");
scanf("%f",&score);
}
switch((int)score/10)
{
case 0:grade='E';break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:grade='D';break;
case 7:grade='C';break;
case 8:grade='B';break;
case 9:grade='A';break;
case 10:grade='A';break;
}
printf("Your score is : %.1f\n",score);
printf("Your grade is : %c\n",grade);
return 0;
}
3、企业按照利润发放奖金***
(1)IF语句实现
#include <stdio.h>
int main()
{
int i;
double bonus,bon1,bon2,bon4,bon6,bon10;
bon1=100000*0.1;
bon2=bon1+100000*0.075; // 前部分继承bon1的算法
bon4=bon2+100000*0.05;
bon6=bon4+100000*0.03;
bon10=bon6+400000*0.015;
printf("请输入利润i:");
scanf("%d",&i);
if (i<=100000)
bonus=i*0.1;
else if (i<=200000)
bonus=bon1+(i-100000)*0.075; // 前部分按照0.1计算,后部分按照约定百分比计算;
else if (i<=400000)
bonus=bon2+(i-200000)*0.05; // 200000前的也有自己的算法bon2,即100000前的按照0.1,100000后的乘于0.75。
else if (i<=600000)
bonus=bon4+(i-400000)*0.03;
else if (i<=1000000)
bonus=bon6+(i-600000)*0.015;
else
bonus=bon10+(i-1000000)*0.01;
printf("奖金是: %10.2f\n",bonus);
return 0;
}
(2)switch语句实现
#include <stdio.h>
int main()
{
int i;
double bonus,bon1,bon2,bon4,bon6,bon10;
int branch;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
printf("请输入利润i:");
scanf("%d",&i);
branch=i/100000;
if (branch>10) branch=10;
switch(branch)
{ case 0:bonus=i*0.1;break;
case 1:bonus=bon1+(i-100000)*0.075;break;
case 2:
case 3: bonus=bon2+(i-200000)*0.05;break;
case 4:
case 5: bonus=bon4+(i-400000)*0.03;break;
case 6:
case 7:
case 8:
case 9: bonus=bon6+(i-600000)*0.015;break;
case 10: bonus=bon10+(i-1000000)*0.01;
}
printf("奖金是 %10.2f\n",bonus);
return 0;
}

浙公网安备 33010602011771号