/
calculator.js
executable file
·176 lines (151 loc) · 4.39 KB
/
calculator.js
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
#!/usr/local/bin/elev8
var elm = require('elm');
var EXPAND_BOTH = { x : 1.0, y : 1.0 };
var FILL_BOTH = { x : -1.0, y : -1.0 };
var stack = new Array;
/* clear the display when the next button is pressed? */
var clear = 1;
var number_string;
function multiply(left, right) {
return left * right;
}
function divide(left, right) {
return left / right;
}
function add(left, right) {
return left + right;
}
function subtract(left, right) {
return left - right;
}
function push(val) {
stack.push(val);
}
function push_entry() {
push(number_string);
}
function set_entry(val) {
number_string = val;
calc.elements.vbox.elements.entry.label = "<align=right>" + number_string;
}
function append_entry(val) {
var cur;
if (clear)
cur = "0";
else
cur = number_string;
clear = 0;
if (val != '.' || -1 == cur.indexOf('.')) {
if (cur == "0" && val != '.')
cur = val;
else
cur += val;
}
set_entry(cur);
}
function append_number(b) {
append_entry(b.label);
}
function evaluate() {
right = stack.pop();
operand = stack.pop();
left = stack.pop();
if (typeof operand != 'function')
return;
if (typeof left != 'string' && typeof left != 'number')
return;
if (typeof right != 'string' && typeof right != 'number')
return;
return operand(parseFloat(left), parseFloat(right));
}
function any_button() {
this.type = "button";
this.weight = { x : -1, y : -1 };
}
function number_button(num) {
return elm.Button({
label : num,
on_click : append_number
});
}
number_button.prototype = new any_button;
function op_button(str, op) {
return elm.Button ({
label : str,
on_click : function () {
push_entry();
push(op);
clear = 1;
}
});
}
op_button.prototype = new any_button;
var calc = elm.realise(elm.Window({
title : "Calculator demo",
elements : {
the_background : elm.Background ({
weight : EXPAND_BOTH,
resize : true,
}),
vbox : elm.Box ({
resize : true,
elements : {
entry : elm.Entry ({
label : "<align=right>0",
align : { x : -1, y : 0 },
}),
hbox1 : elm.Box ({
horizontal : true,
homogeneous : true,
elements : {
b7 : number_button("7"),
b8 : number_button("8"),
b9 : number_button("9"),
divide : op_button("/", divide),
},
}),
hbox2 : elm.Box ({
horizontal : true,
homogeneous : true,
elements : {
b4 : number_button("4"),
b5 : number_button("5"),
b6 : number_button("6"),
multiply : op_button("*", multiply),
},
}),
hbox3 : elm.Box ({
horizontal : true,
homogeneous : true,
elements : {
b1 : number_button("1"),
b2 : number_button("2"),
b3 : number_button("3"),
subtract : op_button("-", subtract),
},
}),
hbox4 : elm.Box ({
horizontal : true,
homogeneous : true,
elements : {
b0 : number_button("0"),
bdot : number_button("."),
equals : elm.Button ({
type : "button",
label : "=",
weight : { x : -1.0, y : -1.0 },
on_click : function () {
push_entry();
set_entry("");
var answer = evaluate();
set_entry(answer);
clear = 1;
},
}),
add : op_button("+", add),
},
}),
},
}),
},
}));