Solucio calculadora m6
De wikiserver
Calculadora PHP (index.php):
<?php
function posFinalPar($oper, $first){
$n = 1;
$i = $first + 1;
while($n>=1 && $i < strlen($oper)){
if($oper[$i] == "(")
{
$n++;
}
if ($oper[$i] == ")"){
$n--;
}
$i++;
}
if($n>=1) { return FALSE;}
return $i--;
}
function parentesis($oper){
$first = strpos($oper, "(");
if($first === FALSE){
return $oper;
}
$last = posFinalPar($oper, $first);
if($last === FALSE){
echo "error falta cerrar un parentesis";
exit();
}
$rigth = substr($oper,$last);//parte derecha de la cadena des de ')'
$left = explode("(", $oper)[0];//parte izquierda de la cadena des de '('
$middle = substr($oper, $first+1, strlen($oper)- strlen($rigth) - strlen($left) - 2); //Parte dentro parentesis
return calcular( ($left) . calcular($middle) . $rigth);
}
function getminpos($oper){
$posx = (strpos($oper, "*") === FALSE)? strlen($oper) : strpos($oper, "*");
$posd = (strpos($oper, "/") === FALSE)? strlen($oper) : strpos($oper, "/");
$posm = (strpos($oper, "%") === FALSE)? strlen($oper) : strpos($oper, "%");
return min($posx,$posd,$posm);
}
function getleft($oper, $sep){
$ini = substr($oper, 0, $sep);
$posmas = (strrpos($ini, "+") === FALSE)? -1 : strrpos($ini, "+");
$posmenos = (strrpos($ini, "-") === FALSE)? -1 : strrpos($ini, "-");
$max = max($posmas,$posmenos);
if($max == -1){ return array($ini,"");}
return array(substr($ini,$max+1),substr($ini,0, $max+1));
}
function getright($oper, $sep){
$ini = substr($oper, $sep+1);
$posmas = (strpos($ini, "+") === FALSE)? strlen($ini) : strpos($ini, "+");
$posmenos = (strpos($ini, "-") === FALSE)? strlen($ini) : strpos($ini, "-");
$posx = (strpos($ini, "*") === FALSE)? strlen($ini) : strpos($ini, "*");
$posd = (strpos($ini, "/") === FALSE)? strlen($ini) : strpos($ini, "/");
$posm = (strpos($ini, "%") === FALSE)? strlen($ini) : strpos($ini, "%");
$min = min($posmas,$posmenos,$posx,$posd,$posm);
if($min == strlen($ini)){ return array($ini,"");}
return array(substr($ini,0,$min), substr($ini,$min));
}
function muldivmod($oper){
$min = getminpos($oper);
if ($min == strlen($oper)){ return $oper; }
$l = getleft($oper,$min);
$r = getright($oper,$min);
$op1 = $l[0];
$op2 = $r[0];
$total=FALSE;
switch ($oper[$min]){
case "*":
$total = $op1 * $op2;
break;
case "/":
$total = $op1 / $op2;
break;
case "%":
$total = $op1 % $op2;
}
$left = $l[1];
$rigth = $r[1];
return $left . muldivmod($total. $rigth);
}
function getminpossum($oper){
$posx = (strpos($oper, "+") === FALSE)? strlen($oper) : strpos($oper, "+");
$posd = (strpos($oper, "-") === FALSE)? strlen($oper) : strpos($oper, "-");
return min($posx,$posd);
}
function sumrest($oper){
$min = getminpossum($oper);
if ($min == strlen($oper)){ return $oper; }
$l = getleft($oper,$min);
$r = getright($oper,$min);
$op1 = $l[0];
$op2 = $r[0];
$total=FALSE;
switch ($oper[$min]){
case "+":
$total = $op1 + $op2;
break;
case "-":
$total = $op1 - $op2;
break;
}
$left = $l[1];
$rigth = $r[1];
return $left . sumrest($total. $rigth);
}
function calcular($oper){
$noparentesisoper = parentesis($oper);
$nomultdivmod = muldivmod($noparentesisoper);
$rest = sumrest($nomultdivmod);
return $rest;
}
if(isset($_REQUEST["operaciones"]) && !empty($_REQUEST["operaciones"])){
$op = $_REQUEST["operaciones"];
echo calcular($op);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="index.php" method="POST">
<label>
Introdueix comandes per calcular
</label>
<input type="text" name="operaciones" value="" />
<input type="submit" value="calcular"/>
</form>
</body>
</html>