首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

cpp 第八章第7题解决办法

2013-04-20 
cpp 第八章第7题#include stdio.h#include stdlib.h/**************************************日期:201

cpp 第八章第7题

#include <stdio.h>
#include <stdlib.h>
/**************************************
日期:2013-4-11
第7章 8.修改练习7中的假设a,使程序提供一个选择工资等级的菜单。用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为各种工资等级和税率定义常量。

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

由字符代替数字:

****************************************/
/* Programming Exercise 7-7 */
#include <stdio.h>

//#define BASEPAY     10      /* $10 per hour         */
#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    */
int get_char(void);
int main(void)
{
    double hours;
    double gross;
    double net;
    double taxes;
    //int digit; //用来读入选项字符
    double BASEPAY;

    printf("*****************************************************************\n");
    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("a) $8.75/hr                                  b)$9.33/hr\n");
    printf("c) $10.00/hr                                 d)$11.20/hr\n");
    printf("e) quit\n");
    printf("*****************************************************************\n");

   /* modify--start*********************/
    while(1)
    {
      switch(get_char()) //用get_char()返回值


      {
        case 'a': BASEPAY= 8.75;break;
        case 'b': BASEPAY= 9.33;break;
        case 'c': BASEPAY= 10.00;break;
        case 'd': BASEPAY= 11.20;break;
        case 'e': printf("quit\n");return 0;
        default:printf("enter the valid char.a or b or c or d or e.\n");continue;//结束这次循环
      }
        printf("you have select $%.2lf\n",BASEPAY);
        printf("input the work hours of a week:");
        scanf("%lf",&hours);
   /* end********************************/
        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);
    }
    return 0;
}

/*得到一个合适的字符(第1个字符为a,b,c,d,e中之一),滤除非法数 */
int get_char(void)
{
    char ch;

    ch=getchar();
    while(getchar()!='\n') //滤除剩字符
      continue;

    return ch; //返回第1个字符
}


问题:
1. 第一次输入:aaaa,能读出第一个字符a,然后第二次输入:a1,不能读取字符a
2. 若第一次输入:a,能读出;再输入aaaa不能读出字符a。
是不是get_char()这个函数写错了?还请高人指点啊。看了半天不怎么错哪里了。非常感谢
[解决办法]
int get_char(void)
{
fflush();

...//your code
}
[解决办法]
在每个最后不带\n的printf后面加fflush(stdout);
在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin);
另外请检查scanf的返回值。

热点排行