一道C语言提,求解答~

来源:百度知道 编辑:UC知道 时间:2024/09/23 06:00:57
编写程序,利用函数实现任意的四在计算,例如,输入"1+3”,输入“=4”,输入输入在主函数上实现~
main()
{
int a,b,c;
char opt;
scanf("%d %c %d",&a,&opt,&b);
c=calculate(a,b,opt);
printf("%d",c);
getch();
}
int calculate(int x,int y,char opt)
{
if (opt=="+") return x+y;
if (opt=="-") return x-y;
if (opt=="*") return x*y;
if (opt=="/") return x/y;
}
错在哪里啊?

opt是char型,所以在比较时应使用单引号,不能用双引号。就改成:

if (opt=='+') return x+y;
if (opt=='-') return x-y;
if (opt=='*') return x*y;
if (opt=='/') return x/y;

希望对你有帮助!

main()
{ int calculate(int x,int y,char opt) ;
int a,b,c;
char opt;
scanf("%d %c %d",&a,&opt,&b);
c=calculate(a,b,opt);
printf("%d",c);
getch();
}
int calculate(int x,int y,char opt)
{
if (opt=='+') return (x+y);
if (opt=='-') return (x-y);
if (opt=='*') return (x*y);
if (opt=='/') return (x/y);
}