| Total Complexity | 4 |
| Complexity/F | 4 |
| Lines of Code | 27 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | const Stack = require('../index.js'); |
||
| 2 | const operators = { |
||
| 3 | "+": true, |
||
| 4 | "-": true, |
||
| 5 | "*": true, |
||
| 6 | "/": true, |
||
| 7 | } |
||
| 8 | const PrefixToInfix = (formula) => { |
||
| 9 | let result = "" |
||
|
|
|||
| 10 | let stack = new Stack() |
||
| 11 | for (let i = formula.length - 1 ; i>= 0; i--) { |
||
| 12 | let c = formula[i] |
||
| 13 | if (!operators[c]) { |
||
| 14 | stack.push(c) |
||
| 15 | continue |
||
| 16 | } |
||
| 17 | let subFormula = stack.pop() + c + stack.pop() |
||
| 18 | if (i !== 0 ) { |
||
| 19 | stack.push('(' + subFormula + ')') |
||
| 20 | } else { |
||
| 21 | stack.push(subFormula) |
||
| 22 | |||
| 23 | } |
||
| 24 | } |
||
| 25 | return stack.pop() |
||
| 26 | } |
||
| 27 | module.exports = PrefixToInfix |
||
| 28 | |||
| 29 |