中綴轉字尾和字首

小星星+yg發表於2020-10-19

中綴轉字尾

#include<bits/stdc++.h>
using namespace std;
bool isOperator(char ch) {
    if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')' || ch == '^')
        return true;
    return false; // 否則返回false
}
int getPriority(char ch) {
    int level = 0; // 優先順序
    
    switch(ch) {
        case '+':
        case '-':
            level = 2;
            break;
        case '*':
        case '/':
            level = 3;
            break;
        case '^':
            level = 4;
            break;
        default:
            break;
    }
    return level;
}
string Postorder;
void Trans(string Inorder){//中轉後
    stack<char> s;
    int len = Inorder.size();
    for(int i = 0; i < len; i++){
        char now = Inorder[i];
        if(isOperator(now)){//是運算子
            if(now == '(') s.push(now);
            else if(now == ')'){
                while(s.top() != '(') {
                    char top = s.top();
                    Postorder.push_back(top);
                    s.pop();
                }
                s.pop();
            }
            else{
                int prior = getPriority(now);//得到優先順序
                while(!s.empty()){
                    char top = s.top();
                    if(prior <= getPriority(top)){//優先順序小於棧頂元素
                        Postorder.push_back(top);//加入字尾表示式並且出棧
                        s.pop();
                    }else break;
                }
                s.push(now);
            }

        }
        else Postorder.push_back(now); //數字直接加入字尾表示式
    }
    while(!s.empty()){
        char top = s.top(); s.pop();
        Postorder.push_back(top);
    }
}


int main(){
    string s;
    cin >> s;
    Trans(s);
    for(int i = 0; i < Postorder.length(); i++) cout << Postorder[i] ;
    return 0;
}

中綴轉字首
思路:從右往左掃描求字尾再將得到的字尾表示式逆序輸出就是字首表示式

#include<bits/stdc++.h>
using namespace std;
bool isOperator(char ch) {
    if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')' || ch == '^')
        return true;
    return false; // 否則返回false
}
int getPriority(char ch) {
    int level = 0; // 優先順序
    
    switch(ch) {
        case '+':
        case '-':
            level = 2;
            break;
        case '*':
        case '/':
            level = 3;
            break;
        case '^':
            level = 4;
            break;
        default:
            break;
    }
    return level;
}
string Postorder;
void Trans(string Inorder){//中轉前
    stack<char> s;
    int len = Inorder.size();
    for(int i = len - 1; i >= 0; i--){//反過來遍歷
        char now = Inorder[i];
        if(isOperator(now)){//是運算子
            if(now == ')') s.push(now);//'('改為')'
            else if(now == '('){//')'改為'('
                while(s.top() != '(') {
                    char top = s.top();
                    Postorder.push_back(top);
                    s.pop();
                }
                s.pop();
            }
            else{
                int prior = getPriority(now);//得到優先順序
                while(!s.empty()){
                    char top = s.top();
                    if(prior <= getPriority(top)){//優先順序小於棧頂元素
                        Postorder.push_back(top);//加入字尾表示式並且出棧
                        s.pop();
                    }else break;
                }
                s.push(now);
            }

        }
        else Postorder.push_back(now); //數字直接加入字尾表示式
    }
    while(!s.empty()){
        char top = s.top(); s.pop();
        Postorder.push_back(top);
    }
}


int main(){
    string s;
    cin >> s;
    Trans(s);
    // reverse(Postorder.begin(),Postorder.end());
    // for(int i = 0; i < Postorder.length(); i++) cout << Postorder[i] ;
    for(int i = Postorder.length() - 1; i >= 0; i--) cout << Postorder[i];//逆序輸出
    return 0;
}

相關文章