index.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * Copyright (C) 2021 echedey
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // Mostrar errores por pantalla
  19. ini_set('display_errors', '1');
  20. ini_set('display_startup_errors', '1');
  21. error_reporting(E_ALL);
  22. // Inicializar variables iniciales
  23. $ResultadoTitle = "Resultado";
  24. $Resultado = "--";
  25. $OperadorPrimario = "";
  26. $OperadorSecundario = "";
  27. // Comprobar que se realiza operacion
  28. if (isset($_REQUEST["operacion"])) {
  29. $Operacion = $_REQUEST["operacion"];
  30. // Validar el tipo de operacion
  31. if ($Operacion == "sumar" || $Operacion == "restar" || $Operacion == "multiplicar" || $Operacion == "dividir" || $Operacion == "potencia" || $Operacion == "raiz") {
  32. // Comprobar que se envian los operadores
  33. if (!isset($_REQUEST["operadorprimario"]) || !isset($_REQUEST["operadorsecundario"])) {
  34. $Resultado = "Deben enviarse los 2 operadores.";
  35. } else {
  36. // Establecer valor a los operadores
  37. $OperadorPrimario = $_REQUEST["operadorprimario"];
  38. $OperadorSecundario = $_REQUEST["operadorsecundario"];
  39. // Validar el tipo de valor de los operadores
  40. if ($OperadorPrimario == "" || $OperadorSecundario == "") {
  41. $Resultado = "Los operadores deben tener valor.";
  42. } else if (is_numeric($OperadorPrimario) && is_numeric($OperadorSecundario)) {
  43. // Realizar la operacion en base al tipo
  44. if ($Operacion == "sumar") {
  45. $Resultado = round($OperadorPrimario + $OperadorSecundario, 1);
  46. } else if ($Operacion == "restar") {
  47. $Resultado = round($OperadorPrimario - $OperadorSecundario, 1);
  48. } else if ($Operacion == "multiplicar") {
  49. $Resultado = round($OperadorPrimario * $OperadorSecundario, 1);
  50. } else if ($Operacion == "dividir") {
  51. if ($OperadorSecundario == 0)
  52. $Resultado = "El divisor debe tener valor mayor o menor que 0.";
  53. else {
  54. $Resultado = round($OperadorPrimario / $OperadorSecundario, 1);
  55. }
  56. } else if ($Operacion == "potencia") {
  57. $Resultado = round($OperadorPrimario ** $OperadorSecundario, 1);
  58. } else {
  59. if ($OperadorPrimario < 0) {
  60. $Resultado = "La cantidad subradical debe tener valor mayor o igual que 0.";
  61. } else if ($OperadorSecundario == 0) {
  62. $Resultado = "El índice de la raíz debe tener valor mayor o menor que 0.";
  63. } else if ($OperadorPrimario == 0 && $OperadorSecundario < 0) {
  64. $Resultado = "Cuando el índice de la raíz es menor que 0, la cantidad subradical debe ser mayor que 0.";
  65. } else {
  66. $Resultado = round($OperadorPrimario ** (1 / $OperadorSecundario), 1);
  67. }
  68. }
  69. } else {
  70. $Resultado = "Los operadores deben tener valor numérico.";
  71. }
  72. }
  73. } else {
  74. $Resultado = "La operación debe ser «sumar», «restar», «multiplicar», «dividir», «potencia» o «raiz».";
  75. }
  76. }
  77. // Comprobar si lanza error
  78. if (!is_numeric($Resultado) && $Resultado != "--") {
  79. $ResultadoTitle = "¡Aviso!";
  80. }
  81. ?>
  82. <?php
  83. include("vista.php");
  84. ?>