-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.php
101 lines (78 loc) · 2.48 KB
/
test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
use PHPMathParser\Math;
require_once 'vendor/autoload.php';
$math = new Math();
//Positive Integer Tests
$answer = $math->evaluate('10 / 5');
var_dump($answer);echo "<br /><br />";
// int(2)
$answer = $math->evaluate('(2 + 3) * 4');
var_dump($answer);echo "<br /><br />";
// int(20)
$answer = $math->evaluate('1 + 2 * ((3 + 4) * 5 + 6)');
// 1 + 2 * (7 * 5 + 6)
// 1 + 2 * (35 + 6)
// 1 + 2 * 41
// 1 + 82
var_dump($answer);echo "<br /><br />";
// int(83)
$answer = $math->evaluate('9 * (3+8) - 6 - 45');
var_dump($answer);echo"<br /><br />";
// 99 - 6 - 45
// int(48)
$answer = $math->evaluate('1 * 2 + ((3 + 4) * 5 + 6)');
// 2 + (7 * 5 + 6)
// 2 + (35 + 6)
var_dump($answer);echo "<br /><br />";
// int(43)
$answer = $math->evaluate('(1 + 2) * (3 + 4) * (5 + 6)');
var_dump($answer);echo "<br /><br />";
// int(231)
$math->registerVariable('a', 4);
$answer = $math->evaluate('($a + 3) * 4');
var_dump($answer);echo "<br /><br />";
// int(28)
$math->registerVariable('a', 5);
$answer = $math->evaluate('($a + $a) * 4');
var_dump($answer);echo "<br /><br />";
// int(40)
//Float Tests
$answer = $math->evaluate('1.45 + 3');
var_dump($answer);echo "<br /><br />";
// float(4.45)
$answer = $math->evaluate('0.45 + 3.5');
var_dump($answer);echo "<br /><br />";
// float(3.95)
$answer = $math->evaluate('10.6 / 1.2');
var_dump($answer);echo "<br /><br />";
// float(8.83333333333)
$answer = $math->evaluate('(1.65 + 2) * (3.1415 + 4) * (5 + 6.8989)');
var_dump($answer);echo "<br /><br />";
// float(310.162379378) (but 310.1623793775 in Apple and Windows Calculators)
$math->registerVariable('a', 5.36464);
$answer = $math->evaluate('($a + $a) * 4');
var_dump($answer);echo "<br /><br />";
// float(42.91712)
//Neg Unary Operator Tests
$answer = $math->evaluate('3 - -3');
var_dump($answer);echo " <br /><br />";
//int(6)
$answer = $math->evaluate('-2 + -3');
var_dump($answer);echo " <br /><br />";
//int(-5)
$answer = $math->evaluate('-2.5 / 0.5');
var_dump($answer);echo "<br /><br />";
//float(-5)
$answer = $math->evaluate('-9 * (-3+8) - 6 - -45');
var_dump($answer);echo "<br /><br />";
//int(-6)
$answer = $math->evaluate('(10 / 5 * -(1 + 2))');
var_dump($answer);echo "<br /><br />";
//int(-6)
$answer = $math->evaluate('-7.3 * (-3.2+8) - 6 - -45.5');
var_dump($answer);echo "<br /><br />";
// float(4.46)
$math->registerVariable('a', -5.5);
$answer = $math->evaluate('($a + $a) * 4');
var_dump($answer);echo "<br /><br />";
// float(-44)