c程序的困惑

作者在 2011-09-16 20:27:03 发布以下内容
#include <stdio.h>
#define YJ 8.75
#define EJ 9.33
#define SJ 10.00
#define SIJ 11.20
#define JBSL 0.15
#define EJSL 0.2
#define ZJSL 0.25
char get_first(void);
int main(void)
{
   int i,num,hours;
   float hmoney,money,shui,jing;
   printf("******************************************************\n");
   printf("Enter the number corresponding to the desired pay rate or action:\n");
   printf("a)$8.75美元\t\t\tb)$9.33美元\nc)$10.00美元\t\t\td)$11.20美元\nq)quit\n");
   printf("******************************************************\n");
   num = get_first();
   while(num != 'q')
   {
         switch(num)
         {
            case 'a':
        hmoney = YJ;
        break;
     case 'b':
        hmoney = EJ;
        break;
     case 'c':
        hmoney = SJ;
        break;
     case 'd':
        hmoney = SIJ;
        break;
     default:
        goto back;
  }
  printf("Please enter your workhours in a week:");
  while(i = scanf("%d",&hours))
  {
     if( i != 1)
     {
        goto back;
     }
     else
     {
        money = 0;
        jing = 0;
        if(hours <= 40)
        {
           money = hours * hmoney;
           if(money <= 300)
       shui = money * JBSL;
           else
       shui = 300 * JBSL +(money - 300) * EJSL;
        }
        else if(hours <= 168)
        {
           money = (40 + (hours -40) * 1.5) * hmoney;
           if(money <= 450)
       shui = 300 * JBSL + (money - 300) * EJSL;
           else
       shui = 300 * JBSL + 150 * EJSL + (money - 450) * ZJSL;
        }
        else
        {
           printf("A week is total 168 hours!\n");
           break;
        }
        jing = money - shui;
        printf("This week the total money is %.2f,shuijin is %.2f,the last is %.2f.\n",money,shui,jing);
        printf("The last weekhours is( > 168 to quit):");
     }
  }
back:printf("a)$8.75美元\t\t\tb)$9.33美元\nc)$10.00美元\t\t\td)$11.20美元\nq)quit\n");
     printf("Please enter your chose again:\n");
     num = get_first();
   }
   return 0;
}
char get_first(void)
{
   int ch;
   while((ch = getchar()) == ' ')
      continue;
   while(getchar() != '\n')
      continue;
   return ch;
}
程序完成后,总有一些小问题,当用户输入大于168小时后,重新选择a~b,q的时候,程序不能读取用户输入?很闹心啊!
程序设计 | 阅读 624 次
文章评论,共2条
ghoster_heng
2011-09-21 01:01
1
num = get_first();<br />
这里改成: began:num = get_first();<br />
然后;给第一个while循环中加个case 'q':printf(&quot;请重新输入:&quot;);goto began;<br />
(本人英语太烂,没办法,如果没帮到你,请见谅::!!)
ghoster_heng
2011-09-21 10:13
2
不好意思,昨晚太疏忽了,忘了改一个地方:把第一个while(num!='q')改成while(num!='\0');;;;呵呵,,粗心也会害死猫的!!@!@
游客请输入验证码
浏览962次