Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <!--
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
- -->
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Zahlen</title>
- </head>
- <body>
- <?php
- class Calculator {
- public function Calc($a, $b, $operator) {
- switch ($operator) {
- case "*":
- return $a * $b;
- case "/":
- return $a / $b;
- case "+":
- return $a + $b;
- case "-":
- return $a - abs($b);
- }
- }
- public function Aufgabe1($function, $value) {
- $function = str_replace(array('x', 'X'), $value, $function);
- $function = str_replace(" ", "", $function);
- $filterDigits = "-?([0-9]*\.[0-9]+|[0-9]+)";
- $filterOperators = "*/+-";
- $filterTestSubterm = "~\((" . $filterDigits . "[" . $filterOperators . "]" . $filterDigits . ")[^)]*\)~";
- preg_match_all($filterTestSubterm, $function, $valuesInBraches);
- foreach ($valuesInBraches[0] as $valueInBrache) {
- $temp = $valueInBrache;
- $temp = str_replace(array("(", ")"), "", $temp);
- $return = $this->Aufgabe1($temp, $value);
- $function = str_replace($valueInBrache, $return, $function);
- }
- $function = str_replace(['(', ')'], "", $function);
- foreach (str_split($filterOperators) as $filterOperator) {
- $filter = $filterDigits . "\\" . $filterOperator . $filterDigits;
- $filter = "~" . $filter . "~";
- while (true) {
- $subTermCount = preg_match($filter, $function, $subTerm);
- if ($subTermCount == 0) {
- break;
- }
- $subTermValuesCount = preg_match_all("~" . $filterDigits . "~", $subTerm[0], $subTermValues);
- if ($filterOperator == '/' && $subTermValues[1] == 0) {
- return "Asymptote";
- }
- $ergebnis = $this->Calc($subTermValues[0][0], $subTermValues[0][1], $filterOperator);
- $function = str_replace($subTerm[0], $ergebnis >= 0 ? "+" . $ergebnis : $ergebnis, $function);
- $function = str_replace("-+", "-", $function);
- $function = str_replace("+-", "-", $function);
- $function = str_replace("--", "+", $function);
- $function = str_replace("++", "+", $function);
- }
- }
- return ltrim($function, "+");
- }
- }
- $function = "((x * x) - 2 / (x + 8 * x) + 15)";
- $value = -15;
- $calc = new Calculator();
- echo $calc->Aufgabe1($function, $value);
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement