Convert from an infix expression to postfix (C++) using Stacks???

I need to convert from an infix expression to postfix using stacks! I need help with the code!could you tell me if what I have is right? THANKS!


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

class ExpressionManager : public ExpressionManagerInterface{
public:
ExpressionManager(){}
~ExpressionManager(){}
bool is_operator(char currentChar){
if (currentChar == '+')
return true;
else if(currentChar == '-')
return true;
else if(currentChar == '*')
return true;
else if(currentChar == '/')
return true;
else if (currentChar == '%')
return true;
return false;
}
/*check precedence of operator*/
int precedence(char p) {
if (p == '+' || p == '-')
return 1;
if (p == '*' || p == '/'|| p == '%')
return 2;
}
string infixToPostfix(string infixExpression){

stack<string> operand_stack;
stringstream ss;
ss<<postfixExpression;
ss>>token;

if (token == '(' || token == '[' || token == '{')
operand_stack.push(token);
if (token == '+' || token == '- '|| token == '/' || token == '%' ){
if (precedence(token)> precedence(operand_stack.top())
//we want to add it in the stack
if (precedence(token)< precedence(operand_stack.top))
//we want to put the low character in the output
if (precedence (token) = precende(operand_stack.top))
//

}
};

Last edited on
Well, we're missing some things so this won't compile, but what you have so far looks correct other than a few places where you forgot a bracket or misspelled a word.

Also, please use code blocks when you post code (that icon to the right that looks like '<>').

I cleaned up your code as far as I could with what you have.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef EXPRESSIONMANAGER_H_
#define EXPRESSIONMANAGER_H_

#include <iostream>
#include <stack>
#include <string>
#include <sstream>

using namespace std;

class ExpressionManager : public ExpressionManagerInterface
{
public:
	ExpressionManager(){}
	~ExpressionManager(){}

	bool is_operator ( char currentChar )
	{
		if ( currentChar == '+' || currentChar == '-' || currentChar == '*'
			|| currentChar == '/' || currentChar == '%' )
			return true;

		else return false;
	}

	/*check precedence of operator*/
	int precedence ( char p )
	{
		if ( p == '+' || p == '-' )
			return 1;

		if ( p == '*' || p == '/'|| p == '%' )
			return 2;
	}

	string infixToPostfix ( string infixExpression )
	{
		stack<string> operand_stack;

		stringstream ss;

		ss << postfixExpression;
		ss >> token;

		if ( token == '(' || token == '[' || token == '{' )
			operand_stack.push(token);
		if ( token == '+' || token == '- '|| token == '/' || token == '%' )
		{
			if ( precedence(token) > precedence(operand_stack.top() ) )
			{
				// add it to the stack
			}
			else if ( precedence(token) < precedence(operand_stack.top) )
			{
				// we want to put the low character in the output
			}
			else if ( precedence (token) == precedence(operand_stack.top) )
			{
			}
		}
	}
};

#endif 
If you need more help this site has a pretty good explanation.
http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/
Topic archived. No new replies allowed.