7-1 表达式转换 (25 分)

原文链接: http://www.cnblogs.com/sykline/p/9762524.html

题目:

算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。

输入格式:

输入在一行中给出不含空格的中缀表达式,可包含+-*\以及左右括号(),表达式不超过20个字符。

输出格式:

在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。

思路:

感谢大佬博客启发过了卡了一个星期的题:https://www.cnblogs.com/8023spz/p/7635353.html

总结一下:

1、建一个空栈储存运算符

2、当碰到数字直接输出,值的注意的是这里的数字可能是小数、负数(负号和数字是一起输出的)或带正号的数字;

3、当碰到运算符的时候,先比较当前符号a与栈顶符号b运算优先级的大小,如果a > b直接压入栈中,否则就输出栈中的运算符直到为空或栈顶元素为‘(’;

4、当碰到')'运算符的时候,直接输出栈中的运算符,直到栈为空或者碰到运算符'(';

5、最后输出栈中的运算符,直到栈为空。

6、最卡格式的一种情况就是运算符和右括号连续出现的情况比如2+(+5)-1,要仔细考虑输出格式的处理;

7、‘+’,‘-’和数字一起输出的情况是正负号的在字符串的第一位或者是正负号前边是‘(’,其余情况都是作为运算符处理的。

第一份被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