By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

infix to postfix expression string for evalution.

P: n/a
Hello!

I am converting an infix expression string into a postfix so that I
will be able to evaluate it easier ->

(5*(((9+8)*(4*6))+7)) == 598+46**7+*

I believe the rule is "Replace all occurances of two operands followed
by an operator by their infix equivalent"

This works fine like so ->

(2*2) == (22*)

but what happens if I need something bigger then 9?

(10*2) == (102*) ->

which would multiple the 0 and the 2 instead of 10*2...
Am I making any sense? Please help me.

Thanks.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"KidLogik" <Si********@nospam.com> wrote in message
news:6a********************************@4ax.com...
Hello!

I am converting an infix expression string into a postfix so that I
will be able to evaluate it easier ->

(5*(((9+8)*(4*6))+7)) == 598+46**7+*

I believe the rule is "Replace all occurances of two operands followed
by an operator by their infix equivalent"

This works fine like so ->

(2*2) == (22*)

but what happens if I need something bigger then 9?

(10*2) == (102*) ->

which would multiple the 0 and the 2 instead of 10*2...
Am I making any sense? Please help me.

Thanks.


I think you need to add a space between the 10 and the 2.

John
Jul 22 '05 #2

P: n/a
While it was 2/2/04 9:39 am throughout the UK, KidLogik sprinkled little
black dots on a white screen, and they fell thus:
Hello!

I am converting an infix expression string into a postfix so that I
will be able to evaluate it easier -> <snip> but what happens if I need something bigger then 9?

(10*2) == (102*) ->

<snip>

Surely you should be building the postfixed expression in a binary form?
For example:

struct RPNNode {
enum { NUM, PLUS, MINUS, TIMES, DIVIDE } op;
int value;
};

That way, you won't have any such problem.

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #3

P: n/a
KidLogik wrote:
I am converting an infix expression string into a postfix so that I
will be able to evaluate it easier -> (5*(((9+8)*(4*6))+7)) == 598+46**7+* I believe the rule is "Replace all occurances of two operands followed
by an operator by their infix equivalent"
Firstly, there is more than one rule! When I wrote this program, I found
that dealing with parentheses and operator precedence was not
straightforward; it took a bit of fiddling around. In any case,
generally speaking, you need to use a stack and a parser...

[snip] but what happens if I need something bigger then 9? (10*2) == (102*) -> which would multiple the 0 and the 2 instead of 10*2...


The parser comes in handy for solving this problem. You can basically
parse the input string as characters using the following loop:

char *p = buf;
while(*p != '\n'){
if(isdigit((int)*p)){
/* operands */
}else
if(strchr("+-*/%^()", *p) == 0){
/* whitespace */
p++;
}else{
/* operators */
}
}

Note that my program does not evaluate the expression (which you seem to
imply as your goal), but rather converts between two strings: infix to
postfix.

HTH,

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 22 '05 #4

P: n/a
David Rubin wrote:
KidLogik wrote:

I am converting an infix expression string into a postfix so that I
will be able to evaluate it easier ->


(5*(((9+8)*(4*6))+7)) == 598+46**7+*


I believe the rule is "Replace all occurances of two operands followed
by an operator by their infix equivalent"

Firstly, there is more than one rule! When I wrote this program, I found
that dealing with parentheses and operator precedence was not
straightforward; it took a bit of fiddling around. In any case,
generally speaking, you need to use a stack and a parser...

[snip]
but what happens if I need something bigger then 9?


(10*2) == (102*) ->


which would multiple the 0 and the 2 instead of 10*2...

The parser comes in handy for solving this problem. You can basically
parse the input string as characters using the following loop:

char *p = buf;
while(*p != '\n'){
if(isdigit((int)*p)){
/* operands */
}else
if(strchr("+-*/%^()", *p) == 0){
/* whitespace */
p++;
}else{
/* operators */
}
}

Note that my program does not evaluate the expression (which you seem to
imply as your goal), but rather converts between two strings: infix to
postfix.

HTH,

/david


If one throws the operators and numbers into a binary tree, then
conversion is just a matter of how the tree is traversed. I wrote
the program so long ago, I don't remember how I handled the parens.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #5

P: n/a
Thomas Matthews wrote:

[snip - infix to postfix]
Firstly, there is more than one rule! When I wrote this program, I found
that dealing with parentheses and operator precedence was not
straightforward; it took a bit of fiddling around. In any case,
generally speaking, you need to use a stack and a parser...

[snip] If one throws the operators and numbers into a binary tree, then
conversion is just a matter of how the tree is traversed. I wrote
the program so long ago, I don't remember how I handled the parens.


I found the stack-based algorithm in the project notes from some random
college intro CS course on the web. I usually just browse college course
web pages when I'm looking for a short "keep your skills sharp" project.
The stack algorithm explicitly described how to handle parens, but it
was apparantly lacking a few details.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.