我是C编程语言的新手。我正在创建一个简单的计算器程序,但由于某种原因,我的函数没有返回正确的结果。这是我的程序:为什么我的计算器功能不能返回正确的结果?
#include <stdio.h>
#include <math.h>
#include <complex.h>
#include <string.h>
int calculator(int x, char operator, int y);
int main()
{
int x;
char operator;
int y;
printf("Enter an arithmetic experession: ");
scanf("%d%s%d", &x, &operator, &y);
int result = calculator(x, operator, y);
if(result == -1)
{
printf("Error! Try again!");
}
else
{
printf("%d", result);
}
return 0;
}
int calculator(int x, char operator, int y)
{
int result = 0;
if(y = 0)
{
return -1;
}
if(operator == '+')
{
result = x + y;
}
else if(operator == '-')
{
result = x - y;
}
else if(operator == '*')
{
result = x * y;
}
else if(operator == '/')
{
result = x/y;
}
else if(operator == '%')
{
result = x % y;
}
else
{
return -1;
}
return result;
}
所以当我运行这个程序时,它要求一个算术表达式。如果我放入5 + 3,它只返回5!经过几次测试,似乎无论如何它只返回第一个操作数。我想这是非常轻微的事情,但我没有看到我错过了什么。
C不支持_methods_,只有_functions_。如果你不知道差别,请不要使用该术语。 – Olaf