#include <iostream>
#include <stack> //栈的库函数
#include <string> //字符串库函数
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
bool ArePair(char opening,char closing){
if(opening == '(' && closing == ')' ||
opening == '[' && closing == ']' ||
opening == '{' && closing == '}' ){
return true;
}
return false;
}
bool ParenthesisMatched(string expression){
stack<char> S;
for(int i = 0;i < expression.length();i++){
if(expression[i] == '(' || expression[i] == '[' || expression[i] == '{'){
S.push(expression[i]);
}
else if(expression[i] == ')' || expression[i] == ']' || expression[i] == '}' ){
if(S.empty() || !ArePair(S.top(),expression[i])){
return false;
}
S.pop();
}
}
if(!S.empty()){
return false;
}
return true;
}
bool IsOperand(char C){
if(C >= '0' && C <= '9' ||
C >= 'a' && C <= 'z' ||
C >= 'A' && C <= 'Z'){
return true;
}
return false;
}
bool IsOperator(char C){
if(C == '+' || C == '-' || C == '*' || C == '/' || C == '^'){
return true;
}
return false;
}
int GetOperatorWeight(char operator0){
int weight = -1;
switch(operator0){
case '+':
case '-':
weight = 1;
break;
case '*':
case '/':
case '^':
weight = 2;
break;
}
return weight;
}
bool HeightPrecedence(char operator1,char operator2){
int op1Weight = GetOperatorWeight(operator1);
int op2Weight = GetOperatorWeight(operator2);
if(op1Weight > op2Weight){
return true;
}
return false;
}
string Transform(string infix){
string postfix = "";
stack<char> S;
for(int i = 0;i < infix.length();i++){
if(infix[i] == ' ' || infix[i] == ',')continue;
else if(infix[i] == '(' || infix[i] == '[' || infix[i] == '{'){
S.push(infix[i]);
}
else if(infix[i] == ')' || infix[i] == ']' || infix[i] == '}'){
while(!ArePair(S.top(),infix[i])){
postfix += S.top();
S.pop();
}
S.pop();
}
else if(IsOperand(infix[i])){
postfix += infix[i];
}
else if(IsOperator(infix[i])){
if(S.empty() || S.top() == '(' || S.top() == '[' || S.top() == '{'){
S.push(infix[i]);
}
else if(HeightPrecedence(infix[i],S.top())){
S.push(infix[i]);
}else{
postfix += S.top();
S.pop();
S.push(infix[i]);
}
}
}
while(!S.empty()){
postfix += S.top();
S.pop();
}
return postfix;
}
int Calculate(string expression) {
stack<char> S;
char c1 = ' ';
char c2 = ' ';
char c3 = ' ';
int num1 = 0;
int num2 = 0;
int result = 0;
for (int i = 0; i < expression.length(); i++) {
if (IsOperand(expression[i])) {
S.push(expression[i]);
}
else {
c2 = S.top();
num2 = atoi(&c2);
S.pop();
c1 = S.top();
num1 = atoi(&c1);
S.pop();
if (expression[i] == '*') {
result = num1 * num2;
}
else if (expression[i] == '/') {
result = num1 / num2;
}
else if (expression[i] == '^') {
result = pow(num1, num2);
}
else if (expression[i] == '+') {
result = num1 + num2;
}
else if (expression[i] == '-') {
result = num1 - num2;
}
S.push(result + '0');
}
}
c3 = S.top();
result = atoi(&c3);
S.pop();
return result;
}
int main() {
string expression = "9 - 5 * (3 - 2)";
int result = 0;
if(ParenthesisMatched(expression)){
expression = Transform(expression);
printf("后缀表达式为: %s",expression.c_str());
result = Calculate(expression);
printf("\nresult = %d",result);
}else{
printf("表达式括号不匹配,请检查后重新输入!");
return 0;
}
}