不说别的,先上代码
#include <iostream>
#include <cmath>
#include <iomanip>
int main() {
double num1, num2;
char operation;
std::cout << "欢迎使用高级计算器!" << std::endl;
std::cout << "请输入第一个数字: ";
std::cin >> num1;
std::cout << "请输入操作符 (+, -, *, /, sqrt): ";
std::cin >> operation;
if (operation == 's' || operation == 'S') {
std::cout << "sqrt(" << num1 << ") = " << std::fixed << std::setprecision(2) << std::sqrt(num1) << std::endl;
} else {
std::cout << "请输入第二个数字: ";
std::cin >> num2;
switch (operation) {
case '+':
std::cout << num1 << " + " << num2 << " = " << std::fixed << std::setprecision(2) << (num1 + num2) << std::endl;
break;
case '-':
std::cout << num1 << " - " << num2 << " = " << std::fixed << std::setprecision(2) << (num1 - num2) << std::endl;
break;
case '*':
std::cout << num1 << " * " << num2 << " = " << std::fixed << std::setprecision(2) << (num1 * num2) << std::endl;
break;
case '/':
if (num2 != 0) {
std::cout << num1 << " / " << num2 << " = " << std::fixed << std::setprecision(2) << (num1 / num2) << std::endl;
} else {
std::cout << "错误: 除数不能为零." << std::endl;
}
break;
default:
std::cout << "错误: 不支持的操作符." << std::endl;
break;
}
}
return 0;
}
代码不长,如果用的是Embarcadero Dev-C++,要把中文改成英文
感谢你看到最后,关注我一下,我会做出更好的C++代码