【发布时间】:2020-02-27 04:27:55
【问题描述】:
我不明白为什么这段代码给我的乘法、除法和减法错误的结果? 它给我任何乘法运算的结果为零,任何除法错误的结果最后减法的结果总是1
#include <stdio.h>
long operations(char x,int num1 ,int num2 );
int main(void) {
char x;
int num1 , num2;
long result;
setbuf(stdout,NULL);
result=operations(x,num1 ,num2 );
}
long operations(char x,int num1 ,int num2 )
{
printf("please enter your operation sum (1) ,subtrcation(2),mul(3),div(4) :");
scanf("%d",&x);
if (x==(1||2||3||4))
{
printf("please enter the first and second numbers : \n ");
scanf ("%d%d" ,&num1 ,&num2);
}
else
{
printf("error ") ;
}
switch (x)
{
case 1:
{
return printf("the result sum is :%d ",(num1+num2)) ;
}
case 2:
{
return printf("the result of subtraction is :%d ",(num1-num2)) ;
}
case 3:
{
return printf("the result multiblication is :%d ",(num1*num2)) ;
}
case 4:
{
return printf("the result division is :%d ",(num1/num2)) ;
}
}
}
【问题讨论】:
-
if (x==(1||2||3||4))与if (x == 1)相同。肯定是骗子 -
编译器为此代码生成 8 个警告。使用 C 进行编程时,您需要启用警告(至少在 gcc 或 clang 中使用
-Wall -Wextra)、阅读警告并修复警告。