infix to postfix expression string for evalution.

K

KidLogik

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.
 
J

John Harrison

KidLogik said:
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
 
S

Stewart Gordon

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 ->
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.
 
D

David Rubin

KidLogik said:
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
 
T

Thomas Matthews

David said:
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
 
D

David Rubin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top