-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpa_lab_5_3_10_(10)-B.cpp
152 lines (139 loc) · 3.86 KB
/
cpa_lab_5_3_10_(10)-B.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
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
class Fraction {
public:
Fraction(int numerator, int denominator);
string toString();
double toDouble();
Fraction plus(Fraction that);
Fraction minus(Fraction that);
Fraction times(Fraction that);
Fraction by(Fraction that);
bool isGreaterThan(Fraction that);
bool isLessThan(Fraction that);
bool isEqual(Fraction that);
private:
int numerator;
int denominator;
void reduce();
};
void Fraction::reduce() {
int a;
if (this->numerator < 0)
a = -this->numerator;
else
a = this->numerator;
int b = this->denominator;
while (a != 0 && b != 0) {
if (a > b)
a %= b;
else
b %= a;
}
this->numerator /= (a + b);
this->denominator /= (a + b);
}
Fraction Fraction::times(Fraction that)
{
int num = this->numerator * that.numerator;
int den = this->denominator * that.denominator;
Fraction result(num, den);
result.reduce();
return result;
}
Fraction Fraction::by(Fraction that)
{
int num = this->numerator * that.denominator;
int den = this->denominator * that.numerator;
Fraction result(num, den);
result.reduce();
return result;
}
Fraction Fraction::plus(Fraction that)
{
int den = this->denominator * that.denominator;
int num = that.denominator * this->numerator + that.numerator * this->denominator;
Fraction result(num, den);
result.reduce();
return result;
}
Fraction Fraction::minus(Fraction that)
{
int den = this->denominator * that.denominator;
int num = that.denominator * this->numerator - that.numerator * this->denominator;
Fraction result(num, den);
result.reduce();
return result;
}
Fraction::Fraction(int numerator, int denominator) {
if (denominator == 0) {
this->denominator = 1;
this->numerator = numerator;
}
else if (denominator < 0) {
this->denominator = -denominator;
this->numerator = -numerator;
}
else {
this->denominator = denominator;
this->numerator = numerator;
}
}
string to_string(int value){
stringstream ss;
ss << value;
return ss.str();
}
string Fraction::toString() {
if (numerator * denominator < 0)
{
if (numerator % denominator == 0)
return to_string(numerator / denominator);
if (numerator / denominator == 0)
return to_string(numerator) + (string)"/" + to_string(denominator);
return to_string(numerator / denominator) + (string)" " +
to_string(-(numerator % denominator)) + (string) "/" + to_string(denominator);
}
if (numerator % denominator == 0)
return to_string(numerator / denominator);
if (numerator / denominator == 0)
return to_string(numerator) + (string)"/" + to_string(denominator);
return to_string(numerator / denominator) + (string)" " +
to_string(numerator % denominator) + (string) "/" + to_string(denominator);
}
double Fraction::toDouble() {
return float(numerator) / denominator;
}
bool Fraction::isGreaterThan(Fraction that)
{
return this->toDouble() > that.toDouble();
}
bool Fraction::isLessThan(Fraction that)
{
return this->toDouble() < that.toDouble();
}
bool Fraction::isEqual(Fraction that)
{
return this->toDouble() == that.toDouble();
}
int main(void) {
int num, den;
std::string input = "", i2;
getline(cin, input);
num = atoi((input.substr(0, input.find("/"))).c_str());
den = atoi((input.substr(input.find("/") + 1)).c_str());
Fraction fraction1(num, den);
getline(cin, i2);
num = atoi((i2.substr(0, i2.find("/"))).c_str());
den = atoi((i2.substr(i2.find("/") + 1)).c_str());
Fraction fraction2(num, den);
if (fraction1.isGreaterThan(fraction2))
cout << fraction1.toString() << " > " << fraction2.toString() << endl;
if (fraction1.isLessThan(fraction2))
cout << fraction1.toString() << " < " << fraction2.toString() << endl;
if (fraction1.isEqual(fraction2))
cout << fraction1.toString() << " = " << fraction2.toString() << endl;
return 0;
}