GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Nov-2006, 08:36
shoegoe shoegoe is offline
New Member
 
Join Date: Nov 2006
Posts: 4
shoegoe is on a distinguished road
Post

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  
Old 02-Nov-2006, 08:57
niek_e niek_e is offline
Junior Member
 
Join Date: Sep 2006
Posts: 39
niek_e is on a distinguished road

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


Allright, 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  
Old 02-Nov-2006, 10:53
shoegoe shoegoe is offline
New Member
 
Join Date: Nov 2006
Posts: 4
shoegoe is on a distinguished road

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


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  
Old 02-Nov-2006, 11:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by shoegoe
amm.... i was plannin to do like this

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

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  
Old 02-Nov-2006, 11:08
shoegoe shoegoe is offline
New Member
 
Join Date: Nov 2006
Posts: 4
shoegoe is on a distinguished road

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


Quote:
Originally Posted by davekw7x
Before being given the assignment, had anyone mentioned the phrase "postfix notation" or maybe "reverse Polish notation"?

Here's a reference:

www.spsu.edu


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

Nope......... Actually, this is given just after teaching basic file operations (). you can imagine how itll be ....

And Thx a lot.. am lookin into it .. Thx again ..
  #6  
Old 02-Nov-2006, 11:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by shoegoe
Nope......... Actually, this is given just after teaching basic file operations (

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  
Old 02-Nov-2006, 11:50
shoegoe shoegoe is offline
New Member
 
Join Date: Nov 2006
Posts: 4
shoegoe is on a distinguished road

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


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 Prelude
Quote:
cboard.cprogramming.com/showthread.php?p=605494&posted=1#post605494
@ gidforums ^_^ Helpin my studies .....ill try to figure it somehow tonight... But might the logic back there work?

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  
Old 02-Nov-2006, 15:14
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

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


There'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...
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 01:48.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.