-
Notifications
You must be signed in to change notification settings - Fork 96
Nick/lesson2 #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Nick/lesson2 #162
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,14 @@ | ||
| import { mathOperators, | ||
| unionOperators } from "./mathOperators"; | ||
|
|
||
| import { zeroPrioritiesCalc, | ||
| firstPrioritiesCalc, | ||
| secondPrioritiesCalc } from "./engine"; | ||
|
|
||
| import { parser } from "./parser"; | ||
|
|
||
| export const isNumber = (item: string): boolean => !isNaN(Number(item)); | ||
|
|
||
| export const isOperator = (item: string): boolean => item in mathOperators; | ||
|
|
||
| export const isUnionOperator = (item: string): boolean => item in unionOperators; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| export type ScalarOperationType = (first: number, second: number) => number; | ||
| export type UnionOperationType = (first: number) => number; | ||
|
|
||
| export type MathOperationType = UnionOperationType | ScalarOperationType; | ||
|
|
||
| export const mul: ScalarOperationType = ( | ||
| first: number, | ||
|
|
@@ -20,19 +23,39 @@ export const minus: ScalarOperationType = ( | |
| second: number | ||
| ): number => first - second; | ||
|
|
||
| export const mathOperators: { [key: string]: ScalarOperationType } = { | ||
| export const pow: ScalarOperationType = ( | ||
| first: number, | ||
| second: number | ||
| ): number => Math.pow(first, second); | ||
|
|
||
| export const factorial: UnionOperationType = (first: number): number => { | ||
| return first ? first * factorial(first - 1) : 1; | ||
| }; | ||
|
|
||
| export const sqr: UnionOperationType = (first: number): number => pow(first, 2); | ||
|
|
||
| export const unionOperators: { [key: string]: MathOperationType } = { | ||
| "!": factorial, | ||
| "**": sqr, | ||
| }; | ||
|
|
||
| export const mathOperators: { [key: string]: MathOperationType } = { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. string можно заменить на enum |
||
| "*": mul, | ||
| "/": div, | ||
| "+": add, | ||
| "-": minus, | ||
| "^": pow, | ||
| }; | ||
|
|
||
| export const mathPriorities: number[] = [1, 2]; | ||
| export const mathPriorities: number[] = [0, 1, 2]; | ||
|
|
||
| const [FIRST, SECOND] = mathPriorities; | ||
| const [ZERO, FIRST, SECOND] = mathPriorities; | ||
|
|
||
| export const mathOperatorsPriorities: { [key: string]: number } = { | ||
| "!": ZERO, | ||
| "**": ZERO, | ||
| "*": FIRST, | ||
| "^": FIRST, | ||
| "/": FIRST, | ||
| "+": SECOND, | ||
| "-": SECOND, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { parseBrackets } from "./parseBrackets"; | ||
|
|
||
| describe("parseBrackets correct cases", () => { | ||
| it("(1 + 32) * 2", () => { | ||
| expect(parseBrackets("(1 + 32) * 2")).toEqual("33 * 2"); | ||
| }); | ||
|
|
||
| it("66 / (6 + 2 * 20)", () => { | ||
| expect(parseBrackets("66 / (6 + 2 * 20)")).toEqual("66 / 46"); | ||
| }); | ||
|
|
||
| it("1 + 32 - 2 + 2", () => { | ||
| expect(parseBrackets("(1 + 32) - (2 + 2)")).toEqual("33 - 4"); | ||
| }); | ||
|
|
||
| it("1 + (4 * (2 + 2))", () => { | ||
| expect(parseBrackets("1 + (4 * (2 + 2))")).toEqual("1 + 16"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("parseBrackets invalid cases", () => { | ||
| it("2 + ((1 + 33 - 2)", () => { | ||
| expect(() => parseBrackets("2 + ((1 + 33 - 2)")).toThrow( | ||
| RangeError("Invalid string length") | ||
| ); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { parser } from "./parser"; | ||
| import { zeroPrioritiesCalc, firstPrioritiesCalc, secondPrioritiesCalc } from "./engine"; | ||
|
|
||
| export const parseBrackets = (line: string): string => { | ||
|
|
||
| var k = 1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. val? очень плохой синтаксис |
||
|
|
||
| do { | ||
| var openBracket = line.lastIndexOf("("); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. надо причесать этот кусок кода, который явно скопирован откуда-то |
||
| if (openBracket < 0) { | ||
| k = 0; | ||
| } | ||
| else { | ||
| var closeBracket = line.indexOf(")",openBracket); | ||
| var inside = line.slice(openBracket+1,closeBracket); | ||
| var parsed = parser(inside) | ||
|
|
||
| if(parsed === null) { | ||
| throw new TypeError("Unexpected string"); | ||
| } | ||
|
|
||
| var zeroPriorities = zeroPrioritiesCalc(parsed) | ||
| var firstPriorities = firstPrioritiesCalc(zeroPriorities) | ||
| var secondPriorities = secondPrioritiesCalc(firstPriorities) | ||
|
|
||
| var changed = secondPriorities | ||
|
|
||
| line = line.substr(0, openBracket) + changed.toString() + line.substr(closeBracket + 1); | ||
|
|
||
| } | ||
| } while (k==1) | ||
|
|
||
| return line; | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,41 @@ | ||
| import { isNumber } from "./helpers"; | ||
| import { mathOperators } from "./mathOperators"; | ||
| import { isNumber, isOperator, isUnionOperator } from "./helpers"; | ||
| import { mathOperators, | ||
| unionOperators } from "./mathOperators"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишний перенос на новую строку |
||
|
|
||
| import { parseBrackets } from "./parseBrackets"; | ||
|
|
||
| export type ParsedLineType = (number | string)[]; | ||
|
|
||
| export const parser = (line: string): ParsedLineType | null => { | ||
| const stack = line.split(" "); | ||
| const parsedBrackets = parseBrackets(line) | ||
| const stack = parsedBrackets.split(" "); | ||
|
|
||
| return stack.reduce<ParsedLineType>((result, item, key) => { | ||
| const prevItem = stack[key - 1]; | ||
|
|
||
| const isValidNumberPush = !isNumber(prevItem) && isNumber(item); | ||
| const isValidNumberPush = !isNumber(prevItem) && isNumber(item) && !unionOperators.hasOwnProperty(prevItem); | ||
| const isValidOperatorPush = | ||
| isNumber(prevItem) && | ||
| (isNumber(prevItem) || isUnionOperator(prevItem)) && | ||
| !isNumber(item) && | ||
| mathOperators.hasOwnProperty(item) && | ||
| isOperator(item); | ||
|
|
||
| const isValidUnionOperatorPush = | ||
| (isNumber(prevItem) || isUnionOperator(prevItem)) && | ||
| !isNumber(item) && | ||
| mathOperators.hasOwnProperty(item); | ||
| unionOperators.hasOwnProperty(item) && | ||
| isUnionOperator(item); | ||
|
|
||
| if (isValidNumberPush) { | ||
| result.push(Number(item)); | ||
| } else if (isValidOperatorPush) { | ||
| result.push(item); | ||
| } else if(isValidUnionOperatorPush) { | ||
| result.push(item); | ||
| } else { | ||
| throw new TypeError("Unexpected string"); | ||
| } | ||
|
|
||
| return result; | ||
| }, []); | ||
| }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string можно заменить на enum