Skip to content

Commit febc378

Browse files
committed
parse
1 parent df8bf37 commit febc378

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

basic-calculator-iv/parse.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
11
import { Poly } from "./Poly.ts";
2+
/** 我们依然使用递归下降法来解决这个问题,文法如下:
23
4+
exp :=( exp "+"||"-" additive) || additive
5+
6+
additive := (additive "*" factor) || additive
7+
8+
factor := num || "("exp")" || variable(变量)
9+
*/
310
export function parse(tokens: (string | number)[]): Poly {
4-
throw new Error("Function not implemented.");
11+
let index = 0;
12+
// console.log(tokens);
13+
function expression(): Poly {
14+
let res = additive();
15+
while (
16+
index < tokens.length &&
17+
(tokens[index] == "+" || tokens[index] == "-")
18+
) {
19+
const op = tokens[index];
20+
index++;
21+
const temp = additive();
22+
// console.log(op);
23+
if (op == "+") {
24+
res = res.add(temp);
25+
} else {
26+
res = res.sub(temp);
27+
}
28+
}
29+
// console.log("expression", [...res]);
30+
return res;
31+
}
32+
function additive(): Poly {
33+
let res = factor();
34+
while (index < tokens.length && tokens[index] == "*") {
35+
// console.log(tokens[index]);
36+
index++;
37+
const fac = factor();
38+
res = res.mul(fac);
39+
}
40+
// console.log("additive", [...res]);
41+
return res;
42+
}
43+
function factor(): Poly {
44+
let res = new Poly();
45+
if (index < tokens.length && tokens[index] == "(") {
46+
// console.log(tokens[index]);
47+
index++;
48+
res = expression();
49+
// console.log(tokens[index]);
50+
index++;
51+
} else if (index < tokens.length) {
52+
if (typeof tokens[index] === "number") {
53+
const num = Number(tokens[index]);
54+
res = res.add(new Poly([[[], num]]));
55+
} else {
56+
const variable = String(tokens[index]);
57+
res = res.add(new Poly([[[variable], 1]]));
58+
}
59+
index++;
60+
}
61+
// console.log("factor", [...res]);
62+
return res;
63+
}
64+
return expression();
565
}

basic-calculator-iv/test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Poly } from "./Poly.ts";
22
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
33
import { tokenize } from "./tokenize.ts";
4+
import basicCalculatorIV from "./index.ts";
45

56
Deno.test("poly", () => {
67
const p1 = new Poly([
@@ -161,3 +162,55 @@ Deno.test("tokenize", () => {
161162
4,
162163
]);
163164
});
165+
Deno.test("basic-calculator-iv", () => {
166+
assertEquals(
167+
basicCalculatorIV(
168+
"e - 8 + temperature - pressure",
169+
["e", "temperature"],
170+
[1, 12],
171+
),
172+
["-1*pressure", "5"],
173+
);
174+
assertEquals(basicCalculatorIV("e + 8 - a + 5", ["e"], [1]), [
175+
"-1*a",
176+
"14",
177+
]);
178+
assertEquals(basicCalculatorIV("(e + 8) * (e - 8)", [], []), [
179+
"1*e*e",
180+
"-64",
181+
]);
182+
assertEquals(basicCalculatorIV("a * b * c + b * a * c * 4", [], []), [
183+
"5*a*b*c",
184+
]);
185+
assertEquals(
186+
basicCalculatorIV(
187+
"((a - b) * (b - c) + (c - a)) * ((a - b) + (b - c) * (c - a))",
188+
[],
189+
[],
190+
),
191+
[
192+
"-1*a*a*b*b",
193+
"2*a*a*b*c",
194+
"-1*a*a*c*c",
195+
"1*a*b*b*b",
196+
"-1*a*b*b*c",
197+
"-1*a*b*c*c",
198+
"1*a*c*c*c",
199+
"-1*b*b*b*c",
200+
"2*b*b*c*c",
201+
"-1*b*c*c*c",
202+
"2*a*a*b",
203+
"-2*a*a*c",
204+
"-2*a*b*b",
205+
"2*a*c*c",
206+
"1*b*b*b",
207+
"-1*b*b*c",
208+
"1*b*c*c",
209+
"-1*c*c*c",
210+
"-1*a*a",
211+
"1*a*b",
212+
"1*a*c",
213+
"-1*b*c",
214+
],
215+
);
216+
});

basic-calculator/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ Deno.test("Parenthesized-expression", () => {
9696
},
9797
});
9898
});
99-
Deno.test("simple-calculate", () => {
99+
Deno.test("basic-calculator-simple-calculate", () => {
100100
assertEquals(calculate("-199+5998"), -199 + 5998);
101101
});
102-
Deno.test("Parenthesized-calculate", () => {
102+
Deno.test("basic-calculator-Parenthesized-calculate", () => {
103103
assertEquals(calculate("-(-199+5998)+87"), -(-199 + 5998) + 87);
104104
});
105-
Deno.test("testcases-calculate", () => {
105+
Deno.test("basic-calculator-test-cases-calculate", () => {
106106
assertEquals(calculate("1 + 1"), 2);
107107
assertEquals(calculate(" 2-1 + 2 "), 3);
108108
assertEquals(calculate("(1+(4+5+2)-3)+(6+8)"), 23);

0 commit comments

Comments
 (0)