我对我的一个成员有疑问:
#include <stdio.h>
int main(void)
{
int num1, num2, sum, difference, product, quotient;
num1 = 0;
num2 = 0;
printf("type in expression");
scanf("%d%d", &num1, &num2);
sum = num1+num2;
difference = num1-num2;
product = num1*num2;
quotient = num1/num2;
switch (num1) {
case 2: printf("%d/%d=%d", num1, num2!=0 , quotient);
break;
case 1: printf("%d*%d=%d", num1, num2, product);
break;
case 0: printf("%d-%d=%d", num1, num2, difference);
break;
default: printf("%d+%d=%d", num1, num2, sum);
break;
}
}
程序可以编译,但是当我运行它时,会显示以下消息:
浮点异常(核心转储)
这是什么意思?此外,如果有任何其他问题,请告诉我。
答案 0 :(得分:0)
我认为这就是你所需要的。
#include <stdio.h>
int main(void)
{
int num1=0, num2=0;
char operation= ' ';
printf("type in expression\n");
scanf("%d %c %d", &num1, &operation, &num2);
switch (operation) {
case '/': {
if(0 == num2) //This is the solution for your issue.
{
printf("\nCan not perform %d/%d", num1, num2);
}
else
{
printf("\n%d / %d=%d", num1, num2, num1/num2);
}
}
break;
case '*': printf("\n%d * %d=%d", num1, num2, num1*num2);
break;
case '-': printf("\n%d - %d=%d", num1, num2, num1-num2);
break;
case '+': printf("\n%d + %d=%d", num1, num2, num1+num2);
break;
default: printf("\nInvalid operation[%c]", operation);
break;
}
return 0;
}