d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help To Make An "easy Calculator" > C
Add Reply New Topic New Poll
Member
Posts: 1,678
Joined: Jul 30 2008
Gold: 63,915.00
Aug 29 2013 10:14am
I d like some tips before starting coding a calculator that can calculate expression such as

"3 + 52 * ( 1 - 3 / (3 + 7) - 1 % 21) + 19"

i need it to calculate with the five operators (+ - * / %) and respect the parentheses "()"

at the moment i don't have much clue so any help is welcome !
Member
Posts: 3,861
Joined: Aug 18 2004
Gold: 4,680.00
Aug 29 2013 11:30am
I am not sure the BEST way to go about this, but my suggestion would be to read it as a string, and then search the string for the order you want things to be done. After evaluating parenthesis, replace that part of the string with the answer. A recursive function to check through the order. Maybe later I'll break it down into a working example but a bit busy now.
Member
Posts: 1,678
Joined: Jul 30 2008
Gold: 63,915.00
Aug 29 2013 05:03pm
Quote (Snickerfritzer @ Aug 29 2013 05:30pm)
I am not sure the BEST way to go about this, but my suggestion would be to read it as a string, and then search the string for the order you want things to be done. After evaluating parenthesis, replace that part of the string with the answer. A recursive function to check through the order. Maybe later I'll break it down into a working example but a bit busy now.


thx for the advice , still hard to see how and where to start , but i m working on it !
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Aug 29 2013 05:58pm
just some random thoughts based on what Snickerfritzer said:

perhaps look for every occurrence of "(" and ")"
generate a depth "score" for each one.


depth = 0
every time you find a "(" add 1 to the depth score and record it for that occurance
every time you find a ")" subtract 1 from the depth score

find the "(" with the largest depth value, and evaluate everything you find until you reach the next ")". Remove the "(" and the ")" then rinse repeat.

This post was edited by Azrad on Aug 29 2013 05:58pm
Member
Posts: 1,678
Joined: Jul 30 2008
Gold: 63,915.00
Aug 30 2013 03:25am
Quote (Azrad @ Aug 29 2013 11:58pm)
just some random thoughts based on what Snickerfritzer said:

perhaps look for every occurrence of "(" and ")"
generate a depth "score" for each one.


depth = 0
every time you find a "(" add 1 to the depth score and record it for that occurance
every time you find a ")" subtract 1 from the depth score

find the "(" with the largest depth value, and evaluate everything you find until you reach the next ")". Remove the "(" and the ")" then rinse repeat.


that sounds like a good idea , i ll work on that to see if i can get anything from it
what is funny when we start c is that it takes 2days to do omething like that when an average developper need like 1h30min
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Member
Posts: 1,678
Joined: Jul 30 2008
Gold: 63,915.00
Aug 31 2013 04:47am
Quote (labatymo @ Aug 30 2013 04:00pm)


yes helps a lot to understand the algo, but it s in c++. I think i ll do it without taking the parentheses into consideration because it makes it really difficult for someone of my lvl

This post was edited by anezk on Aug 31 2013 04:49am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 31 2013 04:58am
Quote (anezk @ Aug 31 2013 06:47am)
yes helps a lot to understand the algo, but it s in c++. I think i ll do it without taking the parentheses into consideration because it makes it really difficult for someone of my lvl


the c++ code is only for postfix which isn't relevant anyway.
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Aug 31 2013 10:35am
Doesn't c++ already incorporate parentheses into ooo
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll