用c++编写一个程序,输入两个实数和一个四则运算符(+,-,*,/),根据运算符执行相应的运算并输出结果。
1个回答
展开全部
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;
}
#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;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询