- 分享
计算器
- @ 2025-12-20 15:54:12
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
// 阶乘计算函数(仅支持非负整数,超出范围返回-1)
long long factorial(int n) {
if (n < 0) return -1;
if (n == 0 || n == 1) return 1;
long long res = 1;
for (int i = 2; i <= n; i++) {
res *= i;
if (res < 0) return -2;
}
return res;
}
// 字符串预处理:去除所有括号
string preprocess(string s) {
s.erase(remove(s.begin(), s.end(), '('), s.end());
s.erase(remove(s.begin(), s.end(), ')'), s.end());
return s;
}
// 拆分无空格阶乘(如3!拆分为"3"和"!")
bool splitFactorial(string &input, string &num_str, string &op) {
size_t pos = input.find('!');
if (pos != string::npos && pos > 0) {
num_str = input.substr(0, pos);
op = "!";
return true;
}
return false;
}
int main() {
cout << "===== 增强版科学无限循环计算器(灵活输入) =====" << endl;
cout << "【双目运算(格式:数字1 运算符 数字2)】" << endl;
cout << "支持:+(加)、-(减)、*(乘)、/(除)、^(幂运算,如 2^3=8)" << endl;
cout << "【单目运算(支持灵活格式)】" << endl;
cout << "支持:sqrt(4)、cos12、5^2、3! 等格式(可无空格、可带括号)" << endl;
cout << "支持:sin、cos、tan(弧度制)、ln、log10、!(阶乘)" << endl;
cout << "【退出方式】:输入 q/Q 即可退出计算器" << endl;
cout << "==============================================" << endl;
while (true) {
double num1, num2;
string op;
double result;
bool is_single_op = false;
bool valid_op = true;
string full_input;
cout << "\n请输入表达式:";
// 读取整行输入
getline(cin, full_input);
// 去除首尾空格
full_input.erase(0, full_input.find_first_not_of(" \t"));
full_input.erase(full_input.find_last_not_of(" \t") + 1);
// 判断退出
if (full_input == "q" || full_input == "Q") {
cout << "计算器已退出,感谢使用!" << endl;
break;
}
// 预处理:去除括号
string processed = preprocess(full_input);
// 拆分输入项(处理无空格场景)
string first_part, second_part, third_part;
size_t pos1 = processed.find_first_of(" +-*/^!");
size_t pos2 = string::npos;
// 场景1:单目运算符开头(如sqrt4、sin12)
if (processed.substr(0, 4) == "sqrt" || processed.substr(0, 3) == "sin" ||
processed.substr(0, 3) == "cos" || processed.substr(0, 3) == "tan" ||
processed.substr(0, 2) == "ln" || processed.substr(0, 5) == "log10") {
is_single_op = true;
if (processed.substr(0, 4) == "sqrt") {
op = "sqrt";
num1 = atof(processed.substr(4).c_str());
} else if (processed.substr(0, 3) == "sin") {
op = "sin";
num1 = atof(processed.substr(3).c_str());
} else if (processed.substr(0, 3) == "cos") {
op = "cos";
num1 = atof(processed.substr(3).c_str());
} else if (processed.substr(0, 3) == "tan") {
op = "tan";
num1 = atof(processed.substr(3).c_str());
} else if (processed.substr(0, 2) == "ln") {
op = "ln";
num1 = atof(processed.substr(2).c_str());
} else if (processed.substr(0, 5) == "log10") {
op = "log10";
num1 = atof(processed.substr(5).c_str());
}
}
// 场景2:阶乘(如3!、5!)
else if (splitFactorial(processed, first_part, op)) {
is_single_op = true;
num1 = atof(first_part.c_str());
}
// 场景3:平方(如5^2、10^2)
else if ((pos1 = processed.find("^2")) != string::npos) {
is_single_op = true;
op = "^2";
num1 = atof(processed.substr(0, pos1).c_str());
}
// 场景4:双目运算(如2+3、4^5)
else {
is_single_op = false;
pos1 = processed.find_first_of("+-*/^");
if (pos1 == string::npos) {
cout << "无效输入,请重新输入!" << endl;
continue;
}
op = processed.substr(pos1, 1);
num1 = atof(processed.substr(0, pos1).c_str());
num2 = atof(processed.substr(pos1 + 1).c_str());
}
// 执行计算逻辑(与原有代码一致,此处省略重复部分,直接复用原有判断)
if (is_single_op) {
if (op == "sqrt") {
if (num1 < 0) {
cout << "错误:负数无法开平方!" << endl;
valid_op = false;
} else {
result = sqrt(num1);
}
} else if (op == "sin") {
result = sin(num1);
} else if (op == "cos") {
result = cos(num1);
} else if (op == "tan") {
result = tan(num1);
} else if (op == "ln") {
if (num1 <= 0) {
cout << "错误:自然对数的输入必须大于0!" << endl;
valid_op = false;
} else {
result = log(num1);
}
} else if (op == "log10") {
if (num1 <= 0) {
cout << "错误:常用对数的输入必须大于0!" << endl;
valid_op = false;
} else {
result = log10(num1);
}
} else if (op == "^2") {
result = num1 * num1;
} else if (op == "!") {
int n = static_cast<int>(num1);
if (n != num1) {
cout << "错误:阶乘仅支持整数输入!" << endl;
valid_op = false;
} else {
long long fact_res = factorial(n);
if (fact_res == -1) {
cout << "错误:阶乘仅支持非负整数!" << endl;
valid_op = false;
} else if (fact_res == -2) {
cout << "错误:阶乘数值过大,超出范围!" << endl;
valid_op = false;
} else {
cout << num1 << "! = " << fact_res << endl;
valid_op = false;
}
}
}
} else {
if (op == "+") {
result = num1 + num2;
} else if (op == "-") {
result = num1 - num2;
} else if (op == "*") {
result = num1 * num2;
} else if (op == "/") {
if (num2 == 0) {
cout << "错误:除数不能为0!" << endl;
valid_op = false;
} else {
result = num1 / num2;
}
} else if (op == "^") {
result = pow(num1, num2);
} else {
cout << "错误:无效双目运算符!" << endl;
valid_op = false;
}
}
if (valid_op && op != "!") {
cout << "计算结果:";
if (is_single_op) {
if (op == "^2") {
cout << num1 << " ^2 = " << fixed << setprecision(4) << result << endl;
} else {
cout << op << "(" << num1 << ") = " << fixed << setprecision(4) << result << endl;
}
} else {
cout << num1 << " " << op << " " << num2 << " = " << fixed << setprecision(4) << result << endl;
}
}
}
return 0;
}
0 条评论
目前还没有评论...