if语句
例题4.1 输入两个学生a和b的成绩,输出其中高的成绩。
编写程序如下:
#include <stdio.h>
int main()
{
float a,b,max;
printf("please enter a and b:");
scanf("%f,%f",&a,&b);
if(a>=b) max=a;
else max=b;
printf("max=%6.2f\n",max);
return 0;
}
例题4.2 输入3个成绩,要求按由高到低的顺序输出
编写程序如下:
#include <stdio.h>
int main()
{
float a,b,c,t;
printf("please enter a,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
if(a<b)
{t=a,a=b;b=t;}
if(a<c)
{t=a,a=c;c=t;}
if(b<c)
{t=b,b=c;c=t;}
printf("%6.2f,%6.2f,%6.2f\n",a,b,c);
return 0;
}
运行结果如下:
将a与b对换(a成为a和b中的大者)
将a与c对换(此时a成为三者中的最大者)
将b与c对换(b成为b、c中的大者,也是三者中的次大者)
例题4.3 给出三角形的三个边长,求三角形的面积
编写程序如下:
#include <stdio.h>
#include <math.h>
int main()
{
double a,b,c,s,area;
printf("please enter a,b,c:");
scanf("%lf,%lf,%lf",&a,&b,&c);
if(a+b>c&&b+c>a&&c+a>b)
{s=0.5*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area=%6.2f\n",area);
}
else
printf("It is not a trilateral.\n");
return 0;
}
运行结果如下:
例题4.4 为促销,对购买货物多的顾客有优惠:凡购买50件以上(含50)的优惠5%,买100件以上(含100)的优惠7.5%,买300件以上(含300)的优惠10%,买500件以上(含500)的优惠15%。要求编写程序,用户输入购买的数量和单价,程序输出应付货款。
解题思路:if的嵌套使用
应付货款=件数*单价*(1-优惠折扣)
编写程序如下:
#include <stdio.h>
#include <math.h>
int main()
{
int number;
double cost,price,total;
printf("please enter number and price:");
scanf("%d,%lf",&number,&price);
if(number>=500) cost=0.15;
else
if(number>=300) cost=0.10;
else
if(number>=100) cost=0.075;
else
if(number>=50) cost=0.05;
else cost=0;
total=number*price*(1-cost);
printf("Total=%10.2f\n",total);
return 0;
}
运行结果如下:
注意:else总是与它上面的最近的未配对的if配对。
例题4.4程序可改写为:
int number;
double cost,price,total;
printf("please enter number and price:");
scanf("%d,%lf",&number,&price);
if(number>=500) cost=0.15;
else if(number>=300) cost=0.10;
else if(number>=100) cost=0.075;
else if(number>=50) cost=0.05;
else cost=0;
total=number*price*(1-cost);
printf("Total=%10.2f\n",total);
return 0;
例题4.5 写程序判断某一年是否闰年
编写代码如下:
#include <stdio.h>
int main()
{
int y,leap;
printf("please enter a year:");
scanf("%d",&y);
if(y%4==0)
{
if(y%100==0)
{
if(y%400==0)
leap=1;
else
leap=0;
}
else
leap=1;
}
else
leap=0;
if(leap)
printf("%d is ",y);
else
printf("%d is not ",y);
printf("a leap year.\n");
return 0;
}
运行结果如下:
程序可改写如下:
int y,leap;
printf("please enter a year:");
scanf("%d",&y);
if((y%4==0&&y%100!=0)||(y%400==0))
leap=1;
else
leap=0;
if(leap)
printf("%d is ",y);
else
printf("%d is not ",y);
printf("a leap year.\n");
return 0;
Switch语句
例题4.6 运输公司对用户计算运费。运输距离(以s表示,单位为千米)越远,单位运费(以每吨丶千米为单位)越低。计算标准如下:
s<250 | 没有折扣 |
250<=s<500 | 2%折扣 |
500<=s<1000 | 5%折扣 |
1000<=s<2000 | 8%折扣 |
2000<=s<3000 | 10%折扣 |
3000<=s | 15%折扣 |
设每吨千米货物的运费为p,货物重为w,距离为s,折扣为d,则总运费f的计算公式为:
f=p*w*s*(1-d)
编写程序如下:
#include <stdio.h>
int main()
{
int c,s;
double p,w,d,f;
printf("请输入单价、重量和距离:");
scanf("%lf,%lf,%d",&p,&w,&s);
if(s>=3000) c=12;
else c=s/250;
switch(c)
{
case 0:d=0;break;
case 1:d=2;break;
case 2:
case 3:d=5;break;
case 4:
case 5:
case 6:
case 7:d=8;break;
case 8:
case 9:
case 10:
case 11:d=10;break;
case 12:d=15;break;
}
f=p*w*s*(1-d/100.0);
printf("运费:%10.2f元\n",f);
return 0;
}
运行结果如下:
例题4.7 输入一个字符,判断是否大写字母,如果是,将它转换成小写字母;如果不是,不转换。然后输出最后的得到的字符。
编写程序如下:
#include <stdio.h>
int main()
{
char ch;
scanf("%c",&ch);
ch=(ch>='A'&&ch<='Z')?(ch+32):ch;
printf("%c\n",ch);
return 0;
}