我是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