PHP: Calculadora básica en PHP
Calculadora básica (y poco optimizada) en PHP. Corrige un fallo del original pero sigue siendo demasiado repetitiva.
Lenguaje: PHP (compilador: PHP 4)
Categoría: Básico
// Fuente procedente de ErrorDeSintaxis.es
// Calculadora básica (y poco optimizada) en
// PHP. Corrige un fallo del original pero
// sigue siendo demasiado repetitiva.
// Compilador: PHP 4
// Nivel: Básico
// Disponible desde 06/08/2011
// Aportado por Nacho
// Web original: http://scripts.ringsworld.com/calculators/calculator/calculator.php.html
<?php function square( $number ) { $newNumber = $number * $number; return $newNumber; } function cube( $number ) { $newNumber = $number * $number * $number; return $newNumber; } ?> <html> <head> <title>Calculator</title> </head> <body> <div align="center"> <center> <form method="POST" action="calculator.php"> <p>To perform calculations such as squares, square roots, <br>and cubes you need only fill in the first number field!<br><br> </p> <table border="0" cellspacing="1" width="304" id="calculator" height="139"> <tr> <td width="292" align="center" height="39"> <h3>Calculator</h3> </td> </tr> <tr> <td width="292" align="center" height="23"> <input type="text" name="this" size="10"><br> <input type="text" name="that" size="10"></td> </tr> <tr> <td width="292" align="center" height="24"> <input type="submit" value="+" name="do"> <input type="submit" value="-" name="do"> <input type="submit" value="*" name="do"> <input type="submit" value="/" name="do"><br> <input type="submit" value="^2" name="do"> <input type="submit" value="^3" name="do"> <input type="submit" value="SqRt" name="do"> </td> </tr> <?php $do=$_POST['do']; if ($do == "+"){ if (($_POST['this'] != "") && ($_POST['that'] != "")) { $result = $_POST['this'] + $_POST['that']; } else { $message = "<font color="red">You did not fill in both fields!</font>"; $result = "N/A"; } } elseif ($do == "-"){ if (($_POST['this'] != "") && ($_POST['that'] != "")) { $result = $_POST['this'] - $_POST['that']; } else { $message = "<font color="red">You did not fill in both fields!</font>"; $result = "N/A"; } } elseif ($do == "*"){ if (($_POST['this'] != "") && ($_POST['that'] != "")) { $result = $_POST['this'] * $_POST['that']; } else { $message = "<font color="red">You did not fill in both fields!</font>"; $result = "N/A"; } } elseif ($do == "/"){ if (($_POST['this'] != "") && ($_POST['that'] != "")) { $result = $_POST['this'] / $_POST['that']; } else { $message = "<font color="red">You did not fill in both fields!</font>"; $result = "N/A"; } } elseif ($do == "^2"){ if ($_POST['this'] != "") { $result = square ( $_POST['this'] ); $that = ""; } else { $message = "<font color="red">You did not fill in the first field!</font>"; $result = "N/A"; } } elseif ($do == "SqRt"){ if ($_POST['this'] != "") { $result = sqrt($_POST['this']); $that = ""; } else { $message = "<font color="red">You did not fill in the first field!</font>"; $result = "N/A"; } } elseif ($do == "^3"){ if ($_POST['this'] != "") { $result = cube ( $_POST['this'] ); $that = ""; } else { $message = "<font color="red">You did not fill in the first field!</font>"; $result = "N/A"; } } ?> <tr> <td width="292" align="center" height="19"><br><b>Result: <?php echo($result); ?><br><?php echo($message); ?></b></td> </tr> </table> </form> </center> </div> </body> </html>