예제 #1
0
<?php

if (isset($_POST)) {
    $complexNumber = new ComplexNumber((int) $_POST['real'], (int) $_POST['imaginary']);
    $complexNumber->square();
    $addcomplexNumber = new ComplexNumber((int) $_POST['add_real'], (int) $_POST['add_imaginary']);
    $complexNumber->add($addcomplexNumber);
    $results = unserialize($_POST['results']);
    $results[] = array($complexNumber->real, $complexNumber->imaginary);
} else {
    $results = array();
}
?>
<form action="#" method="POST">
<fieldset>
	Square: <input type="text" name="real" value="<?php 
if (isset($complexNumber)) {
    echo $complexNumber->real;
}
?>
" /> +
	<input type="text" name="imaginary" value="<?php 
if (isset($complexNumber)) {
    echo $complexNumber->imaginary;
}
?>
" /> i<br/>
	Add: <input type="text" name="add_real" value="<?php 
if (isset($_POST['add_real'])) {
    echo $_POST['add_real'];
}
예제 #2
0
<?php

$maxIterations = 50;
$step = 1 / 20;
$topLeft = array(-1.5, 1);
$bottomRight = array(0.5, -1);
if ('cli' != php_sapi_name()) {
    echo "<pre><center>";
}
$c = new ComplexNumber(-0.8, 0.156);
for ($imaginary = $topLeft[1]; $imaginary > $bottomRight[1]; $imaginary = $imaginary - $step) {
    for ($real = $topLeft[0]; $real < $bottomRight[0]; $real = $real + $step) {
        $z = new ComplexNumber($real, $imaginary);
        $iterationCount = 0;
        while ($z->lessThanTwo() && $iterationCount < $maxIterations) {
            $z->square();
            $z->add($c);
            ++$iterationCount;
        }
        if ($iterationCount >= $maxIterations) {
            echo '*';
        } else {
            echo ' ';
        }
    }
    echo PHP_EOL;
}
if ('cli' != php_sapi_name()) {
    echo "</centre></pre>";
}