-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
103 lines (95 loc) · 2.62 KB
/
script.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
const inputExpression = document.getElementById("input-expression");
const outputResult = document.getElementById("output-result");
const parseBtn = document.getElementById("parse-btn");
parseBtn.addEventListener("click", () => {
const expression = inputExpression.value;
const result = parseExpression(expression);
outputResult.value = result;
});
function parseExpression(expression) {
const generatedParser = new Parser(expression);
expression = generatedParser.parse();
return expression;
}
class Parser {
constructor(input) {
this.input = input.replace(/\s+/g, "");
this.input = this.replaceWords(this.input);
this.index = 0;
}
parse() {
return this.expr();
}
expr() {
let left = this.term();
while (this.index < this.input.length) {
const operator = this.input[this.index];
if (operator === "A" || operator === "S") {
this.index += 1;
const right = this.term();
if (operator === "A") {
left += right;
} else {
left -= right;
}
} else {
break;
}
}
return left;
}
term() {
let left = this.factor();
while (this.index < this.input.length) {
const operator = this.input[this.index];
if (operator === "M" || operator === "D") {
this.index += 1;
const right = this.factor();
if (operator === "M") {
left *= right;
} else {
left /= right;
}
} else {
break;
}
}
return left;
}
replaceWords(str) {
const wordsToReplace = ["अधिक", "वजा", "गुणिले", "भागिले"];
const replacementWords = ["A", "S", "M", "D"];
for (let i = 0; i < wordsToReplace.length; i += 1) {
str = str.replaceAll(wordsToReplace[i], replacementWords[i]);
}
return str;
}
factor() {
let left = this.number();
if (this.index < this.input.length && this.input[this.index] === "(") {
this.index += 1; // Skip opening bracket
left = this.expr(); // Evaluate the expression within the bracket
this.index += 1; // Skip closing bracket
}
return left;
}
number() {
const start = this.index;
let dotFound = false;
while (this.index < this.input.length) {
const char = this.input[this.index];
if (/[\d.]/.test(char)) {
if (char === "." && dotFound) {
// Only one dot allowed in a number
break;
} else if (char === ".") {
dotFound = true;
}
this.index += 1;
} else {
break;
}
}
return parseFloat(this.input.slice(start, this.index));
}
}