简体   繁体   中英

infix to postfix notation

I am doing infix to post fix notation. My program complies, although, for some reason it will not take in any infix expressions, only postfix expressions, which is the opposite of I wanted to do. Here is my program. It took me forever to post on the correct stack exchange group.

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
using namespace std;

string infixexpr (istream&  in)
{
//Holds value in computation
stack<string> postfixstack;
//used to to read in characters from the expression
char ch;
// used to read in numbers from expression
int num;
// Used to remove infix expressions from stack
string lexpr, rexpr;
ch = in.peek();
while ( ch != EOF)
{
    //If we have a whitespace character skip it and continue with
    // the end of the loop.
    if(isspace(ch))
    {
        ch = in.get();
        ch =in.peek();
        continue;
    }

//nonspace character is next to input stream
// if the next character is a number read it and convert it
// to string then put the string onto the postfix stack

if (isdigit (ch))
{
    in >> num;
    // use to convert string
    ostringstream numberstr;
    // convert to number using sstream
    numberstr << num;
    // Push the representing string onto stack0
    postfixstack.push(numberstr.str());
    ch = in.peek();
    continue;
}

// if operator pop the two postfix expressions
// stored on the stack, put the operator after
    // the two expressions
rexpr = postfixstack.top();
postfixstack.pop();
lexpr = postfixstack.top();
postfixstack.pop();


if (ch == '+' || ch == '-' || + ch == '*' || ch == '/' || ch == '%')
    postfixstack.push(rexpr + " " + lexpr + " " + ch);
else
{
    cout << "Error in input expression" << endl;
    exit(1);
}
ch = in.get();
ch = in.peek();
}
return postfixstack.top();
}



int main1()
{
string input;
cout <<  "Enter a infix expression to convert to postfix,"
     << " \nor a blank line to quit the program:";
getline(cin,input);

while (input.size() != 0 )
{
    //convert string to a string stream
    istringstream inputExpr(input);
    cout << "the infix equavilent is: "
         << infixexpr(inputExpr) << endl;
        cout << "Enter a infix Expression to evaluate: ";
        getline(cin,input);
}

return 0;
   }

Here is what I know:

If you run the program at this current state it will take postfix notation: 1 2 + and covert it to postfix. This is not what I wanted.

The problem occurs at lexpr = postfixstack.top() , where top is a pointer to the first node in the stack, I think that it points to nothing. Also I think that when I try to pop something off, there isn't anything to pop off so it goes into error. I am not sure how to fix this.

When the user places in a infix notion: 1 + 2, the program crashes.

*correction I am using the stack header and its functions.

In my opinion, a better method for converting infix to postfix is to use a binary tree. You can generate infix or postfix by your traversal order.

You don't assign any values to lexpr or rexpr , but you use them when you encounter an operator.

Have you tried your algorithm with pen and paper before coding it?

When you encounter a number, you may want to perform these operations:

if number is first value in expression, push value.
else
{
  operator = stack.pop();
  stack.push(number);
  stack.push(operator);
}

Your code listing does not contain a line lexpr = postfix stack.top() like your question suggests. In fact, it doesn't assign anything to lexpr or rexpr anywhere. I'd expect there to be some lines popping values off the stack into lexpr and rexpr.

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