Advertisement
Guest User

calculator

a guest
Jul 24th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2.  
  3. class Calculator {
  4.  
  5.   public $number1;
  6.   public $number2;
  7.   public $op_type; // operation type . + . - . * . /
  8.  
  9.   function set_value($n1,$n2)
  10.   {
  11.     $this -> number1 = $n1;
  12.     $this -> number2 = $n2;
  13.   }
  14.  
  15.   function setop($op) // set operation
  16.   {
  17.     $this -> op_type = $op;
  18.   }
  19.  
  20.   function Calculate()
  21.   {
  22.     switch ($this -> op_type)
  23.     {
  24.       case '+';
  25.       echo $this -> number1 + $this -> number2;
  26.       break;
  27.  
  28.       case '-';
  29.       echo $this -> number1 - $this -> number2;
  30.       break;
  31.  
  32.       case '*';
  33.       echo $this -> number1 * $this -> number2;
  34.       break;
  35.  
  36.       case '/';
  37.       if ($this -> number2 == 0)
  38.       echo " Div by zer0";
  39.       else
  40.       echo $this -> number1 / $this -> number2;
  41.       break;
  42.  
  43.  
  44.     }
  45.   }
  46. }
  47.  
  48. $v = new Calculator();
  49. $v->set_value(10,5);
  50. $v->setop('/');
  51. $v->Calculate();
  52.  
  53. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement