#include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <windows.h>
#include <math.h>
#include <iomanip>
#include <gmpxx.h>
using namespace std;
stack<char> bracket;
bool legitimacy(const string &input) {
//没有问题,省略
}
bool is_negative(const string &s) {
return s[0] == '-';
}//判断正负
int compare(const string &a, const string &b) {
if (is_negative(a)) {
if (is_negative(b)) {
return compare(a.substr(1), b.substr(1));
} else {
return -1;
}
} else {
if (is_negative(b)) {
return 1;
} else {
if (a.size() != b.size()) {
return a.size() - b.size();
} else {
for (int i = 0; i < a.size(); ++i) {
if (a[i] != b[i]) {
return a[i] - b[i];
}
}
return 0;
}
}
}
}//比较两数
mpf_class add(mpf_class a, mpf_class b) {
return a + b;
}//加法
mpf_class subtract(mpf_class a, mpf_class b) {
return a - b;
}//减法
mpf_class multiply(mpf_class a, mpf_class b) {
return a * b;
}//乘法
mpf_class divide(mpf_class a, mpf_class b) {
return a / b;
}//除法
mpf_class calculate(const string &input) {
stack<char> op_stack;
stack<mpf_class> num_stack;
for (int i = 0; i < input.size(); ++i) {
if (isdigit(input[i])) {
mpf_class num = 0;
mpf_class decimal = 0.1;
bool is_decimal = false;
while (i < input.size() && (isdigit(input[i]) || input[i] == '.')) {
if (input[i] == '.') {
is_decimal = true;
} else {
if (is_decimal) {
decimal /= 10;
num += (input[i] - '0') * decimal;
} else {
num = num * 10 + (input[i] - '0');
}
}
++i;
}
--i;
num_stack.push(num);
} else if (input[i] == '(') {
op_stack.push(input[i]);
} else if (input[i] == ')') {
while (!op_stack.empty() && op_stack.top() != '(') {
mpf_class s2 = num_stack.top();
num_stack.pop();
mpf_class s1 = num_stack.top();
num_stack.pop();
char op = op_stack.top();
op_stack.pop();
mpf_class res;
if (op == '+')
res = add(s1, s2);
if (op == '-')
res = subtract(s1, s2);
if (op == '*')
res = multiply(s1, s2);
if (op == '/')
res = divide(s1, s2);
num_stack.push(res);
}
op_stack.pop();
} else if (input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/') {
while (!op_stack.empty() && (input[i] == '+' || input[i] == '-') && (op_stack.top() == '*' || op_stack.top() == '/')) {
mpf_class s2 = num_stack.top();
num_stack.pop();
mpf_class s1 = num_stack.top();
num_stack.pop();
char op = op_stack.top();
op_stack.pop();
mpf_class res;
if (op == '+')
res = add(s1, s2);
if (op == '-')
res = subtract(s1, s2);
if (op == '*')
res = multiply(s1, s2);
if (op == '/')
res = divide(s1, s2);
num_stack.push(res);
}
op_stack.push(input[i]);
} else {
continue;
}
}
while (!op_stack.empty()) {
mpf_class s2 = num_stack.top();
num_stack.pop();
mpf_class s1 = num_stack.top();
num_stack.pop();
char op = op_stack.top();
op_stack.pop();
mpf_class res;
if (op == '+')
res = add(s1, s2);
if (op == '-')
res = subtract(s1, s2);
if (op == '*')
res = multiply(s1, s2);
if (op == '/')
res = divide(s1, s2);
num_stack.push(res);
}
return num_stack.top();
}//逻辑计算
void print_result(const mpf_class &input) {
mpf_class num;
mp_exp_t expo;
string str = num.get_str(expo, 10, 0);
int len = str.length();
char *output = new char[len + 1];
mp_exp_t exp;
mpf_get_str(output, &exp, 10, 0, input.get_mpf_t());
for (size_t i = 0; i < strlen(output); ++i) {
if (output[i] == 'e') {
output[i] = 'E';
}
}
cout << "结果为:" << setprecision(20) << output << endl;
delete[] output;
return;
}//将科学计数法转换为普通数字
int main() {
system("color A");
string input;
do {
cout << "请输入表达式(输入0结束):";
cin >> input;
if (input == "0") {
break;
}
if (!legitimacy(input)) {
cout << "输入不合法,请重新输入:";
continue;
}
mpf_class result = calculate(input);
print_result(result);
} while (true);
return 0;
}
上述代码通过g++编译后,在处理浮点数运算时计算有误,还有可能异常退出,怎么解决