#include <stdio.h> #include <windows.h> void meum(void); double xuanze(char ch); int main(void) { const int jbsj = 40; const double jb = 1.5; const double shui15 = 0.15; const double shui20 = 0.20; const double shui25 = 0.25; const int jd1 = 300; const int jd2 = 450; double sj, base, gz, shui, jgz; char ch; meum(); while((ch = getchar()) != "q") base = xuanze(ch); printf("请输入您工作的时间: \n"); // while ((scanf_s("%lf", &sj)) == 1.00) { while ((sj = getchar()) != "q"){ if (sj > jbsj) sj = 40 + (sj - 40)*1.5; gz = sj * base; if (gz <= 300) { shui = gz * shui15; jgz = gz - shui; } else if (gz <= 450) { shui = jd1 * shui15 + (gz - jd1) * shui20; jgz = gz - shui; } else { shui = jd1 * shui15 + (jd2 - jd1) * shui20 + (gz - jd1) * shui25; jgz = gz - shui; } printf("您的工资为%.2f,税金为%.2f,净工资为%.2f.\n\n\n", gz, shui, jgz); fflush(stdin); meum(); } system("pause"); return 0; } void meum(void) { printf("*************************************************************************\n"); printf("a)$8.75/hr b)$9.33/hr\n"); printf("c)$10.00/hr d)$11.20/hr\n"); printf("q)quit \n"); printf("*************************************************************************\n"); printf("请根据以上选项输入您的每小时的基本工资(q to quit): "); } double xuanze(char ch) { double base; switch (ch) { case "a": base = 8.75; break; case "b": base = 9.33; break; case "c": base = 10.00; break; case "d": base = 11.20; break; default: printf("请输入a、b、c、d,q to quit.\n"); break; } printf("您的基本工资为%.2f/H.\n", base); return base; }
个人感觉的问题在于:
1、xuanze()函数中提取的char值应该为:本人输入的a\b\c\d\q\及回车键,因此每次都是:
“您的基本工资为.
请输入a、b。”
2、不知道为什么此函数循环不正确。
请各位高手指点。
解决方案
10
1.第18行这样用不行while((ch = getchar()) != “q”)
2.第22行之后的while循环里面的逻辑也需要稍微改下
2.第22行之后的while循环里面的逻辑也需要稍微改下
#include <stdio.h> #include <windows.h> void meum(void); double xuanze(char ch); int main(void) { const int jbsj = 40; const double jb = 1.5; const double shui15 = 0.15; const double shui20 = 0.20; const double shui25 = 0.25; const int jd1 = 300; const int jd2 = 450; double sj, base, gz, shui, jgz; char ch; meum(); if((ch = getchar()) != "q") base = xuanze(ch); printf("请输入您工作的时间: \n"); // while ((scanf_s("%lf", &sj)) == 1.00) { while (ch != "q"){ scanf("%lf", &sj); if (sj > jbsj) sj = 40 + (sj - 40)*1.5; gz = sj * base; if (gz <= 300) { shui = gz * shui15; jgz = gz - shui; } else if (gz <= 450) { shui = jd1 * shui15 + (gz - jd1) * shui20; jgz = gz - shui; } else { shui = jd1 * shui15 + (jd2 - jd1) * shui20 + (gz - jd1) * shui25; jgz = gz - shui; } printf("您的工资为%.2f,税金为%.2f,净工资为%.2f.\n\n\n", gz, shui, jgz); fflush(stdin); meum(); ch = getchar(); if(ch == "q") break; base = xuanze(ch); } system("pause"); return 0; } void meum(void) { printf("*************************************************************************\n"); printf("a)$8.75/hr b)$9.33/hr\n"); printf("c)$10.00/hr d)$11.20/hr\n"); printf("q)quit \n"); printf("*************************************************************************\n"); printf("请根据以上选项输入您的每小时的基本工资(q to quit): "); } double xuanze(char ch) { double base; switch (ch) { case "a": base = 8.75; break; case "b": base = 9.33; break; case "c": base = 10.00; break; case "d": base = 11.20; break; default: printf("请输入a、b、c、d,q to quit.\n"); break; } printf("您的基本工资为%.2f/H.\n", base); return base; }
5
将第二层while循环放到第一层循环之中,如下:
while((ch = getchar()) != "q"){ base = xuanze(ch); printf("请输入您工作的时间: \n"); // while ((scanf_s("%lf", &sj)) == 1.00) { while ((sj = getchar()) != "q"){ if (sj > jbsj) sj = 40 + (sj - 40)*1.5; gz = sj * base; if (gz <= 300) { shui = gz * shui15; jgz = gz - shui; } else if (gz <= 450) { shui = jd1 * shui15 + (gz - jd1) * shui20; jgz = gz - shui; } else { shui = jd1 * shui15 + (jd2 - jd1) * shui20 + (gz - jd1) * shui25; jgz = gz - shui; } printf("您的工资为%.2f,税金为%.2f,净工资为%.2f.\n\n\n", gz, shui, jgz); fflush(stdin); meum(); } }
另外,你输入的时间为什么要和‘q’比较,这样没什么意义吧。
时间可以以 scanf(“%lf”,&sj) 的形式输入,不过要在输入之前清空一下缓冲区,即应该是:
fflush(stdin);
scanf(“%lf”,&sj);
10
在每个最后不带\n的printf后面加fflush(stdout);
在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin);
另外请检查scanf的返回值。
在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin);
另外请检查scanf的返回值。
//请今后要用 int c; scanf("%c",&c); //时,都改为 char s[2]; int c; scanf("%1s",s); c=s[0];