forked from tangweikun/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
32 lines (29 loc) · 1.07 KB
/
index.ts
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
import { _calculate } from '../_utils'
import { fractionalSimplify } from '../fractionalSimplify'
export function fractionCalculate(
arr1: number[],
arr2: number[],
operator: string,
) {
const [firstInteger, firstNumerator = 0, firstDenominator = 1] = arr1
const [secondInteger, secondNumerator = 0, secondDenominator = 1] = arr2
const FIRST_SIGN = Math.sign(firstInteger || firstNumerator) || 1
const SECOND_SIGN = Math.sign(secondInteger || secondNumerator) || 1
const foo =
(Math.abs(firstNumerator) + Math.abs(firstInteger) * firstDenominator) *
FIRST_SIGN
const bar =
(Math.abs(secondNumerator) + Math.abs(secondInteger) * secondDenominator) *
SECOND_SIGN
switch (operator) {
case '*':
return fractionalSimplify(foo * bar, firstDenominator * secondDenominator)
case '/':
return fractionalSimplify(foo * secondDenominator, bar * firstDenominator)
default:
return fractionalSimplify(
_calculate(foo * secondDenominator, bar * firstDenominator, operator),
firstDenominator * secondDenominator,
)
}
}