
简单计算器是执行一些基本运算的计算器,例如“+”、“-”、“*”、“/”。计算器可以快速完成基本操作。我们将使用 switch 语句来制作一个计算器。
Operator − ‘+’ => 34 + 324 = 358 Operator − ‘-’ => 3874 - 324 = 3550 Operator − ‘*’ => 76 * 24 = 1824 Operator − ‘/’ => 645/5 = 129
#include <iostream<
using namespace std;
int main() {
char op = '+';
float a = 63, b= 7;
// cout << "Enter operator either + or - or * or /: ";
// cin >> op;
cout<<"The operator is "<<op<<endl;
// cout << "Enter two operands: ";
// cin>> a >> b;
cout<<"a = "<<a<<" b = "<<b<<endl;
switch(op) {
case '+':
cout <<"Sum of"<<a<<"and"<<b<<"is"<< a+b;
break;
case '-':
cout <<"Difference of"<<a<<"and"<<b<<"is"<<a-b;
break;
case '*':
cout <<"Product of"<<a<<"and"<<b<<"is"<<a*b;
break;
case '/':
cout <<"Division of"<<a<<"and"<<b<<"is"<<a/b;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}The operator is + a = 63 b = 7 Sum of63 and 7 is 70
以上就是编写一个简单的计算器的C/C++程序的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号