45_Quadratic_equation_solver.php 2.3 KB

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