Skip to content

Commit

Permalink
修改后缀表达式转换
Browse files Browse the repository at this point in the history
  • Loading branch information
andywowws committed Aug 27, 2023
1 parent a0fc3e6 commit 3b9e141
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 80 deletions.
8 changes: 4 additions & 4 deletions calculator/calcer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void calcer::pushjia() {
QChar a = s[s.length() - 1];
if (a == '.') s.erase(s.end() - 1);
a = s[s.length() - 1];
if (a.isDigit()) s += '+';
if (a.isDigit() || a == ')' || a == '(') s += '+';
ui->textEdit->setText(s);
return;
}
Expand All @@ -245,7 +245,7 @@ void calcer::pushjian() {
/*QChar a=s[s.length()-1];
if(a.isDigit())*/
QChar a = s[s.length() - 1];
if (a.isDigit() || a == '(') s += '-';
if (a.isDigit() || a == ')' || a == '(') s += '-';
ui->textEdit->setText(s);
return;
}
Expand All @@ -255,7 +255,7 @@ void calcer::pushcheng() {
QChar a = s[s.length() - 1];
if (a == '.') s.erase(s.end() - 1);
a = s[s.length() - 1];
if (a.isDigit()) s += '*';
if (a.isDigit() || a == ')' || a == '(') s += '*';
ui->textEdit->setText(s);
return;
}
Expand All @@ -268,7 +268,7 @@ void calcer::pushchu() {
QChar a = s[s.length() - 1];
if (a == '.') s.erase(s.end() - 1);
a = s[s.length() - 1];
if (a.isDigit()) s += '/';
if (a.isDigit() || a == ')' || a == '(') s += '/';
}
ui->textEdit->setText(s);
return;
Expand Down
115 changes: 39 additions & 76 deletions calculator/calcero.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "calcero.h"
#include "calcbase.h"
#include <map>
#define ll long long
using namespace std;
void calcs::mid2last(string a) {
stack<char> signs;
string lastf;
map<char, int> t;
t['+'] = 1, t['-'] = 1, t['*'] = 2, t['/'] = 2, t['^'] = 3;
for (int i = 0; i < a.length(); i++) {
if (a[i] == '-') {
if (i == 0) {
Expand All @@ -21,66 +24,26 @@ void calcs::mid2last(string a) {
if (lastf[lastf.length() - 1] == '.') lastf.pop_back();
if (lastf != "") last.push(lastf);
lastf = "";
if ((a[i] == '+' || a[i] == '-')) {
while (1) {
string s(1, a[i]);
if (signs.empty() || signs.top() == '(')
signs.push(a[i]);
else if ((signs.top() == '*' || signs.top() == '/'))
signs.push(a[i]);
else {
s.pop_back();
s += signs.top();
signs.pop();
last.push(s);
continue;
}
break;
}
}
if ((a[i] == '*' || a[i] == '/')) {
while (1) {
string s(1, a[i]);
if (signs.empty() || signs.top() == '(')
signs.push(a[i]);
else {
s.pop_back();
s += signs.top();
signs.pop();
last.push(s);
continue;
}
break;
if (a[i] == '(')
signs.push('(');
else if (a[i] == ')') {
while (!(signs.top() == '(' || signs.empty())) {
last.push(string(1, signs.top()));
signs.pop();
}
}
if ((a[i] == '^')) {
while (1) {
string s(1, a[i]);
if (signs.empty() || signs.top() == '(')
signs.push(a[i]);
else if ((signs.top() == '+' || signs.top() == '-'))
signs.push(a[i]);
else {
s.pop_back();
s += signs.top();
if (!signs.empty()) signs.pop();
} else {
if (signs.empty())
signs.push(a[i]);
else {
while (!(t.empty() || t[signs.top()] < t[a[i]] ||
signs.top() == '(')) {
last.push(string(1, signs.top()));
signs.pop();
last.push(s);
continue;
}
break;
signs.push(a[i]);
}
}
if (a[i] == '(') {
signs.push('(');
}
if (a[i] == ')') {
while (signs.top() != '(') {
string a(1, signs.top());
last.push(a);
signs.pop();
}
signs.pop();
}
}
}
if (!lastf.empty()) last.push(lastf);
Expand All @@ -91,6 +54,21 @@ void calcs::mid2last(string a) {
}
return;
}
inline pair<double, int> read(string st1) {
stringstream ss;
int pos, w1;
double s1;
pos = st1.find('.');
if (pos != string::npos)
w1 = st1.length() - 1 - pos;
else
w1 = 0;
ss << fixed << setprecision(w1) << st1;
ss >> s1;
ss.clear();
ss.str("");
return make_pair(s1, w1);
}
string calcs::calc() {
stack<string> ls1 = last;
stack<string> ls2;
Expand All @@ -108,31 +86,15 @@ string calcs::calc() {
string st1 = datas.top();
double s1, s2;
int w1, w2, wr, pos;
pos = st1.find('.');
if (pos != string::npos)
w1 = st1.length() - 1 - pos;
else
w1 = 0;
stringstream ss;
ss << fixed << setprecision(w1) << st1;
ss >> s1;
ss.clear();
ss.str("");
datas.pop();
pair<double, int> ret = read(st1);
s1 = ret.first, w1 = ret.second;
if (datas.empty()) (throw -1);
string st2 = datas.top();
pos = st2.find('.');
if (pos != string::npos)
w2 = st2.length() - 1 - pos;
else
w2 = 0;
ss << fixed << setprecision(w2) << st2;
ss >> s2;
ss.clear();
ss.str("");
ret = read(st2);
s2 = ret.first, w2 = ret.second;
datas.pop();
double res;
int n = 0;
string y;
if (x == "+") {
y = calcbase::add(st1, st2, 0);
Expand All @@ -152,11 +114,12 @@ string calcs::calc() {
res = pow(s2, s1);
wr = -1;
}
stringstream ss;
if (wr == -1)
ss << fixed << setprecision(50) << res;
else
ss << fixed << setprecision(wr) << res;
if (x == "/") y = ss.str();
if (x == "/" || x == "^") y = ss.str();
ss.clear();
ss.str("");
int pos2 = y.find('.');
Expand Down

0 comments on commit 3b9e141

Please sign in to comment.