Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- int ppow(int n, int a)
- {
- if (a == 0)
- return 1;
- if (a % 2 == 0)
- {
- int temp = ppow(n, a / 2);
- return temp * temp;
- }
- return n * ppow(n, a - 1);
- }
- class integer
- {
- private:
- int num;
- public:
- integer() {}
- integer(int num)
- {
- this->num = num;
- }
- int get_int()
- {
- return num;
- }
- void change_int(int a)
- {
- num = a;
- }
- integer operator+(integer a)
- {
- return integer(num + a.get_int());
- }
- integer operator-(integer a)
- {
- return integer(num - a.get_int());
- }
- integer operator*(integer a)
- {
- return integer(num * a.get_int());
- }
- integer operator^(integer a)
- {
- return integer(ppow(num, a.get_int()));
- }
- integer modul()
- {
- return integer(abs(num));
- }
- void print()
- {
- cout << num;
- }
- vector<integer> operator*(vector<integer> a)
- {
- int n = a.size();
- vector<integer> res(n);
- for (int i = 0; i < n; i++)
- res[i] = *this * a[i];
- return res;
- }
- string operator*(string a)
- {
- string res = "";
- for (int i = 0; i < num; i++)
- res += a;
- return res;
- }
- void smart_print()
- {
- int d = 2, temp = num;
- while (temp > 1)
- {
- if (temp % d == 0)
- {
- if (temp < num)
- cout << " * ";
- int k = 0;
- while (temp % d == 0)
- {
- temp /= d;
- k++;
- }
- integer(d).print();
- if (k > 1)
- {
- cout << "^";
- integer(k).print();
- }
- }
- d++;
- }
- }
- int get_len()
- {
- int temp = this->modul().get_int(), res = 1;
- while (temp / 10 > 0)
- {
- temp /= 10;
- res++;
- }
- return res;
- }
- };
- class bin_num : public integer
- {
- public:
- bin_num() : integer() {}
- bin_num(int num) : integer(num) {}
- bin_num(string s) : integer()
- {
- int temp = 0, p = 1;
- for (int i = s.size() - 1; i >= 0; i--)
- {
- if (s[i] == '1')
- temp += p;
- p *= 2;
- }
- this->change_int(temp);
- }
- bin_num operator+(bin_num a)
- {
- return bin_num(this->get_int() + a.get_int());
- }
- bin_num operator-(bin_num a)
- {
- return bin_num(this->get_int() - a.get_int());
- }
- bin_num operator*(bin_num a)
- {
- return bin_num(this->get_int() * a.get_int());
- }
- bin_num operator^(bin_num a)
- {
- return bin_num(ppow(this->get_int(), a.get_int()));
- }
- vector<bin_num> operator*(vector<bin_num> a)
- {
- int n = a.size();
- vector<bin_num> res(n);
- for (int i = 0; i < n; i++)
- res[i] = *this * a[i];
- return res;
- }
- string operator*(string a)
- {
- string res = "";
- for (int i = 0; i < this->get_int(); i++)
- res += a;
- return res;
- }
- void print()
- {
- int temp = this->get_int();
- string s = "";
- while (temp > 0)
- {
- if (temp % 2)
- s += '1';
- else
- s += '0';
- temp /= 2;
- }
- reverse(s.begin(), s.end());
- cout << s;
- }
- void smart_print()
- {
- int d = 2, temp = this->get_int();
- while (temp > 1)
- {
- if (temp % d == 0)
- {
- if (temp < this->get_int())
- cout << " * ";
- int k = 0;
- while (temp % d == 0)
- {
- temp /= d;
- k++;
- }
- bin_num(d).print();
- if (k > 1)
- {
- cout << "^";
- bin_num(k).print();
- }
- }
- d++;
- }
- }
- int get_len()
- {
- int p = 2, res = 1;
- while (p < this->get_int())
- {
- p *= 2;
- res++;
- }
- return res;
- }
- };
- class bool_num : public integer
- {
- public:
- bool_num(int num) : integer(num)
- {
- if (this->get_int() != 0)
- this->change_int(1);
- }
- bool_num operator+(bool_num a)
- {
- return bool_num(this->get_int() || a.get_int());
- }
- bool_num operator*(bool_num a)
- {
- return bool_num(this->get_int() && a.get_int());
- }
- bool_num operator^(bool_num a)
- {
- return bool_num(!(this->get_int() || a.get_int()));
- }
- bool_num operator-(bool_num a)
- {
- return bool_num(this->get_int() ^ a.get_int());
- }
- void print()
- {
- if (this->get_int())
- cout << "true";
- else
- cout << "false";
- }
- void smart_print()
- {
- cout << "you can't use this method to this class";
- }
- };
- int main()
- {
- cout << "Integer test:\n";
- integer a(2), b(10);
- (a + b).print();
- cout << "\n";
- (b - a).print();
- cout << "\n";
- (a * b).print();
- cout << "\n";
- (a ^ b).print();
- cout << "\n";
- cout << a * "ab";
- cout << "\n";
- vector<integer> temp(2, 3);
- temp[1] = 6;
- temp = b * temp;
- for (auto elem : temp)
- {
- elem.print();
- cout << " ";
- }
- cout << "\n";
- (a + b).smart_print();
- cout << "\n";
- cout << "Bin_num test:\n";
- bin_num c("101"), d(7);
- (c + d).print();
- cout << "\n";
- (c + d).smart_print();
- cout << "\n";
- cout << "Bool_num test:\n";
- bool_num e(9), f(0);
- e.print();
- cout << "\n";
- f.print();
- cout << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement