我想打印cout
的输出,但是代码不起作用。任何提示都会有所帮助。
int main()
{
cout<<"10+20"<<calc(10,20,+);
cout<<"10*20"<<calc(10,20,*);
cout<<"10-20"<<calc(10,20,-);
cout<<"10/20"<<calc(10,20,/);
}
int calc (int a, int b, char c)
{
int total=0;
if(c=='+'){
total = a+b;
}else if(c=='*'){
total = a*b;
}else if(c=='-'){
total = a-b;
}else if(c=='/'){
total = a/b;
}
return total;
}
当您以字符参数传递时,操作员必须将其包含在单引号中。
int main()
{
cout<<"10+20"<<calc(10,20,'+');
cout<<"10*20"<<calc(10,20,'*');
cout<<"10-20"<<calc(10,20,'-');
cout<<"10/20"<<calc(10,20,'/');
}