45_Quadratic_equation_solver.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  3. require __DIR__ . '/../Header.php';
  4. ?>
  5. <form action="45_Quadratic_equation_solver.php" method="POST">
  6. Enter the coefficients for the Ax<sup>2</sup> + Bx + C = 0
  7. <table border="0" cellpadding="0" cellspacing="0">
  8. <tr><td><b>A&nbsp;</b></td>
  9. <td><input name="A" type="text" size="8" value="<?php echo (isset($_POST['A'])) ? htmlentities($_POST['A']) : ''; ?>"></td>
  10. </tr>
  11. <tr><td><b>B&nbsp;</b></td>
  12. <td><input name="B" type="text" size="8" value="<?php echo (isset($_POST['B'])) ? htmlentities($_POST['B']) : ''; ?>"></td>
  13. </tr>
  14. <tr><td><b>C&nbsp;</b></td>
  15. <td><input name="C" type="text" size="8" value="<?php echo (isset($_POST['C'])) ? htmlentities($_POST['C']) : ''; ?>"></td>
  16. </tr>
  17. </table>
  18. <input name="submit" type="submit" value="calculate"><br />
  19. If A=0, the equation is not quadratic.
  20. </form>
  21. <?php
  22. /** If the user has submitted the form, then we need to execute a calculation * */
  23. if (isset($_POST['submit'])) {
  24. if ($_POST['A'] == 0) {
  25. $helper->log('The equation is not quadratic');
  26. } else {
  27. // Calculate and Display the results
  28. $helper->log('<hr /><b>Roots:</b><br />');
  29. $discriminantFormula = '=POWER(' . $_POST['B'] . ',2) - (4 * ' . $_POST['A'] . ' * ' . $_POST['C'] . ')';
  30. $discriminant = Calculation::getInstance()->calculateFormula($discriminantFormula);
  31. $r1Formula = '=IMDIV(IMSUM(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . ')';
  32. $r2Formula = '=IF(' . $discriminant . '=0,"Only one root",IMDIV(IMSUB(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . '))';
  33. $helper->log(Calculation::getInstance()->calculateFormula($r1Formula));
  34. $helper->log(Calculation::getInstance()->calculateFormula($r2Formula));
  35. $callEndTime = microtime(true);
  36. $helper->logEndingNotes();
  37. }
  38. }