forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path87-fractions1.cpp
159 lines (134 loc) · 4.33 KB
/
87-fractions1.cpp
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
#include <iostream>
#include <stdexcept>
#include <numeric>
class Fraction
{
public:
// Fraction(const std::pair<int, int> &f)
// : Fraction(f.first, f.second)
// {
// }
Fraction(int the_numerator, int the_denominator)
: numerator{the_numerator}, denominator{the_denominator}
{
if (denominator == 0)
{
throw std::invalid_argument{"Denominator must be non-zero."};
}
if (denominator < 0)
{
//If we were provided a negative denominator, multiply
//the whole fraction by (-1)/(-1) to move the negative
//sign to the numerator.
denominator = -denominator;
numerator = -numerator;
}
}
double to_real() const
{
return numerator / (double)denominator;
}
int get_numerator() const
{
return numerator;
}
int get_denominator() const
{
return denominator;
}
Fraction lowest_terms() const
{
//Find the greatest common divisor of the numerator and denominator
auto common_divisor = std::gcd(numerator, denominator);
//Now return a new Fraction with both components divided by that
//common divisor
Fraction result{numerator / common_divisor, denominator / common_divisor};
return result;
}
// When operators are written as member functions, one of the
// operands is implicitly interpreted to be the "current object"
// For a unary operator (like negation), the current object is
// the only operand (so the operator takes no explicit arguments).
//
// For a binary operator (like multiplication), the current object
// is the left operand (so the only explicit argument to the operator
// is the right operand).
Fraction operator-() const
{
//This is the unary minus (i.e. negation) operator
Fraction result{-numerator, denominator};
return result; //Not in lowest terms, for symmetry with the non-negated fraction
}
Fraction operator+(Fraction const &other) const
{
Fraction result{numerator * other.denominator + denominator * other.numerator,
denominator * other.denominator};
return result.lowest_terms();
}
Fraction operator-(Fraction const &other) const
{
//This is the binary minus (i.e. subtraction) operator
Fraction result{numerator * other.denominator - denominator * other.numerator,
denominator * other.denominator};
return result.lowest_terms();
}
Fraction operator*(Fraction const &other) const
{
Fraction result{numerator * other.numerator,
denominator * other.denominator};
return result.lowest_terms();
}
Fraction operator/(Fraction const &other) const
{
Fraction result{numerator * other.denominator,
denominator * other.denominator};
return result.lowest_terms();
}
bool operator<(Fraction const &other) const
{
return numerator * other.denominator < denominator * other.numerator;
}
//Task: Define an assignment operator which allows std::pair<int, int>
// instances to be assigned to Fraction objects.
Fraction &operator=(const std::pair<int, int> &p)
{
this->numerator = p.first;
this->denominator = p.second;
return *this;
// return Fraction{numerator, denominator};
}
private:
Fraction() : numerator{0}, denominator{1}
{
}
int numerator;
int denominator;
};
std::ostream &operator<<(std::ostream &stream, Fraction const &fraction)
{
stream << fraction.get_numerator() << "/" << fraction.get_denominator();
return stream;
}
Fraction assign(Fraction &f, const std::pair<int, int> &p)
{
f = p;
return f;
}
int main()
{
Fraction x{6, 10};
Fraction y{1, 6};
std::pair<int, int> P{1, 87};
//Task 1: Make the following assignment statement work.
x = P;
//Task 2: Make the following assignment statement work.
x = y = P;
// int a{}, b{};
// a = (b = 6);
std::cout << "n: " << (x = P).get_denominator() << std::endl;
// assign(x, P)
std::cout << "x: " << x << std::endl;
std::cout << "y: " << y << std::endl;
std::cout << "x+y: " << x + y << std::endl;
return 0;
}