Converting from Infix to Postfix using stacks (c++)

百般思念 提交于 2019-12-25 05:46:08

问题


I'm currently working on a project to convert from postfix to infix using a stack in the form of a singly linked list. I've managed to convert expressions such as ab+ to (a+b) however when the expression gets longer such as ab+cd*-. It doesn't work. I'm considering pushing the previously converted expression back onto the stack however the stack is of type char and the expression is a string and it complains when I try to push it back. Should I make it a template and if so how would I do it or is there anyway else to solve this problem.

Here is my code:

#include "stack.h"

void convert(string expression){
    stack c;
    string post = " ";
    string rightop = "";
    string leftop = "";
    string op = "";

    for (int i = 0; i <= expression.length(); i++){
        c.push(expression[i]);
        c.print();
        if (expression[i] == '*' ||
            expression[i] == '+' ||
            expression[i] == '-' ||
            expression[i] == '/'){
            cout << c.top() << endl;
            leftop = c.top();
            cout << leftop << endl;

            c.pop();

            rightop = c.top();
            cout << rightop << endl;
            c.pop();
            op = c.top();
            cout << op << endl;
            //c.pop();
            post = "(" + leftop + " " + op + " " + rightop + ")";

            cout << post << endl;
        }
        //c.push(post);
    }
}


int main(){   
    string expression;
    cout << " Enter a Post Fix expression: ";    
    getline(cin, expression);    
    convert(expression);    
    return 0;
}

回答1:


The original code is lacking following declarations to compile :

#include "stack"
#include "string"
#include "iostream"

using namespace std;

Next, the type of the stack should be a string to be able to store full expressions onto it.

You did not take the elements in proper order from the stack : it is first op, next rightop and finally leftop

The currently commented out last c.pop() is necessary to remove 3rd element from the stack, but it must be followed inside the loop with the (again commented out) c.push(post);

The loop on expression goes one step too far : it should be for (int i =0; i<expression.length();i++) (note the < instead of <=)

When this is done, it is enough to make the convert function to return the last post as a string for the program to give expected result.

As you asked in this other question, it would be much better to ignore spaces in the input string : you should add if (isspace(expression[i])) continue; immediately after the for.

With all those fixes, the code could be :

#include <stack>
#include <string>
#include <iostream>
#include <cctypes>

using namespace std;

string convert(string expression){

    stack<string> c;

    string post =" ";
    string rightop="";
    string leftop="";
    string op ="";

    for (int i =0; i<expression.length();i++){
        if (isspace(expression[i])) continue;
        c.push(string(expression.c_str() + i, 1));
        //c.print();
        if(expression[i] == '*' ||
            expression[i] == '+' ||
            expression[i] == '-' ||
            expression[i] == '/'){
                cout<<c.top()<<endl;
                op=c.top();
                cout<<leftop<<endl;

                c.pop();


                rightop=c.top();
                cout<<rightop<<endl;
                c.pop();
                leftop=c.top();
                cout<<op<<endl;
                c.pop();
                post="(" + leftop + " " + op + " " + rightop + ")";

                cout<<post<<endl;
                c.push(post);
        }
    }
    return post;
}

int main(){

    string expression;
    cout<<" Enter a Post Fix expression: ";

    getline(cin,expression);

    string converted = convert(expression);

    cout << "Converted expression : " << converted << endl;

    return 0;
}

And when given ab+cd*- you get at the end ((a + b) - (c * d))

You just have to comment out all the traces form convert method ;-)




回答2:


i get the impression your "stack" is not used properly. e.g. if ab+* is pushed on the stack your variables become leftop = +, rightop = b, op = a, in order to convert a postfix expression it is easiest to create a binary evaluation tree to get the operator precedence right

e.g.

for ab+c* you want

     *
    / \
   +   c
  / \
 a   b 

and then evaluate the tree either recursively or not. everytime the operator is + or - use parenthesis around it,



来源:https://stackoverflow.com/questions/31079297/converting-from-infix-to-postfix-using-stacks-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!