Math expression reducing algorithm

Discussion in 'C' started by ciuchi, Aug 30, 2007.

  1. ciuchi

    ciuchi New Member

    Joined:
    Aug 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I have been searching for a solution to this problem:
    I have an expression in infix which can contain *,/,-,+ and I have to find the reduced form
    of this expression.

    Ex: Input: ((A+45)*16+(B-C)*D/4 )/8+ 55
    Output: A*2+B*D/32-C*D/32 +145

    All that can be calculated must be calculated and the simplest form must be returned.
    A,B,C,D do NOT have numeric values. They are name of variables(strings).
    The expressions that have to be calculated can be more complicated.
    I have found on the net algorithms that only evaluate expressions that have numerical values in them(or variables that are replaced by numerical values).

    What I have done: I have translated the expression into expression tree form and now I'm a bit lost . Maybe I should have gone a different path ?

    Can you help?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. ciuchi

    ciuchi New Member

    Joined:
    Aug 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I'm sorry if I'm missing anything but your article was about evaluating an expression.
    You give each of the variables a numerical value.
    I don't want that.
    My program has to output the expression in a simpler form with all possible calculations being made. But I cannot replace the name of a variable with an value.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Thats the second part of it. First part is what you are looking for.
     
  5. ciuchi

    ciuchi New Member

    Joined:
    Aug 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    By "first part" do you mean the one that transfoorms infix to postfix ?
    Or what ? Can you copy here please exactly the part of code that you mean, cause I can't find it.

    Thanks
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Above the heading "Post Fix Expression Evaluation" is the infix to postfix conversion explanation with detail algo.
     
  7. ciuchi

    ciuchi New Member

    Joined:
    Aug 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I think you don't understand what I need. I already have the expression converted into postfix and then into expression tree.
    Your code seems to be just the algo for converting to postfix and then evaluating that.

    I have found a solution that seemms to be what I need which involves a polynomial class with all the operations.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    But as I understand after reading you need to convert the expression but anyways as you have the solution that is good.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice