GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Dec-2008, 15:40
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Infix to PostFix and Prefix (The Begining...)


Hi all. My final project is about conerting from infix to post and to prefix according to the user selection.
This question will treat the infix to postfix
part, I need to understand how to convert an expression from infix to postfix so i can find the algorithm, but i don't know how to do the convertion.
I am also trying to use the stack built in class:
CPP / C++ / C Code:
 class stack<char>stk;
stk.push('A');
cout<<stk.top();// this line generates an error message
stk.pop();
i am wondering why i have this error (no operator found that takes<<...);
Thanks all.
  #2  
Old 03-Dec-2008, 18:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Infix to PostFix and Prefix (The Begining...)


Quote:
Originally Posted by zatora
...I need to understand how to convert an expression from infix to postfix
So: If it isn't covered in your text or class notes or other class reference materials, find some examples and explanation. The first hit on google gave me http://scriptasylum.com/tutorials/in...tfix/index.htm

If that isn't adequate to meet your needs, look for others. If you have a specific question, I suggest that you post some code and tell us what you don't understand. My feeling is that the best use of this board is to try to get answers to specific questions. The more specific the question, the greater chance of a helpful response (usually).

Quote:
Originally Posted by zatora
I am also trying to use the stack built in class:
CPP / C++ / C Code:
 class stack<char>stk;

This makes the compiler think that you are going to create a whole new class named stack. (And, of course you haven't created any implementation.)

What you really want is to create an object of that the STL class:
CPP / C++ / C Code:
#include <iostream>
#include <stack>

using namespace std;

int main()
{
    stack <char> stk;
    stk.push('A');
    cout << "After push, stk.size() = " << stk.size() 
         << " and stk.top() = '" << stk.top() << "'" << endl;
    stk.pop();
    cout << "After pop,  stk.size() = " << stk.size()  << endl;
    return 0;
}

Output:
Code:
After push, stk.size() = 1 and stk.top() = 'A' After pop, stk.size() = 0

Regards,

Dave
  #3  
Old 04-Dec-2008, 00:41
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Infix to PostFix and Prefix (The Begining...)


thnaks Dave, i already looked into that page. It seems that i am stuck in how to do this using stack so i am trying to scan the infix (string) input create a dynamic array(string * postfix) then allocate it to the size of infix.length();
then i am not able to come up with anything it is like my brain is frozen.
so may be a couple of lines will help( i have to use stack). thanks forward
  #4  
Old 04-Dec-2008, 09:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Infix to PostFix and Prefix (The Begining...)


Quote:
Originally Posted by zatora
...i already looked... i am stuck in how to do this using stack so i am trying to scan the infix (string) input create a dynamic array(string * postfix) then allocate it to the size of infix.length();...

That page just looks at the input string a character at a time (operands are single alphabetic characters; operators are single characters like '+', '*'). It tells you what to do at each step: If it's an operand, just append it to an output string; if its an operator, either push it on the stack or append it to the output string, depending on the state of the stack and the precedence of the operator relative to the top of the stack. If you are going to use the STL stack class, no allocation is needed by the user program. If the output string is a std::string object, no allocation is needed by the user program. See Footnote.

Now, if your operands are not single alphabetic characters, a little more work may be needed to parse them out.

For example: what if the program is supposed to handle operands that are C-Language style identifiers as well as numeric constants? Maybe it's a lot more work.

But you didn't actually tell us what the program requirements are. My personal methodology is to specify exactly what a program is supposed to do before I start writing code...

So: I hate to repeat myself, but
Quote:
Originally Posted by davekw7x
My feeling is that the best use of this board is to try to get answers to specific questions. The more specific the question, the greater chance of a helpful response (usually).

Regards,

Dave

Footnote:
If that page didn't help you, what else did you look at? How about this one: Infix to postfix conversion algorithm

Now, this example uses some special classes (apstring and apclass), which some denizens of the educational community decided (for some godawful reason) would be better for teaching than sticking with the C++ standard library and STL classes. I don't get it, but...

This example does show how to use a stack for the operators in the conversion. I do not (that's not) recommend that you use the special apxxx classes, but you can use the code to see what can be done for a simple problem.

The example assumes that operands are single upper-case alpha characters and operators are '*', '/', '+', '-', with the usual precedence. Parentheses are handled so, with a suitable main() function some runs could look like:
Code:
Enter an infix expression: A+B*C Input : <A+B*C> Output: <ABC*+> Enter an infix expression: A*B+C Input : <A*B+C> Output: <AB*C+> Enter an infix expression: A*(B+C) Input : <A*(B+C)> Output: <ABC+*>

Note that a "serious" program should have some industrial-grade error checking and handling, but this example might get you started.

If you don't like this one, then look for some others.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
inFix -->PostFix and Prefix Algorithm zatora C++ Forum 4 23-Nov-2008 16:07
C calculator proximo C Programming Language 5 09-Apr-2005 10:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:49.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.