首页 > 试题广场 >

修改第7章的练习8,使菜单选项由字符代替数字进行标记。

[问答题]
修改下面的练习,使菜单选项由字符代替数字进行标记:

用switch选择工资等级。程序运行的开头应该像这样:

Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr                  2) $9.33/hr
3) $lO.OO/hr                 4) $11.20/hr
5) quit

如果选择l到4.那么程序应该请求输入工作小时数。程序应该一直循环运行,直到输入5。如果输入l到5以外的选项,那么程序应该提醒用户合适的选项是哪些,然后再循环。用#define为各种工资等级和税率定义常量。

推荐
#include<stdio.h>
#include<ctype.h>
char get_first(void);
//b.加班
#define TIME 40  //加班(超过TIME小时) =
#define ADD  1.5  //ADD倍的时间
//c.税率
#define LIMIT1 300  //前LIMIT1美元为RATE1
#define RATE1 0.15 
#define LIMIT2 150  //下一个LIMIT2美元为RATE2
#define RATE2 0.20
#define RATE3 0.25 //余下的位RATE3
int main(void)
{
 double basic,hours,gross,tax;
 printf("Enter the number corresponding to the desired pay rate or action:\n");
 printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
 printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
 printf("5) quit\n");
 switch( get_first() )
 {
 case '1': basic = 8.75; break;
 case '2': basic = 9.33; break;
 case '3': basic = 10.00; break;
 case '4': basic = 11.20; break;
 default: printf("quit\n"); return(0); //退出程序
 }
 printf("you have select $%.2lf\n",basic);
 printf("input the work hours of a week:");
 scanf("%lf",&hours);
 if (hours > 40) hours = 40 + (hours - 40) * 1.5;
 gross = hours * basic;
 printf("gross income:\t\t%lf\n",gross);
 if (gross <= LIMIT1) tax = gross * RATE1;
 else if (gross <= LIMIT2) tax = LIMIT1 * RATE1 + (gross - LIMIT1) * RATE2;
 else tax = LIMIT1 * RATE1 + LIMIT2 * RATE2 + (gross - LIMIT1 - LIMIT2) * RATE3;
 printf("tax:\t\t\t%lf\n",tax);
 printf("net income:\t\t%lf\n",gross - tax);
 return(0);
}
char get_first(void) //得到字符串中的第一个非空字符
{
 int ch;
 while( isspace( ch = getchar() ) );
 while ( getchar() != '\n');
 return ch;
}

发表于 2018-05-05 21:42:54 回复(0)