-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.cpp
More file actions
110 lines (94 loc) · 2.15 KB
/
Copy pathclass.cpp
File metadata and controls
110 lines (94 loc) · 2.15 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
#include <iostream>
#include <cstdlib>
using namespace std;
class SimpleRational {
int numerator;
int denominator;
public:
SimpleRational(int n, int m) : numerator(n), denominator(m) {
if (m == 0) {
cout << "Zero denominater error\n";
exit(1);
}
};
SimpleRational() : numerator(0), denominator(1) {};
void set_numerator(int n) {
numerator = n;
}
void set_denominator(int m) {
if (m != 0) {
denominator = m;
}
else {
cout << "Zero denominater error\n";
exit(1);
}
}
int get_numerator() {
return numerator;
}
int get_denominator() {
return denominator;
}
};
SimpleRational multiply(SimpleRational f1, SimpleRational f2) {
return {
f1.get_numerator() * f2.get_numerator(), f1.get_denominator() * f2.get_denominator()
};
}
void print_fraction(SimpleRational f) {
std::cout << f.get_numerator() << "/" << f.get_denominator();
}
// 같은 비율인지 판단
bool ratio(SimpleRational f1, SimpleRational f2) {
return (f1.get_numerator() * f2.get_denominator() == f1.get_denominator() * f2.get_numerator());
}
// 기존에는 i, j가 계속 재초기화되는 문제 발생
int gcd(int a, int b)
{
int r = 0;
if (a < b)
{
return gcd(b, a);
}
else if (a % b == 0)
{
return b;
}
else if (true)
{
int q = 1;
r = a - b * q;
return gcd(b, r);
}
}
int main() {
SimpleRational fract(1, 2);
cout << "The fraction is";
print_fraction(fract);
cout << '\n';
fract.set_numerator(19);
fract.set_denominator(4);
cout << "The fraction now is ";
print_fraction(fract);
cout << '\n';
SimpleRational fract3(38, 8);
if (ratio(fract, fract3)) {
cout << "there are same" << endl;
}
else {
cout << "nonono" << endl;
}
SimpleRational fract1{ 5, 9 }, fract2{ 3, 4 };
auto prod = multiply(fract1, fract2);
int gcd2 = gcd(prod.get_denominator(), prod.get_numerator());
cout << "The product of ";
print_fraction(fract1);
cout << " and ";
print_fraction(fract2);
cout << " is ";
SimpleRational bunsu(prod.get_numerator() / gcd2, prod.get_denominator() / gcd2);
print_fraction(bunsu);
cout << '\n';
return 0;
}