Skip to content

Commit f2df107

Browse files
committed
Lab 13 var 6 ver 2 dop ready
1 parent 8f25a79 commit f2df107

File tree

3 files changed

+152
-46
lines changed

3 files changed

+152
-46
lines changed

Variants/Lab_13/var_6/main.cpp

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Создайте класс Rational (рациональная дробь)
22

3-
// ЛАБА 14 ВАРИАНТ 4
4-
53
// TODO:
64
// приватные поля для данных
75
// конструктор, содержащий значения по умолчанию
@@ -13,15 +11,14 @@
1311
// указанные в задании перегруженные операции
1412
// ++ -- -n == != = + += - -= * *= / /= > < >= <=
1513

16-
/*
17-
НЕ ДОП:
18-
поправить вывод
19-
!!!учесть отрицательные числа
20-
ДОП:
21-
добавить целую часть (в сам класс)
22-
операции, соотв., с учетом целой части дробей
23-
сортировка Шелла массива дробей
24-
*/
14+
// НЕ ДОП:
15+
//* - поправить вывод
16+
//* - !!!учесть отрицательные числа
17+
// ДОП:
18+
//* - добавить целую часть (в сам класс)
19+
//* - операции, соотв., с учетом целой части дробей
20+
//* - сортировка Шелла массива дробей
21+
2522
#include "rational.h"
2623
using namespace std;
2724

@@ -34,6 +31,15 @@ int main()
3431

3532
switch (test_n)
3633
{
34+
case 0: // test
35+
{
36+
Rational r1(-11205729, 14420), r2(3349, 14420), r3(11207689, 14420), r4(5439, 103);
37+
cout << r1 << " = " << r1.normalize() << " = " << r1.normalize() << " = " << r1.denormalize() << endl;
38+
cout << r2 << " = " << r2.normalize() << " = " << r2.normalize() << " = " << r2.denormalize() << endl;
39+
cout << r3 << " = " << r3.normalize() << " = " << r3.normalize() << " = " << r3.denormalize() << endl;
40+
cout << r4 << " = " << r4.normalize() << " = " << r4.normalize() << " = " << r4.denormalize() << endl;
41+
break;
42+
}
3743
case 1:
3844
{
3945
// тут тестируются:
@@ -79,49 +85,49 @@ int main()
7985
Rational kek(69, 420);
8086
long long abc = 777;
8187

82-
cout << "(" << lol << ")++ = (" << ++lol << ")\n";
83-
cout << "(" << lol << ")-- = (" << --lol << ")\n";
84-
cout << "-(" << lol << ") = (" << -lol << ")\n";
88+
cout << "++" << lol << " = " << ++lol << "\n";
89+
cout << "--" << lol << " = " << --lol << "\n";
90+
cout << "-" << lol << " = " << -lol << "\n";
8591

86-
cout << "(" << lol << ") + (" << kek << ") = " << lol + kek << endl;
87-
cout << "(" << lol << ") + (" << abc << ") = " << lol + abc << endl;
88-
cout << "(" << abc << ") + (" << lol << ") = " << abc + lol << endl;
92+
cout << lol << " + " << kek << " = " << lol + kek << endl;
93+
cout << lol << " + " << abc << " = " << lol + abc << endl;
94+
cout << abc << " + " << lol << " = " << abc + lol << endl;
8995

90-
cout << "(" << lol << ") - (" << kek << ") = " << lol - kek << endl;
91-
cout << "(" << lol << ") - (" << abc << ") = " << lol - abc << endl;
92-
cout << "(" << abc << ") - (" << lol << ") = " << abc - lol << endl;
96+
cout << lol << " - " << kek << " = " << lol - kek << endl;
97+
cout << lol << " - " << abc << " = " << lol - abc << endl;
98+
cout << abc << " - " << lol << " = " << abc - lol << endl;
9399

94-
cout << "(" << lol << ") * (" << kek << ") = " << lol * kek << endl;
95-
cout << "(" << lol << ") * (" << abc << ") = " << lol * abc << endl;
96-
cout << "(" << abc << ") * (" << lol << ") = " << abc * lol << endl;
100+
cout << lol << " * " << kek << " = " << (lol * kek).normalize() << endl;
101+
cout << lol << " * " << abc << " = " << (lol * abc).normalize() << endl;
102+
cout << abc << " * " << lol << " = " << (abc * lol).normalize() << endl;
97103

98-
cout << "(" << lol << ") / (" << kek << ") = " << lol / kek << endl;
99-
cout << "(" << lol << ") / (" << abc << ") = " << lol / abc << endl;
100-
cout << "(" << abc << ") / (" << lol << ") = " << abc / lol << endl;
104+
cout << lol << " / " << kek << " = " << lol / kek << endl;
105+
cout << lol << " / " << abc << " = " << lol / abc << endl;
106+
cout << abc << " / " << lol << " = " << abc / lol << endl;
101107

102108
lol = {42, 618};
103109
kek = {69, 420};
104110
abc = 777;
105-
cout << "(" << lol << ") += (" << kek << ") -> " << (lol += kek) << endl;
106-
cout << "(" << lol << ") += (" << abc << ") -> " << (lol += abc) << endl;
111+
cout << lol << " += " << kek << " -> " << (lol += kek) << endl;
112+
cout << lol << " += " << abc << " -> " << (lol += abc) << endl;
107113

108114
lol = {42, 618};
109115
kek = {69, 420};
110116
abc = 777;
111-
cout << "(" << lol << ") -= (" << kek << ") -> " << (lol -= kek) << endl;
112-
cout << "(" << lol << ") -= (" << abc << ") -> " << (lol -= abc) << endl;
117+
cout << lol << " -= " << kek << " -> " << (lol -= kek) << endl;
118+
cout << lol << " -= " << abc << " -> " << (lol -= abc) << endl;
113119

114120
lol = {42, 618};
115121
kek = {69, 420};
116122
abc = 777;
117-
cout << "(" << lol << ") *= (" << kek << ") -> " << (lol *= kek) << endl;
118-
cout << "(" << lol << ") *= (" << abc << ") -> " << (lol *= abc) << endl;
123+
cout << lol << " *= " << kek << " -> " << (lol *= kek) << endl;
124+
cout << lol << " *= " << abc << " -> " << (lol *= abc) << endl;
119125

120126
lol = {42, 618};
121127
kek = {69, 420};
122128
abc = 777;
123-
cout << "(" << lol << ") /= (" << kek << ") -> " << (lol /= kek) << endl;
124-
cout << "(" << lol << ") /= (" << abc << ") -> " << (lol /= abc) << endl;
129+
cout << lol << " /= " << kek << " -> " << (lol /= kek) << endl;
130+
cout << lol << " /= " << abc << " -> " << (lol /= abc) << endl;
125131

126132
break;
127133
}
@@ -174,5 +180,30 @@ int main()
174180
}
175181
break;
176182
}
183+
case 5: // shell sort
184+
{
185+
int count = 9;
186+
Rational *arr[count] = {
187+
new Rational(1, 2),
188+
new Rational(12, 21),
189+
new Rational(13, 22),
190+
new Rational(14, 23),
191+
new Rational(15, 24),
192+
new Rational(0, 1),
193+
new Rational(666, 6),
194+
new Rational(16, 25),
195+
new Rational(17, 26)};
196+
cout << "original:\n";
197+
for (int i = 0; i < count; i++)
198+
{
199+
cout << *(arr[i]) << endl;
200+
}
201+
cout << "sorted:\n";
202+
ShellSort(count, arr);
203+
for (int i = 0; i < count; i++)
204+
{
205+
cout << *(arr[i]) << endl;
206+
}
207+
}
177208
}
178209
}

