Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <array>
- #include <iostream>
- #include <string>
- #include <vector>
- typedef unsigned int uint;
- template<int n>
- class BigUInteger {
- public:
- std::string toString() const {
- std::string result;
- result += char(last_digit_) + '0';
- result += low_.toString();
- return result;
- }
- private:
- uint last_digit_ = 5;
- BigUInteger<n - 1> low_;
- };
- template<>
- class BigUInteger<1> {
- public:
- std::string toString() const {
- std::string result;
- result += char(digit_) + '0';
- return result;
- }
- private:
- uint digit_ = 3;
- };
- template<int n>
- class BigInteger2 {
- public:
- std::string toString() const {
- std::string unsigned_str = unsigned_part_.toString();
- return is_positive_ ? unsigned_str : '-' + unsigned_str;
- }
- private:
- bool is_positive_ = false;
- BigUInteger<n> unsigned_part_;
- };
- int main1() {
- BigInteger2<4> x;
- std::cout << x.toString() << std::endl;
- return 0;
- }
- // Из семинара 4
- class BigInteger {
- public:
- std::string ToString() const;
- BigInteger operator+(const BigInteger& second) const { /*STUB*/ return *this; }
- BigInteger operator-(const BigInteger& second) const;
- BigInteger operator*(const BigInteger& second) const;
- BigInteger operator/(const BigInteger& second) const;
- BigInteger operator%(const BigInteger& second) const;
- BigInteger& operator+=(const BigInteger& second);
- BigInteger& operator-=(const BigInteger& second);
- BigInteger& operator*=(const BigInteger& second);
- BigInteger& operator/=(const BigInteger& second);
- BigInteger& operator%=(const BigInteger& second);
- BigInteger operator++(int);
- BigInteger& operator++();
- bool operator==(const BigInteger& second) const;
- bool operator!=(const BigInteger& second) const { return !(*this == second);}
- private:
- typedef std::vector<uint> Long;
- Long digits; // Вначале младшие разряды
- bool is_positive = true; // true, если положительно или 0
- bool IsNull() const { return digits.empty(); }
- static Long Sum(const Long& l, uint r);
- static Long Sub(const Long& l, uint r); // l > r
- static Long ShiftLeft(const Long& l, int k);
- friend std::istream& operator>>(std::istream& stream, BigInteger& bigint);
- };
- std::istream& operator>>(std::istream& stream, BigInteger& bigint);
- std::ostream& operator<<(std::ostream& stream, const BigInteger& bigint);
- BigInteger& BigInteger::operator+=(const BigInteger& second) {
- *this = *this + second;
- return *this;
- // Или так:
- // return *this = *this + second;
- }
- // Постфиксный
- BigInteger BigInteger::operator++(int) {
- BigInteger copy = *this;
- ++*this;
- return copy;
- }
- BigInteger& BigInteger::operator++() {
- if (is_positive) {
- digits = Sum(digits, 1);
- } else if (digits.size() == 1 && digits[0] == 1) {
- digits.clear();
- is_positive = true;
- } else {
- digits = Sub(digits, 1);
- }
- return *this;
- }
- BigInteger::Long BigInteger::Sum(const Long& l, uint r) {
- Long result = l;
- // ... result.back() += r; + Переполнение
- return result;
- }
- BigInteger::Long BigInteger::Sub(const Long& l, uint r) {
- Long result = l;
- // ... result.back() -= r; + Переполнение
- return result;
- }
- // A = 100000000 * B + R
- // R =
- // Сдвиг влево
- BigInteger::Long BigInteger::ShiftLeft(const Long& l, int k) {
- int full = k / 32;
- int part = k % 32;
- Long result(l.size() + full + 1);
- for (int i = l.size() - 1; i >= 0; --i) {
- // 0000111000110010 << 6
- // 000011 1000110010
- result[i + full] = l[i] << part;
- result[i + full + 1] += l[i] >> (32 - part);
- }
- if (result.back() == 0) result.pop_back();
- return result;
- }
- int main() {
- BigInteger a, b;
- BigInteger c = (a += b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement