Last active
December 23, 2021 16:44
-
-
Save kirilltobola/e4962456cc6fa74f39fded5324a74fbc to your computer and use it in GitHub Desktop.
lab 9.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class rational { | |
private: | |
long long _nom, _denom; | |
long long gcd(long long a, long long b) { | |
if (a * b == 0) return a + b; | |
if (b > a) { | |
swap(a, b); | |
} | |
return gcd(a % b, b); | |
} | |
void reduce() { | |
long long common_divisor = gcd(abs(_nom), abs(_denom)); | |
_nom /= common_divisor; | |
_denom /= common_divisor; | |
} | |
public: | |
rational() { | |
_nom = 0; | |
_denom = 1; | |
} | |
rational(int i) { | |
_nom = i; | |
_denom = 1; | |
} | |
rational(long long nom, long long denom) { | |
_nom = nom; | |
_denom = denom; | |
reduce(); | |
} | |
explicit operator int () { | |
return _nom / _denom; | |
} | |
explicit operator double () { | |
return 1.0 * _nom / _denom; | |
} | |
rational& operator = (const rational &other) { | |
_nom = other._nom; | |
_denom = other._denom; | |
return *this; | |
} | |
rational& operator += (const rational &other) { | |
long long temp = _denom; | |
_denom *= other._denom; | |
_nom *= other._denom; | |
_nom += other._nom * temp; | |
return *this; | |
} | |
rational& operator -= (const rational &other) { | |
long long temp = _denom; | |
_denom *= other._denom; | |
_nom *= other._denom; | |
_nom -= other._nom * temp; | |
return *this; | |
} | |
rational& operator *= (const rational &other) { | |
_nom *= other._nom; | |
_denom *= other._denom; | |
return *this; | |
} | |
rational& operator /= (const rational &other) { | |
_nom *= other._denom; | |
_denom *= other._nom; | |
return *this; | |
} | |
friend rational operator + (const rational&, const rational&); | |
friend rational operator - (const rational&, const rational&); | |
friend rational operator * (const rational&, const rational&); | |
friend rational operator / (const rational&, const rational&); | |
friend bool operator > (const rational&, const rational&); | |
friend bool operator < (const rational&, const rational&); | |
friend bool operator >= (const rational&, const rational&); | |
friend bool operator <= (const rational&, const rational&); | |
friend bool operator == (const rational&, const rational&); | |
friend bool operator != (const rational&, const rational&); | |
friend ostream& operator << (ostream&, const rational&); | |
friend istream& operator >> (istream&, rational&); | |
}; | |
rational operator + (const rational &a, const rational &b) { | |
return rational(a._nom * b._denom + b._nom * a._denom, a._denom * b._denom); | |
} | |
rational operator - (const rational &a, const rational &b) { | |
return rational(a._nom * b._denom - b._nom * a._denom, a._denom * b._denom); | |
} | |
rational operator * (const rational &a, const rational &b) { | |
return rational(a._nom * b._nom, a._denom * b._denom); | |
} | |
rational operator / (const rational &a, const rational &b) { | |
return rational(a._nom * b._denom, a._denom * b._nom); | |
} | |
bool operator > (const rational &a, const rational &b) { | |
if (a._nom * b._denom > b._nom * a._denom) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
bool operator < (const rational &a, const rational &b) { | |
if (a._nom * b._denom < b._nom * a._denom) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
bool operator >= (const rational &a, const rational &b) { | |
if (a._nom * b._denom >= b._nom * a._denom) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
bool operator <= (const rational &a, const rational &b) { | |
if (a._nom * b._denom <= b._nom * a._denom) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
bool operator == (const rational &a, const rational &b) { | |
if (a._nom * b._denom == b._nom * a._denom) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
bool operator != (const rational &a, const rational &b) { | |
if (a._nom * b._denom != b._nom * a._denom) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
ostream& operator << (ostream &out, const rational &a) { | |
out << a._nom << '/' << a._denom; | |
return out; | |
} | |
istream& operator >> (istream& is, rational& a) { | |
char t; | |
is >> a._nom >> t >> a._denom; | |
a.reduce(); | |
return is; | |
} | |
int main() | |
{ | |
rational x; | |
cin >> x; | |
cout << x; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rational: | |
def __init__(self, nom=0, denom=1): | |
self.__nom = nom | |
self.__denom = denom | |
self.__reduce() | |
def __gcd(self, a: int, b: int) -> int: | |
if a * b == 0: | |
return a + b | |
if b > a: | |
a, b = b, a | |
return self.__gcd(a % b, b) | |
def __reduce(self) -> None: | |
common_denom: int = self.__gcd(abs(self.__nom), abs(self.__denom)) | |
self.__nom //= common_denom | |
self.__denom //= common_denom | |
def __add__(self, x): | |
return Rational( | |
self.__nom * x.__denom + x.__nom * self.__denom, | |
self.__denom * x.__denom | |
) | |
def __sub__(self, x): | |
return Rational( | |
self.__nom * x.__denom - x.__nom * self.__denom, | |
self.__denom * x.__denom | |
) | |
def __mul__(self, x): | |
return Rational( | |
self.__nom * x.__nom, | |
self.__denom * x.__denom | |
) | |
def __truediv__(self, x): | |
return Rational( | |
self.__nom * x.__denom, | |
self.__denom * x.__nom | |
) | |
def __iadd__(self, x): | |
self = self + x | |
return self | |
def __isub__(self, x): | |
self = self - x | |
return self | |
def __imul__(self, x): | |
self = self * x | |
return self | |
def __itruediv__(self, x): | |
self = self / x | |
return self | |
def __eq__(self, x) -> bool: | |
if self.__nom * x.__denom == x.__nom * self.__denom: | |
return True | |
else: | |
return False | |
def __ne__(self, x) -> bool: | |
if self.__nom * x.__denom != x.__nom * self.__denom: | |
return True | |
else: | |
return False | |
def __gt__(self, x) -> bool: | |
if self.__nom * x.__denom > x.__nom * self.__denom: | |
return True | |
else: | |
return False | |
def __ge__(self, x) -> bool: | |
if self.__nom * x.__denom >= x.__nom * self.__denom: | |
return True | |
else: | |
return False | |
def __lt__(self, x) -> bool: | |
if self.__nom * x.__denom < x.__nom * self.__denom: | |
return True | |
else: | |
return False | |
def __le__(self, x) -> bool: | |
if self.__nom * x.__denom <= x.__nom * self.__denom: | |
return True | |
else: | |
return False | |
def __float__(self): | |
return self.__nom / self.__denom | |
def __int__(self): | |
return self.__nom // self.__denom | |
def __str__(self) -> str: | |
return f'{self.__nom}/{self.__denom}' | |
if __name__ == '__main__': | |
r1 = Rational(3, 4) | |
r2 = Rational(6, 8) | |
r3 = Rational(10, 47) | |
print(r1 <= r2) | |
print(r1 != r3) | |
print(r1, r2) | |
print(r1 + r2) | |
r1 = r2 | |
print(r1, r2) | |
r1 = Rational(5, 7) | |
r4 = r1 + r2 | |
print(r4) | |
r4 += r3 | |
print(r4) | |
r4 -= r3 | |
print(r4) | |
r4 /= r2 | |
print(r4) | |
r4 *= r2 | |
print(r4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
class mstack { | |
private: | |
int _n; | |
vector<int> *vec; | |
public: | |
mstack(int n) { | |
_n = n; | |
vec = new vector<int>(); | |
} | |
mstack(const mstack &other) { | |
_n = other._n; | |
vec = new vector<int>(); | |
*vec = *other.vec; | |
} | |
mstack(mstack &&other) { | |
_n = other._n; | |
vec = other.vec; | |
other.vec = nullptr; | |
} | |
mstack& operator << (const int x) { | |
vec->emplace(vec->begin(), x); | |
return *this; | |
} | |
mstack& operator >> (int &x) { | |
x = (*vec)[0]; | |
vec->erase(vec->begin()); | |
return *this; | |
} | |
int& operator [] (int i) { | |
return (*vec)[i]; | |
} | |
mstack& operator = (const mstack &other) { | |
_n = other._n; | |
vec = new vector<int>(); | |
*vec = *other.vec; | |
return *this; | |
} | |
mstack& operator = (mstack &&other) { | |
delete vec; | |
_n = other._n; | |
vec = other.vec; | |
other.vec = nullptr; | |
return *this; | |
} | |
}; | |
int main() | |
{ | |
int x, y, z; | |
mstack s(20); | |
s << 10 << 20 << 30; | |
cout << s[0] << s[1] << s[2]; // 30 20 10 ok | |
cout << endl; | |
mstack t = s; | |
t[1] = 25; | |
t >> x >> y >> z; | |
cout << x << y << z; // 30 25 10 ok | |
cout << endl; | |
t = s; | |
t >> x >> y >> z; | |
cout << x << y << z; // 30 20 10 ok | |
cout << endl; | |
s >> x >> y >> z; | |
cout << x << y << z; // 30 20 10 ok | |
int n = 100000; | |
mstack a(n), b(n); | |
for (int i = 0; i < n; ++i) { | |
a << i, b << n - i; | |
} | |
for (int i = 0; i < 1000000; ++i) { | |
swap(a, b); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment