Added Quadratic Equation Solver examples of calculation engine usage

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@61104 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2010-09-17 22:46:31 +00:00
parent 31196f0a14
commit 5f5e793c05
3 changed files with 133 additions and 0 deletions

68
Tests/Quadratic.php Normal file
View File

@ -0,0 +1,68 @@
<html>
<head>
<title>Quadratic Equation Solver</title>
</head>
<body>
<?php
/** Error reporting **/
error_reporting(E_ALL);
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
?>
<h1>Quadratic Equation Solver</h1>
<form action="Quadratic.php" method="POST">
Enter the coefficients for the Ax<sup>2</sup> + Bx + C = 0
<table border="0" cellpadding="0" cellspacing="0">
<tr><td><b>A&nbsp;</b></td>
<td><input name="A" type="text" size="8" value="<?php echo (isset($_POST['A'])) ? htmlentities($_POST['A']) : ''; ?>"></td>
</tr>
<tr><td><b>B&nbsp;</b></td>
<td><input name="B" type="text" size="8" value="<?php echo (isset($_POST['B'])) ? htmlentities($_POST['B']) : ''; ?>"></td>
</tr>
<tr><td><b>C&nbsp;</b></td>
<td><input name="C" type="text" size="8" value="<?php echo (isset($_POST['C'])) ? htmlentities($_POST['C']) : ''; ?>"></td>
</tr>
</table>
<input name="submit" type="submit" value="calculate"><br />
If A=0, the equation is not quadratic.
</form>
<?php
/** If the user has submitted the form, then we need to execute a calculation **/
if (isset($_POST['submit'])) {
if ($_POST['A'] == 0) {
echo 'The equation is not quadratic';
} else {
/** So we include PHPExcel to perform the calculations **/
include 'PHPExcel/IOFactory.php';
/** Load the quadratic equation solver worksheet into memory **/
$objPHPExcel = PHPExcel_IOFactory::load('./Quadratic.xlsx');
/** Set our A, B and C values **/
$objPHPExcel->getActiveSheet()->setCellValue('A1', $_POST['A']);
$objPHPExcel->getActiveSheet()->setCellValue('B1', $_POST['B']);
$objPHPExcel->getActiveSheet()->setCellValue('C1', $_POST['C']);
/** Calculate and Display the results **/
echo '<hr /><b>Roots:</b><br />';
$callStartTime = microtime(true);
echo $objPHPExcel->getActiveSheet()->getCell('B5')->getCalculatedValue().'<br />';
echo $objPHPExcel->getActiveSheet()->getCell('B6')->getCalculatedValue().'<br />';
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo '<hr />Call time for Quadratic Equation Solution was '.sprintf('%.4f',$callTime).' seconds<br /><hr />';
echo ' Peak memory usage: '.(memory_get_peak_usage(true) / 1024 / 1024).' MB<br />';
}
}
?>
</body>
<html>

BIN
Tests/Quadratic.xlsx Normal file

Binary file not shown.

65
Tests/Quadratic2.php Normal file
View File

@ -0,0 +1,65 @@
<html>
<head>
<title>Quadratic Equation Solver</title>
</head>
<body>
<?php
/** Error reporting **/
error_reporting(E_ALL);
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
?>
<h1>Quadratic Equation Solver</h1>
<form action="Quadratic2.php" method="POST">
Enter the coefficients for the Ax<sup>2</sup> + Bx + C = 0
<table border="0" cellpadding="0" cellspacing="0">
<tr><td><b>A&nbsp;</b></td>
<td><input name="A" type="text" size="8" value="<?php echo (isset($_POST['A'])) ? htmlentities($_POST['A']) : ''; ?>"></td>
</tr>
<tr><td><b>B&nbsp;</b></td>
<td><input name="B" type="text" size="8" value="<?php echo (isset($_POST['B'])) ? htmlentities($_POST['B']) : ''; ?>"></td>
</tr>
<tr><td><b>C&nbsp;</b></td>
<td><input name="C" type="text" size="8" value="<?php echo (isset($_POST['C'])) ? htmlentities($_POST['C']) : ''; ?>"></td>
</tr>
</table>
<input name="submit" type="submit" value="calculate"><br />
If A=0, the equation is not quadratic.
</form>
<?php
/** If the user has submitted the form, then we need to execute a calculation **/
if (isset($_POST['submit'])) {
if ($_POST['A'] == 0) {
echo 'The equation is not quadratic';
} else {
/** So we include PHPExcel to perform the calculations **/
include 'PHPExcel/Calculation.php';
/** Calculate and Display the results **/
echo '<hr /><b>Roots:</b><br />';
$callStartTime = microtime(true);
$discriminantFormula = '=POWER('.$_POST['B'].',2) - (4 * '.$_POST['A'].' * '.$_POST['C'].')';
$discriminant = PHPExcel_Calculation::getInstance()->calculateFormula($discriminantFormula);
$r1Formula = '=IMDIV(IMSUM(-'.$_POST['B'].',IMSQRT('.$discriminant.')),2 * '.$_POST['A'].')';
$r2Formula = '=IF('.$discriminant.'=0,"Only one root",IMDIV(IMSUB(-'.$_POST['B'].',IMSQRT('.$discriminant.')),2 * '.$_POST['A'].'))';
echo PHPExcel_Calculation::getInstance()->calculateFormula($r1Formula).'<br />';
echo PHPExcel_Calculation::getInstance()->calculateFormula($r2Formula).'<br />';
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo '<hr />Call time for Quadratic Equation Solution was '.sprintf('%.4f',$callTime).' seconds<br /><hr />';
echo ' Peak memory usage: '.(memory_get_peak_usage(true) / 1024 / 1024).' MB<br />';
}
}
?>
</body>
<html>