-
Notifications
You must be signed in to change notification settings - Fork 0
Teste CodeRabbit 1 #2
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| function solve(firstNumber: number, secondNumber: number, operator: string): number { | ||
| const operations: { [index: string]: Function } = { | ||
| "+": () => firstNumber + secondNumber, | ||
| "-": () => firstNumber - secondNumber, | ||
| "*": () => firstNumber * secondNumber, | ||
| "/": () => firstNumber / secondNumber | ||
| }; | ||
|
|
||
| return operations[operator](); | ||
| } | ||
|
|
||
|
|
||
| function solveSimplifiedExpr(output: Array<string>, operators: Array<string>): void { | ||
| const secondNumber = Number(output.pop()); | ||
| const firstNumber = Number(output.pop()); | ||
|
|
||
| const operator = operators.pop(); | ||
|
|
||
| if (operator == null) | ||
| throw "Operation Not Supported."; | ||
|
|
||
|
|
||
| output.push(solve(firstNumber, secondNumber, operator).toString()); | ||
| } | ||
|
|
||
| function isDigit(character: string) { | ||
| return character.charCodeAt(0) >= "0".charCodeAt(0) | ||
| && character.charCodeAt(0) <= "9".charCodeAt(0); | ||
| } | ||
|
|
||
|
|
||
| function parseExpression(expression: string) { | ||
| const operators: Array<string> = ["+", "-", "*", "/"]; | ||
| const expr: Array<string> = []; | ||
| const parenthesesStack: Array<string> = []; | ||
|
|
||
| let lastToken: string | null = null; | ||
| let number = ""; | ||
| let hasDecimal = false; | ||
|
|
||
| for (const token of expression.replaceAll(" ", "")) { | ||
| if (isDigit(token)) { | ||
| number += token; | ||
| } else if (token === ".") { | ||
| if (hasDecimal || !number.length) | ||
| throw "Número decimal mal formado."; | ||
| number += token; | ||
| hasDecimal = true; | ||
| } else if (token === "(") { | ||
| parenthesesStack.push(token); | ||
| expr.push(token); | ||
| hasDecimal = false; | ||
| } else if (token === ")") { | ||
| if (!parenthesesStack.length) | ||
| throw "Parêntese fechado sem abertura correspondente."; | ||
| parenthesesStack.pop(); | ||
|
|
||
| if (number.length) { | ||
| expr.push(number); | ||
| number = ""; | ||
| hasDecimal = false; | ||
| } | ||
|
|
||
| expr.push(token); | ||
| } else if (operators.indexOf(token) >= 0) { | ||
| if (!number.length | ||
| && token === "-" | ||
| && (lastToken == null | ||
| || operators.indexOf(lastToken) >= 0 | ||
| || lastToken == "(")) { | ||
| number += token; | ||
| } else { | ||
| if (!number.length) | ||
| throw `Operador '${token}' mal posicionado na expressão.`; | ||
| expr.push(number); | ||
| number = ""; | ||
| hasDecimal = false; | ||
| expr.push(token); | ||
| } | ||
| } else throw `Caractere inválido na expressão: '${token}'`; | ||
|
|
||
| lastToken = token; | ||
| } | ||
|
|
||
| if (!number.length && ( | ||
| lastToken == null | ||
| || operators.indexOf(lastToken) >= 0)) | ||
| throw `Operador '${lastToken}' mal posicionado na expressão.`; | ||
|
|
||
| if (parenthesesStack.length) | ||
| throw "Parêntese aberto sem fechamento correspondente."; | ||
|
|
||
| if (number.length) | ||
| expr.push(number); | ||
|
|
||
| return expr; | ||
| } | ||
|
|
||
| function solveExpression(expression: string) { | ||
| const priorities: { [index: string]: number } = { | ||
| "+": 1, | ||
| "-": 1, | ||
| "*": 2, | ||
| "/": 2 | ||
| }; | ||
|
|
||
| const operators: Array<string> = []; | ||
| const output: Array<string> = []; | ||
|
|
||
| for (const token of parseExpression(expression)) { | ||
| if (token === "(") { | ||
| operators.push(token); | ||
| } else if (token === ")") { | ||
| while (operators[operators.length - 1] !== "(") | ||
| solveSimplifiedExpr(output, operators); | ||
| operators.pop(); | ||
| } else if (priorities.hasOwnProperty(token)) { | ||
| while (operators.length | ||
| && operators[operators.length - 1] != "(" | ||
| && priorities[operators[operators.length - 1]] >= priorities[token]) | ||
| solveSimplifiedExpr(output, operators); | ||
| operators.push(token); | ||
| } else { | ||
| output.push(token); | ||
| } | ||
| } | ||
|
|
||
| while (operators.length) | ||
| solveSimplifiedExpr(output, operators); | ||
|
|
||
| return Number(output.pop()); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Handle unrecognized operators before calling the operation function.
If
operatoris not in theoperationsmap,operations[operator]will beundefined, causing a runtime error. Consider adding a fallback or a check that throws a clear error when the operator is invalid.📝 Committable suggestion