核心代码与思路:
int GetExprValue(vector<string> srcVec)
{
stack<int> temp;
char op = '\0';
int a = 0;
int b = 0;
for (int i = 0; i < srcVec.size(); i++) {
op = srcVec[i][srcVec[i].length() - 1];
if (isdigit(op)) {
temp.push(atoi(srcVec[i].c_str()));
} else {
a = temp.top();
temp.pop();
b = temp.top();
temp.pop();
temp.push(CalFunc(b, op, a));
}
}
return temp.top();
}
vector<string> GetSufix(string src)
{
vector<string> dest;
stack<char> opStack;
int i = 0;
string temp = "";
while (i < src.length()) {
if (isdigit(src[i])) {
dest.push_back(ReadIntNum(src, i));
i--;
} else if (IsLeftBrac(src[i])) {
opStack.push(src[i]);
} else if (IsRightBrac(src[i])) {
while (opStack.top()!= MatchBrac(src[i])) {
temp += opStack.top();
dest.push_back(temp);
opStack.pop();
temp = "";
}
opStack.pop();
} else if (IsOprand(src, i)){
while ((!opStack.empty()) && (GetPri(opStack.top()) >= GetPri(src[i])) &&
!IsLeftBrac(opStack.top())) {
temp += opStack.top();
dest.push_back(temp);
opStack.pop();
temp = "";
}
opStack.push(src[i]);
}
i++;
}
while (!opStack.empty()) {
temp += opStack.top();
dest.push_back(temp);
opStack.pop();
temp = "";
}
return dest;
}
完整代码:
#include <iostream>
#include <vector>
#include <stack>
#include <cctype>
#include <cstdlib>
using namespace std;
bool IsLeftBrac(char ch)
{
return (ch == '(' || ch == '[' || ch == '{');
}
bool IsRightBrac(char ch)
{
return (ch == ')' || ch == ']' || ch == '}');
}
char MatchBrac(char ch)
{
char temp;
switch (ch) {
case ')':
temp = '(';
break;
case ']':
temp = '[';
break;
case '}':
temp = '{';
break;
default:
temp = '\0';
break;
}
return temp;
}
bool IsOprand(string src, int idx)
{
bool isSign = (src[idx] == '+' || src[idx] == '-' || src[idx] == '*' || src[idx] == '/');
bool isValid = isdigit(src[idx - 1]) || IsRightBrac(src[idx - 1]);
return (isSign && isValid);
}
string ReadIntNum(string src, int &idx)
{
string value = "";
if (src[idx - 1] == '-' && !IsOprand(src, idx - 1)){
value += '-';
}
while (isdigit(src[idx])) {
value += src[idx++];
}
return value;
}
int CalFunc(int a, char op, int b)
{
int ret = 0;
switch (op) {
case '+':
ret = a + b;
break;
case '-':
ret = a - b;
break;
case '*':
ret = a * b;
break;
case '/':
ret = a / b;
break;
default:
ret = 0;
break;
}
return ret;
}
int GetPri(char ch)
{
int pri = 0;
switch (ch) {
case '+':
case '-':
pri = 1;
break;
case '*':
case '/':
pri = 2;
break;
default:
pri = 0;
break;
}
return pri;
}
int GetExprValue(vector<string> srcVec)
{
stack<int> temp;
char op = '\0';
int a = 0;
int b = 0;
for (int i = 0; i < srcVec.size(); i++) {
op = srcVec[i][srcVec[i].length() - 1];
if (isdigit(op)) {
temp.push(atoi(srcVec[i].c_str()));
} else {
a = temp.top();
temp.pop();
b = temp.top();
temp.pop();
temp.push(CalFunc(b, op, a));
}
}
return temp.top();
}
vector<string> GetSufix(string src)
{
vector<string> dest;
stack<char> opStack;
int i = 0;
string temp = "";
while (i < src.length()) {
if (isdigit(src[i])) {
dest.push_back(ReadIntNum(src, i));
i--;
} else if (IsLeftBrac(src[i])) {
opStack.push(src[i]);
} else if (IsRightBrac(src[i])) {
while (opStack.top()!= MatchBrac(src[i])) {
temp += opStack.top();
dest.push_back(temp);
opStack.pop();
temp = "";
}
opStack.pop();
} else if (IsOprand(src, i)){
while ((!opStack.empty()) && (GetPri(opStack.top()) >= GetPri(src[i])) &&
!IsLeftBrac(opStack.top())) {
temp += opStack.top();
dest.push_back(temp);
opStack.pop();
temp = "";
}
opStack.push(src[i]);
}
i++;
}
while (!opStack.empty()) {
temp += opStack.top();
dest.push_back(temp);
opStack.pop();
temp = "";
}
return dest;
}
int main()
{
string src;
while (cin >> src) {
vector<string> ret = GetSufix(src);
cout << GetExprValue(ret) << endl;
}
return 0;
}
|