postfix and prefix

This is a discussion on postfix and prefix within the C++ Programming forums, part of the General Programming Boards category; Hello all wise gurus here at cprogamming dot com. I was in class today and we talked about conversion of ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    postfix and prefix

    Hello all wise gurus here at cprogamming dot com. I was in class today and we talked about conversion of something like this
    a*(b+c)+d to something like this abc+*d+. okay this made sense when we discussed it but when I actually tried it in a program I get compiler errors up the wazoo. Is there a library that has to be included with this or is this stuff a bunch of hoo haa. Perhaps somebody in here can explain too me or show me an example of this in a practical looking simple program

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    perhaps you could try it yourself and show us some code, or perhaps you could search these boards........or, perhaps you could search google. Perhaps, that is.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You will need an algorithm, and the algorithm may need to use a stack. You could use the one from standard template library
    Code:
    #include <stack>
    using namespace std;
    
    int main() {
        stack<int> myStack;
    
        // read the operand and operator one by one
        // and print, push, or pop according to the algorithm
    
        return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Question Was the topic of discussion 'prefix & postfix' ?

    Because, AFAIK prefix and postfix only apply to the ++ and -- operators (increment & decrement).

    These are the unary math operators, as they operate on only one variable.

    And this: abc+*d+ is going to give you errors for at least 3 reasons:
    - It's looking for a variable named abc.
    - You can't have +* without a variable in between.
    - d+ doesn't mean anything.
    Last edited by DougDbug; 09-23-2004 at 06:41 PM.

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by DougDbug
    Because, AFAIK prefix and postfix only apply to the ++ and -- operators (increment & decrement).

    ....
    The OP was, I believe, talking about different ways you can write/read mathematical expression.
    So "abc+*d+" will read...
    - read a and push to stack
    - read b and push to stack
    - read c and push to stack
    - read +, pop two operands (b & c) and apply the operation, push the result (x) to stack
    - read *, pop two operands (x & a) and apply the operation, push the result (y) to stack
    - read d and push to stack
    - read -, pop two operands (y & d) and apply the operation, push the result (FINAL RESULT) to stack, and pop it
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    alphaoide you da bomb

    alphaoide you da bomb I think that is exactly whut dude was asking for I did manage to do some raw dogg data manipulation check it out.
    Code:
    #include<iostream>
    using namespace std;
    template <class T, int n>
    class stack
    {
     private: T elt[n];
          int counter;
     public:
      void clearstack()
      {
       counter=-1;
      }
      bool emptystack()
      {
       return counter==-1?true:false;
      }
      bool fullstack()
      {
       return counter==n - 1?true:false;
      }
      void push(T x)
      {
       counter++;
       elt[counter]=x;
      }
      T pop()
      {
       T x;
       x=elt[counter];
       counter--;
       return x;
      }
     
    };
    void main()
    {
     stack<float, 4> s;
     stack<float, 4> t;
     s.clearstack();
     while(! s.fullstack() )
     {
      float data;
      cout<<"Enter an integer data: ";
      cin >>data;
      s.push(data);
     }
    int a,b,c,d;
    d=s.pop();
    cout<<d<<endl;
    c=s.pop();
    cout<<c<<endl;
    b=s.pop();
    cout<<b<<endl;
    b= b + c;
    a=s.pop();
    a=a*b;
    a=a+d;
    s.push(a);
    cout << "The total is: " << a << endl;
    }
    so that was the plug and punch method. Any suggestions

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Hello all wise gurus here at cprogamming dot com. I was in class today and we talked about conversion of something like this
    a*(b+c)+d to something like this abc+*d+. okay this made sense when we discussed it but when I actually tried it in a program I get compiler errors up the wazoo. Is there a library that has to be included with this or is this stuff a bunch of hoo haa. Perhaps somebody in here can explain too me or show me an example of this in a practical looking simple program
    Maybe, depends on how the prefix, postfix is done. One way is to write a small grammar for the infix expression with attributes pre and post denoting the prefix and postfix string, respectively. This method is more work than necessary, but is he most straightward way if you are famaliar with it.

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> so that was the plug and punch method. Any suggestions
    Well your program only solves one problem: given a, b, c, and d, what is abc+*d+ ??
    You should make your program do something like this:
    For any given postfix expression,
    1. when you see an operand, push
    2. when you see an operator, pop two operands and apply the operation, push the result
    3. when there's only one element in the stack, that is the final result

    I believe that will solve any postfix expression
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by drdodirty2002
    template <class T, int n>
    class stack
    This is forbidden in C++
    Use the built-in std::stack located in the header <stack>.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little question about postfix and prefix
    By firebird in forum C Programming
    Replies: 3
    Last Post: 05-08-2008, 07:27 AM
  2. Is prefix faster than postfix?
    By dwks in forum C Programming
    Replies: 6
    Last Post: 07-28-2005, 11:51 AM
  3. stacks postfix aww fooey
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2004, 10:00 AM
  4. postfix and prefix...???
    By matheo917 in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2002, 01:19 PM
  5. Data Structures / prefix and postfix
    By rickc77 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-08-2001, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21