Thread: How to print an equation in order of precedence of operators

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    7

    How to print an equation in order of precedence of operators

    Hi! ..My First Post

    Am... I need a logic and/or some kind of help to code a c program which accepts a numeric equation and prints it step by step in order of precedence..

    Eg:

    the c program should accept any equation like : 5+(5-3)*6%10/6++ ie the equation may cointain +,-,*,/,%,++,-- and parantheses(any number of groups like (5(5(6(5(6)%10)))(5) etc with numbers( say integers)..

    so, it should accept the equation and print it like this

    Given Equation : 5+(5-3)*6%10/6++

    Solution with precedence:

    5+2*6%10/6++
    5+3*6%10/7
    5+18%10/7
    5+18%1
    5+0
    5


    the equation can be of any size...

    ANY help is greatly appreciatied.. Please!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You'll essentially be writing the lion's share of an expression parser. The first step in your research should be expression trees for parsing infix notation. But be aware that this is not a trivial project since you're working with the general case (even though the number of operators are minimal).
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    7

    Post

    amm.... i was plannin to do like this

    get equation as a string

    check if proper characters are used, and equal open and close paranthesis

    use a loop to send the string to a function say checkequation(), till the loop has a single number, in the loop print
    the string each time the function is returned

    in checkequation(),

    get the inner most parantheses' indexes,
    say
    a= index of innermost '('
    b=index of innermost ')'.
    if no paranthesis is got send a=0, b= index of last char

    probably the above is another function checkinnerparanthesis() and returns a and b


    [

    i need to know how to get the innermost paranthesis?...i could check for the last '(' and get the index of next ')' as a and b.... but lets take this case


    ((5+4)*(5+3))*(9+3)
    so, if i check for the last paranthesis, i would get ( and ) in 9+3... but should i need ( and ) in 5+4 as paranthesis is from left to right?...

    ]

    lets say the function returns a and b as planned...

    now send the string from index a to b to another function evaluateequation ()

    in the function evaluateequation()

    the input string has no brackets . hence we have to search for post++,post--,pre++(R-L),pre--(R-L),*,/,%,+,- in order.. if a single occurance of one in order is got, get the strings(string1,string2) on both the sides(or in one side for ++,--) till the next operator,convert both strings to integers with atoi, do the correspondin operation,and store the result in the same string in the .....string1"operator"string2.... place, shift the rest characters and arrange the string.. thus we get .....result...... in the original string..

    Also , if the ..result.. has ( and ) on either side, it is removed as the ( and ) are no longer necessory

    return to function checkequation()

    from checkequation(), it is returned to main(), where in the loop the string gets printed. But, since the string is not having a single char, the same process continues. when a single value(ie with no operators is got) exit the program



    its just a rough logic i had.. .Please Help me out here. . .this is kinda VERY important and timed to be finished within 2 days ( this sunday max). . . Please help me out..

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >amm.... i was plannin to do like this
    Did you even attempt to do the research I suggested? If you only have two days, then floundering about with half-assed attempts isn't going to be productive. The easiest solution that does what you want is an expression tree. No amount of hacking and kludging will change that.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Quote Originally Posted by Prelude
    >amm.... i was plannin to do like this
    Did you even attempt to do the research I suggested? If you only have two days, then floundering about with half-assed attempts isn't going to be productive. The easiest solution that does what you want is an expression tree. No amount of hacking and kludging will change that.

    sorry ......but to know my condition...this proj was given just 2 days ago, supposed to be the easiest, ((PS.we've just entered dealing with files))....

    Thx a lot...ill go through your suggestion right away.. Still, i dont know what u meant..but ill surely get through it tonight ..( problems of a raw newbie right?)

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Quote Originally Posted by shoegoe
    sorry ......but to know my condition...this proj was given just 2 days ago, supposed to be the easiest, ((PS.we've just entered dealing with files))....

    Thx a lot...ill go through your suggestion right away.. Still, i dont know what u meant..but ill surely get through it tonight ..( problems of a raw newbie right?)
    Well, I think you would get much more enthusiastic help from everyone if you posted some code, that is, if you have coded anything at all. This board is not meant to do your homework for you.

    Also, using the excuse that the project was given 2 days ago does not fly either. That means you have had 2 days to at least attempt to code it before coming here. In the professional world of programming last minute projects due as of yesterday is not out of the norm.

    Sam

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >supposed to be the easiest, ((PS.we've just entered dealing with files))....
    I wouldn't describe this project as "easy" for a beginner. It involves quite a few concepts that are beyond a lot of professional programmers, like recursion and binary trees, parsing and lexing, and intermediate string handling. p.s. Curriculums vary wildly in where they place file I/O, so you could be further along than it appears.

    >this proj was given just 2 days ago
    Tight deadlines are a part of the business. You learn to live with them (either by being a slacker with a golden tongue, or by excelling at the craft) or find another field.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Quote Originally Posted by Prelude
    >supposed to be the easiest, ((PS.we've just entered dealing with files))....
    I wouldn't describe this project as "easy" for a beginner. It involves quite a few concepts that are beyond a lot of professional programmers, like recursion and binary trees, parsing and lexing, and intermediate string handling. p.s. Curriculums vary wildly in where they place file I/O, so you could be further along than it appears.

    >this proj was given just 2 days ago
    Tight deadlines are a part of the business. You learn to live with them (either by being a slacker with a golden tongue, or by excelling at the craft) or find another field.

    Ure just like davekw7x Click Here @ gidforums ^_^ Helpin my studies .....ill try to figure it somehow tonight... But might the logic back there work?


    ps..pls tell me if outside forum links arent allowed...if so.. id remove the link ^_^

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How long ago it was given isn't important.
    How long you've got left however is.

    If it's a week long assignment, then there's still plenty of time to do some research on the subject (at least 2 days worth).

    Plan a little, don't just dive into the code and start hacking away at code 5 minutes after you're given the assignment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But might the logic back there work?
    You could force it to work. Of course, you can force a lot of really bad solutions to work, so that can't be used as a measure of quality.

    >Ure just like davekw7x
    Not really. davekw7x doesn't seem to grasp that postfix notation and a stack based approach would be awkward in this case since you need to preserve the infix expression to show each step of the evaluation. That's why I didn't suggest it. I also don't think you should be left out in the cold to figure things out on your own. The best foundation for invention is a strong understanding of existing solutions (or to be more precise, existing techniques for creating a solution).
    My best code is written with the delete key.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Geez, how many boards have you spammed with your homework?
    I count 3 already, and that's not including the reference to gidforums.

    Maybe I should post conflicting answers on all of them and let you figure it out
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Sorry Salem.,, was just frustated to know that i was the ONLY one to get this type of program. We've started c just 3 months back in col( we're just @ How to open a Text file ) ...if the program was within this limit, id have done it(ive done for most of programs for ,my friends others like Office management, mirror image, calender, etc etc)..they were ok (To this level.)

    I didnt know to use a stack or wats a stack till yesterday.. ive just understood it(from infix to prefix) and going to binary trees (for prefix to infix). . All my col mates learns Datastructures only in their second years..im on my first and that too only 3 months in c. . if the program given was within my knowledge limit, id have glady done it...I was somewhat sad to get such a program..

    Wouldnt u be?..

    tats what happened yesterday.. And that too ive asked for hints/logic to get through, not the code.... i didnt know where to begin .. i thought of a logic and also gave it in my thread.. Since i faced many problems, i wasnt able to do it

    Homework Policy:

    The purpose of these board is not for other people to do your homework for you. Try things out work on your own, homework has a purpose. If you still have trouble with a specific piece of code or concept please feel free to ask. But please do not ask people to do your entire homework for you, it simply annoys people most of the time.
    I read it and thought well before asking.. Please understand my situation that time..


    And amm........Sorry to Annoy(if) you again......

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if the program given was within my knowledge limit, id have glady done it...I was somewhat sad to get such a program..
    If you know how to solve every problem, you're not learning anything. The point of college isn't to do make-work that you already know how to do, it's to extend your knowledge base and teach you how to teach yourself.

    >And that too ive asked for hints/logic to get through
    Hints and logic were happily provided to you. You seemed to ignore them and force your own ineffective solution. If we seem annoyed, it's because we generally take that as a sign that you're not interested in learning.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    My apologies again... It was just that i didnt know what a prefix was.. .sorry again...

    Now i understand a bit...

    But.. since i want to print my equation step by step in infix form, do i have to convert to prefix, do 1 operation, convert back to infix, print it, then go back to prefix, do operation, and so on?

    isnt there any other way.. am on my way learnin binary tree to convert prefix/postfix to infix, but can both be done in binary tree without stack?

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    and infix- prefix works on single operators,, but what to do about ++/-- in pre or post.. i thought of sub $ for ++ and ^ for -- and use it as operators and put a special case.. . will this be ok?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print Array in reverse order
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 01:41 PM
  2. order of precedence
    By the bassinvader in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 05:11 PM
  3. i want to print my linked list in the reverse order please help
    By raghu_equinox in forum C Programming
    Replies: 9
    Last Post: 10-14-2006, 12:45 AM
  4. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 06:26 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM