题目:
8.修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单。使用switch完成工资等级选择。运行程序后,显示的菜单应该类似这样:
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
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 BASE_SALARY 10 //基础工资10美元/小时
#define BASE_TIME 40 //加班时间线
#define OVERTIME_MULTI 1.5 //加班时间倍率
#define TAX_RATE_1 0.15 //第一档税率15%
#define TAX_RATE_2 0.2 //第二档税率20%
#define TAX_RATE_3 0.25 //第三档税率25%
int main()
{
float worktime; //需要输入的工作时间
float total_salary; //税前总收入
float taxes; //税金
float net_income; //净收入
int select_number;
float BASE_SALARY;
Stage1:printf("\n"); //stage1l用于数字不在选择范围内,提示后重开
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("*****************************************************************\n");
while (scanf("%d",&select_number))
{
switch (select_number) //基础小时工资选择,选择1~4后跳到stage2的计算部分
{
case 1:
BASE_SALARY = 8.75;
goto Stage2;
case 2:
BASE_SALARY = 9.33;
goto Stage2;
case 3:
BASE_SALARY = 10.00;
goto Stage2;
case 4:
BASE_SALARY = 11.20;
goto Stage2;
case 5:
goto Stage3; //直接跳到stage3结束
default:
printf("Wrong entry, please entry number 1 ~ 5\n");
goto Stage1; //跳到stage1重开
}
}
Stage2:printf("\n");
printf("Please entry your working time per week:\n");
while (scanf("%f",&worktime) == 1)
{
if (worktime <= BASE_TIME) //如果工作时间小于40小时
{
total_salary=worktime*BASE_SALARY;
if (total_salary <= 300) //一档税率检查
{
taxes=total_salary*TAX_RATE_1;
}
else
taxes=(300*TAX_RATE_1)+(total_salary-300.0)*TAX_RATE_2; //二档税率检查,但工资最大到400
}
else //如果工作时间大于40小时的情况
{
total_salary=BASE_TIME*BASE_SALARY+(worktime-BASE_TIME)*OVERTIME_MULTI*BASE_SALARY;
if (total_salary <= 450)
{
taxes=(300*TAX_RATE_1)+(total_salary-300.0)*TAX_RATE_2; //二档税率检查,工资<=450
}
else
taxes=300*TAX_RATE_1+150*TAX_RATE_2+(total_salary-450)*TAX_RATE_3; //三档税率检查,工资>450
}
break; //不加break需要输入非数字结束循环
}
printf("Your working time is %.1f hours per week!\n",worktime); //打印每周总工时
printf("Your total salary is: %.1f\n", total_salary); //打印税前周薪
printf("Your taxes is: %.1f\n",taxes); //打印税金
printf("Your net income is: %.1f\n",total_salary-taxes); //打印净收入,周薪-税金
Stage3:printf("Done!");
return 0;
}
428

被折叠的 条评论
为什么被折叠?



