Advertisement
MBrendecke

Termrechner

Dec 11th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <!--
  3. To change this license header, choose License Headers in Project Properties.
  4. To change this template file, choose Tools | Templates
  5. and open the template in the editor.
  6. -->
  7. <html>
  8.     <head>
  9.         <meta charset="UTF-8">
  10.         <title>Zahlen</title>
  11.     </head>
  12.     <body>
  13.         <?php
  14.  
  15.         class Calculator {
  16.  
  17.             public function Calc($a, $b, $operator) {
  18.                 switch ($operator) {
  19.                     case "*":
  20.                         return $a * $b;
  21.                     case "/":
  22.                         return $a / $b;
  23.                     case "+":
  24.                         return $a + $b;
  25.                     case "-":
  26.                         return $a - abs($b);
  27.                 }
  28.             }
  29.  
  30.             public function Aufgabe1($function, $value) {
  31.                 $function = str_replace(array('x', 'X'), $value, $function);
  32.                 $function = str_replace(" ", "", $function);
  33.  
  34.                 $filterDigits = "-?([0-9]*\.[0-9]+|[0-9]+)";
  35.                 $filterOperators = "*/+-";
  36.                 $filterTestSubterm = "~\((" . $filterDigits . "[" . $filterOperators . "]" . $filterDigits . ")[^)]*\)~";
  37.  
  38.                 preg_match_all($filterTestSubterm, $function, $valuesInBraches);
  39.  
  40.                 foreach ($valuesInBraches[0] as $valueInBrache) {
  41.                     $temp = $valueInBrache;
  42.                     $temp = str_replace(array("(", ")"), "", $temp);
  43.  
  44.                     $return = $this->Aufgabe1($temp, $value);
  45.                     $function = str_replace($valueInBrache, $return, $function);
  46.                 }
  47.  
  48.                 $function = str_replace(['(', ')'], "", $function);
  49.  
  50.                 foreach (str_split($filterOperators) as $filterOperator) {
  51.                     $filter = $filterDigits . "\\" . $filterOperator . $filterDigits;
  52.                     $filter = "~" . $filter . "~";
  53.  
  54.                     while (true) {
  55.                         $subTermCount = preg_match($filter, $function, $subTerm);
  56.  
  57.                         if ($subTermCount == 0) {
  58.                             break;
  59.                         }
  60.                         $subTermValuesCount = preg_match_all("~" . $filterDigits . "~", $subTerm[0], $subTermValues);
  61.  
  62.                         if ($filterOperator == '/' && $subTermValues[1] == 0) {
  63.                             return "Asymptote";
  64.                         }
  65.  
  66.                         $ergebnis = $this->Calc($subTermValues[0][0], $subTermValues[0][1], $filterOperator);
  67.  
  68.                         $function = str_replace($subTerm[0], $ergebnis >= 0 ? "+" . $ergebnis : $ergebnis, $function);
  69.  
  70.                         $function = str_replace("-+", "-", $function);
  71.                         $function = str_replace("+-", "-", $function);
  72.                         $function = str_replace("--", "+", $function);
  73.                         $function = str_replace("++", "+", $function);
  74.                     }
  75.                 }
  76.  
  77.                 return ltrim($function, "+");
  78.             }
  79.  
  80.         }
  81.  
  82.         $function = "((x * x) - 2 / (x + 8 * x) + 15)";
  83.         $value = -15;
  84.  
  85.         $calc = new Calculator();
  86.         echo $calc->Aufgabe1($function, $value);
  87.         ?>
  88.     </body>
  89. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement