Thread: A doubt about changing notation

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    66

    Question A doubt about changing notation

    Hello, I have been trying to do aschool project where I must change from a kind of notation to another and solve the wanted operation, it must do what supostly is done by the compiler, it should change the notation, like this:
    4 + 4
    to
    4 4 +

    and it should work for all operative signs(+ - * / %)

    but keeping the importance of the operations, also it should return the correct results of course
    here's another example
    (6 + 2) * 5 - 8 / 4 should change to

    6 2 + 5 * 8 4 / -

    please, it is urgent, any idea will be extremely apreciated
    thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Consider using a stack.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    I'd use a Binary Tree adapted to math expressions, since all operators are binary.

    EDIT:
    Example:
    Code:
    Expression: 1*2+3/4
    Tree:
        +  
       / \
      *   '/'
    /  \ /  \
    1  2 3  4
    
    Expression: 1*2*3+4
    Tree:
          4
         /
        *
       / \
      *   3
     / \
    1   2
    Then to print in prefix, in each node print the operator then the two sub-nodes (left and right). To print infix print the left subnode, the operator then the right node. To print postfix print the subnodes then the operator. If you want to use parenthesis, in each node print the opening one, them the expression, them close them.
    These rules for writing apply both when subnodes have operator or arithmetic values.
    And, the lower the node, the higher the operator precedence.
    Last edited by xErath; 11-21-2004 at 09:35 PM.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    The problem is I donŽt know how to use a binary tree,
    or a stack

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Read my 1st post. You don't know how to use the tree so here's an example. A tree is nothing more than a bunch of nodes connected together.
    Code:
    //done on the fly, in the post editor
    #define NODETYPE_OPERATOR 1
    #define NODETYPE_NUMBER 2
    struct Node{
        int value;//interger, or ascii value of operator
        int type;
        Node *left;
        Node *right;
    }
    
    //to print, in infix
    void print(Node *n){
        if(n->type==NODETYPE_OPERATOR){
            print(n->left);
            //print operator your way
            print(n->right);
        }else if(n->type== NODETYPE_NUMBER){
            //print number your way
        }        
    }
    Last edited by xErath; 11-21-2004 at 09:36 PM.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    ok, iŽll try that thx
    if anyone comes with another thing i would be glad to try it also

    thank you

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What you are looking for is converting infix to postfix. There is a good walkthrough on how to do this at http://www.qiksearch.com/articles/cs/infix-postfix/

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    Thank you, I am checking it right now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. CamelCase VS Hungarian notation, which is better?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2007, 09:31 PM
  3. I need help with RPN notation!!!
    By schnoor22 in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 05:12 PM
  4. Replies: 10
    Last Post: 11-06-2005, 09:29 PM
  5. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM