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
| //Реализация
#include "rational.h"
#include <iostream>
#include <iomanip>
using namespace std;
Rational::Rational(int ch, int zn)
{
setRational(ch, zn);
}
Rational::Rational(Rational &arg)
{
setRational(arg.numer, arg.denom);
}
Rational::~Rational() {};
int Rational::nodR(int x, int y)
{
if (y == 0)
return x;
else
return nodR(y, x % y);
}
int Rational::nokR(int x, int y)
{
for (int i = 1; i > 0; i++)
if (i % x == 0 && i % y == 0)
return i;
return 1;
}
void Rational::setRational(int ch, int zn)
{
int nod;
if (ch >= zn)
nod = nodR(ch, zn);
else
nod = nodR(zn, ch);
if (!zn)
cout << "Деление на \"0\" невозможно." << endl;
else
if (ch < 0 && zn < 0)
{
numer = (ch / nod) * -1;
denom = (zn / nod) * -1;
}
else
{
numer = ch / nod;
denom = zn / nod;
}
}
void Rational::sumRational(Rational x, Rational y)
{
int nok = nokR(x.denom, y.denom);
setRational((x.numer * (nok / x.denom)) + (y.numer * (nok / y.denom)), nok);
}
void Rational::diffRational(Rational x, Rational y)
{
int nok = nokR(x.denom, y.denom);
setRational((x.numer * (nok / x.denom)) - (y.numer * (nok / y.denom)), nok);
}
void Rational::prodRational(Rational x, Rational y)
{
setRational(x.numer * y.numer, x.denom * y.denom);
}
void Rational::quoRational(Rational x, Rational y)
{
setRational(x.numer * y.denom, x.denom * y.numer);
}
void Rational::maxRational(Rational x, Rational y)
{
int nok = nokR(x.denom, y.denom);
setRational((x.numer * (nok / x.denom)) > (y.numer * (nok / y.denom)), nok);
}
/*
void Rational::minRational(Rational x, Rational y)
{
int nok = nokR(x.denom, y.denom);
setRational((x.numer * (nok / x.denom)) < (y.numer * (nok / y.denom)), nok);
}
*/
void Rational::printRational()
{
cout << numer << "/" << denom;
}
void Rational::printDoubleRational()
{
cout << setprecision(6) << setiosflags(ios::fixed | ios::showpoint)
<< (double)numer / (double)denom;
}
ostream &operator << (ostream &output, Rational &right)
{
right.printRational();
return output;
}
istream &operator >> (istream &input, Rational &right)
{
input >> right.numer;
cin.get();
input >> right.denom;
right.setRational(right.numer, right.denom);
return input;
}
Rational Rational::operator+(Rational &right)
{
Rational temp = *this;
temp.sumRational(temp, right);
return temp;
}
Rational Rational::operator-(Rational &right)
{
Rational temp = *this;
temp.diffRational(temp, right);
return temp;
}
Rational Rational::operator*(Rational &right)
{
Rational temp = *this;
temp.prodRational(temp, right);
return temp;
}
Rational Rational::operator/(Rational &right)
{
Rational temp = *this;
temp.quoRational(temp, right);
return temp;
}
/*
Rational Rational::operator >(Rational &)
{
Rational temp = *this;
temp.minRational(temp, right);
return temp;
}*/
Rational Rational::operator <(Rational &right)
{
Rational temp = *this;
temp.maxRational(temp, right);
return temp;
} |