}
return0;
}
4、设计一个程序,根据用户输入的年、月,打印出该年、该月的天数。
解1
#include
intmain()
{intyear,month,days;
cout<<"Pleaseinputyear,month:
";
cin>>year>>month;
switch(month)
{
case1:
case3:
case5:
case7:
case8:
case10:
case12:
days=31;
break;
case4:
case6:
case9:
case11:
days=30;
break;
case2:
if(year%400==0||year%4==0&&year%100!
=0)
days=29;
else
days=28;
break;
}
cout<<"Thenumberofdaysof"<}
解2
#include
voidmain()
{
intx,y,z,w;
cout<<"请输入年月:
";
cin>>x>>y;
if(x%4==0&&x%100!
=0||x%400==0)
{
z=366;
switch(y)
{
case1:
;
case3:
;
case5:
;
case7:
;
case8:
;
case10:
;
case12:
w=31;break;
case2:
w=29;break;
case6:
;
case4:
;
case9:
;
case11:
w=30;break;
}
}
else
{
z=365;
switch(y)
{
case1:
;
case3:
;
case5:
;
case7:
;
case8:
;
case10:
;
case12:
w=31;break;
case2:
w=28;break;
case6:
;
case4:
;
case9:
;
case11:
w=30;break;
}
}
cout<<"\n";
cout<<"本年的天数为:
"<"<}
解3
#include
intmain()
{intyear,month,days,ydays;
boolisleap;
cout<<"Pleaseinputyear,month:
";
cin>>year>>month;
isleap=(year%400==0||year%4==0&&year%100!
=0);
if(isleap)ydays=366;
elseydays=365;
switch(month)
{
case1:
case3:
case5:
case7:
case8:
case10:
case12:
days=31;
break;
case4:
case6:
case9:
case11:
days=30;
break;
case2:
if(isleap)days=29;
else
days=28;
break;
}
cout<return0;
}
5.企业发放的奖金来自利润提成。
利润i低于或等于10万元的,可提成10%为奖金;利润i高于10万元,低于或等于20万元(100000
从键盘输入当月利润i,求应发奖金总数。
要求:
用switch语句编程序。
#include
usingnamespacestd;
intmain()
{longi;
floatbonus,bon1,bon2,bon4,bon6,bon10;
intc;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
cout<<"enteri:
";
cin>>i;
c=i/100000;
if(c>10)c=10;
switch(c)
{case0:
bonus=i*0.1;break;
case1:
bonus=bon1+(i-100000)*0.075;break;
case2:
case3:
bonus=bon2+(i-200000)*0.05;break;
case4:
case5:
bonus=bon4+(i-400000)*0.03;break;
case6:
case7:
case8:
case9:
bonus=bon6+(i-600000)*0.015;break;
case10:
bonus=bon10+(i-1000000)*0.01;
}
cout<<"bonus="<return0;
}