C Primer Plus 8.7

/*
7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总
额、税金和净收入。做如下假设:
a.基本工资 = 10美元/小时
b.加班(超过40小时) = 1.5倍的时间
c.税率: 前300美元为15%
续150美元为20%
余下的为25%
用#define定义符号常量。不用在意是否符合当前的税法。
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
创建符号常量表示各工资等级和税率。
7.修改第7章的编程练习8,用字符代替数字标记菜单的选项。用q代替5
作为结束输入的标记。
*/

#include<stdio.h>
#include<ctype.h>
#define STD_H 40.0			//周标准工时
#define OES 1.5				//加班时间转换倍数
#define SALARY1 300.0			
#define SALARY2 450.0
#define TAX_RATE1 0.15

#define TAX_RATE2 0.20
#define TAX_RATE3 0.25

char get_first(void);
char get_choice(void);
double get_hour(void);


int main(void)
{
	char ch;
	double hour, wage=0, revenue, taxes, net_revenue;   //总工时、时薪、总薪水、税收、税后薪水
	ch = get_choice();		//获取时薪
	while (ch != 'q')
	{
		hour = get_hour();		//获取总工时
		switch (ch)
		{
		case 'a':wage = 8.75;
			break;
		case 'b':wage = 9.33;
			break;
		case 'c':wage = 10.00;
			break;
		case 'd':wage = 11.20;
			break;
		default:printf("程序错误\n");
			break;
		}
		if (hour <= STD_H)
			revenue = wage * hour;					//时薪*总工时
		else
			revenue = wage * (hour + OES * (hour - STD_H));
		if (revenue <= SALARY1)
		{
			taxes = revenue * TAX_RATE1;
		}
		else if (revenue > SALARY1 && revenue <= SALARY2)
		{
			taxes = (SALARY1 * TAX_RATE1) + TAX_RATE2 * (revenue - SALARY1);
		}
		else
		{
			taxes = (SALARY1 * TAX_RATE1) + TAX_RATE2 * (SALARY2 - SALARY1) + TAX_RATE3 * (revenue - SALARY2);
		}
		net_revenue = revenue - taxes;
		printf("税前薪水:%4.2f		扣税:%4.2f		税后薪水:%4.2f\n", revenue, taxes, net_revenue);
		printf("**********************************************************************************\n");
		ch = get_choice();
	}
	return 0;
}

char get_choice(void)
{
	char ch;
	printf("\n请选择你的时薪:\n");
	printf("a) $ 8.75/小时		b) $ 9.33/小时\n");
	printf("c) $10.00/小时		d) $11.20/小时\n");
	printf("q) 退出\n");
	ch = get_first();
	while ((ch<'a'|| ch>'d')&&ch!='q')
	{
		printf("请选择正确的选项,例如:a、b、c、d 或 q ;\n");
		ch = get_first();
	}
	return ch;
}
char get_first(void)
{
	char ch,i;
	ch = getchar();
	while ((i=getchar()) != '\n')
	{
		if (islower(ch)!=1)
		{
			ch = i;
			break;
		}
		else	
		continue;
	}
	return ch;
}
double get_hour(void)
{
	float hour;
	printf("请输入总工时数:\n");
	while (scanf_s("%f", &hour) != 1)
	{
		while (getchar() != '\n')
			continue;
		printf("输入不准确,请重新输入工时数:\n");
		continue;
	}
	return hour;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值