How to print an equation in order of precedence of operators

Status
Not open for further replies.

shoegoe

Broken In
Hey all ^_^

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!



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=


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..

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 

sakumar79

Technomancer
First, find the first ), then the corresponding (, this will give first innermost bracket.Whatever is within the bracket needs to be processed first. Send the items (Str1) before the (, the items (Str2) with the brackets and the items (Str3) after the ) to a function for processing items with (). Here, filter internal equations with *,/, etc one after the other and at each stage, print the processed Str2 with Str1 before and Str3 after...

Repeat this until you run out of )

Note that you may need to check syntax errors such as mismatching brackets, starting with operators, etc... but you can even add this check separately at the end and just give a link to the function that runs the check... This will be a tricky thing because there are so many possible errors, it is hard to identify each one of them...

Arun
 
Status
Not open for further replies.
Top Bottom