Results 1 to 8 of 8

Thread: Bodmas Maths Equation Generation...

  1. #1
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Bodmas Maths Equation Generation...

    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

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    What version of PHP are you using?

    When I put in (using PHP Version 5.2.8):

    PHP Code:
    <?php
    echo 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 Code:
    <?php
    echo + (3);
    ?>

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    http://www.php.net/manual/en/languag...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.
    Last edited by JasonDFR; 03-02-2009 at 05:38 PM.

  4. #4
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    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

    PHP Code:
    $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

  5. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    May not be exactly what you wanted, but I've made a generator. Just paste the code and test it out:

    PHP Code:
    <!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(23);
    $rand2 mt_rand(01);


    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

  6. #6
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    May not be exactly what you wanted, but I've made a generator. Just paste the code and test it out:

    PHP Code:
    <!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(23);
    $rand2 mt_rand(01);


    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

  7. #7
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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.

  8. #8
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Any further suggestions?
    Thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •