使用Switch语句编写一个模拟简单计算器的程序。依次输入两个整数和一个字符,并用空格隔开。如果该字符是 5
使用Switch语句编写一个模拟简单计算器的程序。依次输入两个整数和一个字符,并用空格隔开。如果该字符是一个“+”,则打印和;如果该字符是一个“-”,则打印差;如果该字符...
使用Switch语句编写一个模拟简单计算器的程序。依次输入两个整数和一个字符,并用空格隔开。如果该字符是一个“+”,则打印和;如果该字符是一个 “-”,则打印差;如果该字符是一个“*”,则打印积;如果该字符是“/”,则打印商;如果该字符是一个“%”,则打印余数。打印结果后输出一个空行。
#include <iostream>
using namespace std;
int main(){ double a,b,enum operate{+,-,*,/,%}; cout<<"Please enter two integers:"<<endl; cin>>a>>b; cout<<"Please enter two operators:"<<endl; cin>>operate; switch(operate) { case +:cout<<a+b<<endl;break; case -:cout<<a-b<<endl;break; case *:cout<<a*b<<endl;break; case /:cout<<a/b<<endl;break; case %:cout<<a%b<<endl;break; default:cout<<"operator out of range + - * / %"<<endl;break;} cout<<"\n"; return 0;}我用的是Microsoft Visual Studio 2008版
#include <iostream>
using namespace std;
int main(){
double a,b,enum operate{+,-,*,/,%};
cout<<"Please enter two integers:"<<endl;
cin>>a>>b;
cout<<"Please enter two operators:"<<endl;
cin>>operate;
switch(operate)
{
case +:cout<<a+b<<endl;break;
case -:cout<<a-b<<endl;break;
case *:cout<<a*b<<endl;break;
case /:cout<<a/b<<endl;break;
case %:cout<<a%b<<endl;break;
default:cout<<"operator out of range + - * / %"<<endl;break;
}
cout<<"\n";
return 0;
} 展开
#include <iostream>
using namespace std;
int main(){ double a,b,enum operate{+,-,*,/,%}; cout<<"Please enter two integers:"<<endl; cin>>a>>b; cout<<"Please enter two operators:"<<endl; cin>>operate; switch(operate) { case +:cout<<a+b<<endl;break; case -:cout<<a-b<<endl;break; case *:cout<<a*b<<endl;break; case /:cout<<a/b<<endl;break; case %:cout<<a%b<<endl;break; default:cout<<"operator out of range + - * / %"<<endl;break;} cout<<"\n"; return 0;}我用的是Microsoft Visual Studio 2008版
#include <iostream>
using namespace std;
int main(){
double a,b,enum operate{+,-,*,/,%};
cout<<"Please enter two integers:"<<endl;
cin>>a>>b;
cout<<"Please enter two operators:"<<endl;
cin>>operate;
switch(operate)
{
case +:cout<<a+b<<endl;break;
case -:cout<<a-b<<endl;break;
case *:cout<<a*b<<endl;break;
case /:cout<<a/b<<endl;break;
case %:cout<<a%b<<endl;break;
default:cout<<"operator out of range + - * / %"<<endl;break;
}
cout<<"\n";
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询