Advertisement
WolandV

Untitled

Nov 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <?php
  2.  
  3. class Calc
  4. {
  5.     public $result;
  6.  
  7.     public function __construct($string)
  8.     {
  9.         $this->parse($string);
  10.     }
  11.  
  12.     protected function parse($string)
  13.     {
  14.         while (true) {
  15.             preg_match('/\(([^()]*)\)/', $string, $matches);
  16.             if (!count($matches)) {
  17.                 break;
  18.             }
  19.             $replace = $this->calculate($matches[1]);
  20.             $string = str_replace($matches[0], $replace, $string);
  21.         }
  22.         $this->result = $this->calculate($string);
  23.     }
  24.  
  25.     protected function calculate($string)
  26.     {
  27.         $operations = ['*' => 'mul', '/' => 'div'];
  28.  
  29.         foreach ($operations as $operation => $method) {
  30.             while (true) {
  31.                 preg_match('/(-?\d+) \\' . $operation . ' (-?\d+)/', $string, $matches);
  32.                 if (!count($matches)) {
  33.                     break;
  34.                 }
  35.                 $string = $this->$method($string, $matches);
  36.             }
  37.         }
  38.  
  39.         while (true) {
  40.             preg_match('/(-?\d+) (\+|-) (-?\d+)/', $string, $matches);
  41.             if (!count($matches)) {
  42.                 break;
  43.             }
  44.             if ($matches[2] == '+') {
  45.                 $string = $this->sum($string, $matches);
  46.             } else {
  47.                 $string = $this->sub($string, $matches);
  48.             }
  49.         }
  50.  
  51.         return $string;
  52.     }
  53.  
  54.     protected function mul($string, $matches)
  55.     {
  56.         return str_replace($matches[0], $matches[1] * $matches[2], $string);
  57.     }
  58.  
  59.     protected function div($string, $matches)
  60.     {
  61.         return str_replace($matches[0], $matches[1] / $matches[2], $string);
  62.     }
  63.  
  64.     protected function sum($string, $matches)
  65.     {
  66.         return str_replace($matches[0], $matches[1] + $matches[3], $string);
  67.     }
  68.  
  69.     protected function sub($string, $matches)
  70.     {
  71.         var_dump($matches);
  72.         return str_replace($matches[0], $matches[1] - $matches[3], $string);
  73.     }
  74. }
  75.  
  76. $calc = new Calc('3 * 5 + 7 * 2');
  77. var_dump('3 * 5 + 7 * 2 = ' . $calc->result);
  78.  
  79. $calc = new Calc('5 * (7 * (5 + 3) / 4 - 4)');
  80. var_dump('5 * (7 * (5 + 3) / 4 - 4) = ' . $calc->result);
  81.  
  82. $calc = new Calc('(2 * (2 * 2 * (2 * 2)))');
  83. var_dump('(2 * (2 * 2 * (2 * 2))) = ' . $calc->result);
  84.  
  85. $calc = new Calc('-5 + (3 * (-2 - 4))');
  86. var_dump('-5 + (3 * (-2 - 4)) = ' . $calc->result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement