Advertisement
Cherry83

11.SuperCalculator

Feb 17th, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.15 KB | None | 0 0
  1. <?php
  2.  
  3. $result = [];
  4. $finally = false;
  5.  
  6. function sum()
  7. {
  8.     global $result, $finally;
  9.     if ($finally) {
  10.         if (count($result) > 0)
  11.             echo array_sum($result);
  12.         return true;
  13.     } else {
  14.         $n1 = floatval(trim(fgets(STDIN)));
  15.         $n2 = floatval(trim(fgets(STDIN)));
  16.         $result[] = $n1 + $n2;
  17.     }
  18. }
  19.  
  20. function multiply()
  21. {
  22.     global $result, $finally;
  23.     if ($finally) {
  24.         if (count($result) > 0)
  25.             echo array_product($result);
  26.         return true;
  27.     } else {
  28.         $n1 = floatval(trim(fgets(STDIN)));
  29.         $n2 = floatval(trim(fgets(STDIN)));
  30.         $result[] = $n1 * $n2;
  31.     }
  32. }
  33.  
  34. function divide()
  35. {
  36.     global $result, $finally;
  37.     if ($finally) {
  38.         $temp = $result;
  39.         while (count($temp) > 1) {
  40.             if ($temp[1] == 0) {
  41.                 echo "Caught exception: Division by zero.\n";
  42.                 return false;
  43.             }
  44.             $temp[] = $temp[0] / $temp[1];
  45.             unset($temp[0]);
  46.             unset($temp[1]);
  47.             $temp = array_values($temp);
  48.         }
  49.         echo $temp[0];
  50.         return true;
  51.     } else {
  52.         $n1 = floatval(trim(fgets(STDIN)));
  53.         $n2 = floatval(trim(fgets(STDIN)));
  54.         if ($n2 == 0) {
  55.             echo "Caught exception: Division by zero.\n";
  56.         } else {
  57.             $result[] = $n1 / $n2;
  58.         }
  59.     }
  60. }
  61.  
  62. function subtract()
  63. {
  64.     global $result, $finally;
  65.     if ($finally) {
  66.         $temp = $result;
  67.         while (count($temp) > 1) {
  68.             $temp[] = $temp[0] - $temp[1];
  69.             unset($temp[0]);
  70.             unset($temp[1]);
  71.             $temp = array_values($temp);
  72.         }
  73.         echo $temp[0];
  74.         return true;
  75.     } else {
  76.         $n1 = floatval(trim(fgets(STDIN)));
  77.         $n2 = floatval(trim(fgets(STDIN)));
  78.         $result[] = $n1 - $n2;
  79.     }
  80. }
  81.  
  82. function fact($n)
  83. {
  84.     $res = 1;
  85.     while ($n > 1) {
  86.         $res *= $n;
  87.         $n--;
  88.     }
  89.     return $res;
  90. }
  91.  
  92. function factorial()
  93. {
  94.     global $result, $finally;
  95.     if ($finally) {
  96.         $temp = [];
  97.         foreach ($result as $value) {
  98.             $temp[] = fact($value);
  99.         }
  100.         echo implode(", ", $temp);
  101.         return true;
  102.     } else {
  103.         $n = floatval(trim(fgets(STDIN)));
  104.         $result[] = fact($n);
  105.     }
  106. }
  107.  
  108. function root()
  109. {
  110.     global $result, $finally;
  111.     if ($finally) {
  112.         $temp = [];
  113.         foreach ($result as $value) {
  114.             if ($value < 0) {
  115.                 echo "Caught exception: Can't take the root of a negative number\n";
  116.                 return false;
  117.             }
  118.             $temp[] = sqrt($value);
  119.         }
  120.         echo implode(", ", $temp);
  121.         return true;
  122.     } else {
  123.         $n = floatval(trim(fgets(STDIN)));
  124.         if ($n < 0) {
  125.             echo "Caught exception: Can't take the root of a negative number\n";
  126.         } else {
  127.             $result[] = sqrt($n);
  128.         }
  129.     }
  130. }
  131.  
  132. function power()
  133. {
  134.     global $result, $finally;
  135.     if ($finally) {
  136.         while (count($result) > 1) {
  137.             $result[] = pow($result[0], $result[1]);
  138.             unset($result[0]);
  139.             unset($result[1]);
  140.             $result = array_values($result);
  141.         }
  142.         echo $result[0];
  143.         return true;
  144.     } else {
  145.         $n1 = floatval(trim(fgets(STDIN)));
  146.         $n2 = floatval(trim(fgets(STDIN)));
  147.         $result[] = pow($n1, $n2);
  148.     }
  149. }
  150.  
  151. function absolute()
  152. {
  153.     global $result, $finally;
  154.     if ($finally) {
  155.         for ($i = 0; $i < count($result); $i++) {
  156.             $result[$i] = abs($result[$i]);
  157.         }
  158.         echo implode(", ", $result);
  159.         return true;
  160.     } else {
  161.         $n = floatval(trim(fgets(STDIN)));
  162.         $result[] = abs($n);
  163.     }
  164. }
  165.  
  166. function pythagorean()
  167. {
  168.     global $result, $finally;
  169.     if ($finally) {
  170.         while (count($result) > 1) {
  171.             $result[] = sqrt(pow($result[0], 2) + pow($result[1], 2));
  172.             unset($result[0]);
  173.             unset($result[1]);
  174.             $result = array_values($result);
  175.         }
  176.         echo $result[0];
  177.         return true;
  178.     } else {
  179.         $n1 = floatval(trim(fgets(STDIN)));
  180.         $n2 = floatval(trim(fgets(STDIN)));
  181.         $result[] = sqrt(pow($n1, 2) + pow($n2, 2));
  182.     }
  183. }
  184.  
  185. function triangleArea()
  186. {
  187.     global $result, $finally;
  188.     if ($finally) {
  189.         $temp = $result;
  190.         while (count($temp) > 2) {
  191.             $a = $temp[0];
  192.             $b = $temp[1];
  193.             $c = $temp[2];
  194.             unset($temp[0]);
  195.             unset($temp[1]);
  196.             unset($temp[2]);
  197.             $temp = array_values($temp);
  198.             $s = ($a + $b + $c) / 2;
  199.             $area = sqrt($s * ($s - $a) * ($s - $b) * ($s - $c));
  200.             if (is_nan($area)) {
  201.                 echo "Caught exception: Can't take the root of a negative number\n";
  202.                 return false;
  203.             }
  204.             $temp[] = $area;
  205.         }
  206.         echo implode(", ", $temp);
  207.         return true;
  208.     } else {
  209.         $a = floatval(trim(fgets(STDIN)));
  210.         $b = floatval(trim(fgets(STDIN)));
  211.         $c = floatval(trim(fgets(STDIN)));
  212.         $s = ($a + $b + $c) / 2;
  213.         $area = sqrt($s * ($s - $a) * ($s - $b) * ($s - $c));
  214.         if ($area == NAN) {
  215.             echo "Caught exception: Can't take the root of a negative number\n";
  216.         } else {
  217.             $result[] = $area;
  218.         }
  219.     }
  220. }
  221.  
  222. function quadratic()
  223. {
  224.     global $result, $finally;
  225.     if ($finally) {
  226.         // 10-Ρ‚ΠΈ ОК
  227.         $temp = $result;
  228.         while (count($temp) > 2) {
  229.             $a = $temp[0];
  230.             $b = $temp[1];
  231.             $c = $temp[2];
  232.             unset($temp[0]);
  233.             unset($temp[1]);
  234.             unset($temp[2]);
  235.             $temp = array_values($temp);
  236.             if ($a == 0) {
  237.                 echo "Caught exception: Division by zero. \n";
  238.                 return false;
  239.             }
  240.             $d = $b * $b - 4 * $a * $c;
  241.             if ($d <= 0) {
  242.                 $temp[] = 0;
  243.             } elseif ($d == 0) {
  244.                 $temp[] = (-$b / 2 * $a);
  245.             } else {
  246.                 $temp[] = ((-$b + sqrt($d)) / (2 * $a)) + ((-$b - sqrt($d)) / (2 * $a));
  247.             }
  248.         }
  249.         echo implode(", ", $temp);
  250.         return true;
  251.     } else {
  252.         $a = floatval(trim(fgets(STDIN)));
  253.         $b = floatval(trim(fgets(STDIN)));
  254.         $c = floatval(trim(fgets(STDIN)));
  255.         if ($a == 0) {
  256.             echo "Caught exception: Division by zero. \n";
  257.         } else {
  258.             $d = $b * $b - 4 * $a * $c;
  259.             if ($d <= 0) {
  260.                 $result[] = 0;
  261.             } elseif ($d == 0) {
  262.                 $result[] = -$b / 2 * $a;
  263.             } else {
  264.                 $result[] = ((-$b + sqrt($d)) / (2 * $a)) + ((-$b - sqrt($d)) / (2 * $a));
  265.             }
  266.         }
  267.     }
  268. }
  269.  
  270. while (true) {
  271.     $command = trim(fgets(STDIN));
  272.     if ($command == 'finally') {
  273.         break;
  274.     }
  275.     if (function_exists($command))
  276.         $command();
  277. }
  278.  
  279.  
  280. $finally = true;
  281.  
  282. while (true) {
  283.     $command = trim(fgets(STDIN));
  284.     if (function_exists($command))
  285.         if ($command()) break;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement