Discord
Steam
/
Developers
/
Issue with Qua..
Login/Join
Event Log
Issue with Quadratic Equation Solver Script - PHP Beginner
2 replies, posted
Search
In This Thread
I've just started with PHP (literally, a few days ago) and I'm trying to make a script that allows people to enter in quadratic equations (you know the ones, AX^2 + BX + C = 0) and solve them. I know the equation to solve them. This is my attempt so far: [code] <html> <head> </head> <body> <?php function convert($A, $B, $C_In, $D) { $C = $C_In - $D; if ($A < 0) { $Astate = "-"; } else if ($A = "") { $fail = "1"; } else { $Astate = ""; } if ($B < 0) { $Bstate = "-"; } elseif ($B = "") { $fail = "1"; } else { $Bstate = "+"; } if ($C_In < 0) { $Cstate = "-"; } elseif ($C_In = "") { $fail = "1"; } else { $Cstate = "+"; } if ($D < 0) { $Dstate = "-"; } elseif ($D = "") { $fail = "1"; } else { $Dstate = "+"; } if ($fail != "1") { $X1 = ((0 - $B) + sqrt(($B * $B) - (4 * $A * $C))) / (2 * $A); $X2 = ((0 - $B) - sqrt(($B * $B) - (4 * $A * $C))) / (2 * $A); $X1Final = 0 - $X1; $X2Final = 0 - $X2; echo "<Center><B>".$Astate.$A."X<sup>2</sup> ".$Bstate.$B."X ".$Cstate.$C." = ".$Dstate.$D."</b>: X = <b>".$X1Final."</b> and <b>".$X2Final."</b></center>"; echo "<br />"; } } if(!isset($_POST['submit'])) { ?> <center><b>A</b>x<sup>2</sup> + <b>B</b>x + <b>C</b> = <b>D</b></center><br> <center> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> A:<input type="text" name="A1" size="2" /> B:<input type="text" name="B1" size="2" /> C:<input type="text" name="C1" size="2" /> D:<input type="text" name="D1" size="2" /><br /> A:<input type="text" name="A2" size="2" /> B:<input type="text" name="B2" size="2" /> C:<input type="text" name="C2" size="2" /> D:<input type="text" name="D2" size="2" /><br /> A:<input type="text" name="A3" size="2" /> B:<input type="text" name="B3" size="2" /> C:<input type="text" name="C3" size="2" /> D:<input type="text" name="D3" size="2" /><br /> A:<input type="text" name="A4" size="2" /> B:<input type="text" name="B4" size="2" /> C:<input type="text" name="C4" size="2" /> D:<input type="text" name="D4" size="2" /><br /> A:<input type="text" name="A5" size="2" /> B:<input type="text" name="B5" size="2" /> C:<input type="text" name="C5" size="2" /> D:<input type="text" name="D5" size="2" /><br /> A:<input type="text" name="A6" size="2" /> B:<input type="text" name="B6" size="2" /> C:<input type="text" name="C6" size="2" /> D:<input type="text" name="D6" size="2" /><br /> A:<input type="text" name="A7" size="2" /> B:<input type="text" name="B7" size="2" /> C:<input type="text" name="C7" size="2" /> D:<input type="text" name="D7" size="2" /><br /> A:<input type="text" name="A8" size="2" /> B:<input type="text" name="B8" size="2" /> C:<input type="text" name="C8" size="2" /> D:<input type="text" name="D8" size="2" /><br /> A:<input type="text" name="A9" size="2" /> B:<input type="text" name="B9" size="2" /> C:<input type="text" name="C9" size="2" /> D:<input type="text" name="D9" size="2" /><br /> A:<input type="text" name="A10" size="2" /> B:<input type="text" name="B10" size="2" /> C:<input type="text" name="C10" size="2" /> D:<input type="text" name="D10" size="2" /><br /> <input type="submit" name="submit" value="Submit" /> </form> </center> <?php } else { for ($x = 1; $x <= 10; $x++) { convert(($_POST['A'.$x]), ($_POST['B'.$x]), ($_POST['C'.$x]), ($_POST['D'.$x])); } } ?> </body> </html> [/code] However, that returns with a "division by 0" error. I know it could be a really silly mistake, but PHP is pretty much my first language (aside from some HTML), hence why the script could be a lot more compact. So, any really simple errors? Thanks for your time :)
[code] <html> <head> </head> <body> <?php function convert($A, $B, $C, $D = 0) { $C -= $D; $Discriminant = $B * $B - 4 * $A * $C; $Zeros = 2; if ($Discriminant == 0) $Zeros = 1; else if ($Discriminant < 0) $Zeros = 0; if ($Zeros <= 0) { echo "<strong>{$A}x<sup>2</sup> + {$B}x + $C = $D</strong> has no solution.<br />"; return; } $X1 = ($B*-1 + sqrt($Discriminant)) / (2 * $A); $X2 = ($B*-1 - sqrt($Discriminant)) / (2 * $A); if ($Zeros == 1) { echo "<strong>{$A}x<sup>2</sup> + {$B}x + $C = $D</strong>: x = <strong>$X1</strong><br />"; } else { echo "<strong>{$A}x<sup>2</sup> + {$B}x + $C = $D</strong>: x = <strong>$X1</strong> and <strong>$X2</strong<br />"; } } if(!isset($_POST['submit'])) { ?> <center><b>A</b>x<sup>2</sup> + <b>B</b>x + <b>C</b> = <b>D</b></center><br> <center> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <?php for ($i = 0; $i <= 9; $i++) { ?> A: <input type="text" size="2" name="A[]" /> B: <input type="text" size="2" name="B[]" /> C: <input type="text" size="3" name="C[]" /> D: <input type="text" size="3" name="D[]" /> <br /> <?php } ?> <input type="submit" name="submit" value="Submit" /> </form> </center> <div align="center"> <?php } else { $i = 0; foreach ($_POST["A"] as $A) { $B = $_POST["B"][$i]; $C = $_POST["C"][$i]; $D = $_POST["D"][$i]; if ($A != "" && $B != "" && $C != "" && $D != "") { convert($A, $B, $C, $D); } $i++; } } ?> </div> </body> </html> [/code]
[QUOTE=cold12141;19050774][code] <html> <head> </head> <body> <?php function convert($A, $B, $C, $D = 0) { $C -= $D; $Discriminant = $B * $B - 4 * $A * $C; $Zeros = 2; if ($Discriminant == 0) $Zeros = 1; else if ($Discriminant < 0) $Zeros = 0; if ($Zeros <= 0) { echo "<strong>{$A}x<sup>2</sup> + {$B}x + $C = $D</strong> has no solution.<br />"; return; } $X1 = ($B*-1 + sqrt($Discriminant)) / (2 * $A); $X2 = ($B*-1 - sqrt($Discriminant)) / (2 * $A); if ($Zeros == 1) { echo "<strong>{$A}x<sup>2</sup> + {$B}x + $C = $D</strong>: x = <strong>$X1</strong><br />"; } else { echo "<strong>{$A}x<sup>2</sup> + {$B}x + $C = $D</strong>: x = <strong>$X1</strong> and <strong>$X2</strong<br />"; } } if(!isset($_POST['submit'])) { ?> <center><b>A</b>x<sup>2</sup> + <b>B</b>x + <b>C</b> = <b>D</b></center><br> <center> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <?php for ($i = 0; $i <= 9; $i++) { ?> A: <input type="text" size="2" name="A[]" /> B: <input type="text" size="2" name="B[]" /> C: <input type="text" size="3" name="C[]" /> D: <input type="text" size="3" name="D[]" /> <br /> <?php } ?> <input type="submit" name="submit" value="Submit" /> </form> </center> <div align="center"> <?php } else { $i = 0; foreach ($_POST["A"] as $A) { $B = $_POST["B"][$i]; $C = $_POST["C"][$i]; $D = $_POST["D"][$i]; if ($A != "" && $B != "" && $C != "" && $D != "") { convert($A, $B, $C, $D); } $i++; } } ?> </div> </body> </html> [/code][/QUOTE] That all works well, thanks! Just one problem, the number returned is always in the wrong status (that sounds retarded, in other words, it's -7 instead of 7) but I can easily fix that. Thanks again.
Sorry, you need to
Log In
to post a reply to this thread.