|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
char get_first();
float get_num();
char get_choice();
void main()
{
char cho;
float a, b;
while((cho = get_choice()) != 'q')
{
printf("Enter first number: ");
a = get_num();
printf("Enter second number: ");
b = get_num();
switch (cho)
{
case 'a': printf("%g + %g = %g\n", a, b, a + b);
break;
case 's': printf("%g - %g = %g\n", a, b, a - b);
break;
case 'm': printf("%g * %g = %g\n", a, b, a * b);
break;
case 'd':
while (b == 0)
{
printf("Enter a number other than 0: ");
b = get_num();
}
printf("%g / %g = %g\n", a, b, a / b);
break;
default :printf("Program error !\n");
break;
}
}
printf("Bye!\n");
}
float get_num()
{
float num;
char cht;
while ((scanf("%f", &num)) != 1)
{
while ((cht = getchar()) != '\n')
putchar(cht);
printf(" is not a num.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
return num;
}
char get_first()
{
char ch;
ch = getchar();
while ( getchar() != '\n' )
continue;
return ch;
}
char get_choice()
{
char chi;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
chi = get_first();
while((chi != 'a') && (chi != 's') && (chi != 'm') && (chi != 'd') && (chi != 'q'))
{
printf("Please respond with a, s, m, d or q!\n");
chi = get_first();
}
return chi;
}
这个程序中第一次运行正常,第二次输入字符时不能正常运行,直到再输入一次才正常,求助这是怎么回事呀,运行结果如下:
Enter the operation of your choice:\n");
a. add s. subtract
m. multiply d. divide
q. quit
a
Enter first number: 22
Enter second number: 1
22 + 1 = 23
d
Please respond with a,s, m, d, q!
d
Enter first number:................(剩下的略过,每次都需要输入两边才能正常执行) 求教这是怎么回事
#include <stdio.h>
char get_first();
float get_num();
char get_choice();
void main()
{
char cho;
float a, b;
while((cho = get_choice()) != 'q')
{
printf("Enter first number: ");
a = get_num();
printf("Enter second number: ");
b = get_num();
switch (cho)
{
case 'a': printf("%g + %g = %g\n", a, b, a + b);
break;
case 's': printf("%g - %g = %g\n", a, b, a - b);
break;
case 'm': printf("%g * %g = %g\n", a, b, a * b);
break;
case 'd':
while (b == 0)
{
printf("Enter a number other than 0: ");
b = get_num();
}
printf("%g / %g = %g\n", a, b, a / b);
break;
default :printf("Program error !\n");
break;
}
}
printf("Bye!\n");
}
float get_num()
{
float num;
char cht;
while ((scanf("%f", &num)) != 1)
{
while ((cht = getchar()) != '\n')
putchar(cht);
printf(" is not a num.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
while ( getchar() != '\n' );//清空键盘缓冲区数据
return num;
}
char get_first()
{
char ch;
ch = getchar();
while ( getchar() != '\n' );//清空键盘缓冲区数据
return ch;
}
char get_choice()
{
char chi;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
chi = get_first();
while((chi != 'a') && (chi != 's') && (chi != 'm') && (chi != 'd') && (chi != 'q'))
{
printf("Please respond with a, s, m, d or q!\n");
chi = get_first();
}
return chi;
}
|
|