帮我写个计算器的c++代码
当然可以帮你写一个简单的计算器的C++代码。以下是一个基本的计算器程序示例:
#include <iostream>
using namespace std;
int main() {
char operation;
double num1, num2;
cout << "请输入操作符(+,-,*,/): ";
cin >> operation;
cout << "请输入两个操作数: ";
cin >> num1 >> num2;
switch(operation) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
break;
case '/':
if(num2 != 0)
cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
else
cout << "错误:除数不能为0!" << endl;
break;
default:
cout << "错误:无效的操作符!" << endl;
}
return 0;
}
这个程序会要求用户输入一个操作符和两个操作数,然后根据操作符进行相应的计算并输出结果。请注意,这只是一个简单的示例程序,没有进行错误处理和边界检查。在实际的计算器实现中,需要考虑更多的情况和错误处理。
原文地址: http://www.cveoy.top/t/topic/jatn 著作权归作者所有。请勿转载和采集!