项目中用到,主要用于在一个TEXTBOX中实现类似EXCEL中的公式功能,随手写了一个,没怎么调试,可能问题比较多,主要是用着方便就行;记录下来,可能会用到。
计算(不考虑括号)
1 function JSQ(obj)
2 {
3
var strExpression = obj.value;
4
if(strExpression=="")
5
{
6
return;
7
}
8
if(strExpression.substr(0,1)=="=")
9
{
10
var expression = strExpression.substr(1)
11
if(expression != "")
12
{
13
obj.value = JsMath(expression);
14
}
15
}
16 }
17
18 function JsMath(strExpression)
19 {
20
var flVal = parseFloat(0);
21
var MathExpression = strExpression.split("+");
22
for(var i=0;i<MathExpression.length;i++)
23
{
24
if(i==0)
25
flVal = parseFloat(JsMathLess(MathExpression[i]));
26
else
27
flVal = flVal + parseFloat(JsMathLess(MathExpression[i]));
28
}
29
return flVal;
30 }
31 function JsMathLess(strExpression)
32 {
33
var flVal = parseFloat(0);
34
var MathExpression = strExpression.split("-");
35
for(var i=0;i<MathExpression.length;i++)
36
{
37
if(i==0)
38
flVal = parseFloat(JsMathMult(MathExpression[i]));
39
else
40
flVal = flVal - parseFloat(JsMathMult(MathExpression[i]));
41
}
42
return flVal;
43 }
44 function JsMathMult(strExpression)
45 {
46
var flVal = parseFloat(0);
47
var MathExpression = strExpression.split("*");
48
for(var i=0;i<MathExpression.length;i++)
49
{
50
if(i==0)
51
flVal = parseFloat(JsMathAddu(MathExpression[i]));
52
else
53
flVal = flVal * parseFloat(JsMathAddu(MathExpression[i]));
54
}
55
return flVal;
56 }
57 function JsMathAddu(strExpression)
58 {
59
var flVal = parseFloat(0);
60
var MathExpression = strExpression.split("http://www.likecs.com/");
61
for(var i=0;i<MathExpression.length;i++)
62
{
63
if(i==0)
64
flVal = parseFloat(MathExpression[i]);
65
else
66
flVal = flVal / parseFloat(MathExpression[i]);
67
}
68
return flVal;
69 }