Jump to content

Solving equation through expression


shamuraq

Recommended Posts

Hi all,

 

I'm trying to programme a PHP script (i'm a newbie in need of a guidance) to generate random algebraic expression including the addition and subtraction symbol. After which the reflected randomized will also solve the problem.

 

Randomising part:

<tr>

                <td> 

                  <?

$x = rand(0,20);

$y = rand(0,20);

$z = rand(0,20);

$quote[] = "-";

$quote[] = "+";

$quotes[] = "-";

$quotes[] = "+";

$randomquote = rand(0, count($quote)-1);

$randomquotes = rand(0,count($quotes)-1);

 

//eg1

//2y-x+3=0

printf("$y y $quotes[$randomquotes] $x x $quote[$randomquote] $z = 0");

?>

          </td>

 

      </tr>

 

Solving the randomised equation (this is the problem):

 

<tr>

        <td> 

<?

$a= $y * ($quotes[$randomquotes]$x);

print("$a");

?></td>

      </tr>

 

I am successful with all the randomized result but i cannot get PHP to recognize that the randomized operator (addition and subtraction) are actually real operators not just some symbol.

Attached is the script for your perusal. Thank you...

Link to comment
https://forums.phpfreaks.com/topic/150948-solving-equation-through-expression/
Share on other sites

There are many ways of doing what you're explaining. The simplest is just to use eval, but I won't say whether or not using eval is a good thing, because there are strong feelings both for and against it.

 

If you want some other methods, google "parsing a string as a mathematical equation" (or math parser or mathematic expression evaluator, etc), it should give you a good starting point (though not necessarily a php one) if you wish to learn about how to avoid using eval and to help yourself answer the question whether it's worth all of the trouble to avoid the function.

If the equation you're generating is of the form 2y-x+3=0, and you want to calculate the values of x and y, then you'll need a little more than eval to do this.... you'll need to do a bit of mathematics.

 

Start by figuring out how to do it on paper, then (once you know the method) you can try implementing it in PHP.

 

  • 3 weeks later...

Yes genericnumber1, I already have the value of x and y (i already have the working solution base on this particular template). However being a newbie and a dumbwit i must admit, what is eval()? seems like it has its downside. I apologise for the late respond (internet intermittent problem at my location).

You could be using eval to evaluate the expression, using something like

 

$a= eval("return $y * {$quotes[$randomquotes]}$x;");

 

Personally I feel that is not very good practice, since using eval tends to lead into bad programming practices and often even into security holes in PHP code (when, for example, evaluating input that comes from the user).

 

A better way, in my opinion, to solve your problem would be to use something like a simple if clause, like

 

if ($quotes[$randomquotes] == '-')
{
$a = $y * -$x;
}
else
{
$a = $y * $x;
}

 

Much easier to understand in code and doesn't make the simple issue overly complicated. If you want to write it in a bit shorter form, you can use the ternary operator like:

 

$a = $y * ($quotes[$randomquotes] == '-' ? -$x : $x);

 

(it checks whether the expression left of '?' is true, and if so, it evaluates and returns the value before the ':' character, and otherwise it evaluates and returns the value right from the ':' character)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.