-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.html
85 lines (83 loc) · 2.74 KB
/
calculator.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
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="./complex.js"></script>
<script src="./complex_grammar.js"></script>
<script>
$(function(){
var f0 = "z**2";
var x0 = "2";
var y0 = "0";
var f = null;
var z = null;
function refresh_f() {
try {
f = expression.parse($("#f").val());
$("#f").removeClass("parseError");
} catch(err) {
$("#f").addClass("parseError");
}
}
function refresh_z() {
var _z = new Complex(
parseFloat($("#x").val(), 10),
parseFloat($("#y").val(), 10)
);
if(!Complex.isNaN(_z)) {
z = _z;
$(this).removeClass("parseError");
} else {
$(this).addClass("parseError");
}
}
$("#f").change(refresh_f);
$("#x, #y").change(refresh_z);
$("#f, #x, #y").change(function(){
$("#output").val(f(z));
$("#code").val(f.code);
});
(function init() {
$("#f").val(f0);
$("#x").val(x0);
$("#y").val(y0);
refresh_f();
refresh_z();
$("#f, #x, #y").trigger('change')
.keyup(function(){$(this).trigger('change')});
})();
});
</script>
<style>
body {
font-family: "Monaco", courier;
}
input {
font: inherit;
border: 1px solid #AAA;
background-color: #FFF;
}
input.parseError {
background-color: #FEE;
}
#x, #y {
width: 60px;
}
#f {
width: 400px;
}
#output {
width: 400px;
}
#code {
width: 400px;
height: 100px;
}
</style>
</head>
<body>
<p>f(z) = <input type="text" id="f"/></p>
<p>f(<input type="text" id="x"> + <input type="text" id="y">i) = <input type="text" disabled="true" id="output" /></p>
<textarea id="code"></textarea>
</body>
</html>