Simple and Easy CPP Code!💯✅☮️
Code\ncpp []\nclass Solution {\npublic:\n    string fractionAddition(string exp) {\n        int num=0;\n        int deno=1;\n        int i=0;\n        int n=exp
# Code\n```cpp []\nclass Solution {\npublic:\n    string fractionAddition(string exp) {\n        int num=0;\n        int deno=1;\n        int i=0;\n        int n=exp.size();\n        while(i<n)\n        {\n            int curnum=0;\n            int curdeno=0;\n            bool isneg=(exp[i]==\'-\');\n            if(exp[i]==\'+\' or exp[i]==\'-\')\n            {\n                i++;\n            }\n            while(i<n and isdigit(exp[i]))\n            {\n                int val=exp[i]-\'0\';\n                curnum=(curnum*10)+val;\n                i++;\n            }\n            i++;\n            if(isneg==true)\n            {\n                curnum*=-1;\n            }\n            while(i<n and isdigit(exp[i]))\n            {\n                int val=exp[i]-\'0\';\n                curdeno=(curdeno*10)+val;\n                i++;\n            }\n            num=(num*curdeno)+(curnum*deno);\n            deno=deno*curdeno;\n        }\n        int gcd=abs(__gcd(num,deno));\n        num/=gcd;\n        deno/=gcd;\n        return to_string(num)+"/"+to_string(deno);\n    }\n};\n```\n\n# Intuition\nThe goal is to parse the string representation of the expression, add or subtract fractions accordingly, and return the result in its simplest form. \n\n# Approach\n\n1. **Initialization:** The code`initializes` `num to 0` and `den to 1`, which will hold the numerator and denominator of the cumulative fraction, respectively.\n2. **Iteration:** The loop iterates through the string exp to parse each fraction:\n- It first checks if the current character is a sign (\'+\' or \'-\'). `If` it\'s a `\'-\'`, it sets a `flag to true` to indicate that the numerator should be negative.\n- It then `parses the numerator` (curnum) by iterating through the digits.\n- The loop continues to `parse the denominator` (curdeno) after encountering the \'/\' character.\n3. **Fraction Arithmetic:** The code updates the cumulative numerator (num) and denominator (den) by using the `formula` for adding fraction\n$$`newNum=num\xD7curdeno+curnum\xD7den`$$            \n$$`newDen=den\xD7curdeno`$$\n4. **Simplification:** After processing all fractions, the code calculates the greatest common divisor (GCD) of the numerator and denominator using the` __gcd function`, and then divides both by the GCD to simplify the fraction.\n5. **Return:** Finally, the code converts the simplified numerator and denominator to strings and concatenates them in the form `"numerator/denominator"` to return as the result.\n\n# Example Walkthrough\n\n### Input: `"-1/2+1/2"`\n\n### Step-by-Step Execution:\n\n1. **Initialization**:\n   - `num = 0`, `den = 1`, `i = 0`, `n = 7` (length of the string).\n\n2. **First Fraction Parsing** (`"-1/2"`):\n   - **Check for Sign**:\n     - The character at `exp[0]` is `\'-\'`, so `flag` is set to `true`.\n     - `i` is incremented to 1 to move to the next character.\n   - **Numerator Parsing**:\n     - The character at `exp[1]` is `\'1\'`, which is a digit.\n     - `curnum` is updated to 1 (as `curnum = 0 * 10 + 1`).\n     - Since `flag` is `true`, `curnum` is multiplied by `-1`, making `curnum = -1`.\n     - `i` is incremented to 2, and the character at `exp[2]` is `\'/\'`, so the loop for the numerator ends.\n   - **Denominator Parsing**:\n     - `i` is incremented to 3, and the character at `exp[3]` is `\'2\'`, which is a digit.\n     - `curdeno` is updated to 2 (as `curdeno = 0 * 10 + 2`).\n     - `i` is incremented to 4, and the loop for the denominator ends.\n   - **Fraction Arithmetic**:\n     - Now, `num = 0 * 2 + (-1) * 1 = -1`.\n     - `den = 1 * 2 = 2`.\n\n3. **Second Fraction Parsing** (`"+1/2"`):\n   - **Check for Sign**:\n     - The character at `exp[4]` is `\'+\'`, so `flag` is set to `false`.\n     - `i` is incremented to 5 to move to the next character.\n   - **Numerator Parsing**:\n     - The character at `exp[5]` is `\'1\'`, which is a digit.\n     - `curnum` is updated to 1 (as `curnum = 0 * 10 + 1`).\n     - Since `flag` is `false`, `curnum` remains positive.\n     - `i` is incremented to 6, and the character at `exp[6]` is `\'/\'`, so the loop for the numerator ends.\n   - **Denominator Parsing**:\n     - `i` is incremented to 7, and the character at `exp[7]` is `\'2\'`, which is a digit.\n     - `curdeno` is updated to 2 (as `curdeno = 0 * 10 + 2`).\n     - `i` is incremented to 8, and the loop for the denominator ends.\n   - **Fraction Arithmetic**:\n     - Now, `num = -1 * 2 + 1 * 2 = 0`.\n     - `den = 2 * 2 = 4`.\n\n4. **Simplification**:\n   - The GCD of `num` (0) and `den` (4) is calculated using `__gcd(0, 4)`, which is 4.\n   - Simplified `num` and `den` are `0 / 4 = 0` and `4 / 4 = 1`.\n\n5. **Result**:\n   - The final result is `"0/1"`.\n\n### Output:\nThe code correctly simplifies `"-1/2 + 1/2"` to `"0/1"`.\n\n# Complexity\n- Time complexity:`O(n)`,where n is the length of the input string exp. This is because the code iterates through the string once to parse the numerators and denominators.\n\n- Space complexity:`O(1)`, as the algorithm only uses a constant amount of extra space for variables like num, den, curnum, and curdeno.\n\n
['C++']