PDA

View Full Version : Solving Quadratics with PHP



Moonbat
07-08-2008, 11:06 AM
Well, seeing how the new school year (for me) is approaching, I'm bored, and I suck at math. So I'm just gonna write up a few things in PHP to solve certain math problems for myself. Here is a simple quadratic equation solver.


<?php
/****************************
* PHP Quadratic *
* Equation Solver *
* Written By Moonbat *
* July 7, 2008 *
****************************/

if (isset($_POST['quadratica']) && isset($_POST['quadraticb']) && isset($_POST['quadraticc'])) {
$a = $_POST['quadratica'];
$b = $_POST['quadraticb'];
$c = $_POST['quadraticc'];
function SolveQuadratic($a, $b, $c)
{
echo "You provided: <b>a = $a | b = $b | c = $c </b><br><br>";
$twoa = 2 * $a;
$negb = 0 - $b;
$discriminant = ($b * $b) - (4 * $a * $c);
$sqrtdiscriminant = sqrt($discriminant);
$finalanswerone = ($negb + $sqrtdiscriminant) / $twoa;
$finalanswertwo = ($negb - $sqrtdiscriminant) / $twoa;

echo "Solution #* (adding then dividing): ";
echo "<font color=\"red\"><b>$finalanswerone</b></font>";
echo "<br>";
echo "Solution #2 (subtracting then dividing): ";
echo "<font color=\"red\"><b>$finalanswertwo</b></font>";
}
SolveQuadratic($a, $b, $c);
echo "<hr>";
}

$htmlone = "<html>
<head><title>Quadratic Equation Solver/title></head>
<body>
<center><h2>Quadratic Equation Solver</h2></center>
<center>This is a handy script to solve quadratic equations<br>
Use the subtraction sign (-) to indicate a number is negative<br>
If NAN is given as a solution, it indicates that the solution couldn't be expressed as a number</center>
<br>
<center>
<b>Values</b>
<form name=\"quadraticform\" method=\"POST\" action=\"$PHP_SELF\">
a: <input type=\"text\" name=\"quadratica\"><br>
b: <input type=\"text\" name=\"quadraticb\"><br>
c: <input type=\"text\" name=\"quadraticc\"><br><br>
<input type=\"Submit\" value=\"Submit\">
</form></center>
</body>
</html>";
echo $htmlone;
?>

Moonbat
07-12-2008, 01:27 PM
How do you get a plus&minus sign to work? Too bad PHP doesn't have anything like that (as far as I know). :(

And it's better to be a Python fanboy than a Java fanboy.