#include <iostream>
#include "num.h"

Num::Num(int value, int modulo) {
    this->value = value % modulo;
    this->modulo = modulo;
}

Num& Num::operator=(const Num& other) {
    this->value = other.value;
    this->modulo = other.modulo;
    return *this;
}

Num Num::operator+(const Num& other) {
    int64_t buffer = this->value;
    buffer += other.value;
    return Num(buffer % this->modulo, this->modulo);
}

Num Num::operator-(const Num& other) {
    int64_t buffer = this->value;
    buffer -= other.value;
    return Num(buffer % this->modulo, this->modulo);
}

Num Num::operator*(const Num& other) {
    int64_t buffer = this->value;
    buffer *= other.value;
    return Num(buffer % this->modulo, this->modulo);
}

Num Num::operator+(int num) {
    int64_t buffer = this->value;
    buffer += num;
    return Num(buffer % this->modulo, this->modulo);
}

Num Num::operator-(int num) {
    int64_t buffer = this->value;;
    buffer -= num;
    return Num(buffer % this->modulo, this->modulo);
}

Num Num::operator*(int num) {
    int64_t buffer = this->value;;
    buffer *= num;
    return Num(buffer % this->modulo, this->modulo);
}

Num& Num::operator+=(const Num& other) {
    int64_t buffer = this->value;
    buffer += other.value;
    if (buffer > this->modulo) {
        buffer -= this->modulo;
    }
    this->value = buffer % this->modulo;
    this->modulo = modulo;
    return *this;
}

Num& Num::operator-=(const Num& other) {
    int64_t buffer = this->value;
    buffer -= other.value;
    if (buffer < this->modulo) {
        buffer += this->modulo;
    }
    this->value = buffer % this->modulo;
    this->modulo = modulo;
    return *this;
}

Num& Num::operator*=(const Num& other) {
    int64_t buffer = this->value;
    buffer *= other.value;
    if (buffer < this->modulo) {
        buffer += this->modulo;
    }
    if (buffer > this->modulo) {
        buffer -= this->modulo;
    }
    this->value = buffer % this->modulo;
    this->modulo = modulo;
    return *this;
}

Num& Num::operator+=(int num) {
    int64_t buffer = this->value;
    buffer += num;
    if (buffer > this->modulo) {
        buffer -= this->modulo;
    }
    this->value = buffer % this->modulo;
    return *this;
}

Num& Num::operator-=(int num) {
    int64_t buffer = this->value;
    buffer -= num;
    if (buffer < this->modulo) {
        buffer += this->modulo;
    }
    this->value = buffer % this->modulo;
    return *this;
}

Num& Num::operator*=(int num) {
    int64_t buffer = this->value;
    buffer *= num;
    if (buffer < this->modulo) {
        buffer += this->modulo;
    }
    if (buffer > this->modulo) {
        buffer -= this->modulo;
    }
    this->value = buffer % this->modulo;
    return *this;
}