-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathFraction Addition and Subtraction.cpp
52 lines (47 loc) · 1.86 KB
/
Fraction Addition and Subtraction.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public:
string fractionAddition(string expression) {
int res_num = 0; //keep track of numerator
int res_denom = 1; //keep track of denominator
char sign = '+'; //keep track of sign
for(int i = 0; i < expression.size(); i++){ //parse the expression string
int next_num = 0;
int next_denom = 0;
if(expression[i] == '+' || expression[i] == '-') //updating
sign = expression[i];
else{
int j = i+1;
while(j < expression.size() && expression[j] != '/') j++; build next numerator
next_num = stoi(expression.substr(i, j-i));
int k = j+1;
while(k < expression.size() && expression[k] >= '0' && expression[k] <= '9') k++; //build next denominator
next_denom = stoi(expression.substr(j+1, k-(j+1)));
if(res_denom != next_denom){ //update result numerator and denominator
res_num *= next_denom;
next_num *= res_denom;
res_denom *= next_denom;
}
if(sign == '+') res_num += next_num;
else res_num -= next_num;
i = k-1;
}
}
return lowestFraction(res_num, res_denom); //put the fraction into lowest terms and return as string
}
private:
int findGCD(int a, int b) { //find Greatest Common Denominator
if(b == 0) return a;
return findGCD(b, a % b);
}
string lowestFraction(int num, int denom){ //use GCD to put fraction into lowest terms and return as string
int gcd;
gcd = findGCD(num, denom);
num /= gcd;
denom /= gcd;
if(denom < 0){
denom *= -1;
num *= -1;
}
return to_string(num) + '/' + to_string(denom);
}
};