-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculator.js
146 lines (134 loc) · 4.43 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
var calculator = function(){
var exports={};
function calculate(string){
//console.log("click");
var tokens = tokenize(string);
var result;
try{
result=evaluate(tokens);
if(tokens.length>0)
throw "unrecognized operator";
return String(result);
}catch(err)
{
return"error: "+err;
}
}
exports.calculate=calculate;
function tokenize(string){
var regex = /\d*[\.]\d+|\d+|[\+\/\*\-\(\)]|sin|cos|tan|ln|sqrt/g;
var tokens = string.match(regex);
return tokens;
//g is a flag, means global match
}
function read_operand(tokens){
var firstToken=tokens.shift();
if(isOpenParen(firstToken)){
var evaluatedOperand=evaluate(tokens);
if(tokens.shift()!=")")
throw "expected )";
return evaluatedOperand;
}else if(isFunc(firstToken)){
var secondToken=tokens.shift();
if(!isOpenParen(secondToken))
throw "need paren after a function i.e. sin(30)";
var functionOperand=evaluate(tokens);
if(tokens.shift()!=")")
throw "expected )";
return funcEval(firstToken, functionOperand);
}
else if(isNeg(firstToken)){
return returnNum(tokens.shift())*-1;
}
else{
return returnNum(firstToken);
}
}
function isValidEvalOp(operator){
return operator=="+" || operator=="-"|| operator==")"; /*"|| operator=="/"||operator=="*")*/
}
function isValidTermOp(operator){
return operator=="*" || operator=="/"|| operator==")"||operator=="+" || operator=="-";
}
function isOpenParen(operand){
return operand=="(";
}
function isNeg(operand){
return operand=="-";
}
function isFunc(operand){
return operand=="sin"||operand=="cos"||operand=="tan"||operand=="ln"||operand=="sqrt";
}
function funcEval(func, eval){
if(func=='sin')
return Math.sin(eval);
else if (func=='cos')
return Math.cos(eval);
else if (func=='tan')
return Math.tan(eval);
else if (func=='sqrt')
return Math.sqrt(eval);
else if (func=='ln')
return Math.log(eval);
else if (func=='cos')
return Math.cos(eval);
}
function returnNum(token){
var operand
if(token.indexOf('.')<0)
operand=parseInt(token);
else
operand=parseFloat(token);
//console.log(operand);
if(isNaN(operand))
throw "number expected";
return operand;
}
//evaluates a list of tokens as a linear calculator
function evaluate(tokens){
if(tokens.length<1)
throw "missing operand";
var operand1 = read_term(tokens);
while(tokens.length>0){
var operator = tokens[0];
if (!isValidEvalOp(operator))
throw "unrecognized operator";
if(operator==")")
return operand1;
tokens.shift();
if(tokens.length<=0)
throw "missing operand";
var operand2 = read_term(tokens);
if(operator =="+" )
operand1 =operand1+operand2;
else if(operator =="-" )
operand1 =operand1-operand2;
else
throw "unrecognized operator"; //redundant step
//console.log(operand1);
}
return operand1;
}
function read_term(tokens){
var operand1=read_operand(tokens);
while(tokens.length>0){
var operator=tokens[0];
if (!isValidTermOp(operator))
throw "unrecognized operator";
if(operator==")"||operator=="+" ||operator=="-")
return operand1;
tokens.shift();
if(tokens.length<=0)
throw "missing operand";
var operand2 = read_operand(tokens);
if(operator =="*" )
operand1 =operand1*operand2;
else if(operator =="/" )
operand1 =operand1/operand2;
else
throw "unrecognized operator";
}
return operand1;
}
return exports;
}();