postfix

Write a program that converts an infix expression into an equivalent postfix expression.
Your answer must be something similar to the following form:
Infix Expression: ((A + B) - C);
Postfix Expression: A B + C -

And keep in mind that its will be based on these:


b. If Token is (i) a left parenthesis: Push it onto the stack (ii) a right parenthesis: Pop and display stack elements until a left parenthesis is popped, but do not display it. (It is an error if the stack becomes empty with no left parenthesis found.) (iii) an operator: If the stack is empty or
Token has a higher priority than the top stack, push Token onto the stack. Otherwise, pop and display the top stack
element: then repeat the comparison of Token
with the new top stack item. A left parenthesis in the stack is assumed to have a lower priority than that of operators. (iv) an operand: Display it.
#include <iostream>
#include "stack.h"

using namespace std;

int main ()
{
string line;
stack s;
getline (cin, line);
for (int i=0;i <line.length();i++)
{ if ( line [i]==('('))
s.push('(');
else
while( ! s.isEmpty())
{
if (line [i]==')'&&line[i]!='(')
cout <<s.pop();
}
if (line[i]=='*'||'/'||'%')
s.push(line[i]);
else
if (line[i]=='+'||'-')
s.pop();
cout<<line[i];
}
return 0;
}

that is what i tried so far..
i am kind of knowing where is the mistake but i need help explain that please
So... ¿where is it?

Also, code tags.
sorry what did you mean?
i know ppl here a using to write the code in a gd way but idk how to write it like u did cuz it is my second day in the forum
Well, to start with:

#1 This line

if (line[i]=='*'||'/'||'%')

should be

if (line[i]=='*'||line[i]=='/'||line[i]=='%')

(it would read better with some extra spaces?)

Your line is evaluating line[i]=='*' and then or-ing that with '/' and '%', which are both non-zero and therefore equivalent to zero. (That is, operator== can only compare one thing at a time)

#2 The same deal with line

if (line[i]=='+'||'-')

#3 The line

cout<<line[i];

looks like it just prints out the whole of the original string?

But I note that your problem statement refers to tokens whereas you're working with chars. They're not generally the same thing.

Andy

PS See this page for how to use formatting tags (and ideally go back and edit your post to use them)

How to use tags
http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
#3 the line
cout <<line[i];
is based on:
operators. (iv) an operand: Display it.

thank u i got what u just said but would please explain how to do this one
(iii) an operator: If the stack is empty or
Token has a higher priority than the top stack, push Token onto the stack. Otherwise, pop and display the top stack
element: then repeat the comparison of Token

with the new top stack item. A left parenthesis in the stack is assumed to have a lower priority than that of ???
1
2
3
4
5
6
7
8
9
10
11
12
13
if(
   is_empty(the_stack) 
   or higher_priority(
      token,
      the_stack.top()
   )
){
   the_stack.push(token);
}
else{
   the_stack.pop();
   display( the_stack.top() );
}
¿what you don't get?
#include <iostream>
#include "stack.h"

using namespace std;

int main ()
{
string line;
stack s;
getline (cin, line);
for (int i=0;i <line.length();i++)
{ if ( line [i]==('('))
s.push('(');
else
while( ! s.isEmpty())
{
if (line [i]==')'&&line[i]!='(')
cout <<s.pop();
}
s.pop();

if (line[i]==s.isEmpty()||'*'||line[i]=='/'||line[i]=='%')
{
s.push(line[i]);
cout<<line[i];
}
else
if (line[i]=='+'||line[i]=='-')
{s.pop();
cout<<line[i];
}
cout<<line[i];
}
return 0;
}


i did it but i cant see where is the mistake! the output all the time gave my (((( like this
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
#include <iostream>
#include "stack.h"
#include <string>
using namespace std;

int main ()
{
    string line;
    stack s;
    getline (cin, line);
   for (int i=0;i <line.length();i++)
{   if ( line [i]==('('))
    s.push('(');}

     if  (line[i]==')')
    {while( ! s.isEmpty())
     {
        while (line[i]!='(')
        cout <<s.pop();

     s.pop();
    }
    }
    else
    if (line[i]==s.isEmpty()||'*'||line[i]=='/'||line[i]=='%')
       {
        s.push(line[i]);
        cout<<line[i];
       }
       else
        if (line[i]=='+'||line[i]=='-')
        {s.pop();
        cout<<line[i];
        }
        else
        cout<<line[i];
 }
return 0;
}


its with lookup of 'i' change for ISO for scopping error?
any idea?
your brackets don't match

(you should ask the IDE to indent your code)
what is the IDE?
no my brackets are fine the program is working but with wrong output
for example if i test it by ((a+b)-c) the output will show up like ba+4040404
Here is your code, properly indented
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
#include <iostream>
#include "stack.h"
#include <string>
using namespace std;

int main ()
{
	string line;
	stack s;
	getline (cin, line);
	for (int i = 0; i < line.length (); i++)
	{
		if (line[i] == ('('))
			s.push ('(');
	}

	if (line[i] == ')')
	{
		while (!s.isEmpty ())
		{
			while (line[i] != '(')
				cout << s.pop ();

			s.pop ();
		}
	}
	else if (line[i] == s.isEmpty () || '*' || line[i] == '/' || line[i] == '%')
	{
		s.push (line[i]);
		cout << line[i];
	}
	else if (line[i] == '+' || line[i] == '-')
	{
		s.pop ();
		cout << line[i];
	}
	else
		cout << line[i];
}

return 0;
}
As you can see, the braces mismatch.
It does not compile, so whatever you are running is from another version.

http://en.wikipedia.org/wiki/Integrated_development_environment (¿too hard?)
Visual Studio, CodeBlocks, Eclipse, ZinjaI...
Last edited on
well what about if the braces are match there is an error is the code? that i cant figure it out
actually i didnt understand where is the error i need some highlights please
any idea? asap
Topic archived. No new replies allowed.