Files
PhpSpreadsheet/samples/Basic4/45_Quadratic_equation_solver.php
T
oleibman b3de003aa5 Reorganize Samples
The samples have become unwieldy when running them from a browser. In particular, the drop-down lists are fixed size with no scrolling, and many of them are now just too large. I have moved all the Calculation samples up a level, and broken several categories (Basic, Chart, DateTime, Engineering, Financial, and Reader) into several pieces.

Convert-Online (now found in the Engineering category) had a number of different problems which are now resolved. It is the only member with any significant code change.
2024-02-03 07:25:29 -08:00

72 lines
2.6 KiB
PHP

<?php
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\Settings;
require __DIR__ . '/../Header.php';
$helper = new Sample();
if ($helper->isCli()) {
$helper->log('This example should only be run from a Web Browser' . PHP_EOL);
return;
}
?>
<form action="45_Quadratic_equation_solver.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'], Settings::htmlEntityFlags()) : ''; ?>">
</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'], Settings::htmlEntityFlags()) : ''; ?>">
</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'], Settings::htmlEntityFlags()) : ''; ?>">
</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) {
$helper->log('The equation is not quadratic');
} else {
// Calculate and Display the results
$helper->log('<hr /><b>Roots:</b><br />');
$discriminantFormula = '=POWER(' . $_POST['B'] . ',2) - (4 * ' . $_POST['A'] . ' * ' . $_POST['C'] . ')';
$discriminant = 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'] . '))';
/** @var string */
$output = Calculation::getInstance()->calculateFormula($r1Formula);
$helper->log("$output");
/** @var string */
$output = Calculation::getInstance()->calculateFormula($r2Formula);
$helper->log("$output");
$callEndTime = microtime(true);
$helper->logEndingNotes();
}
}