switch:
#include<iostream>
using namespace std;
void main()
{
char ch;
double m,n;
cout<<"请输入两个实数:";
cin>>m>>n;
cout<<"请输入运算符(+,-,*,/):";
cin>>ch;
switch(ch)
{
case '+':
cout<<m<<"+"<<n<<"="<<m+n<<endl;
break;
case '-':
cout<<m<<"-"<<n<<"="<<m-n<<endl;
break;
case '*':
cout<<m<<"*"<<n<<"="<<m*n<<endl;
break;
case '/':
cout<<m<<"/"<<n<<"="<<m/n<<endl;
break;
default:
cout<<"输入的字符有误!"<<endl;
}
}
if:
#include<iostream>
using namespace std;
void main()
{
char ch;
double m,n;
cout<<"请输入两个实数:";
cin>>m>>n;
cout<<"请输入运算符(+,-,*,/):";
cin>>ch;
if(ch=='+')
cout<<m<<"+"<<n<<"="<<m+n<<endl;
else if(ch=='-')
cout<<m<<"-"<<n<<"="<<m-n<<endl;
else if(ch=='*')
cout<<m<<"*"<<n<<"="<<m*n<<endl;
else if(ch=='/')
cout<<m<<"/"<<n<<"="<<m/n<<endl;
else
cout<<"输入的运算符有误!"<<endl;
}