7-1式変換(25点)

オリジナルリンク: http://www.cnblogs.com/sykline/p/9762524.html

トピック:

演算式表記プレフィックス、インフィックスとポストフィックス表記表現方法。演算式は、二つのオペランドの中央に毎日使用するインフィックス表記法、即ち、バイナリ演算子の使用です。後置記法への発現を中置するためのプログラムを設計してください。

入力フォーマット:

入力を含むことができる行中置式で自由空間を与え+-*\左右の括弧は()、式が20文字を超えることはありません。

出力フォーマット:

出力ラインポストフィックス表記は、異なるオブジェクト(オペランド演算記号)との間に必要な空間はなく、余分なスペースの端部によって分離され、変換されます。

アイデア:

今週の質問以上のお兄さんのブログインスピレーションを得たカードをありがとうございます。https://www.cnblogs.com/8023spz/p/7635353.html

要約すると:

空のストレージ・オペレータのスタックを構築するための1、

それは、直接デジタル出力値となるが、ここでは、正の符号またはデジタルの10進数、負(負の符号とデジタル出力である)であってもよいことに留意されたい2、。

スタックが空になるまで> B直接さもなければ出力スタックまたはオペレータ、スタックにプッシュされた場合、オペレータは、現在のシンボルAのスタックサイズとシンボルBの第1の比較演算の優先順位に遭遇3、要素 '(';

「演算子、直接出力スタック操作者はスタックが空であるか遭遇オペレータになるまで)」4、それが来ます「(」。

図5に示すように、最終的な出力スタックオペレータスタックが空になるまで。

図6に示すように、カードのフォーマットは、ほとんどの演算子と右ブラケット状況連続など2 +(5)-1、出力フォーマットを処理する熟慮。

図7に示すように、「+」、「 - 」及びデジタル出力の場合は、第1のビット列または記号の前の記号である「(」であり、残りの場合は、オペレータとして扱われます。

最初の2 +(5)-1吐血カードのコードに:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
typedef long long ll;
stack<char> sta;

int main() {
    map<char,int> mp;
    string str;
    cin>>str;
    bool isfirst = true;
    mp['-'] = 1,mp['+'] = 1;
    mp['*'] = 2,mp['/'] = 2;
    mp['('] = 3,mp[')'] = 3;
    for(int i = 0; i<str.size(); i++) {
        if(((i==0||str[i-1]=='(') && (str[i]=='+' || str[i] =='-')) || (str[i]>='0'&&str[i]<='9') || (str[i]=='.')) {
            if(str[i]!='+') {
                if(!isfirst) {////仔细考虑第一个输出格式的位置处理,输入2+(+5)-1
                    cout<<" ";////输出25 + 1 -,格式错误,为了找这个错误,举例子举到欲仙欲死
                }////
                cout<<str[i];
            }
            while(str[i+1]=='.' || (str[i+1]>='0' && str[i+1]<='9')) {
                i++;
                cout<<str[i];
            }
            isfirst = false;
        } else {
            if(str[i]==')') {
                while(!sta.empty() && sta.top()!='(') {
                    cout<<' '<<sta.top();
                    sta.pop();
                }
                sta.pop();
            } else if(sta.empty()||mp[str[i]] > mp[sta.top()]) {
                sta.push(str[i]);
            } else {
                while(!sta.empty() && sta.top()!='(') {
                    cout<<' '<<sta.top();
                    sta.pop();
                }
                sta.push(str[i]);
            }
        }
    }
    while(!sta.empty()) {
        cout<<' '<<sta.top();
        sta.pop();
    }
    return 0;
}
/*
输入样例:
2+3*(7-4)+8/4
样例输出:
2 3 7 4 - * + 8 4 / +
*/
View Code

更正全对的代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
typedef long long ll;
stack<char> sta;

int main() {
    map<char,int> mp;
    string str;
    cin>>str;
    bool isfirst = true;
    mp['-'] = 1,mp['+'] = 1;
    mp['*'] = 2,mp['/'] = 2;
    mp['('] = 3,mp[')'] = 3;
    for(int i = 0; i<str.size(); i++) {
        if(((i==0||str[i-1]=='(') && (str[i]=='+' || str[i] =='-')) || (str[i]>='0'&&str[i]<='9') || (str[i]=='.')) {
            if(!isfirst) {
                cout<<" ";
            }
            if(str[i]!='+') {
                cout<<str[i];
            }
            while(str[i+1]=='.' || (str[i+1]>='0' && str[i+1]<='9')) {
                i++;
                cout<<str[i];
            }
            isfirst = false;
        } else {
            if(str[i]==')') {
                while(!sta.empty() && sta.top()!='(') {
                    cout<<' '<<sta.top();
                    sta.pop();
                }
                sta.pop();
            } else if(sta.empty()||mp[str[i]] > mp[sta.top()]) {
                sta.push(str[i]);
            } else {
                while(!sta.empty() && sta.top()!='(') {
                    cout<<' '<<sta.top();
                    sta.pop();
                }
                sta.push(str[i]);
            }
        }
    }
    while(!sta.empty()) {
        cout<<' '<<sta.top();
        sta.pop();
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/sykline/p/9762524.html

おすすめ

転載: blog.csdn.net/weixin_30687587/article/details/94789354