原题链接:592. 分数加减运算
模拟:
class Solution {
public:
//辗转相除法求最大公约数
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
string fractionAddition(string expression) {
int n = 0;
for(auto &x :expression) {
if(x == '/') n++;
}
expression = '+' + expression;
int a = 0,b = 1,offset = 0;
int c, d;
char e;
for (int i = 0; i < n; i ++ ) {
sscanf(expression.c_str() + offset, "%c%d/%d", &e, &c, &d);
offset += (e + to_string(c) + '/' + to_string(d)).size();
if (e == '-') c = -c;
int x = a * d + b * c, y = b * d;
int z = gcd(x, y);
a = x / z, b = y / z;
}
if(b < 0) b = -b, a = -a;
return to_string(a) + '/' + to_string(b);
}
};