帮我看下这代码,有一功能不知道该怎实现
第一组非数值时全字母,结果显示正确,第二组非数字字母开头,存在数值,显示也正确,但是当数值开头带字符,显示就不正确了,这个该怎么改?求帮忙看下,谢谢了! 运行图和代码附下:#include <stdio.h>
int main (void)
{
float first;
float second = 0.0;
char first_ch;
char second_ch;
char choice;
printf ("Enter the operation of your choice:\n");
printf ("a. add s. subtract\n");
printf ("m. multiply d. divide\n");
printf ("q. quit\n");
while ((choice = getchar ()) != 'q')
{
if (choice == '\n')
continue;
else if (choice == 'a' || choice == 's' || choice == 'm' || choice == 'd')
{
printf ("Enter first number:");
while (scanf ("%f", &first) != 1)
{
while ((first_ch = getchar ()) != '\n')
putchar (first_ch);
printf (" is not a number.\n");
printf ("Please enter a number such as 2.5, -1.78E8 or 3:");
}
printf ("Enter second number:");
if (choice = 'd')
{
while (second == 0)
{
while (scanf ("%f", &second) != 1)
{
while ((second_ch = getchar ()) != '\n')
putchar (second_ch);
printf (" is not a number.\n");
printf ("Please enter a number such as 2.5, -1.78E8 or 3:");
}
if (second == 0)
printf ("Enter a number other than 0:");
}
}
else
{
while (scanf ("%f", &second) != 1)
{
while ((second_ch = getchar ()) != '\n')
putchar (second_ch);
printf (" is not a number.\n");
printf ("Please enter a number, such as 2.5, -1.78E8 or 3:");
}
}
}
else
{
printf ("Please enter a, s, m, d or q:");
continue;
}
switch (choice)
{
case 'a': printf ("%g + %g = %g\n", first, second, first + second);
break;
case 's': printf ("%g - %g = %g\n", first, second, first - second);
break;
case 'm': printf ("%g * %g = %g\n", first, second, first * second);
break;
case 'd': printf ("%g / %g = %g\n", first, second, first / second);
break;
}
printf ("Enter the operation of your choice:\n"); /* 输入提示 */
printf ("a. add s. subtract\n");
printf ("m. multiply d. divide\n");
printf ("q. quit\n");
second = 0.0;
}
return 0;
}
[ 本帖最后由 DarylL 于 2013-2-16 10:32 编辑 ]