Struct Output Parameter

Lab C Intermediate 20 points

Write void add(Fraction *a, Fraction *b, Fraction *result) that stores the sum, reduced by the GCD, in *result. Read two fractions a/b c/d and print the sum as p/q.

Input

One line: a/b c/d (positive denominators).

Output

One line.

Example

Input

1/2 1/3

Output

5/6
Sample input available Sample output available
Editor
C

Hints

Set the fields one by one (this runtime doesn't support {..} struct initialisers).

scanf("%d/%d") matches the slash literally.

Solution explanation

Filling in a struct through a pointer is a common C style: the caller owns the memory, the function fills it.

Show solution
#include <stdio.h>

typedef struct {
    int num;
    int den;
} Fraction;

int gcd(int a, int b) {
    if (a < 0) a = -a;
    while (b != 0) {
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}

void add(Fraction *a, Fraction *b, Fraction *result) {
    result->num = a->num * b->den + b->num * a->den;
    result->den = a->den * b->den;
    int g = gcd(result->num, result->den);
    result->num /= g;
    result->den /= g;
}

int main() {
    Fraction a, b, sum;
    scanf("%d/%d %d/%d", &a.num, &a.den, &b.num, &b.den);
    add(&a, &b, &sum);
    printf("%d/%d\n", sum.num, sum.den);
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD