# Infix to Postfix

Page 1 of 1

## 10 Replies - 12005 Views - Last Post: 05 April 2008 - 07:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=36790&amp;s=600c6b6524b8632da90d1cc7f5794183&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 C++++

Reputation: 0
• Posts: 19
• Joined: 03-November 07

# Infix to Postfix

Posted 04 November 2007 - 02:59 PM

i have a code let you calculate a fully parenthesized arithmetic expression only (with bracket)

my question is : how can I calculate it without bracket

for example:
((12-2)*(9/3))
if we put it as privious way we shall get 30

but if I put it as
12-2*9/3

how can i solve this problem

my code :

```// written by C++++ from www.dreamincode.net

#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

void evaluate_stack_tops(stack<double>& numbers, stack<char>& operations);

int main( )
{

cout << "Type a fully parenthesized arithmetic expression:" << endl;
cout << "That evaluates to " << answer << endl;

return EXIT_SUCCESS;
}

{
const char DECIMAL = '.';
const char RIGHT_PARENTHESIS = ')';

stack<double> numbers;
stack<char> operations;
double number;
char symbol;

while (ins && ins.peek( ) != '\n')
{
if (isdigit(ins.peek( )) || (ins.peek( ) == DECIMAL))
{
ins >> number;
numbers.push(number);
}
else if (strchr("+-*/", ins.peek( )) != NULL)
{
ins >> symbol;
operations.push(symbol);
}
else if (ins.peek( ) == RIGHT_PARENTHESIS)
{
ins.ignore( );
evaluate_stack_tops(numbers, operations);
}
else
ins.ignore( );
}

return numbers.top( );
}

void evaluate_stack_tops(stack<double>& numbers, stack<char>& operations)
{
double operand1, operand2;

operand2 = numbers.top( );
numbers.pop( );
operand1 = numbers.top( );
numbers.pop( );
switch (operations.top( ))
{
case '+': numbers.push(operand1 + operand2);
break;
case '-': numbers.push(operand1 - operand2);
break;
case '*': numbers.push(operand1 * operand2);
break;
case '/': numbers.push(operand1 / operand2);
break;
}
operations.pop( );
}

```

This post has been edited by Martyr2: 04 November 2007 - 04:30 PM

Is This A Good Question/Topic? 0

## Replies To: Infix to Postfix

### #2 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Infix to Postfix

Posted 04 November 2007 - 04:37 PM

Well, there is a slight problem: ((12-2)*(9/3)) and 12-2*9/3 are very different expression that evaluate to very different answers... The fist one is 10*3 and should be 30... the second is 12-6=6... if you want to make it so that they both evaluate to the same value you will need to break your program so that it does not properly evaluate expressions...

Now if you mean that the first expression evaluates properly, but the second does not... Well then what you need to do is think about operator precedence.

### #3 C++++

Reputation: 0
• Posts: 19
• Joined: 03-November 07

## Re: Infix to Postfix

Posted 04 November 2007 - 05:02 PM

but my question how can i let 12-2*9/3 gives the correct answer ?
i want it to give me answer same as your answer (in calculation by changing in the code)

we can say the program gives priority for *&/ at first & +&- after it .

and you have my thanks .

C++++

### #4 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Infix to Postfix

Posted 04 November 2007 - 05:21 PM

The general approach is to convert from infix to pre or post-fix (I have always used postfix for RPN). If you search google for "infix to postfix" you can find many examples (even a snippet on this site). The process is a little involved but not hard (its actually a form of FSM).

Once you have the expression in postfix it is really easy to evaluate.

12-2*9/3 becomes 12 2 9 * 3 / -
How is this evaluated?
well, when you find a number you push it onto the stack, when you find a binary operator you preform the operation on the bottom two elements of the stack the push the result back onto the stack.

so:
push 12
push 2
push 9
pop into operand1, pop into operand2
push operand2 * operand1
push 3
pop into operand1, pop into operand2
push operand2 / operand1
pop into operand1, pop into operand2
push operand2 - operand1

no more elements in input stream? pop answer off stack (whatever is the bottom is considered the answer... in infix to postfix calculations this should be the only thing on the stack, but in RPN calculations this is not always the only element on the stack.)

This is how 99% of the scientific calculators work. They convert infix to postfix and then evaluate.

You CAN evaluate infix directly, but it tends to be a rather complicated.

### #5 C++++

Reputation: 0
• Posts: 19
• Joined: 03-November 07

## Re: Infix to Postfix

Posted 05 November 2007 - 12:09 AM

thanks for help that's what i talking about

but how can I write it in C++ ??

( pop & push in this case )

### #6 MikeRaines

Reputation: 1
• Posts: 78
• Joined: 30-October 07

## Re: Infix to Postfix

Posted 05 November 2007 - 06:42 AM

O.o Fix positions, this stuff was always very interesting. Good stuff to be learning.

I have some old C code laying around with some fix algorithms I will try to sort through if you are still having issues.

### #7 C++++

Reputation: 0
• Posts: 19
• Joined: 03-November 07

## Re: Infix to Postfix

Posted 05 November 2007 - 01:15 PM

I hope you post your codes as fast as you can .

and you have my thanks
C++++

### #8 MikeRaines

Reputation: 1
• Posts: 78
• Joined: 30-October 07

## Re: Infix to Postfix

Posted 05 November 2007 - 01:55 PM

Actually there is already a great snippet on this site. Save me some time digging around.

http://www.dreaminco.../snippet490.htm

### #9 Topher84

• D.I.C Regular

Reputation: 113
• Posts: 359
• Joined: 04-June 07

## Re: Infix to Postfix

Posted 05 November 2007 - 01:56 PM

C++++, on 5 Nov, 2007 - 01:15 PM, said:

I hope you post your codes as fast as you can .

and you have my thanks
C++++

I did something like this a few years ago.. forgot what i made on it but here...

It is in C++

http://www.objectles...tures/index.htm

Lab 2

Feel free to use what you want off the site.. just leave credit

### #10 C++++

Reputation: 0
• Posts: 19
• Joined: 03-November 07

## Re: Infix to Postfix

Posted 06 November 2007 - 10:22 AM

Thanks for all of you for you informations .I need any thing just tell me

any time and any where .

[email protected]
or
[email protected]

thank you a lot guys .

C++++

### #11 blacksnake

Reputation: 0
• Posts: 20
• Joined: 23-July 07

## Re: Infix to Postfix

Posted 05 April 2008 - 07:12 PM

i try this code on DEV C++, but i don't see the answer