本周学习的C
运用switch的方法解决实际问题。
一、计算天数
#include<stdio.h>
main()
{
int year,month,day;
int sum=0;
printf("请输入日期,格式为:年-月-日):");
scanf("%d-%d-%d",&year,&month,&day);
switch(month-1)
{
case 12:
sum=sum+31;
case 11:
sum=sum+30;
case 10:
sum=sum+31;
case 9:
sum=sum+30;
case 8:
sum=sum+31;
case 7:
sum=sum+31;
case 6:
sum=sum+30;
case 5:
sum=sum+31;
case 4:
sum=sum+30;
case 3:
sum=sum+31;
case 2:
if(year%4==0&&year%100!=0)
sum=sum+29;
else
sum=sum+28;
case 1:
sum=sum+31;
break;
}
sum=sum+day;
printf("%d年%d月%d日是%d年的第%d天",year,month,day,year,sum);
}
!注意int sum=0;要写在前面。
二、计算利率
#include<stdio.h>
main()
{
int n,k;
float m,d;
printf("输入利润:");
scanf("%d",&n);
m=n/100000;
k=(int)m;
switch(k)
{
case 0:
d=n*0.1;
break;
case 1:
d=(n-100000)*0.075+100000*0.1;
break;
case 2:
d=(n-200000)*0.05+100000*0.075+100000*0.1;
break;
case 3:
d=(n-200000)*0.05+100000*0.075+100000*0.1;
break;
case 4:
d=(n-400000)*0.03+200000*0.05+100000*0.075+100000*0.1;
break;
case 5:
d=(n-400000)*0.03+200000*0.05+100000*0.075+100000*0.1;
break;
case 6:
d=(n-600000)*0.015+200000*0.03+200000*0.05+100000*0.075+100000*0.1;
break;
case 7:
d=(n-600000)*0.015+200000*0.03+200000*0.05+100000*0.075+100000*0.1;
break;
case 8:
d=(n-600000)*0.015+200000*0.03+200000*0.05+100000*0.075+100000*0.1;
break;
case 9:
d=(n-600000)*0.015+200000*0.03+200000*0.05+100000*0.075+100000*0.1;
break;
case 10:
d=(n-1000000)*0.01+400000*0.015+200000*0.03+200000*0.05+100000*0.075+100000*0.1;
break;
}
printf("利润为%d的奖金=%.2f\n",n,d);
}
利用强制转换运算。
本周参与了创业实验项目,了解基本思路,制作答辩ppt