KidLogik wrote:
> I am converting an infix expression string into a postfix so that I
> will be able to evaluate it easier ->
> (5*(((9+
*(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