-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRationalNumber.cpp
More file actions
178 lines (138 loc) · 5.12 KB
/
RationalNumber.cpp
File metadata and controls
178 lines (138 loc) · 5.12 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "rationalnumber.h"
#include <stdio.h>;
#include <stdlib.h>;
// The modern Euclidian Algorithm. This function's code was adapted from the information which www.wikipedia.org provides.
int rnEuclid(int a, int b)
{
if (b==0) return a;
return rnEuclid(b, a%b);
}
bool rnIsValid(RationalNumber n)
{
if (n.denominator == 0) return false;
return true;
}
// Normalizes the given RationalNumber using the Euclidian Algorithm
RationalNumber rnNormalize(RationalNumber n)
{
// checking if the given RationalNumbers are valid
if (!rnIsValid(n))
{
printf("At least one of the RationalNumbers is NOT valid! The result of this function might not be correct or valid!");
}
// abs() has to be used to avoid a negative gcd
int gcd = rnEuclid(abs(n.numerator), abs(n.denominator));
n.numerator = n.numerator/gcd;
n.denominator = n.denominator/gcd;
return n;
}
// Returns true if the given RationalNumber is negative (when the denominator is negative and the numerator positive and other way round).
bool rnIsNegative(RationalNumber n)
{
if ((n.numerator > 0 && n.denominator > 0) || (n.numerator < 0 && n.denominator < 0)) return false;
return true;
}
bool rnEqual(RationalNumber n1, RationalNumber n2)
{
// checking if the given RationalNumbers are valid
if (!rnIsValid(n1) || !rnIsValid(n2) )
{
printf("At least one of the RationalNumbers is NOT valid! The result of this function might not be correct or valid!");
}
n1 = rnNormalize(n1);
n2 = rnNormalize(n2);
if (n1.numerator == n2.numerator && n1.denominator == n2.denominator) return true;
return false;
}
// all cases for positive and negative RationalNumbers are calculated seperately
bool rnLessThan(RationalNumber n1, RationalNumber n2)
{
// checking if the given RationalNumbers are valid
if (!rnIsValid(n1) || !rnIsValid(n2) )
{
printf("At least one of the RationalNumbers is NOT valid! The result of this function might not be correct or valid!");
return false;
}
if (rnIsNegative(n1) && !rnIsNegative(n2)) return true;
if (!rnIsNegative(n1) && rnIsNegative(n2)) return false;
if (!rnIsNegative(n1) && !rnIsNegative(n2))
{
// the first if can catch some of the int-overflows!
if (n1.denominator == n2.denominator)
{
if (n1.numerator < n2.numerator) return true;
}
n1.numerator = abs(n1.numerator) * abs(n2.denominator);
n2.numerator = abs(n2.numerator) * abs(n1.denominator);
if (n1.numerator < n2.numerator) return true;
return false;
}
if (rnIsNegative(n1) && rnIsNegative(n2))
{
// the first if can catch some of the int-overflows!
if (n1.denominator == n2.denominator)
{
if (n1.numerator > n2.numerator) return true;
}
n1.numerator = abs(n1.numerator) * abs(n2.denominator);
n2.numerator = abs(n2.numerator) * abs(n1.denominator);
if (n1.numerator > n2.numerator) return true;
return false;
}
return false;
}
RationalNumber rnAdd(RationalNumber n1, RationalNumber n2)
{
RationalNumber result;
// checking if the given RationalNumbers are valid
if (!rnIsValid(n1) || !rnIsValid(n2) )
{
printf("At least one of the RationalNumbers is NOT valid! The result of this function might not be correct or valid!");
}
result.numerator = (n1.numerator * n2.denominator) + (n2.numerator *n1.denominator);
result.denominator = (n1.denominator * n2.denominator);
result = rnNormalize(result);
return result;
}
RationalNumber rnSubtract(RationalNumber n1, RationalNumber n2)
{
RationalNumber result;
// checking if the given RationalNumbers are valid
if (!rnIsValid(n1) || !rnIsValid(n2) )
{
printf("At least one of the RationalNumbers is NOT valid! The result of this function might not be correct or valid!");
}
result.numerator = (n1.numerator * n2.denominator) - (n2.numerator *n1.denominator);
result.denominator = (n1.denominator * n2.denominator);
result = rnNormalize(result);
return result;
}
RationalNumber rnMultiply(RationalNumber n1, RationalNumber n2)
{
RationalNumber result;
// checking if the given RationalNumbers are valid
if (!rnIsValid(n1) || !rnIsValid(n2) )
{
printf("At least one of the RationalNumbers is NOT valid! The result of this function might not be correct or valid!");
}
result.numerator = n1.numerator * n2.numerator;
result.denominator = n1.denominator * n2.denominator;
result = rnNormalize(result);
return result;
}
RationalNumber rnDivide(RationalNumber n1, RationalNumber n2)
{
RationalNumber result;
// checking if a division is possible
if (!rnIsValid(n1) || !rnIsValid(n2) )
{
printf("At least one of the RationalNumbers is NOT valid! The result of this function is not correct!");
result.numerator = 1;
result.denominator = 1;
return result;
}
result.numerator = n1.numerator * n2.denominator;
result.denominator = n1.denominator * n2.numerator;
result = rnNormalize(result);
return result;
}