View Full Version : Bodmas Maths Equation Generation...
clowes
03-02-2009, 02:57 PM
Hello,
I am trying to make a script which generates a sum with between 2, and 4 numbers using the operators +,-,*, and / (although I may want to expand this is the future.
I essentially want the script to generate a random sum, and its answer.
Examples would be:
$sum='2 * 4 + 3';
$anser='11';
The problem I am having is with BODMAS: the process by which dividion is done before multiplication, then addition, then subtraction.
What I have at the moment outputs
2 + 5 * 3 = 21
whereas it should output 2 + (5 * 3) = 17
How can I make it generate sums taking into consideration BODMAS, and how can I add the respective brackets?
Thanks
Schmoopy
03-02-2009, 03:16 PM
What version of PHP are you using?
When I put in (using PHP Version 5.2.8):
<?php
echo 2 + 5 * 3;
?>
I get 17, it takes into account BODMAS, not sure why yours isn't =/
But anyway, the solution is very simple - you just put the brackets around the numbers you want to be calculated first:
<?php
echo 2 + (5 * 3);
?>
JasonDFR
03-02-2009, 05:23 PM
http://www.php.net/manual/en/language.operators.precedence.php
As far as I know, PHP has always followed the normal order of operations.
PHP may differ from BODMAS in that Multiplication and Division are done left to right to determine which operation is done first. They both have equal precedence. If the multiplication comes first when reading left to right, it is done first.
If you absolutely must have division evaluated before multiplication, there is a way to do it ( anything is possible), but off the top of my head, I don't know.
The example you give in your post is wrong. It correctly evaluates to 17 like Schmoopy said.
If you are still having problems, post your code.
clowes
03-02-2009, 06:29 PM
Sorry, Ive been an idiot. I never actually tried, I simply assumed it would not work. It is not all happy days however.
In the case of the example
$answer=2+5*3; //17
All of the constituents are generated, so I want it to pick a random number between 1 and 9 3 times, then pick 2 random operators to make my question.
I want it to then put brackets where neccesary...
For example if i put 2+5*3 into google it displays 2 + (5*3)..
How would I go about putting brackets into the uestion?
Thanks
Schmoopy
03-02-2009, 07:22 PM
May not be exactly what you wanted, but I've made a generator. Just paste the code and test it out:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Maths Generator</title>
<?php
$gen1 = mt_rand(1,9);
$gen2 = mt_rand(1,9);
$gen3 = mt_rand(1,9);
$operators = array("+", "-", "/", "*");
$rand = mt_rand(2, 3);
$rand2 = mt_rand(0, 1);
echo $gen1 . " " . $operators[$rand] . " " . $gen2 . " " . $operators[$rand2] . " " . $gen3 . "<br />";
switch ($operators[$rand])
{
case "*":
$first = $gen1 * $gen2;
break;
case "/":
$first = $gen1 / $gen2;
break;
}
switch ($operators[$rand2])
{
case "+":
$second = $first + $gen3;
break;
case "-":
$second = $first - $gen3;
break;
}
$answer = $second;
echo $answer;
?>
</body>
</html>
As you can see, the first two operators are higher in BODMAS, so you never have the problem of having the sum done in the wrong way.
Doing it another way would be a bit more complicated, hope this helps :)
clowes
03-02-2009, 07:56 PM
May not be exactly what you wanted, but I've made a generator. Just paste the code and test it out:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Maths Generator</title>
<?php
$gen1 = mt_rand(1,9);
$gen2 = mt_rand(1,9);
$gen3 = mt_rand(1,9);
$operators = array("+", "-", "/", "*");
$rand = mt_rand(2, 3);
$rand2 = mt_rand(0, 1);
echo $gen1 . " " . $operators[$rand] . " " . $gen2 . " " . $operators[$rand2] . " " . $gen3 . "<br />";
switch ($operators[$rand])
{
case "*":
$first = $gen1 * $gen2;
break;
case "/":
$first = $gen1 / $gen2;
break;
}
switch ($operators[$rand2])
{
case "+":
$second = $first + $gen3;
break;
case "-":
$second = $first - $gen3;
break;
}
$answer = $second;
echo $answer;
?>
</body>
</html>
As you can see, the first two operators are higher in BODMAS, so you never have the problem of having the sum done in the wrong way.
Doing it another way would be a bit more complicated, hope this helps :)
Many Thanks for the reply/taking the effort to put pen to paper.
At first I was a little confused, but now I see what you have done.
What I ideally want to do is have it so it is completely random... you could have 2 * operators or 2 + operators...
You could get
3*7*4
3*7+4 etc
In the two cases what i would like is to have it such that they are written on the page
3*7*4 and (3*7)+4 respectively. I.E putting the multiplication/division in brackets where neccesary.
I then wanted to expand it so the number of digit/operators was also random so you could get an output such as
(1 * 3) + ((5 * 6) / 4) + 5 - 21 - (3 * 9) = -32.5
An example of what I want can be seen on google.. if you type in '1*3+5*6/4+5-21-3*9' it is formatted as above.
Hope that makes sense.
Thanks
Schmoopy
03-02-2009, 08:07 PM
Yes, I can see what you mean, it's just doing it that way there will be a lot of variables, of course it's possible but it would take more time. Let's see what the others come up with.
clowes
03-04-2009, 02:29 PM
Any further suggestions?
Thanks
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.