#include <iostream>
#include <sstream>
#include <algorithm>
// 计算最大公约数
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// 将结果转化为最简分数形式
void simplifyFraction(int& numerator, int& denominator) {
int divisor = gcd(numerator, denominator);
numerator /= divisor;
denominator /= divisor;
}
// 计算两个有理数的和
void calculateSum(int a1, int b1, int a2, int b2, int& sumNumerator, int& sumDenominator) {
sumNumerator = a1 * b2 + a2 * b1;
sumDenominator = b1 * b2;
simplifyFraction(sumNumerator, sumDenominator);
}
int main() {
int a1, b1, a2, b2;
char slash;
std::cin >> a1 >> slash >> b1 >> a2 >> slash >> b2;
int sumNumerator, sumDenominator;
calculateSum(a1, b1, a2, b2, sumNumerator, sumDenominator);
// 输出结果
if (sumDenominator == 1) {
std::cout << sumNumerator << std::endl;
} else {
std::cout << sumNumerator << "/" << sumDenominator << std::endl;
}
return 0;
}
|