switch(x1>0)
{ case 0 :y=0; break;
case 1 : switch((int)x2/10)
{ case 0: y=x2; break;
case 1: y=x2+3;break;
case 2:
case 3: y=-0.5*x2+10;break;
default: y=2*x2-5;break;
}
}
printf("x=%f,y=%f",x1,y);
}
5.以下程序计算某年某月有几天,闰年与平年的二月份天数不同。判别闰年的条件是:能被4整除但不能被100整除的年是闰年,或者能被400整除的年也是闰年。请分析程序填空。
#include "stdio.h"
#include "stdio.h"
main()
{ int yy,mm,days;
printf("input year and month:");
scanf("%d %d",&yy,&mm);
switch(mm)
{ case 1: case 3: case 5: case 7:case 8:
case 10:case 12: days=31 ;break; case 4: case 6: case 9: case 11:days=30;break;
case 2:if(yy%4==0&&yy%100!=0||yy%400==0) days=29 ;
else days=28;
break;
default:printf("input error");break;
}
printf("the days of %d %d is %d\n",yy,mm,days);
}
6.假设奖金税率如下(ma代表奖金,tr代表税率), 利用switch语句编写程序对输入的一个奖金数,输出税率和应交税款以及实得奖金数(扣除奖金税后),
① ma<1000时, tr=0%;
② 1000 ≤ma<2000时, tr=5%;
③ 2000 ≤ma<4000时, tr=8%;
④ 4000 ≤ma时, tr=10%。
参考答案:
#include "stdio.h"
main()
{ float ma, tr,ma_tr;
scanf("%f",&ma);
printf("奖金数为%8.2f",ma);
switch(ma>=1000)
{ case 0 :tr=0; break;
case 1 : switch((int)ma/1000)
{ case 1: tr=0.05;break;
case 2: