修改练习7中的假设a,使程序提供一个选择工资等级的菜单。用switch选择工资等级。程序运行的开头应该像这样:
*****************************************************************
Enter the number corresponding to the desired pay rate or acion:
1)$8.75/hr 2) $9.33/hr
3) $10.00/hr 4) $11.20/hr
5) quit
*****************************************************************
如果选择1到4,那么程序应该请求输入工作小时数。程序应该一直循环运行,直到输入5.如果输入1到5以外的选项,那么程序应该提醒用户合适的选项是哪些,然后再循环。用#define为各种工资等级和税率定义常量。
# include <stdio.h>
# define GRADE1 8.75
# define GRADE2 9.33
# define GRADE3 10.00
# define GRADE4 11.20
# define RATE1 0.15
# define RATE2 0.20
# define RATE3 0.25
void wagefn(double , int );
int main(void )
{
int grade;
double hour;
printf ("****************************************************************\n" );
printf ("Enter the number corresponding to the desired pay rate or acion:\n" );
printf ("1')' $8.75'/'hr 2')' $9.33'/'hr\n" );
printf ("1')' $10.00'/'hr 2')' $11.20'/'hr\n" );
printf ("5')' quit\n" );
printf ("****************************************************************\n" );
while (1 )
{
printf ("请输入工资等级:\n" );
scanf ("%d" , &grade);
if (grade <= 0 || grade>5 )
{
printf ("请输入1到5以内的数!\n" );
}
switch (grade)
{
case 1 : printf ("请输入工作小时数:\n" );
scanf ("%lf" , &hour);
wagefn(hour,grade);
break ;
case 2 : printf ("请输入工作小时数:\n" );
scanf ("%lf" , &hour);
wagefn(hour,grade);
break ;
case 3 : printf ("请输入工作小时数:\n" );
scanf ("%lf" , &hour);
wagefn(hour,grade);
break ;
case 4 : printf ("请输入工作小时数:\n" );
scanf ("%lf" , &hour);
wagefn(hour,grade);
}
if (5 == grade)
break ;
}
return 0 ;
}
void wagefn(double hour, int grade)
{
double wage, tax;
switch (grade)
{
case 1 : wage = GRADE1;
break ;
case 2 : wage = GRADE2;
break ;
case 3 : wage = GRADE3;
break ;
case 4 : wage = GRADE4;
}
if (hour < 0 )
return ;
if (hour <= 40 )
{
wage = wage * hour;
if (wage > 450 )
tax = 70 + (wage - 450 ) * RATE3;
else if (wage > 300 )
tax = 45 + (wage - 300 ) * RATE2;
else if (wage >= 0 )
tax = wage * RATE1;
}
else if (hour > 40 )
{
wage = wage * (40 + (hour-40 ) * 1.5 );
if (wage > 450 )
tax = 70 + (wage - 450 ) * RATE3;
else if (wage > 300 )
tax = 45 + (wage - 300 ) * RATE2;
else if (wage >= 0 )
tax = wage * RATE1;
}
printf ("工资总额:%.3lf 税金:%.3lf 净工资:%.3lf\n" , wage, tax, wage-tax);
}