Google Calculator in PHP?
Moderator: General Moderators
Google Calculator in PHP?
If any of you haven't used Google's calculator go to google and type in something like:
2 + 2 * 3
You will not only get the result, but, google will also rewrite your question inserting parenthesis to show the order of operations. So the previous problem would be re-written as follows:
2 + (2 * 3)
Would anyone be willing to provide an example of how something like this would be done? Pseudocode would be great, I just want to figure out the logic behind something like that, I asume you would use some sort of REGX but not 100% sure.
2 + 2 * 3
You will not only get the result, but, google will also rewrite your question inserting parenthesis to show the order of operations. So the previous problem would be re-written as follows:
2 + (2 * 3)
Would anyone be willing to provide an example of how something like this would be done? Pseudocode would be great, I just want to figure out the logic behind something like that, I asume you would use some sort of REGX but not 100% sure.
you're looking for an infix algebra parser.
http://www.math.wpi.edu/IQP/BVCalcHist/calc5.html might be a good start
http://www.math.wpi.edu/IQP/BVCalcHist/calc5.html might be a good start
Google calculator also has some easter eggs. Check this out 
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Have to disagree totally, as volka says multiplication and division should always happen before addition and subtraction. Mathematically the Google calculator is therefore correct in what it is doing.Gen-ik wrote:2 + 2 * 3 should be seen as (2 + 2) * 3 not, as Google puts it, 2 + (2 * 3)
From a calculators point of you if you type in 2 + 2 * 3 it will give you 12... which is (2 + 2) * 3....... so Google ain't that smart
Mac
-
Cruzado_Mainfrm
- Forum Contributor
- Posts: 346
- Joined: Sun Jun 15, 2003 11:22 pm
- Location: Miami, FL
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
or...
BEDMAS
Brackets
Exponents
Division/multiplication
Addition/subtration
I remeber my gr 7 (long time ago, but still) teacher used to follow this to a "T". He thought that in any given mathematical equation, division must be done before multiplication and addition before subtraction. So to him 9*2/3 is 9(2/3) rather than (9*2)/3.
BEDMAS
Brackets
Exponents
Division/multiplication
Addition/subtration
I remeber my gr 7 (long time ago, but still) teacher used to follow this to a "T". He thought that in any given mathematical equation, division must be done before multiplication and addition before subtraction. So to him 9*2/3 is 9(2/3) rather than (9*2)/3.