My omnium-gatherom of scripts and source code.
1#include <assert.h>
2#include <iostream>
3#include <numeric>
4#include <utility>
5
6int main() {
7 std::pair<int, int> firstFraction;
8 std::pair<int, int> secondFraction;
9
10 std::cout
11 << "This program adds two fractions!\n"
12 "Enter the numerator and the denominator of the first fraction: ";
13 std::cin >> firstFraction.first >> firstFraction.second;
14 assert(firstFraction.second != 0 && "Denominator must not be zero.");
15
16 std::cout
17 << "Enter the numerator and the denominator of the second fraction: ";
18 std::cin >> secondFraction.first >> secondFraction.second;
19 assert(secondFraction.second != 0 && "Denominator must not be zero.");
20
21 std::pair<int, int> fraction;
22 fraction.first = firstFraction.first * secondFraction.second +
23 firstFraction.second * secondFraction.first;
24 fraction.second = firstFraction.second * secondFraction.second;
25
26 int gcd = std::gcd(fraction.first, fraction.second);
27 fraction.first /= gcd;
28 fraction.second /= gcd;
29
30 std::cout << "The sum of " << firstFraction.first << "/"
31 << firstFraction.second << " and " << secondFraction.first << "/"
32 << secondFraction.second << " is " << fraction.first << "/"
33 << fraction.second << ".\n";
34 return 0;
35}