我尝试编写我的第一个计算器,并在网上找到了一些示例,然后我对其进行了更改以使它们在流程方面更容易。但是,当我从此更改流程时:
#include <stdio.h>
main()
{
char operator;
float num1,num2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c" ,&operator);
printf("Enter first operand: ");
scanf("%f" ,&num1);
printf("Enter second operand: ");
scanf("%f" ,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f\n" ,num1+num2);
break;
case '-':
printf("num1-num2=%.2f\n" ,num1-num2);
break;
case '*':
printf("num1*num2=%.2f\n" ,num1*num2);
break;
case '/':
printf("num1/num2=%.2f\n" ,num1/num2);
break;
default: //of operator is other than +, -, *, /, erros message shown
printf("Error! Invalid operator, this is basic math only.\n");
}
return 0;
}
对此:
#include <stdio.h>
main()
{
char operator;
float num1,num2;
printf("Enter first operand: ");
scanf("%f" ,&num1);
printf("Enter an operator (+, -, *, /): ");
scanf("%c" ,&operator);
printf("Enter second operand: ");
scanf("%f" ,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f\n" ,num1+num2);
break;
case '-':
printf("num1-num2=%.2f\n" ,num1-num2);
break;
case '*':
printf("num1*num2=%.2f\n" ,num1*num2);
break;
case '/':
printf("num1/num2=%.2f\n" ,num1/num2);
break;
default: //of operator is other than +, -, *, /, erros message shown
printf("Error! Invalid operator, this is basic math only.\n");
}
return 0;
}
基本上改变了流程:输入运算符,然后输入第一个数字,然后输入第二个数字。To:输入第一个数字,然后输入运算符,然后输入第二个数字。我的问题是,当我这样做时,我看到了 Enter 运算符,但程序跳过了输入运算符的选项并要求:输入第一个数字,然后输入第二个数字。响应是默认开关。