Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Solutions Sem. 01 / exercise02.cpp
blob: 78b7616bbb22c932802b76281458e6c444b4f0c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

struct Rational {
	int m;
	int n;
};

Rational sum(Rational a, Rational b) {
	int divisor = a.n * b.n;
	return { a.m * divisor + b.m * divisor, divisor };
}

Rational sub(Rational a, Rational b) {
	b.m *= -1;
	return sum(a, b);
}

Rational mult(Rational a, Rational b) {
	return { a.m * b.m, a.n * b.n };
}

Rational div(Rational a, Rational b) {
	int temp = b.m;
	b.m = b.n;
	b.n = temp;

	return mult(a, b);
}

bool isValid(Rational a) {
	return a.n != 0;
}

bool isWholeNumber(Rational a) {
	return a.n % a.m == 0;
}