Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- using namespace std;
- class Int {
- bool sign = true;
- string value_string;
- void align_number_length(int lenght) {
- while (value_string.size() < lenght) {
- value_string = "0" + value_string;
- }
- }
- void strip_number() {
- while (value_string[0] == '0' && value_string.size() > 1)
- {
- value_string.erase(value_string.begin());
- }
- }
- static int sum_char_digits(char object_digit, char other_digit) {
- return (object_digit - 48) + (other_digit - 48);
- }
- static int dif_char_digits(char object_digit, char other_digit) {
- return (object_digit - 48) - (other_digit - 48);
- }
- char& operator[](int index) {
- return value_string[index];
- }
- public:
- Int(int value) {
- string str_value = to_string(value);
- if (str_value[0] == '-') {
- sign = false;
- str_value.erase(str_value.begin());
- }
- value_string = str_value;
- }
- Int(string value) {
- if (value[0] == '-') {
- sign = false;
- value.erase(value.begin());
- }
- value_string = value;
- }
- Int(string value, int n) {
- Int res;
- int len = value.size();
- int k = 1;
- for (int i = len - 1; i >= 0; --i)
- {
- char char_digit = tolower(value[i]);
- int int_digit;
- if (char_digit >= '0' && char_digit <= '9') {
- int_digit = char_digit - '0';
- if (int_digit >= n) {
- throw invalid_argument("Unknown digit");
- }
- }
- else if ('a' <= char_digit && char_digit <= 'z') {
- int_digit = char_digit - 'a';
- }
- else {
- throw invalid_argument("Unknown digit");
- }
- Int digit((int_digit) * k);
- res += digit;
- k *= n;
- }
- value_string = res.value_string;
- sign = res.sign;
- }
- Int() {
- value_string = "0";
- }
- size_t size() {
- return value_string.size();
- }
- friend ostream& operator<<(ostream& out, Int& object){
- if (!object.sign)
- out << '-';
- out << object.value_string;
- return out;
- }
- friend istream& operator>>(istream& in, Int& object) {
- string temp_val;
- in >> temp_val;
- if (temp_val[0] == '-') {
- object.sign = false;
- temp_val.erase(temp_val.begin());
- }
- object.value_string = temp_val;
- return in;
- }
- Int operator-() const{
- Int res;
- res.value_string = this->value_string;
- res.sign = !res.sign;
- return res;
- }
- friend Int operator++(Int& object) {
- object += 1;
- return object;
- }
- friend bool operator>(Int& object, int other_int) {
- Int other(other_int);
- return object > other;
- }
- friend bool operator<(Int& object, int other_int) {
- Int other(other_int);
- return object < other;
- }
- friend bool operator==(Int& object, int other_int) {
- return object.value_string == to_string(other_int);
- }
- friend bool operator>=(Int& object, int other_int) {
- return object == other_int || object > other_int;
- }
- friend bool operator<=(Int& object, int other_int) {
- return object == other_int || object < other_int;
- }
- // ВЫЧИТАНИЕ
- friend Int operator-(Int& object, int other_int) {
- Int other(other_int);
- return object - other;
- }
- friend Int operator-=(Int& object, int other_int) {
- Int other(other_int);
- return object -= other;
- }
- // СЛАЖЕНИЕ
- friend Int operator+(Int& object, int other_int) {
- Int other(other_int);
- return object + other;
- }
- friend Int operator+=(Int& object, int other_int) {
- Int other(other_int);
- return object += other;
- }
- // УМНОЖЕНИЕ
- friend Int operator*(Int& object, int other_int) {
- Int other(other_int);
- return object * other;
- }
- friend Int operator*=(Int& object, int other_int) {
- Int other(other_int);
- object *= other;
- return object;
- }
- // ДЕЛЕНИЯ
- friend Int operator/(Int& object, int other_int) {
- Int other(other_int);
- return object / other;
- }
- friend Int operator/=(Int& object, int other_int) {
- Int other(other_int);
- return object = object / other;
- }
- ///////////////////////////////////////////////////////////////////
- // int / Int
- ///////////////////////////////////////////////////////////////////
- friend bool operator>(Int& object, Int& other) {
- if (object.sign != other.sign)
- return object.sign;
- if (object.size() != other.size())
- return object.size() > other.size();
- for (int i = 0; i < object.size(); i++)
- {
- if (object[i] != other[i])
- return object[i] > other[i];
- }
- return false;
- }
- friend bool operator<(Int& object, Int& other) {
- if (object.sign != other.sign)
- return !object.sign;
- if (object.size() != other.size())
- return object.size() < other.size();
- for (int i = 0; i < object.size(); i++)
- {
- if (object[i] != other[i])
- return object[i] < other[i];
- }
- return false;
- }
- friend bool operator==(Int& object, Int& other) {
- return object.value_string == other.value_string;
- }
- friend bool operator>=(Int& object, Int& other) {
- return object == other || object > other;
- }
- friend bool operator<=(Int& object, Int& other) {
- return object == other || object < other;
- }
- // ВЫЧИТАНИЕ
- friend Int operator-(Int& object, Int& other) {
- if (other.sign == false) {
- other.sign = true;
- Int sum = object + other;
- other.sign = false;
- return sum;
- }
- if (object.sign == false) {
- object.sign = true;
- Int sum = other + object;
- sum.sign = false;
- object.sign = false;
- return sum;
- }
- bool is_swaped = false;
- if (object < other) {
- swap(object, other);
- is_swaped = true;
- }
- int object_length = object.value_string.size();
- int other_length = other.value_string.size();
- int final_length = object_length;
- if (object_length > other_length) {
- other.align_number_length(object_length);
- final_length = object_length;
- }
- else if (object_length < other_length)
- {
- object.align_number_length(other_length);
- final_length = other_length;
- }
- Int dif;
- dif.align_number_length(final_length);
- for (int i = 0; i < final_length; ++i)
- {
- int digits_dif = Int::dif_char_digits(object[i], other[i]);
- if (digits_dif < 0)
- {
- digits_dif += 10;
- dif[i] = '0' + digits_dif;
- for (int j = i - 1; ; --j)
- {
- if (dif[j] == '0')
- dif[j] = '9';
- else {
- dif[j]--;
- break;
- }
- }
- }
- dif[i] = char(digits_dif + 48);
- }
- object.strip_number();
- other.strip_number();
- dif.strip_number();
- if (is_swaped) {
- swap(object, other);
- return -dif;
- }
- return dif;
- }
- friend Int operator-=(Int& object, Int& other) {
- return object = object - other;;
- }
- // СЛАЖЕНИЕ
- friend Int operator+(Int& object, Int& other) {
- if (object.sign == true && other.sign == false) {
- other.sign = true;
- Int sum = object - other;
- other.sign = false;
- return sum;
- }
- if (object.sign == false && other.sign == true) {
- object.sign = true;
- Int sum = other - object;
- object.sign = false;
- return sum;
- }
- bool sign = object.sign;
- int object_length = object.value_string.size();
- int other_length = other.value_string.size();
- int final_length = object_length;
- if (object_length > other_length) {
- other.align_number_length(object_length);
- final_length = object_length;
- }
- else if (object_length < other_length)
- {
- object.align_number_length(other_length);
- final_length = other_length;
- }
- Int sum;
- sum.align_number_length(final_length);
- sum.sign = sign;
- int additional_sum = 0;
- for (int i = final_length - 1; i >= 0; --i)
- {
- int digits_sum = Int::sum_char_digits(object[i], other[i]) + additional_sum;
- sum[i] = char(digits_sum % 10 + 48);
- additional_sum = digits_sum / 10;
- }
- if (additional_sum > 0) {
- sum.align_number_length(final_length + 1);
- sum[0] = additional_sum + 48;
- }
- object.strip_number();
- other.strip_number();
- sum.strip_number();
- return sum;
- }
- friend Int operator+=(Int& object, Int& other) {
- return object = object + other;;
- }
- // УМНОЖЕНИЕ
- friend Int operator*(Int& object, Int& other) {
- bool sign = object.sign ^ other.sign;
- Int res("0");
- for (Int i(0); i < other; i += 1)
- {
- res;
- }
- return res;
- }
- friend Int operator*=(Int& object, Int& other) {
- return object = object * other;
- }
- // ДЕЛЕНИЯ
- friend Int operator/(Int& object, Int& other) {
- Int res(0);
- if (object < other)
- return res;
- do
- {
- object -= other;
- res += 1;
- } while (object >= 0);
- res -= 1;
- return res;
- }
- friend Int operator/=(Int& object, Int& other) {
- return object = object / other;
- }
- };
- int
- maina()
- {
- Int a(1);
- cout << a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement