123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- /*
- * Copyright (C) 2021 echedey
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- // Mostrar errores por pantalla
- ini_set('display_errors', '1');
- ini_set('display_startup_errors', '1');
- error_reporting(E_ALL);
- // Inicializar variables iniciales
- $ResultadoTitle = "Resultado";
- $Resultado = "--";
- $OperadorPrimario = "";
- $OperadorSecundario = "";
- // Comprobar que se realiza operacion
- if (isset($_REQUEST["operacion"])) {
- $Operacion = $_REQUEST["operacion"];
- // Validar el tipo de operacion
- if ($Operacion == "sumar" || $Operacion == "restar" || $Operacion == "multiplicar" || $Operacion == "dividir" || $Operacion == "potencia" || $Operacion == "raiz") {
- // Comprobar que se envian los operadores
- if (!isset($_REQUEST["operadorprimario"]) || !isset($_REQUEST["operadorsecundario"])) {
- $Resultado = "Deben enviarse los 2 operadores.";
- } else {
- // Establecer valor a los operadores
- $OperadorPrimario = $_REQUEST["operadorprimario"];
- $OperadorSecundario = $_REQUEST["operadorsecundario"];
- // Validar el tipo de valor de los operadores
- if ($OperadorPrimario == "" || $OperadorSecundario == "") {
- $Resultado = "Los operadores deben tener valor.";
- } else if (is_numeric($OperadorPrimario) && is_numeric($OperadorSecundario)) {
- // Realizar la operacion en base al tipo
- if ($Operacion == "sumar") {
- $Resultado = round($OperadorPrimario + $OperadorSecundario, 1);
- } else if ($Operacion == "restar") {
- $Resultado = round($OperadorPrimario - $OperadorSecundario, 1);
- } else if ($Operacion == "multiplicar") {
- $Resultado = round($OperadorPrimario * $OperadorSecundario, 1);
- } else if ($Operacion == "dividir") {
- if ($OperadorSecundario == 0)
- $Resultado = "El divisor debe tener valor mayor o menor que 0.";
- else {
- $Resultado = round($OperadorPrimario / $OperadorSecundario, 1);
- }
- } else if ($Operacion == "potencia") {
- $Resultado = round($OperadorPrimario ** $OperadorSecundario, 1);
- } else {
- if ($OperadorPrimario < 0) {
- $Resultado = "La cantidad subradical debe tener valor mayor o igual que 0.";
- } else if ($OperadorSecundario == 0) {
- $Resultado = "El índice de la raíz debe tener valor mayor o menor que 0.";
- } else if ($OperadorPrimario == 0 && $OperadorSecundario < 0) {
- $Resultado = "Cuando el índice de la raíz es menor que 0, la cantidad subradical debe ser mayor que 0.";
- } else {
- $Resultado = round($OperadorPrimario ** (1 / $OperadorSecundario), 1);
- }
- }
- } else {
- $Resultado = "Los operadores deben tener valor numérico.";
- }
- }
- } else {
- $Resultado = "La operación debe ser «sumar», «restar», «multiplicar», «dividir», «potencia» o «raiz».";
- }
- }
- // Comprobar si lanza error
- if (!is_numeric($Resultado) && $Resultado != "--") {
- $ResultadoTitle = "¡Aviso!";
- }
- ?>
- <?php
- include("vista.php");
- ?>
|