-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
199 lines (147 loc) · 5.17 KB
/
index.html
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>JS表达式运算</title>
</head>
<body>
<h1>请使用Chrome并按F12进行调试和测试</h1>
<pre>
var context = new CalContext();
//增加值栈——变量
context.putData("$a",5);
//增加值栈——变量
context.putData("PI",3);
//增加值栈——无参数函数
context.putData("fun0",function(){ return 1000;});
//增加值栈——单参数函数
context.putData("fun1",function(a){ return 10 * a;});
//增加值栈——多参数函数
context.putData("fun2",function(a,b){ return 10 * a + b;});
//增加值栈——对象参数函数
var t = {
year: 28,
fun: function(a) {
return a + this.year;
}
};
context.putData("fun3",t.fun,t);
//增加值栈——对象
context.putData("people",{
age: 28,
bat: 3,
fun1: function(){
return this.age + 2;
},
fun2: function(a){
return this.age + 10 * a;
},
fun3: function(a,b){
return this.age + 10 * a + b;
},
skill: {
lang : function(a){
return a + this.bat;
}
}
});
//获取值
//var result = context.calc(expr);
//下面是测试用例
context.assertEqual("40+8*2",56);
context.assertEqual("40*8+2",322);
context.assertEqual("40/8+2",7);
context.assertEqual("40+8/2",44);
context.assertEqual("(40+8)/2",24);
context.assertEqual("40*(8-2)",240);
context.assertEqual("10%3 + 1",2);
context.assertEqual("0.1 + 1 + 0.3",1.4);
context.assertEqual("0.1 *10 + 6.3",7.3);
context.assertEqual("$a + 5",10);
context.assertEqual("($a + 1)/2 + 1",4);
context.assertEqual("$a + fun0()",1005);
context.assertEqual("$a + fun0() + 100",1105);
context.assertEqual("$a + fun1(1)",15);
context.assertEqual("$a + fun2(2,1)",26);
context.assertEqual("$a + fun2(3+7,3)",108);
context.assertEqual("$a + fun2(fun1(2),3) + 20",228);
context.assertEqual("$a + fun3(2)",35);
context.assertEqual("11 + people.age / 2 + 6",31);
context.assertEqual("11 + people.age / 2 + fun1(1)",35);
context.assertEqual("11 + fun1(people.age / 2)/2 + 1",82);
context.assertEqual("$a + fun3(2) + people.fun1(1)",65);
context.assertEqual("$a + fun3(2) + people.skill.lang(1)",39);
//10 + (28+2) + 106 * 2 + 24 = 276
context.assertEqual("10 + fun3(2) + people.skill.lang(fun2(3+7,3)) * 2 + 24",276);
context.assertEqual("PI * 2 + 3",9);
</pre>
<script type="text/javascript" src="expression.js"></script>
<script>
var context = new CalContext();
//增加值栈——变量
context.putData("$a",5);
//增加值栈——变量
context.putData("PI",3);
//增加值栈——无参数函数
context.putData("fun0",function(){ return 1000;});
//增加值栈——单参数函数
context.putData("fun1",function(a){ return 10 * a;});
//增加值栈——多参数函数
context.putData("fun2",function(a,b){ return 10 * a + b;});
//增加值栈——对象参数函数
var t = {
year: 28,
fun: function(a) {
return a + this.year;
}
};
context.putData("fun3",t.fun,t);
//增加值栈——对象
context.putData("people",{
age: 28,
bat: 3,
fun1: function(){
return this.age + 2;
},
fun2: function(a){
return this.age + 10 * a;
},
fun3: function(a,b){
return this.age + 10 * a + b;
},
skill: {
lang : function(a){
return a + this.bat;
}
}
});
context.assertEqual("40+8*2",56);
context.assertEqual("40*8+2",322);
context.assertEqual("40/8+2",7);
context.assertEqual("40+8/2",44);
context.assertEqual("(40+8)/2",24);
context.assertEqual("40*(8-2)",240);
context.assertEqual("10%3 + 1",2);
context.assertEqual("0.1 + 1 + 0.3",1.4);
context.assertEqual("0.1 *10 + 6.3",7.3);
context.assertEqual("$a + 5",10);
context.assertEqual("($a + 1)/2 + 1",4);
context.assertEqual("$a + fun0()",1005);
context.assertEqual("$a + fun0() + 100",1105);
context.assertEqual("$a + fun1(1)",15);
context.assertEqual("$a + fun2(2,1)",26);
context.assertEqual("$a + fun2(3+7,3)",108);
context.assertEqual("$a + fun2(fun1(2),3) + 20",228);
context.assertEqual("$a + fun3(2)",35);
context.assertEqual("11 + people.age / 2 + 6",31);
context.assertEqual("11 + people.age / 2 + fun1(1)",35);
context.assertEqual("11 + fun1(people.age / 2)/2 + 1",82);
context.assertEqual("$a + fun3(2) + people.fun1(1)",65);
context.assertEqual("$a + fun3(2) + people.skill.lang(1)",39);
//10 + (28+2) + 106 * 2 + 24 = 276
context.assertEqual("10 + fun3(2) + people.skill.lang(fun2(3+7,3)) * 2 + 24",276);
context.assertEqual("PI * 2 + 3",9);
</script>
</body>
</html>