Ver Mensaje Individual
  #10 (permalink)  
Antiguo 09/07/2003, 18:21
Avatar de Maycol
Maycol
 
Fecha de Ingreso: diciembre-2001
Ubicación: Ávila (España)
Mensajes: 539
Antigüedad: 22 años, 4 meses
Puntos: 0
a ver que os parece este, lo remodele como dije anteriormente.

Código PHP:
<?
class calculadora {

// definimos las variables
var $numero1;
var 
$numero2;
var 
$operador;
var 
$error;
var 
$resultado;
var 
$retornar;



//creamos la funcion de calculo
function calcular($num1,$oper,$num2){
    if(empty(
$num1))$this->error .= "Numero1 no definido<br>";
    if(empty(
$num2))$this->error .= "Numero2 no definido<br>";
    if(empty(
$oper))$this->error .= "Operador no definido<br>";
    
    
$this->numero1 $num1;
    
$this->numero2 $num2;
    
$this->operador $oper;
    
    
//calculamos
    
if($this->operador == "+"){
        
$this->resultado $this->numero1+$this->numero2;
    }elseif(
$this->operador == "-"){
        
$this->resultado $this->numero1-$this->numero2;
    }elseif(
$this->operador == "*"){
        
$this->resultado $this->numero1*$this->numero2;
    }elseif(
$this->operador == "/"){
        if(
$this->numero1 == or $this->numero2 == 0){
            
$this->error .= "No existen divisiones con denominador o numerador igual a 0<br>";
        }else{
            
$this->resultado $this->numero1/$this->numero2;
        }
    }elseif(
$this->operador == "%"){
        if(
$this->numero1 == or $this->numero2 == 0){
            
$this->error .= "No existen divisiones con denominador o numerador igual a 0<br>";
        }else{
            
$this->resultado $this->numero1%$this->numero2;
        }
    }else{
        
$this->error .= "El operador no es valido<br>";
    }
    
    if(!empty(
$this->error)){
        
$this->error .= "Han ocurrido los siguientes errores:<br>";
        
$this->retornar $this->error;
    }else{
        
$this->retornar $this->resultado;
    }
    return 
$this->retornar;
}

//creamos la funcion de calculo teniendo como primer numero el resultado del ultimo calculo
function calcular2($oper,$num2){
    if(empty(
$this->resultado))$this->error .= "No se ha realizado anteriormente un calculo";
    if(empty(
$num2))$this->error .= "Numero2 no definido<br>";
    if(empty(
$oper))$this->error .= "Operador no definido<br>";
    
    
$this->numero2 $num2;
    
$this->operador $oper;
    
    
//calculamos
    
if($this->operador == "+"){
        
$this->resultado $this->resultado+$this->numero2;
    }elseif(
$this->operador == "-"){
        
$this->resultado $this->resultado-$this->numero2;
    }elseif(
$this->operador == "*"){
        
$this->resultado $this->resultado*$this->numero2;
    }elseif(
$this->operador == "/"){
        if(
$this->numero1 == or $this->numero2 == 0){
            
$this->error .= "No existen divisiones con denominador o numerador igual a 0<br>";
        }else{
            
$this->resultado $this->resultado/$this->numero2;
        }
    }elseif(
$this->operador == "%"){
        if(
$this->numero1 == or $this->numero2 == 0){
            
$this->error .= "No existen divisiones con denominador o numerador igual a 0<br>";
        }else{
            
$this->resultado $this->resultado%$this->numero2;
        }
    }else{
        
$this->error .= "El operador no es valido<br>";
    }
    
    if(!empty(
$this->error)){
        
$this->error .= "Han ocurrido los siguientes errores:<br>";
        
$this->retornar $this->error;
    }else{
        
$this->retornar $this->resultado;
    }
    return 
$this->retornar;
}

// fin de la clase calculadora


$cal = new calculadora;
echo 
$cal->calcular("2","+","3")."<br>".$cal->calcular2("*","2");
?>
__________________
Zepsilon.com
Diseño e imaginacion