note 33910 added to control-structures
| From: | Merve at rack1 dot php dot net | Date: | Thu, 10 Jul 2003 18:54:31 +0000 |
| Subject: | note 33910 added to control-structures | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51864@lists.php.net to get a copy of this message | ||
A multi-function calculator using if-then-else statements.
<?php
if ($operation=="plus") {
$c = ($a+$b);
print (" $a + $b = $c");
}
elseif ($operation=="minus") {
$c = ($a-$b);
print (" $a - $b = $c");
}
elseif ($operation=="times") {
$c = ($a*$b) ;
print (" $a * $b = $c");
}
elseif ($operation=="divided") {
$c = ($a/$b);
print (" $a / $b = $c");
}
elseif ($operation=="power") {
if ($b==0) {
print (" $a<sup>$b</sup> = 1");
}
elseif ($b<0) {
$c = $a;
$d = $b;
$f = ($b*-1);
while ($f>1)
{
$a = ($a*$c);
$f--;
}
$e = (1/$a);
print (" $c<sup>$d</sup> = $e");
} else {
$c = $a;
$d = $b;
while ($b>1)
{
$a = ($a*$c);
$b--;
}
print (" $c<sup>$d</sup> = $a");
}
}
elseif ($operation=="sqrt") {
$c = sqrt($b);
print (" The square root of $b = $c");
}
elseif ($operation=="factorial") {
$c = ($a-1);
$d = $a;
while ($c>=1)
{
$a = ($a*$c);
$c--;
}
print (" $d! = $a");
}
elseif ($operation=="percentage") {
$c = (($a/100)*($b));
print (" $a% of $b = $c");
}
elseif ($operation=="rebate") {
$c = (($b)-(($a/100)*($b)));
print (" A/an $a% rebate on $b would give $c");
}
elseif ($operation=="tax") {
$c = (($b)+(($a/100)*($b)));
print (" A/an $a% tax on $b would give $c");
}
elseif ($operation=="hypotenuse") {
$c = sqrt(($a*$a)+($b*$b));
print (" If the sides of a right triangle forming the right angle are $a and $b respectively,
then the hypotenuse would be $c");
}
elseif ($operation=="misside") {
$c = sqrt(($a*$a)-($b*$b));
print (" If the hypotenuse of right triangle is $a and another side is $b, then the missing
side would be $c");
}
elseif ($operation=="circumference") {
$diameter = ($a/M_PI);
$diameter2 = round ($diameter, 4);
$radius = ($diameter/2);
$radius2 = round ($radius, 4);
$area = ((M_PI)*($radius*$radius));
$area2 = round ($area, 4);
print (" If the circumference of a circle is $a unit(s), then the diameter is $diameter2
unit(s), the radius is $radius2 unit(s), and the area is $area2 square unit(s).");
}
elseif ($operation=="diameter") {
$circumference = ($a*M_PI);
$circumference2 = round ($circumference, 4);
$radius = ($a/2);
$radius2 = round ($radius, 4);
$area = ((M_PI)*($radius*$radius));
$area2 = round ($area, 4);
print (" If the diameter of a circle is $a unit(s), then the circumference is $circumference2
unit(s), the radius is $radius2 unit(s), and the area is $area2 square unit(s).");
}
elseif ($operation=="radius") {
$diameter = ($a*2);
$diameter2 = round ($diameter, 4);
$circumference = ($diameter*M_PI);
$circumference2 = round ($circumference, 4);
$area = ((M_PI)*($a*$a));
$area2 = round ($area, 4);
print (" If the radius of a circle is $a unit(s), then the circumference is $circumference2
unit(s), the diameter is $diameter2 unit(s), and the area is $area2 square unit(s).");
}
elseif ($operation=="area") {
$radius = sqrt($a/M_PI);
$radius2 = round ($radius, 4);
$diameter = ($radius*2);
$diameter2 = round ($diameter, 4);
$circumference = ($diameter*M_PI);
$circumference2 = round ($circumference, 4);
print (" If the area of a circle is $a square unit(s), then the circumference is
$circumference2 unit(s), the diameter is $diameter2 unit(s), and the radius is $radius2
unit(s).");
}
?>
It's pretty straightforward except for the factorial and the powers, but I added a note about
that in the while loops section. For the circles, you'll notice I rounded the answers to 4
digits after the decimal point. Believe me, it makes life so much simpler. Everything is calculated
without rounding and is rounded off at the end.
----
Manual Page -- http://www.php.net/manual/en/control-structures.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33910
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33910&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33910&report=yes