Last active
December 23, 2021 18:28
-
-
Save zindmax/e6256cc747c0f2d72897bc4087bf66b3 to your computer and use it in GitHub Desktop.
lab 8
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 stack { | |
int *data; | |
int size; | |
int count = 0; | |
public: | |
stack(int _size) : size(_size) { | |
data = new int[size]; | |
} | |
// перегрузка присваивания | |
stack(stack&& x) { | |
data = x.data; | |
size = x.size; | |
count = x.count; | |
x.data = nullptr; | |
} | |
stack(const stack &x) { | |
data = new int[x.size]; | |
size = x.size; | |
count = x.count; | |
for (int i = 0; i < size; i++) { | |
data[i] = x.data[i]; | |
} | |
} | |
int &operator[](int n) { | |
return data[count - n - 1]; | |
} | |
stack& operator=(const stack &x) { | |
delete[] data; | |
data = new int[x.size]; | |
size = x.size; | |
count = x.count; | |
for (int i = 0; i < x.size; i++) { | |
data[i] = x.data[i]; | |
} | |
return *this; | |
} | |
stack& operator=(stack&& x){ | |
delete[] data; | |
data = x.data; | |
size = x.size; | |
count = x.count; | |
x.data = nullptr; | |
return *this; | |
} | |
stack& operator<<(int x) { | |
data[count] = x; | |
count ++; | |
return *this; | |
} | |
stack& operator>>(int &x) { | |
count--; | |
x = data[count]; | |
return *this; | |
} | |
}; | |
int main() | |
{ | |
// int n=100000; | |
// stack 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); | |
// } | |
int x, y, z; | |
stack s(20); | |
s << 10 << 20 << 30; | |
cout << s[0] << s[1] << s[2] << endl; | |
stack t = s; | |
t[1] = 25; | |
t >> x >> y >> z; | |
cout << x << y << z << endl; | |
t=s; | |
t >> x >> y >> z; | |
cout << x << y << z << endl; | |
s >> x >> y >> z; | |
cout << x << y << z; | |
return 0; | |
} |
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 stack { | |
int *data; | |
int size; | |
int count = 0; | |
public: | |
stack(int _size) : size(_size) { | |
data = new int[size]; | |
} | |
// перегрузка присваивания | |
stack(stack&& x) { | |
data = x.data; | |
size = x.size; | |
count = x.count; | |
x.data = nullptr; | |
} | |
stack(const stack &x) { | |
data = new int[x.size]; | |
size = x.size; | |
count = x.count; | |
for (int i = 0; i < size; i++) { | |
data[i] = x.data[i]; | |
} | |
} | |
int &operator[](int n) { | |
return data[count - n - 1]; | |
} | |
stack& operator=(const stack &x) { | |
delete[] data; | |
data = new int[x.size]; | |
size = x.size; | |
count = x.count; | |
for (int i = 0; i < x.size; i++) { | |
data[i] = x.data[i]; | |
} | |
return *this; | |
} | |
stack& operator=(stack&& x){ | |
delete[] data; | |
data = x.data; | |
size = x.size; | |
count = x.count; | |
x.data = nullptr; | |
return *this; | |
} | |
stack& operator<<(int x) { | |
data[count] = x; | |
count ++; | |
return *this; | |
} | |
stack& operator>>(int &x) { | |
count--; | |
x = data[count]; | |
return *this; | |
} | |
}; | |
int main() | |
{ | |
int n=100000; | |
stack 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); | |
} | |
// int x, y, z; | |
// stack s(20); | |
// s << 10 << 20 << 30; | |
// cout << s[0] << s[1] << s[2] << endl; | |
// stack t = s; | |
// t[1] = 25; | |
// t >> x >> y >> z; | |
// cout << x << y << z << endl; | |
// s >> x >> y >> z; | |
// cout << x << y << z; | |
return 0; | |
} |
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 { | |
long long nom, denom; | |
private: | |
void getNOD() { | |
int nod = 0; | |
long long nom_c = std::abs(nom); | |
long long denom_c = std::abs(denom); | |
while (nom_c * denom_c != 0) { | |
if (nom_c > denom_c) { | |
nom_c = nom_c % denom_c; | |
}else { | |
denom_c = denom_c % nom_c; | |
} | |
} | |
nod = nom_c + denom_c; | |
nom /= nod; | |
denom /= nod; | |
} | |
public: | |
rational(long long x, long long y) : nom(x), denom(y) { | |
this -> getNOD(); | |
} | |
rational(long long x) : nom(x), denom(1) {} | |
rational() : nom(0), denom(1) {} | |
explicit operator int() { | |
return nom / denom; | |
} | |
explicit operator double() { | |
return 1.0 * nom / denom; | |
} | |
rational& operator=(const rational &x) { | |
nom = x.nom; | |
denom = x.denom; | |
return *this; | |
} | |
rational& operator+=(const rational &x) { | |
*this = *this + x; | |
return *this; | |
} | |
rational& operator-=(const rational &x) { | |
*this = *this - x; | |
return *this; | |
} | |
rational& operator*=(const rational &x) { | |
*this = *this * x; | |
return *this; | |
} | |
rational& operator/=(const rational &x) { | |
*this = *this / x; | |
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 istream& operator>>(istream&, rational&); | |
friend ostream& operator<<(ostream&, const rational&); | |
}; | |
rational operator*(const rational &x, const rational &y) { | |
return rational(x.nom*y.nom, x.denom*y.denom); | |
} | |
rational operator+(const rational &x, const rational &y) { | |
return rational(x.nom*y.denom + x.denom*y.nom, x.denom*y.denom); | |
} | |
rational operator-(const rational &x, const rational &y) { | |
return rational(x.nom*y.denom - x.denom*y.nom, x.denom*y.denom); | |
} | |
rational operator/(const rational &x, const rational &y){ | |
return rational(x.nom*y.denom, x.denom*y.nom); | |
} | |
bool operator<(const rational &x, const rational &y) { | |
return x.nom*y.denom < x.denom*y.nom; | |
} | |
bool operator<=(const rational &x, const rational &y) { | |
return x.nom*y.denom <= x.denom*y.nom; | |
} | |
bool operator>(const rational &x, const rational &y) { | |
return x.nom*y.denom > x.denom*y.nom; | |
} | |
bool operator>=(const rational &x, const rational &y) { | |
return x.nom*y.denom >= x.denom*y.nom; | |
} | |
bool operator==(const rational &x, const rational &y) { | |
return x.nom*y.denom == x.denom*y.nom; | |
} | |
bool operator!=(const rational &x, const rational &y) { | |
return x.nom*y.denom != x.denom*y.nom; | |
} | |
istream& operator>>(istream &input, rational &x) { | |
input >> x.nom; | |
input >> x.denom; | |
x.getNOD(); | |
return input; | |
} | |
ostream& operator<<(ostream &out, const rational &x) { | |
out<< x.nom << "/" << x.denom; | |
return out; | |
} | |
int main() | |
{ | |
rational r, r1, r2; | |
std::cin >> r >> r1 >> r2; | |
std::cout << r << '\n'; | |
std::cout << r1 << '\n'; | |
std::cout << r2 << '\n'; | |
r1 += r + r2; | |
std::cout << r1 << '\n'; | |
r1 /= r - r2; | |
std::cout << r1 << '\n'; | |
r1 *= r / r2; | |
std::cout << r1 << '\n'; | |
r1 -= r + r2; | |
std::cout << r1 << '\n'; | |
return 0; | |
} |
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 __getNOD(self): | |
nod = 0; | |
nom_c = abs(self.nom) | |
denom_c = abs(self.denom) | |
while (nom_c * denom_c != 0): | |
if (nom_c > denom_c): | |
nom_c = nom_c % denom_c | |
else: | |
denom_c = denom_c % nom_c | |
nod = nom_c + denom_c; | |
self.nom //= nod; | |
self.denom //= nod; | |
def __init__ (self, _nom=0, _denom=1): | |
self.nom = _nom | |
self.denom = _denom | |
self.__getNOD(); | |
def __int__ (self): | |
return self.nom // self.denom | |
def __float__ (self, x): | |
return 1.0 * self.nom / self.denom | |
def __add__(self, x): | |
return rational(self.nom*x.denom + self.denom*x.nom, self.denom*x.denom) | |
def __sub__(self, x): | |
return rational(self.nom*x.denom - self.denom*x.nom, 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 __floordiv__(self, x): | |
return rational(self.nom*x.denom, self.denom*x.nom) | |
def __lt__(self, x): | |
return self.nom*x.denom < self.denom*x.nom | |
def __le__(self, x): | |
return self.nom*x.denom <= self.denom*x.nom | |
def __eq__(self, x): | |
return self.nom*x.denom == self.denom*x.nom | |
def __gt__(self, x): | |
return self.nom*x.denom > self.denom*x.nom | |
def __ge__(self, x): | |
return self.nom*x.denom >= self.denom*x.nom | |
def __ne__(self, x): | |
return self.nom*x.denom != self.denom*x.nom | |
def __iadd__(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 __ifloordiv__(self, x): | |
self = self / x | |
return self | |
def __isub__(self, x): | |
self = self - x | |
return self | |
def __str__(self): | |
return str(self.nom) + "/" + str(self.denom) | |
def 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) | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment