Quadratic2.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <html>
  2. <head>
  3. <title>Quadratic Equation Solver</title>
  4. </head>
  5. <body>
  6. <?php
  7. /** Error reporting **/
  8. error_reporting(E_ALL);
  9. /** Include path **/
  10. set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/../Classes/');
  11. ?>
  12. <h1>Quadratic Equation Solver</h1>
  13. <form action="Quadratic2.php" method="POST">
  14. Enter the coefficients for the Ax<sup>2</sup> + Bx + C = 0
  15. <table border="0" cellpadding="0" cellspacing="0">
  16. <tr><td><b>A&nbsp;</b></td>
  17. <td><input name="A" type="text" size="8" value="<?php echo (isset($_POST['A'])) ? htmlentities($_POST['A']) : ''; ?>"></td>
  18. </tr>
  19. <tr><td><b>B&nbsp;</b></td>
  20. <td><input name="B" type="text" size="8" value="<?php echo (isset($_POST['B'])) ? htmlentities($_POST['B']) : ''; ?>"></td>
  21. </tr>
  22. <tr><td><b>C&nbsp;</b></td>
  23. <td><input name="C" type="text" size="8" value="<?php echo (isset($_POST['C'])) ? htmlentities($_POST['C']) : ''; ?>"></td>
  24. </tr>
  25. </table>
  26. <input name="submit" type="submit" value="calculate"><br />
  27. If A=0, the equation is not quadratic.
  28. </form>
  29. <?php
  30. /** If the user has submitted the form, then we need to execute a calculation **/
  31. if (isset($_POST['submit'])) {
  32. if ($_POST['A'] == 0) {
  33. echo 'The equation is not quadratic';
  34. } else {
  35. /** So we include PHPExcel to perform the calculations **/
  36. include 'PHPExcel/Calculation.php';
  37. /** Calculate and Display the results **/
  38. echo '<hr /><b>Roots:</b><br />';
  39. $callStartTime = microtime(true);
  40. $discriminantFormula = '=POWER('.$_POST['B'].',2) - (4 * '.$_POST['A'].' * '.$_POST['C'].')';
  41. $discriminant = PHPExcel_Calculation::getInstance()->calculateFormula($discriminantFormula);
  42. $r1Formula = '=IMDIV(IMSUM(-'.$_POST['B'].',IMSQRT('.$discriminant.')),2 * '.$_POST['A'].')';
  43. $r2Formula = '=IF('.$discriminant.'=0,"Only one root",IMDIV(IMSUB(-'.$_POST['B'].',IMSQRT('.$discriminant.')),2 * '.$_POST['A'].'))';
  44. echo PHPExcel_Calculation::getInstance()->calculateFormula($r1Formula).'<br />';
  45. echo PHPExcel_Calculation::getInstance()->calculateFormula($r2Formula).'<br />';
  46. $callEndTime = microtime(true);
  47. $callTime = $callEndTime - $callStartTime;
  48. echo '<hr />Call time for Quadratic Equation Solution was '.sprintf('%.4f',$callTime).' seconds<br /><hr />';
  49. echo ' Peak memory usage: '.(memory_get_peak_usage(true) / 1024 / 1024).' MB<br />';
  50. }
  51. }
  52. ?>
  53. </body>
  54. <html>