-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcalculator.ts
82 lines (71 loc) · 1.91 KB
/
calculator.ts
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
import { Stack } from '../utils/stack';
import { Postfix } from './postfix';
import { OperatorTokens } from './tokens';
export class Calculator {
/**
* The input provided by the user
*
* @type {string}
*/
text: string;
/**
* The Postfix class instance
*
* @private
* @type {Postfix}
*/
private postfix: Postfix;
/**
* Stack used after infix to postfix conversion.
* Used for calculating the value of the expression.
*
* @private
* @type {Stack<number>}
*/
private stack: Stack<number>;
constructor(text: string) {
this.text = text;
this.postfix = new Postfix(text);
this.stack = new Stack();
}
public calculate(): number {
// First convert the infix notation to postfix notation.
const queue = this.postfix.parse();
while (queue.size() > 0) {
// Get the next token from the queue.
const str = queue.dequeue()!;
// If the token is an operator, pop two values from the stack,
if (OperatorTokens.has(str)) {
const operator = str;
const operand2 = this.stack.pop()!;
const operand1 = this.stack.pop()!;
let value: number;
switch (operator) {
case '^':
value = operand1 ** operand2;
break;
case '/':
value = operand1 / operand2;
break;
case '*':
value = operand1 * operand2;
break;
case '+':
value = operand1 + operand2;
break;
case '-':
value = operand1 - operand2;
break;
default:
throw new Error(`Invalid operator ${str}`);
}
// Perform the operation and push the result to the stack.
this.stack.push(value);
} else {
// If the token is an operand, push it to the stack.
this.stack.push(parseFloat(str));
}
}
return this.stack.pop()!;
}
}