-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode Sample #5
132 lines (132 loc) · 3.33 KB
/
Code Sample #5
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Course: CS2400-60 Computer Science 2
// Name: Abdalkarim, Marina
// Assignment: Programming Assignment P5.1
// Remark: The program tests all functions.
#include <iostream>
#include <string>
using namespace std;
struct rational
{
int num; // numerator
int den; // denominator; b ≠ 0
};
void set(int aa, int bb, rational &r);
// Postcondition: rational number r is set to aa / bb
void display(rational &r);
// Postcondition: displays a rational number r in the following format: a / b, e.g., 1 / 2, -5 /9 (not 5 / -9), 1 / 4 (not 2 / 8, etc.)
rational add(const rational &r1, const rational &r2);
// Postcondition: (r1 + r2) -- a rational number -- is returned (notice the return type is rational!)
rational subtract(const rational &r1, const rational &r2);
// Postcondition: (r1 - r2) -- a rational number -- is returned
rational multiply(const rational &r1, const rational &r2);
// Postcondition: (r1 * r2) -- a rational number -- is returned
rational divide(const rational &r1, const rational &r2);
// Postcondition: (r1 / r2) -- a rational number -- is returned
int compare(const rational &r1, const rational&r2);
// Postcondition: returns 1 if r1 is greater than r2; 0 if r1 is equal to r2; -1 is r1 is less than r2
int GCD(const rational &r);
// You must use the Euclidean algorithm. https://en.wikipedia.org/wiki/Euclidean_algorithm
// Postcondition: returns the "greatest common divisor" between r.a and r.b
int main()
{
rational x, y, sum, diff, mul, div;
int com;
set(-1, 2, x);
set(1, 3, y);
sum = add(x, y);
diff = subtract(x, y);
mul = multiply(x, y);
div = divide(x, y);
com = compare(x, y);
cout << "r1 = ";
display(x);
cout << "r2 = ";
display(y);
cout << "r3 = r1 + r2 = ";
display(sum);
cout << "r4 = r1 - r2 = ";
display(diff);
cout << "r5 = r1 * r2 = ";
display(mul);
cout << "r6 = r1 / r2 = ";
display(div);
if (com == 1)
cout << "r1 is greater than r2" << endl;
else if (com == -1)
cout << "r1 is less than r2" << endl;
if (com == 0)
cout << "r1 is equal to r2" << endl;
return 0;
}
rational add(const rational &r1, const rational &r2)
{
rational sum;
sum.num = (r1.num * r2.den) + (r2.num * r1.den);
sum.den = r1.den * r2.den;
return sum;
}
void set(int a, int b, rational &r)
{
r.num = a;
r.den = b;
}
void display(rational &r)
{
int gcd = GCD(r);
int a, b;
a = r.num / gcd;
b = r.den / gcd;
if (b < 0)
{
a = a * (-1);
b = b * (-1);
}
cout << a << " / " << b << endl;
}
rational subtract(const rational &r1, const rational &r2)
{
rational diff;
diff.num = (r1.num * r2.den) - (r2.num * r1.den);
diff.den = r1.den * r2.den;
return diff;
}
rational multiply(const rational &r1, const rational &r2)
{
rational mul;
mul.num = r1.num * r2.num;
mul.den = r1.den * r2.den;
return mul;
}
rational divide(const rational &r1, const rational &r2)
{
rational div, temp;
temp.num = r2.den;
temp.den = r2.num;
div.num = r1.num * temp.num;
div.den = r1.den * temp.den;
return div;
}
int compare(const rational &r1, const rational&r2)
{
rational diff;
diff = subtract(r1, r2);
if (diff.num == 0)
return 0;
else if (diff.num < 0)
return -1;
else if (diff.num > 0)
return 1;
return 0;
}
int GCD(const rational &r)
{
int a = 0, b = 0;
int remainder = r.num % r.den;
while (remainder != 0)
{
a = r.den;
b = remainder;
remainder = a % b;
}
return b;
}