php-rekenmachine
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
class calc {
const numregex = '[0-9]*\\.?[0-9]+';
const grpregex = '\\([^()]*\\)';
public function format($input) {
return preg_replace('/[^\\/\\-0-9.+*()^=]+/','',$input);
}
public function evaluate($input, $format = true) {
if($format) {
$input = $this->format($input);
}
//Remove double positive/negative identifiers.
do{$input=str_replace(array('+-','-+'),'-',$input,$count);}while($count);
do{$input=str_replace(array('++','--'),'+',$input,$count);}while($count);
//Evaluate input between '(' and ')' operators
while(preg_match('/'.self::grpregex.'/',$input,$match,PREG_OFFSET_CAPTURE)) {
$input =
substr($input,0,$match[0][1])
.$this->evaluate(substr($match[0][0],1,-1),false)
.substr($input,$match[0][1]+strlen($match[0][0]));
}
//Calculate the Y'th power of X
while(preg_match('/('.self::numregex.')\\^([+-]?'.self::numregex.')/',$input,$match,PREG_OFFSET_CAPTURE)) {
$y = pow(floatval($match[1][0]),floatval($match[2][0]));
$input = substr($input,0,$match[0][1]).$y.substr($input,$match[0][1]+strlen($match[0][0]));
}
//Multiply and Divisions
while(preg_match('/('.self::numregex.')([\\*\\/])([+-]?'.self::numregex.')/',$input,$match,PREG_OFFSET_CAPTURE)) {
if($match[2][0]=='*') {
$y = floatval($match[1][0])*floatval($match[3][0]);
} elseif($match[2][0]=='/') {
$y = floatval($match[1][0])/floatval($match[3][0]);
}
$input = substr($input,0,$match[0][1]).$y.substr($input,$match[0][1]+strlen($match[0][0]));
}
//Add and Substract
while(preg_match('/('.self::numregex.')([+-])([+-]?'.self::numregex.')/',$input,$match,PREG_OFFSET_CAPTURE)) {
if($match[2][0]=='+') {
$y = floatval($match[1][0])+floatval($match[3][0]);
} elseif($match[2][0]=='-') {
$y = floatval($match[1][0])-floatval($match[3][0]);
}
$input = substr($input,0,$match[0][1]).$y.substr($input,$match[0][1]+strlen($match[0][0]));
}
if(preg_match('/([+-]?'.self::numregex.')=([+-]?'.self::numregex.')/',$input,$match)) {
$input = (floatval($match[1][0])==floatval($match[2][0]));
} else {
$input = floatval($input);
}
return $input;
}
public static function ulate($input) {
return calc::ulator()->evaluate($input);
}
public static function ulator() {
if(!isset($GLOBALS['_calc'])) {
$GLOBALS['_calc'] = new self;
}
return $GLOBALS['_calc'];
}
}
?>
class calc {
const numregex = '[0-9]*\\.?[0-9]+';
const grpregex = '\\([^()]*\\)';
public function format($input) {
return preg_replace('/[^\\/\\-0-9.+*()^=]+/','',$input);
}
public function evaluate($input, $format = true) {
if($format) {
$input = $this->format($input);
}
//Remove double positive/negative identifiers.
do{$input=str_replace(array('+-','-+'),'-',$input,$count);}while($count);
do{$input=str_replace(array('++','--'),'+',$input,$count);}while($count);
//Evaluate input between '(' and ')' operators
while(preg_match('/'.self::grpregex.'/',$input,$match,PREG_OFFSET_CAPTURE)) {
$input =
substr($input,0,$match[0][1])
.$this->evaluate(substr($match[0][0],1,-1),false)
.substr($input,$match[0][1]+strlen($match[0][0]));
}
//Calculate the Y'th power of X
while(preg_match('/('.self::numregex.')\\^([+-]?'.self::numregex.')/',$input,$match,PREG_OFFSET_CAPTURE)) {
$y = pow(floatval($match[1][0]),floatval($match[2][0]));
$input = substr($input,0,$match[0][1]).$y.substr($input,$match[0][1]+strlen($match[0][0]));
}
//Multiply and Divisions
while(preg_match('/('.self::numregex.')([\\*\\/])([+-]?'.self::numregex.')/',$input,$match,PREG_OFFSET_CAPTURE)) {
if($match[2][0]=='*') {
$y = floatval($match[1][0])*floatval($match[3][0]);
} elseif($match[2][0]=='/') {
$y = floatval($match[1][0])/floatval($match[3][0]);
}
$input = substr($input,0,$match[0][1]).$y.substr($input,$match[0][1]+strlen($match[0][0]));
}
//Add and Substract
while(preg_match('/('.self::numregex.')([+-])([+-]?'.self::numregex.')/',$input,$match,PREG_OFFSET_CAPTURE)) {
if($match[2][0]=='+') {
$y = floatval($match[1][0])+floatval($match[3][0]);
} elseif($match[2][0]=='-') {
$y = floatval($match[1][0])-floatval($match[3][0]);
}
$input = substr($input,0,$match[0][1]).$y.substr($input,$match[0][1]+strlen($match[0][0]));
}
if(preg_match('/([+-]?'.self::numregex.')=([+-]?'.self::numregex.')/',$input,$match)) {
$input = (floatval($match[1][0])==floatval($match[2][0]));
} else {
$input = floatval($input);
}
return $input;
}
public static function ulate($input) {
return calc::ulator()->evaluate($input);
}
public static function ulator() {
if(!isset($GLOBALS['_calc'])) {
$GLOBALS['_calc'] = new self;
}
return $GLOBALS['_calc'];
}
}
?>