Quadratic.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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="Quadratic.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/IOFactory.php';
  37. /** Load the quadratic equation solver worksheet into memory **/
  38. $objPHPExcel = PHPExcel_IOFactory::load('./Quadratic.xlsx');
  39. /** Set our A, B and C values **/
  40. $objPHPExcel->getActiveSheet()->setCellValue('A1', $_POST['A']);
  41. $objPHPExcel->getActiveSheet()->setCellValue('B1', $_POST['B']);
  42. $objPHPExcel->getActiveSheet()->setCellValue('C1', $_POST['C']);
  43. /** Calculate and Display the results **/
  44. echo '<hr /><b>Roots:</b><br />';
  45. $callStartTime = microtime(true);
  46. echo $objPHPExcel->getActiveSheet()->getCell('B5')->getCalculatedValue().'<br />';
  47. echo $objPHPExcel->getActiveSheet()->getCell('B6')->getCalculatedValue().'<br />';
  48. $callEndTime = microtime(true);
  49. $callTime = $callEndTime - $callStartTime;
  50. echo '<hr />Call time for Quadratic Equation Solution was '.sprintf('%.4f',$callTime).' seconds<br /><hr />';
  51. echo ' Peak memory usage: '.(memory_get_peak_usage(true) / 1024 / 1024).' MB<br />';
  52. }
  53. }
  54. ?>
  55. </body>
  56. <html>