如何将程序从主发送到另一种方法

How can I send the program from main to another method?

本文关键字:另一种 方法 程序      更新时间:2023-10-16

我想打印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,'/');
}