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. 5 5943
"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
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.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tony Johansson |
last post by:
Hello Experts!
I'm reading i a book about C++ and they mention infix with telling what it
is.
I hope you out there can do so.
Many thanks!
//Tony
|
by: caramel |
last post by:
i've been working on this program forever! now i'm stuck and going
insane because i keep getting a syntax error msg and i just can't see
what the compiler is signaling to!
#include <stdio.h>...
|
by: tomerdr |
last post by:
Hi,
My infix expression never have brackets,
So how do i convert it to postfix?
For example 112*20 will yield 11220*
Which cannot be correctly evaluate(which is it 1*1220 or 11*220...?)
Is...
|
by: franklyn |
last post by:
converting infix to postfix expression in 'c'
|
by: Thumbski |
last post by:
Alright basically I just have one simple question. I can code perfectly fine and don't have any problems with the .cpp of the infix class but with my header I can't get anything to work really, any...
|
by: Xah Lee |
last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully
Functional Notations
Xah Lee, 2006-03-15
In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
|
by: coolguyjas07 |
last post by:
plzzzzzz help me out to run my source code .........
i hav written dis code to convert infix expression to postfix but not getting desired output.... plzzz help me to get correct output.. d source...
|
by: aitia |
last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help?
import java.io.*;
import java.util.*;
public class Postfix
{
private...
|
by: zeroeight |
last post by:
Hi guys,
I'm a newbie here, I just want to seek for help regarding my program. I want to implement an infix to postfix conversion only accepting numbers. Right now, I already debugged the errors...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
| |