C primer plus第七章一道练习题 有人能救救小弟我吗?
C primer plus第七章一道练习题 有人能救救我吗??
大家先看下原题
是第八题,但是是基于第七题的 所以一并发上来。
我编写的程序如下,按理说我的第一个while语句已经限定了,如果输入了字母是进不去循环的,但是经测试输入字母后死循环那句please enter 1-5. 还有, 输入数字居然也没反应 大家帮我看看到底是哪里错了,是不是有什么知识我没搞懂。 本人是纯初学者! 先谢谢各位了!
源代码如下:
大家先看下原题
是第八题,但是是基于第七题的 所以一并发上来。
我编写的程序如下,按理说我的第一个while语句已经限定了,如果输入了字母是进不去循环的,但是经测试输入字母后死循环那句please enter 1-5. 还有, 输入数字居然也没反应 大家帮我看看到底是哪里错了,是不是有什么知识我没搞懂。 本人是纯初学者! 先谢谢各位了!
源代码如下:
/* Programming Exercise 7-8 */
#include <stdio.h>
#define BASEHRS 40 /* hours at basepay */
#define OVERTIME 1.5 /* 1.5 time */
#define AMT1 300 /* 1st rate tier */
#define AMT2 150 /* 2st rate tier */
#define RATE1 0.15 /* rate for 1st tier */
#define RATE2 0.20 /* rate for 2nd tier */
#define RATE3 0.25 /* rate for 3rd tier */
#define BASEPAY1 8.75
#define BASEPAY2 9.33
#define BASEPAY3 10.00
#define BASEPAY4 11.20
int main(void)
{
int choice;
double hours;
double gross;
double net;
double taxes;
double BASEPAY;
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1)$%.2f/hr 2)$%.2f/hr\n",BASEPAY1,BASEPAY2);
printf("3)$%.2f/hr 4)$%.2f/hr\n",BASEPAY3,BASEPAY4);
printf("5)quit\n");
printf("*****************************************************************\n");
again:
while(scanf("%d",&choice)==1);
{
switch(choice)
{
case 1:
BASEPAY=BASEPAY1;
break;
case 2:
BASEPAY=BASEPAY2;
break;
case 3:
BASEPAY=BASEPAY3;
break;
case 4:
BASEPAY=BASEPAY4;
break;
case 5:
goto out;
default:
printf("Please enter number 1-5.\n");
goto again;
}
printf("Enter the number of hours worked this week: ");
scanf("%lf", &hours);
if (hours <= BASEHRS)
gross = hours * BASEPAY;
else
gross = BASEHRS * BASEPAY + (hours - BASEHRS) * BASEPAY * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f; taxes: $%.2f; net: $%.2f\n", gross, taxes, net);
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1)$%.2f/hr 2)$%.2f/hr\n",BASEPAY1,BASEPAY2);
printf("3)$%.2f/hr 4)$%.2f/hr\n",BASEPAY3,BASEPAY4);
printf("5)quit\n");
printf("*****************************************************************\n");
}
out:
printf("Done!\n");
getchar();
getchar();
return 0;
}
相关解决方案