![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
How to print an equation in order of precedence of operatorsHi! ..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
|
|||
|
|||
Re: How to print an equation in order of precedence of operatorsAllright, let's say you put your input in a chararray[]
Then you can use a loop to determen how many brackets är in your equation. Then you look for the last openingbracket ( ( ) with a loop, and start your way calculating back from there. Next you will have to look if the char between the brackets are operators or numbers. If they are numbers the can easily be converted ( int 0 = char 0 - 48 ). Hope this helps you on your way. Regards Niek |
|
#3
|
|||
|
|||
Re: How to print an equation in order of precedence of operatorsamm.... 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
|
|||
|
|||
Re: How to print an equation in order of precedence of operatorsQuote:
Before being given the assignment, had anyone mentioned the phrase "postfix notation" or maybe "reverse Polish notation"? Here's a reference: Who the heck was this Łukasiewicz guy, anyhow? Here's the first sentence in the article on the link: "Every student of computer science should understand the concept of postfix notation and the use of stacks. " Regards, Dave |
|
#5
|
|||
|
|||
Re: How to print an equation in order of precedence of operatorsQuote:
Nope......... Actually, this is given just after teaching basic file operations ( And Thx a lot.. am lookin into it .. Thx again .. |
|
#6
|
|||
|
|||
Re: How to print an equation in order of precedence of operatorsQuote:
Sometimes it's better to try to figure it out on your own, without seeing a "neat" way to do things (that compiler designers have been using since the middle of the 20th century). Many times, thinking about things on your own gets you into a proper frame of mind to accept instruction on actual, practical, methods, and I wouldn't want to interfere with the learning process (if that's what the instructor had in mind when he/she made the assignment). I just thought that the animated lecture was kind of cool. Regards, Dave |
|
#7
|
|||
|
|||
Re: How to print an equation in order of precedence of operatorsQuote:
Ure just like Prelude Quote:
Sorry again if i annoy u both refering back ... ill try it.. ps..pls tell me if outside forum links arent allowed...if so.. id remove the link ^_^ |
|
#8
|
||||
|
||||
Re: How to print an equation in order of precedence of operatorsThere's a reason Dave and Prelude think alike -- they are experienced and know what they are talking about. Prelude is correct, to a point. It noes not necessarily require recursion and binary trees. I did this in ForTran which has no recursion, and binary trees can be used but also not necessary. Read up on Dave's link. It seems to be pretty good.
And you are not annoying anyone. If anything it's your instructor -- giving a project that you may not be ready for. By the way, be sure to read the Guidelines so you don't annoy anyone later... __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
Developing GUIs with wxPython (Part 4) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Fortran problem... | Justin Fox | Miscellaneous Programming Forum | 6 | 24-Oct-2006 16:30 |
| The evaluation of if statement. | kobi_hikri | C Programming Language | 8 | 19-May-2005 04:27 |
| Two virtual hosts, cgi script behaves differently on each | blimbo | Apache Web Server Forum | 0 | 04-Aug-2004 10:35 |
| Guestbook error | BobbyDouglas | Web Design Forum | 1 | 16-Oct-2003 23:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The