Small simple C++/Java/Python
Keep the overall result in A / B, read the next fraction into a / b. Their sum is (Ab + aB) / Bb (but cancel their greatest common divisor).\n\nC++:\n\n    stri
Keep the overall result in `A / B`, read the next fraction into `a / b`. Their sum is `(Ab + aB) / Bb` (but cancel their greatest common divisor).\n\n**C++:**\n\n    string fractionAddition(string expression) {\n        istringstream in(expression);\n        int A = 0, B = 1, a, b;\n        char _;\n        while (in >> a >> _ >> b) {\n            A = A * b + a * B;\n            B *= b;\n            int g = abs(__gcd(A, B));\n            A /= g;\n            B /= g;\n        }\n        return to_string(A) + '/' + to_string(B);\n    }\n\n**Java:**\n\n    public String fractionAddition(String expression) {\n        Scanner sc = new Scanner(expression).useDelimiter("/|(?=[-+])");\n        int A = 0, B = 1;\n        while (sc.hasNext()) {\n            int a = sc.nextInt(), b = sc.nextInt();\n            A = A * b + a * B;\n            B *= b;\n            int g = gcd(A, B);\n            A /= g;\n            B /= g;\n        }\n        return A + "/" + B;\n    }\n\n    private int gcd(int a, int b) {\n        return a != 0 ? gcd(b % a, a) : Math.abs(b);\n    }\n\n**Python 3:**\nAdded this after @lee215 reminded me about Python 3's `math.gcd` with his solution in the comments.\n\n    def fractionAddition(self, expression):\n        ints = map(int, re.findall('[+-]?\\d+', expression))\n        A, B = 0, 1\n        for a in ints:\n            b = next(ints)\n            A = A * b + a * B\n            B *= b\n            g = math.gcd(A, B)\n            A //= g\n            B //= g\n        return '%d/%d' % (A, B)
[]