* @copyright 2025 Bartolomé Sintes Marco
* @license http://www.gnu.org/licenses/agpl.txt AGPL 3 or later
* @version 2025-02-08
* @link https://www.mclibre.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
function cabecera($texto)
{
print "\n";
print "\n";
print "
\n";
print " \n";
print " \n";
print " Calculos estadísticos 2 ($texto). Repaso 3.\n";
print " Ejercicios. PHP. Bartolomé Sintes Marco. www.mclibre.org\n";
print " \n";
print " \n";
print " \n";
print "\n";
print "\n";
print "\n";
print " Calculos estadísticos 2 ($texto) \n";
print "\n";
}
// Función de recogida de datos
function recoge($key, $type = "")
{
if (!is_string($key) && !is_int($key) || $key == "") {
trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR);
} elseif ($type !== "" && $type !== []) {
trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR);
}
$tmp = $type;
if (isset($_REQUEST[$key])) {
if (!is_array($_REQUEST[$key]) && !is_array($type)) {
$tmp = trim(htmlspecialchars($_REQUEST[$key]));
} elseif (is_array($_REQUEST[$key]) && is_array($type)) {
$tmp = $_REQUEST[$key];
array_walk_recursive($tmp, function (&$value) {
$value = trim(htmlspecialchars($value));
});
}
}
return $tmp;
}
define("FORM_METHOD", "get");
define("NUM_VALORES_INICIAL", 4);
define("NUM_VALORES_MINIMO", 2);
define("NUM_VALORES_MAXIMO", 15);
$valores = recoge("valor", []);
$valoresOk = [];
$valoresTodoOk = true;
$suma = (recoge("suma") == "on");
$media = (recoge("media") == "on");
$maximo = (recoge("maximo") == "on");
$minimo = (recoge("minimo") == "on");
// Recoge el número de datos y lo valida, aumenta o reduce
$numeroValores = recoge("numeroValores");
if ($numeroValores < NUM_VALORES_MINIMO) {
$numeroValores = NUM_VALORES_MINIMO;
} elseif ($numeroValores > NUM_VALORES_MAXIMO) {
$numeroValores = NUM_VALORES_MAXIMO;
}
if (isset($_REQUEST["anyadir"]) && ($numeroValores < NUM_VALORES_MAXIMO)) {
$numeroValores++;
$valores[$numeroValores] = ""; // Al añdir se crea un nuevo valor vacío
} elseif (isset($_REQUEST["quitar"]) && ($numeroValores > NUM_VALORES_MINIMO)) {
$numeroValores--;
}
for ($i = 1; $i <= $numeroValores; $i++) {
$valoresOk[$i] = true;
if (!isset($valores[$i])) { // Por si falta un valor en la matriz
$valoresTodoOk = false;
$valores[$i] = "";
} elseif ($valores[$i] == "") { // Por si un valor es vacío
$valoresTodoOk = false;
} elseif ($valores[$i] != "" && !is_numeric($valores[$i])) { // Por si un valor no es numérico
$valoresOk[$i] = false;
$valoresTodoOk = false;
}
}
$valoresTodoVacio = true;
for ($i = 1; $i <= $numeroValores; $i++) {
if ($valores[$i] != "") {
$valoresTodoVacio = false;
}
}
if ($valoresTodoOk) {
cabecera("Resultado válido");
$sumaTotal = 0;
print " Ha introducido $numeroValores valores: ";
foreach ($valores as $valor) {
print "$valor ";
$sumaTotal += $valor;
}
print "
\n";
print "\n";
if ($suma) {
print " La suma de los valores es $sumaTotal .
\n";
print "\n";
}
if ($media) {
print " La media de los valores es " . round($sumaTotal / $numeroValores, 2) . " .
\n";
print "\n";
}
if ($maximo) {
print " El valor más grande es " . max($valores) . " .
\n";
print "\n";
}
if ($minimo) {
print " El valor más pequeño es " . min($valores) . " .
\n";
print "\n";
}
print " Volver al principio
\n";
print "\n";
} elseif (
!$valoresTodoVacio
&& (isset($_REQUEST["enviar"]) || isset($_REQUEST["anyadir"]) || isset($_REQUEST["quitar"]))
) {
cabecera("Resultado inválido");
print " Por favor, corrija los datos incorrectos y/o complete todas las casillas:
\n";
print "\n";
print " \n";
} else {
cabecera("Formulario");
print " Escriba $numeroValores números:
\n";
print "\n";
print " \n";
print "\n";
}
print " \n";
print "\n";
print "\n";