从键盘输入两个操作数和运算符,用switch语句实现两个数的加减乘除运算
1个回答
展开全部
#include<iostream>
using namespace std;
int main()
{
int a,b,sum;
char choice,end;
cout<<"enter a,b:";
cin>>a;
cin>>b;
cout<<"your choice(+,-,*,/):";
cin>>choice;
switch(choice)
{
case '+':
sum=a+b;
//cout<<"加法运算:sum="<<sum<<endl;
break;
case '-':
if(a>b)
sum=a-b;
else
sum=b-a;
//cout<<"减法运算:sum="<<sum<<endl;
break;
case '*':
sum=a*b;
//cout<<"乘法运算:sum="<<sum<<endl;
break;
case '/':
sum=a/b;
//cout<<"除法运算:sum="<<sum<<endl;
break;
}
cout<<"sum="<<sum<<endl;
cout<<"please enter any key to exit\n";
cin>>end;
}
using namespace std;
int main()
{
int a,b,sum;
char choice,end;
cout<<"enter a,b:";
cin>>a;
cin>>b;
cout<<"your choice(+,-,*,/):";
cin>>choice;
switch(choice)
{
case '+':
sum=a+b;
//cout<<"加法运算:sum="<<sum<<endl;
break;
case '-':
if(a>b)
sum=a-b;
else
sum=b-a;
//cout<<"减法运算:sum="<<sum<<endl;
break;
case '*':
sum=a*b;
//cout<<"乘法运算:sum="<<sum<<endl;
break;
case '/':
sum=a/b;
//cout<<"除法运算:sum="<<sum<<endl;
break;
}
cout<<"sum="<<sum<<endl;
cout<<"please enter any key to exit\n";
cin>>end;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询