编写一个程序,提示用户输入一周的工作小时数,然后打印工资总数、税金和净收入。做如下假设:
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创建符号常量表示各工资等级和税率。
b. 加班(超过40小时) = 1.5倍工作时间
c. 税率: 前300美元为15%
续150美元为20%
余下的为25%
以下为代码
#include <stdio.h>
#include <ctype.h>
#define BREAK1 300
#define BREAK2 450
#define base1 (BREAK1 * (1 - rate1))
#define base2 (base1 + (BREAK2 - BREAK1) * (1 - rate2))
#define rate1 0.85
#define rate2 0.8
#define rate3 0.75
#define basicincome1 8.75
#define basicincome2 9.33
#define basicincome3 10.00
#define basicincome4 11.20
#define extratime 40
int main(void)
{
float worktime = 0.0;
double totalincome = 0.0;
int choice;
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");
scanf("%d",&choice);
while(choice != 5)
{
if(choice > 5 || choice < 1)
{
printf("please enter the right number:");
scanf("%d", &choice);
continue;
}
printf("Enter your work time per week: ");
scanf("%f", &worktime);
switch (choice){
case 1: if(worktime <= extratime)
totalincome = worktime * basicincome1;
else
totalincome = (worktime - extratime) * 1.5 * basicincome1 + extratime * basicincome1;
break;
case 2: if(worktime <= extratime)
totalincome = worktime * basicincome2;
else
totalincome = (worktime - extratime) * 1.5 * basicincome2 + extratime * basicincome2;
break;
case 3: if(worktime <= extratime)
totalincome = worktime * basicincome3;
else
totalincome = (worktime - extratime) * 1.5 * basicincome3 + extratime * basicincome3;
break;
case 4: if(worktime <= extratime)
totalincome = worktime * basicincome4;
else
totalincome = (worktime - extratime) * 1.5 * basicincome4 + extratime * basicincome4;
break;
}
if(totalincome <= BREAK1)
printf("your total income is %.2lf$, tax money is %.2lf$, and real income is %.2lf$",
totalincome, (1 - rate1) * totalincome, rate1 * totalincome);
else if(totalincome > BREAK1 && totalincome <= BREAK2)
printf("your total income is %.2lf$, tax money is %.2lf$, and real income is %.2lf$",
totalincome, (1 - rate2) * (totalincome - BREAK1) + base1, (totalincome - BREAK1) * rate2 + BREAK1 * rate1);
else
printf("your total income is %.2lf$, tax money is %.2lf$, and real income is %.2lf$",
totalincome, (1 - rate3) * (totalincome - BREAK2) + base2, (totalincome - BREAK2) * rate2 + BREAK1 * rate1 + BREAK2 * rate3);
printf("Enter the number corresponding to the desired pau 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");
scanf("%d",&choice);
}
printf("Done!\n");
return 0;
}