-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathString.php
More file actions
132 lines (111 loc) · 4.01 KB
/
MathString.php
File metadata and controls
132 lines (111 loc) · 4.01 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
define("DEBUG", 0);
class MathString
{
/*
* calculate the passed string and solve the math expression
* only takes binary operators
* @math_string string
* @return integer
*/
function calculate($math_string)
{
if(DEBUG)
{
echo $math_string;
echo PHP_EOL;
}
$split_string = str_split($math_string);
//echo "<pre>";echo "string";echo PHP_EOL;
//print_r($math_string);
// array for numbers
$values = array();
// array for Operators
$operators = array();
for ($i = 0; $i < count($split_string); $i++) {
if ($split_string[$i] == ' ') //if we have a white space or a space string
continue;
// Current string is a number, push it to array for numbers
if ($split_string[$i] >= '0' && $split_string[$i] <= '9') {
$single_string = '';
// There may be more than one digits in number
while ($i < count($split_string) && $split_string[$i] >= '0' && $split_string[$i] <= '9'){
$single_string.=$split_string[$i];
$i+=1;
}
$i-=1;
// echo "number-".$single_string;echo PHP_EOL;
array_unshift($values, $single_string);
}
else if ($split_string[$i] == '(') {
//echo "(-";echo PHP_EOL;
array_unshift($operators, $split_string[$i]);
}
// if colsing braces found solve the problem
else if ($split_string[$i] == ')') {
//echo ")-";echo PHP_EOL;
while ($operators[0] != '('){
array_unshift($values, $this->calculate_two_strings(array_shift($operators), array_shift($values), array_shift($values)));
}
array_shift($operators);
}
// operator found.
else if ($split_string[$i] == '+' || $split_string[$i] == '-' || $split_string[$i] == '*' || $split_string[$i] == '/') {
// echo "operator found";echo PHP_EOL;
while (!empty($operators) && $this->has_high_val($split_string[$i], $operators[0]))
array_unshift($values, $this->calculate_two_strings(array_shift($operators), array_shift($values), array_shift($values)));
array_unshift($operators, $split_string[$i]);
}
$this->printStack($values);
//print_r($operators);
}
// solve remaining equation
while (!empty($operators))
array_unshift($values, $this->calculate_two_strings(array_shift($operators), array_shift($values), array_shift($values)));
$this->printStack($values);
//print_r($operators);
// return result
return array_shift($values);
}
function has_high_val($op1, $op2)
{
if ($op2 == '(' || $op2 == ')')
return false;
if (($op1 == '*' || $op1 == '/') && ($op2 == '+' || $op2 == '-'))
return false;
else
return true;
}
function calculate_two_strings($operator, $s2, $s1)
{
$this->printStack('performing-'.$s1.$operator.$s2);
switch ($operator)
{
case '+':
return $s1 + $s2;
case '-':
return $s1 - $s2;
case '*':
return $s1 * $s2;
case '/':
if ($s2 == 0)
die("Invalid");
return $s1 / $s2;
}
return 0;
}
function printStack($a)
{
if(DEBUG)
{
echo PHP_EOL."<pre>";
print_r($a);
echo "</pre>".PHP_EOL;
}
}
}
//create an object of the class
$mathClass = new solveMathString();
//print the result
echo $exp = $mathClass->calculate("(20*(30+ 10) / (10*2))");
?>