简单的中缀表达式转换为逆波兰表达式

#include <stdio.h>
#include "../SqStack.h"
/*
双目四则运算符:
+ - * /  (部分)
*/
bool IsOperator(char ch){
    return ch == '+' || ch == '-' || ch == '*' || ch == '/' ? true : false;
}
int OprPrior(char o1, char o2){
/*
返回-1 表示左边操作符优先级高
返回0 表示同优先级
返回1 表示右边操作符优先级高
返回-2 表示o1 或 o2 非操作符
*/
    if(!IsOperator(o1) || !IsOperator(o2)) return -2;
    if(o1 == '+' && o2 == '*'){
        return 1;
    }else if(o1 == '+' && o2 == '/'){
        return 1;
    }else if(o1 == '-' && o2 == '*'){
        return 1;
    }else if(o1 == '-' && o2 == '/'){
        return 1;
    }else if(o2 == '+' && o1 == '*'){
        return -1;
    }else if(o2 == '+' && o1 == '/'){
        return -1;
    }else if(o2 == '-' && o1 == '*'){
        return -1;
    }else if(o2 == '-' && o1 == '/'){
        return -1;
    }else{
        return 0;
    }
}

void ReversedPrint(SqStack S){
    SqStack P;
    InitStack(P);
    SElemType ch;
    while(!StackEmpty(S)){
        Pop(S, ch);
        Push(P, ch);
    }
    while(!StackEmpty(P)){
        Pop(P, ch);
        printf("%c", ch);
    }
    printf("\n");
}
void ToPost(char pre[]){
    SqStack oper, num;
    InitStack(oper);
    InitStack(num);
    int i = 0;
    int j = 0;
    char ch;
    while(pre[i]){
        if(IsOperator(pre[i])){
        	/*
        	压符号入栈的时候要判断,如果将入栈符号优先级低于栈顶符号,
        	则要将符号栈元素全部出栈压至num栈,然后才将将入栈符号压入符号栈。
			*/
            GetTop(oper, ch);
            if(OprPrior(pre[i], ch) == 1){
                while(!StackEmpty(oper)){
                    Pop(oper, ch);
                    Push(num, ch);
                }
                Push(oper, pre[i]);
            }else{
                Push(oper, pre[i]);
            }
        }else{
            Push(num, pre[i]);
        }
        i++;
    }
    while(!StackEmpty(oper)){
        Pop(oper, ch);
        Push(num, ch);
    }
    ReversedPrint(num);
}



int main(){
    char exp[] = "A*B+C/D";
    ToPost(exp);
    return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将中缀算数表达式转换为逆波兰式的C++代码: ```c++ #include <iostream> #include <stack> #include <string> #include <sstream> #include <vector> using namespace std; bool is_operator(const string& token) { return token == "+" || token == "-" || token == "*" || token == "/"; } int precedence(const string& token) { if (token == "+" || token == "-") { return 1; } else if (token == "*" || token == "/") { return 2; } else { return 0; } } vector<string> infix_to_postfix(const vector<string>& infix) { stack<string> s; vector<string> postfix; for (const auto& token : infix) { if (is_operator(token)) { while (!s.empty() && is_operator(s.top()) && precedence(token) <= precedence(s.top())) { postfix.push_back(s.top()); s.pop(); } s.push(token); } else if (token == "(") { s.push(token); } else if (token == ")") { while (!s.empty() && s.top() != "(") { postfix.push_back(s.top()); s.pop(); } s.pop(); } else { postfix.push_back(token); } } while (!s.empty()) { postfix.push_back(s.top()); s.pop(); } return postfix; } int evaluate_postfix(const vector<string>& postfix) { stack<int> s; for (const auto& token : postfix) { if (is_operator(token)) { int b = s.top(); s.pop(); int a = s.top(); s.pop(); if (token == "+") { s.push(a + b); } else if (token == "-") { s.push(a - b); } else if (token == "*") { s.push(a * b); } else { s.push(a / b); } } else { stringstream ss(token); int x; ss >> x; s.push(x); } } return s.top(); } int main() { // 中缀表达式: (3+4)*5-6/2 vector<string> infix = {"(", "3", "+", "4", ")", "*", "5", "-", "6", "/", "2"}; vector<string> postfix = infix_to_postfix(infix); for (const auto& token : postfix) { cout << token << " "; } cout << endl; int result = evaluate_postfix(postfix); cout << "Result: " << result << endl; return 0; } ``` 这个代码使用了 `stack` 来实现算法,其中 `infix_to_postfix` 函数将中缀表达式转换为后缀表达式, `evaluate_postfix` 函数计算后缀表达式的值。在 `main` 函数中,我们使用了 `(3+4)*5-6/2` 这个表达式来测试我们的代码,输出结果为: ``` 3 4 + 5 * 6 2 / - Result: 29 ``` 这表明我们的代码成功地将中缀表达式转换为了后缀表达式,并正确地计算了表达式的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值