473,545 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

infix to postfix expression string for evalution.

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
5 5959

"KidLogik" <Si********@nos pam.com> wrote in message
news:6a******** *************** *********@4ax.c om...
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
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
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
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.l earn.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
3768
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
19
11290
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> #include <stdlib.h> #include <string.h> #include <ctype.h>
1
2038
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 putting brackets when pushing operands on the stack is a good idea? 112*20 will yield (112)(20)* as a postfix string.
7
7770
by: franklyn | last post by:
converting infix to postfix expression in 'c'
5
3737
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 guideline as to how to start this would be great. It has to work with assign.cpp which is like this: int main() { Infix infix; while...
30
5435
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”. Likewise, they write “(if test this that)” to mean “if (test) {this} else {that}”. LISP codes are all of the form “(a b c ...)”, where the
0
2515
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 code is given below: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<ctype.h> #include<string.h> #include<process.h>
1
6527
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 static Stack operators = new Stack(); private static Stack operands = new Stack();
2
5561
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 and encountering loop everytime I execute it. Here's my sample code: ******************************************************************** ...
0
7464
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7396
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7751
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5968
agi2029
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.