C Primer Plus 第六版 第7章的编程练习8答案

C Primer Plus 第六版 第7章的编程练习8答案

编译环境:Visual Studio 2019

编译环境的更新使得C Primer Plus第6版中的答案存在bug,对初学者很不友好,kkk我就是,第一篇博客,记录学习过程!

遇到的问题有三:
一、回车问题,原代码:

switch (selected)
		{
		case '1':
			printf("Hello, you select $8.75/hr. Enter the work hours: ");
			scanf_s("%f", &hours);
			calc_salary(8.75,hours);
			break;
		case '2':
			printf("Hello, you select $9.33/hr. Enter the work hours: ");
			scanf_s("%f", &hours);
			calc_salary(9.33,hours);
			break;
		case '3':
			printf("Hello, you select $10.00/hr. Enter the work hours: ");
			scanf_s("%f", &hours);
			calc_salary(10.00,hours);
			break;
		case '4':
			printf("Hello, you select $11.20/hr. Enter the work hours: ");
			scanf_s("%f", &hours);
			calc_salary(11.20,hours);
			break;
		case '5':
			break;
		default:
			printf("Error selected! please retry!\n");
			getchar();
			break;
		}

这样写的后果无法输入5无法输出我们期望的结果,以及无法循环运行,原因在于每次输入一个值后我们会打入一个回车键’\n’,而下一次进入循环时scanf_s读取的不是我们输入的值,而是上次输入的回车,所以导致循环无法进行。
二:scanf_s在输入字符或字符串时,会出现报错,即没有为格式字符串传递足够的参数,对于字符串 例num[40],只要scanf_s("%s", num);对于字符 例num ,只要scanf_s("%c", &num, sizeof(num))就可解决错误,对字符串%s而言&可有可无,但对其他类型都必须加;sizeof()以字节为单位返回运算对象的大小。
三:定义selected为char类型,因此后续出现的数字均要加’ ',因为char类型要以单引号表示字符,要注意do while(selected != ‘5’),此处的的单引号!!!
四:强制类型转换,double转换为float,double精度要更高,此处有两种办法,1-将float类型都换为double,因为读取时均读取为double类型;2-在数字后面加一个f,告诉计算机读取float类型。

完整代码:

#include <stdio.h>
#define EXTRA_HOUR 1.5
#define BASE_TAX 0.15
#define EXTRA_TAX 0.2
#define EXCEED_TAX 0.25
void show_menu(void);
void calc_salary(double base_salary,double hours);
int main(void)
{
	float hours = 0;
	char selected;
	do
	{
		show_menu();
		scanf_s("%c", &selected, sizeof(selected));
		switch (selected)
		{
		case '1':
			printf("Hello, you select $8.75/hr. Enter the work hours: ");
			scanf_s("%f", &hours);
			selected = getchar();
			calc_salary(8.75,hours);
			break;
		case '2':
			printf("Hello, you select $9.33/hr. Enter the work hours: ");
			scanf_s("%f", &hours);
			selected = getchar();
			calc_salary(9.33,hours);
			break;
		case '3':
			printf("Hello, you select $10.00/hr. Enter the work hours: ");
			scanf_s("%f", &hours);
			selected = getchar();
			calc_salary(10.00,hours);
			break;
		case '4':
			printf("Hello, you select $11.20/hr. Enter the work hours: ");
			scanf_s("%f", &hours);
			selected = getchar();
			calc_salary(11.20,hours);
			break;
		case '5':
			break;
		default:
			printf("Error selected! please retry!\n");
			selected = getchar();
			break;
		}
	}while (selected != '5');
	printf("Done!\n");
	return 0;
}
void show_menu(void)
{
	char s1[] = "1) $8.75/hr";
	char s2[] = "2) $9.33/hr";
	char s3[] = "3) $10.00/hr";
	char s4[] = "4) $11.20/hr";
	char s5[] = "5) quit";

	printf("*****************************************************************\n");
	printf("Enter the number correspongding to the desired pay rate or action\n");
	printf("%s", s1);
	printf("%40s\n", s2);
	printf("%s", s3);
	printf("%40s\n", s4);
	printf("%s\n", s5);
	printf("*****************************************************************\n");
}
void calc_salary(double base_salary,double hours)
{
	double salary, tax, taxed_salary;
	if (hours <= 40)
	{
		if (hours <= 30)
		{
			salary = base_salary * hours;
			tax = salary * BASE_TAX;
			taxed_salary = salary - tax;
		}
		else
		{
			salary = hours * base_salary;
			tax = 300 * BASE_TAX + (salary - 300.0) * EXTRA_TAX;
			taxed_salary = salary - tax;
		}
	}
	else
	{
		salary = 40 * base_salary + (hours - 40.0) * EXTRA_HOUR * base_salary;
		if (salary <= 450)
			tax = 300 * BASE_TAX + (salary - 300.0) * EXTRA_TAX;
		else
			tax = 300 * BASE_TAX + 150 * EXTRA_TAX + (salary - 450.0) * EXCEED_TAX;
		taxed_salary = salary - tax;
	}
	printf("Your salary before tax is %.2lf, tax is %.2lf, salary after tax is %.2lf\n", salary, tax, taxed_salary);
	printf("\ncontinue...\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值