简体   繁体   中英

converting from postfix to infix using stacks

I'm currently working on a project converting from postfix to infix using stacks in the form of linked lists. I'm currently trying to read in the whole line as a string then placing it into a character array then when a symbol is found placing one element into a right operand another into a left operand then printing it back out inlcuding the operator. however after placing the first item into the left operand and then popping the stack im not able to place the other item into the right operand. What could be the problem? I its with my pop fucntion.

Here is my code:

#include "stack.h"

stack::~stack()
{
    cout<<"Inside !stack \n";
    while(s_top != 0)
    {
        pop();
    }
}

void stack::pop()
{
    cout<<"Inside pop \n";
    stack_node *p;

    if (s_top != 0)
    {
        p = s_top;
        s_top = s_top->next;
        delete p;
    }

}

void stack::push(char a)
{
    cout<<"Inside push \n";
    stack_node *p = new stack_node;

    p->data = a;
    p->next = s_top;
    s_top = p;
}

void stack::print()
{
    cout<<"Inside print \n";

    for(stack_node *p = s_top; p!=0; p=p->next)
    {
        cout<<p->data<<endl;
    }
}

stack_element stack::top()
{
    cout<<"Inside top \n";

    if (s_top == 0)
    {
        exit(1);
    }
    else
    {
        return s_top->data;
    }
}

/*stack::stack(const stack & Org)
{
    cout<<"Inside the Copy Constructor\n";
    stack_node *p=Org.s_top;

    (*this).s_top = 0;

    while(p!=0)
    {
        (*this).push(p->data);
        p=p->next;  
    }
}

and here is my cpp where it doesnt completely work

#include "stack.h"

string 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]);

if(expression[i]=='*'||'+'||'-'||'/'){
cout<<c.top()<<endl;
leftop=c.top();


c.pop();


rightop=c.top();
cout<<rightop<<endl;
c.pop();
op=c.top();

c.pop();
}
}

}



int main(){

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

getline(cin,expression);

convert(expression);

return 0;

}

Here's an issue:
(expression[i]=='*'||'+'||'-'||'/'
This does not do what you think it does.

The fix:

(expression[i] == '*' ||
  expression[i] == '+' ||
  expression[i] == '-' ||
  expression[i] == '/')

Edit 1: Searching strings
Another method is:

char c = expression[i];
const std::string operators="*+-/";
if (operators.find(c) != std::string::npos)
{
  // expression[i] is an operator character
}

The commonly posted solution is to use switch :

switch (expression[i])
{
  case '+':  Process_Operator_Plus(); break;
  case '-':  Process_Operator_Minus(); break;
  case '*':  Process_Operator_Multiply(); break;
  case '/':  Process_Operator_Divide(); break;
}

Remember, you will need to handle operator precedence when evaluating expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM