3
string fractionAddition(string expression) {4
int res_num = 0; // keep track of numerator5
int res_denom = 1; // keep track of denominator6
char sign = '+'; // keep track of sign8
for (int i = 0; i < expression.size(); i++) { // parse the expression string11
if (expression[i] == '+' || expression[i] == '-') // updating15
while (j < expression.size() && expression[j] != '/') j++;16
build next numerator next_num = stoi(expression.substr(i, j - i));18
while (k < expression.size() && expression[k] >= '0' && expression[k] <= '9')19
k++; // build next denominator20
next_denom = stoi(expression.substr(j + 1, k - (j + 1)));21
if (res_denom != next_denom) { // update result numerator and23
res_num *= next_denom;24
next_num *= res_denom;25
res_denom *= next_denom;34
return lowestFraction(res_num,35
res_denom); // put the fraction into lowest terms and return as string39
int findGCD(int a, int b) { // find Greatest Common Denominator41
return findGCD(b, a % b);44
string lowestFraction(int num,45
int denom) { // use GCD to put fraction into lowest46
// terms and return as string48
gcd = findGCD(num, denom);55
return to_string(num) + '/' + to_string(denom);