-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.js
223 lines (220 loc) · 7.56 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// expression that's being built up. Always the same as the output display.
var expression = "";
$(document).ready(function(){
$('.numbutton').bind("click", function(event){
expression+=$(event.target).text()
$('#output').text(expression)
});
$('.clearbutton').bind("click", function(event){
expression=""
$('#output').text(expression)
});
$('#equals').bind("click", function(){
expression=calculate(expression)
$('#output').text(expression)
});
$('.plotbutton').bind("click", function(){
expression=graphcalc.graph(expression)
$('#output').text(expression)
});
});
function read_operand(tokenArray) {
var operand = tokenArray.shift();
if (operand == '(') {
operand = evaluate(tokenArray)
}
operand = parseInt(operand)
if (isNaN(operand)){
throw 'Number expected';
}
else {
return operand;
}
};
function evaluate(tokenArray) {
if (tokenArray.length<1) {
throw 'Missing operand';
}
var value = read_operand(tokenArray);
while (tokenArray.length>=1) {
var operator = tokenArray.shift();
var valid_operators = ['+', '-','*','/',')','(','\u00f7','\u00D7','\u2212'];
if ($.inArray(operator, valid_operators) == -1){ //jQuery.inArray is used to check if the operator is valid. If perator is not in the array valid_operators, inArray returns -1.
throw 'Invalid operator';
}
if (operator == ')') {
return value;
}
var tempOperand = read_operand(tokenArray);
//The following section is porbably not the best way to do this- it seems repetitive. I should replace this at some point.
if (operator == "+") {
value = value + tempOperand;
}
if ((operator == "-")||(operator=='\u2212')) {
value = value - tempOperand;
}
if ((operator == "*")||(operator=='\u00D7')) {
value = value * tempOperand;
}
if ((operator == "/") || (operator == '\u00f7')) {
value = value / tempOperand;
}
}
return value
}
function calculate(text) {
var pattern = /\d+|\+|\-|\*|\/|\(|\)|\u00F7|\u00D7|\u2212/g;
var tokens = text.match(pattern);
var value = evaluate(tokens);
if (tokens.length>0){
throw 'Ill formed expression';
}
else {
return value;
}
}
//var built_in_environment = {
// pi: Math.PI,
// e: Math.E,
// };
//function new_environment() {
// var env = {};
// for (var v in built_in_environment) {
// env[v] = built_in_environment[v];
// }
// return env;
//}
//exports.new_environment = new_environment;
//
//// if first token is t, consume it and return true
//function read_token(t, tokens) {
// if (tokens.length > 0 && tokens[0] == t) {
// tokens.shift();
// return true;
// }
// return false;
//}
//function parse_expression(tokens) {
// var expression = parse_term(tokens);
// while (true) {
// if (read_token('+', tokens)) {
// expression = ['+', expression, parse_term(tokens)];
// }
// else if (read_token('-', tokens)) {
// expression = ['-', expression, parse_term(tokens)];
// }
// else break;
// }
// return expression;
// }
//
// function parse_term(tokens) {
// var term = parse_exp(tokens);
// while (true) {
// if (read_token('*', tokens)) {
// term = ['*', term, parse_exp(tokens)];
// }
// else if (read_token('/', tokens)) {
// term = ['/', term, parse_exp(tokens)];
// }
// else break;
// }
// return term;
// }
//
// function parse_exp(tokens) {
// var term = parse_unary(tokens);
// while (true) {
// if (read_token('^', tokens)) {
// term = ['^', term, parse_unary(tokens)];
// }
// else break;
// }
// return term;
// }
//
// function parse_unary(tokens) {
// if (read_token('-', tokens)) {
// return ['neg', parse_factor(tokens)];
// }
// else if (read_token('+', tokens)) {}
// return parse_factor(tokens);
// }
//
//
// function parse_factor(tokens) {
// if (read_token('(', tokens)) {
// var exp = parse_expression(tokens);
// if (read_token(')', tokens)) {
// return exp;
// }
// else throw 'Missing ) in expression';
// }
// else if (tokens.length > 0) {
// var token = tokens.shift();
// if (token.search(/[a-zA-Z_]\w*/) != -1) {
// // variable name
// if (read_token('(', tokens)) {
// // a function call, parse the argument(s)
// var args = [];
// // code assumes at least one argument
// while (true) {
// args.push(parse_expression(tokens));
// if (read_token(',', tokens)) continue;
// if (read_token(')', tokens)) break;
// throw "Expected comma or close paren in function call";
// }
// if (!(token in built_in_functions)) throw "Call to unrecognized function: " + token;
// return ['call ' + token].concat(args);
// }
// // otherwise its just a reference to a variable
// return token;
// }
// // only option left: a number
// var n = parseFloat(token, 10);
// if (isNaN(n)) throw 'Expected a number, got ' + String(token);
// return n;
// }
// else throw 'Unexpected end of expression';
// }
//
//function evaluate(tree, environment) {
// if (environment === undefined) environment = built_in_environment;
// if (typeof tree == 'number') return tree;
// else if (typeof tree == 'string') return environment[tree]; // might be undefined
// else {
// // expecting [operator,tree,...]
// var args = tree.slice(1).map(function(subtree) {
// return evaluate(subtree, environment);
// });
// if (tree[0].search(/^call /) != -1) {
// // call of built-in function
// var f = tree[0].slice(5);
// f = built_in_functions[f];
// if (f === undefined) throw "Unknown function: " + f;
// return f.apply(undefined, args);
// }
// // otherwise its just an operator
// else switch (tree[0]) {
// case 'neg':
// return -args[0];
// case '+':
// return args[0] + args[1];
// case '-':
// return args[0] - args[1];
// case '*':
// return args[0] * args[1];
// case '/':
// return args[0] / args[1];
// case '^':
// return Math.pow(args[0],args[1]);
// default:
// throw 'Unrecognized operator ' + tree[0];
// }
// }
// }
//function calculate(expression){
// var expressionHolder = parse_expression(expression);
// expression = evaluate(expressionHolder)
// return expression
//}