Variants/Lab_13/var_6/rational.cpp

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ long long lcm(long long a, long long b)
1515
return (a * b) / gcd(a, b);
1616
}
1717

18-
Rational::Rational(long long numerator, long long denominator) : _numerator(denominator > 0 ? numerator : -numerator),
18+
Rational::Rational(long long numerator, long long denominator) : _integer(0),
19+
_numerator(denominator > 0 ? numerator : -numerator),
1920
_denominator(denominator ? (denominator > 0 ? denominator : -denominator) : 1)
2021
{
2122
assert(denominator != 0);
2223
this->simplify();
2324
}
2425

25-
Rational::Rational(const Rational &temp, bool invert) : _numerator(invert ? temp._denominator : temp._numerator),
26+
Rational::Rational(const Rational &temp, bool invert) : _integer(0),
27+
_numerator(invert ? temp._denominator : temp._numerator),
2628
_denominator(invert ? temp._numerator : temp._denominator)
2729
{
2830
this->simplify();
@@ -45,6 +47,11 @@ long long &Rational::denominator()
4547
return _denominator;
4648
}
4749

50+
long long &Rational::integer()
51+
{
52+
return _integer;
53+
}
54+
4855
double Rational::get_value()
4956
{
5057
return (double)_numerator / (double)_denominator;
@@ -60,6 +67,24 @@ Rational &Rational::simplify()
6067
_numerator /= factor;
6168
_denominator /= factor;
6269
}
70+
normalize();
71+
return *this;
72+
}
73+
74+
Rational &Rational::normalize()
75+
{
76+
if (!_integer)
77+
_integer = _numerator / _denominator;
78+
_numerator %= _denominator;
79+
// if (_integer && _numerator < 0) _numerator *= -1;
80+
return *this;
81+
}
82+
83+
Rational &Rational::denormalize()
84+
{
85+
// if (_integer < 0 && _numerator > 0) _numerator *= -1;
86+
_numerator += _integer * _denominator;
87+
_integer = 0;
6388
return *this;
6489
}
6590

@@ -74,7 +99,29 @@ istream &operator>>(istream &in, Rational &temp)
7499

