Extremely Simple C++ solution with explanation
Stringstream is the easiest solution when signs and different numbers are to be extracted from a string. The advantage is that it automatically includes the \'+
Stringstream is the easiest solution when signs and different numbers are to be extracted from a string. The advantage is that it automatically includes the \'+\' or \'-\' sign. \nWe maintain \'a\' as our numerator and \'b\' as our denominator throughout the process. So a/b + c/d can be written as (a*d+b*c)/(bd) . Divide that by gcd to reduce it into smallest fraction. \nRest of the explanation is in code. Comment for doubt.\n```\nclass Solution {\npublic:\n    string fractionAddition(string expression) {\n        stringstream ss(expression);\n        char op;\n        int a,b,c,d;\n        int num,den;\n        ss>>a;ss>>op;ss>>b; //extracting the 1st 2 numbers\n        while(ss>>c) //til we have the number\n        {\n            ss>>op; // op is the operator. which is \'/\' in our case\n            ss>>d;\n            num= a*d + b*c;\n            den= b*d;\n            a= num/__gcd(abs(num),abs(den));\n            b= den/__gcd(abs(num),abs(den));\n        }\n        c=__gcd(abs(a),abs(b));\n        a=a/c;\n        b=b/c;\n        return to_string(a)+\'/\'+to_string(b);\n    }\n};\n```
['Math', 'C']