75100
ostream &operator<<(ostream &out, const Rational &r)
76101
{
77-
out << r._numerator << " / " << r._denominator;
102+
long long num = r._numerator;
103+
if (r._integer)
104+
{
105+
out << r._integer;
106+
if (num)
107+
{
108+
if (r._integer > 0)
109+
out << "+";
110+
if (r._integer < 0)
111+
out << "-";
112+
if (num < 0)
113+
num *= -1;
114+
}
115+
}
116+
if (r._numerator)
117+
{
118+
out << "(" << num;
119+
if (r._denominator != 1)
120+
out << "/" << r._denominator;
121+
out << ")";
122+
}
123+
else
124+
out << 0;
78125
return out;
79126
}
80127

@@ -96,7 +143,8 @@ Rational &Rational::operator=(const long long &temp)
96143

97144
bool operator==(const Rational &r1, const Rational &r2)
98145
{
99-
return (r1._numerator == r2._numerator &&
146+
return (r1._integer == r2._integer &&
147+
r1._numerator == r2._numerator &&
100148
r1._denominator == r2._denominator);
101149
}
102150

@@ -107,12 +155,12 @@ bool operator!=(const Rational &r1, const Rational &r2)
107155

108156
const Rational Rational::operator+()
109157
{
110-
return Rational(_numerator, _denominator);
158+
return Rational(_numerator + _integer * _denominator, _denominator);
111159
}
112160

113161
const Rational Rational::operator-()
114162
{
115-
return Rational(-_numerator, _denominator);
163+
return Rational(-(_numerator + _integer * _denominator), _denominator);
116164
}
117165

118166
Rational &Rational::operator++()
@@ -146,8 +194,8 @@ Rational Rational::operator--(int)
146194
Rational operator+(const Rational &r1, const Rational &r2)
147195
{
148196
Rational temp(
149-
r1._numerator * r2._denominator +
150-
r2._numerator * r1._denominator,
197+
(r1._numerator + r1._integer * r1._denominator) * r2._denominator +
198+
(r2._numerator + r2._integer * r2._denominator) * r1._denominator,
151199
r1._denominator * r2._denominator);
152200
return temp.simplify();
153201
};
@@ -184,7 +232,7 @@ Rational operator-(long long value, const Rational &r)
184232
Rational operator*(const Rational &r1, const Rational &r2)
185233
{
186234
Rational temp(
187-
r1._numerator * r2._numerator,
235+
(r1._numerator + r1._integer * r2._denominator) * (r2._numerator + r2._integer + r2._denominator),
188236
r1._denominator * r2._denominator);
189237
return temp.simplify();
190238
};
@@ -274,8 +322,8 @@ bool operator>(const Rational &r1, const Rational &r2)
274322
{
275323
long long L = lcm(r1._denominator, r2._denominator);
276324
return (
277-
r1._numerator * L / r1._denominator >
278-
r2._numerator * L / r2._denominator);
325+
(r1._numerator + r1._integer * r1._denominator) * L / r1._denominator >
326+
(r2._numerator + r1._integer * r1._denominator) * L / r2._denominator);
279327
};
280328

281329
bool operator<=(const Rational &r1, const Rational &r2)
@@ -291,4 +339,25 @@ bool operator<(const Rational &r1, const Rational &r2)
291339
bool operator>=(const Rational &r1, const Rational &r2)
292340
{
293341
return (r1 == r2 || r1 > r2);
294-
};
342+
};
343+
344+
// сортировка Шелла
345+
346+
void ShellSort(int n, Rational *mass[])
347+
{
348+
int i, j, step;
349+
Rational *tmp;
350+
for (step = n / 2; step > 0; step /= 2)
351+
for (i = step; i < n; i++)
352+
{
353+
tmp = mass[i];
354+
for (j = i; j >= step; j -= step)
355+
{
356+
if (*tmp < *mass[j - step])
357+
mass[j] = mass[j - step];
358+
else
359+
break;
360+
}
361+
mass[j] = tmp;
362+
}
363+
}

Variants/Lab_13/var_6/rational.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using namespace std;
1010
class Rational
1111
{
1212
private:
13+
long long _integer; // целая часть
1314
long long _numerator; // числитель
1415
long long _denominator; // знаминатель
1516

@@ -18,11 +19,14 @@ class Rational
1819
Rational(const Rational &temp, bool invert = false);
1920

2021
Rational &set(long long numerator, long long denominator);
22+
long long &integer();
2123
long long &numerator();
2224
long long &denominator();
2325

2426
double get_value();
2527
Rational &simplify();
28+
Rational &normalize(); // вычисление целой части числа
29+
Rational &denormalize(); // обратное действие для упрощения вычислений
2630

2731
friend istream &operator>>(istream &in, Rational &temp);
2832
friend ostream &operator<<(ostream &out, const Rational &temp);
@@ -78,4 +82,6 @@ class Rational
7882

7983
friend bool operator<(const Rational &r1, const Rational &r2);
8084
friend bool operator>=(const Rational &r1, const Rational &r2);
85+
86+
friend void ShellSort(int n, Rational *mass[]);
8187
};

0 commit comments

Comments
 (0)