//------------------------------------------------------------
// Klasa za rad sa velikim brojevima
// Autor: Nenad Bozidarevic
// E-mail: nbozidarevic@gmail.com
// Web sajt: http://www.bozidarevic.com/
//------------------------------------------------------------

#ifndef VELIKIBROJ_H_INCLUDED
#define VELIKIBROJ_H_INCLUDED

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

//------------------------------
// Klasa
//------------------------------

class VelikiBroj
{
private:
  enum Znak
  {
    NEGATIVAN,
    POZITIVAN
  };

  vector<char> _cifre;
  Znak _znak;

  // Brisanje nula sa pocetka broja
  void ukloniVodeceNule();
  // Formiranje broja iz integera
  void izIntegera(int);

  VelikiBroj saberi(const VelikiBroj&) const;
  VelikiBroj oduzmi(const VelikiBroj&) const;
  VelikiBroj pomnozi(const VelikiBroj&) const;
  VelikiBroj podeli(const VelikiBroj&) const;

  int uporediApsolutne(const VelikiBroj&) const;
  int uporedi(const VelikiBroj&) const;

public:
  // Default konstruktor koji broj postavlja na nulu
  VelikiBroj();
  // Konstruktor na osnovu broja
  VelikiBroj(int);
  // Konstruktor na osnovu stringa
  VelikiBroj(const string);

  // Formiranje broja iz stringa
  void izStringa(string);
  // Vraca broj kao string
  string uString() const;

  VelikiBroj operator + (const VelikiBroj& desni) const;
  VelikiBroj operator - (const VelikiBroj& desni) const;
  VelikiBroj operator * (const VelikiBroj& desni) const;
  VelikiBroj operator / (const VelikiBroj& desni) const;
  VelikiBroj& operator += (const VelikiBroj& desni);
  VelikiBroj& operator -= (const VelikiBroj& desni);
  VelikiBroj& operator *= (const VelikiBroj& desni);
  VelikiBroj& operator /= (const VelikiBroj& desni);
  bool operator == (const VelikiBroj& desni) const;
  bool operator != (const VelikiBroj& desni) const;
  bool operator < (const VelikiBroj& desni) const;
  bool operator <= (const VelikiBroj& desni) const;
  bool operator > (const VelikiBroj& desni) const;
  bool operator >= (const VelikiBroj& desni) const;
};

//------------------------------
// Konstruktori
//------------------------------

VelikiBroj::VelikiBroj() {
  this->_znak = POZITIVAN;
}

VelikiBroj::VelikiBroj(int broj) {
  this->izIntegera(broj);
}

VelikiBroj::VelikiBroj(const string broj) {
  this->izStringa(broj);
}

//------------------------------
// Pomocne funkcije
//------------------------------

void VelikiBroj::izIntegera(int broj) {
  char cifra;

  // Odredjujemo znak broja
  if (broj < 0) {
    this->_znak = NEGATIVAN;
  } else {
    this->_znak = POZITIVAN;
  }

  // Brisemo sve cifre
  this->_cifre.clear();

  // Broj ne negiramo zbog INT_MIN, vec uzimamo apsolutne cifre
  while (broj != 0) {
    // Uzimamo krajnju desnu cifru broja
    cifra = (char)abs(broj % 10);
    // Ubacujemo je u nas vektor
    this->_cifre.push_back(cifra);
    broj /= 10;
  }
}

void VelikiBroj::izStringa(string broj) {
  this->_cifre.clear();
  this->_znak = POZITIVAN;

  int pocetakBroja = 0;
  int krajBroja = broj.length() - 1;

  // Ako je string prazan, broj je nula
  if (krajBroja < 0) {
    this->_cifre.clear();
    this->_znak = POZITIVAN;
    return;
  }

  // Ako je pozitivan broj
  if (broj[0] == '+') {
    pocetakBroja++;
  }

  if (broj[0] == '-') {
    this->_znak = NEGATIVAN;
    pocetakBroja++;
  }

  // Ako nema nijedne cifre, broj je nula
  if (krajBroja < pocetakBroja) {
    this->_cifre.clear();
    this->_znak = POZITIVAN;
    return;
  }

  // Ubacujemo cifre pocevsi od krajnje desne
  for (int i = krajBroja; i >= pocetakBroja; i--) {
    // Ako nije cifra, broj je nula
    if (broj[i] > '9' || broj[i] < '0') {
      this->_cifre.clear();
      this->_znak = POZITIVAN;
      return;
    }
    this->_cifre.push_back(broj[i] - '0');
  }

  // Ako je korisnik uneo vodece nule
  this->ukloniVodeceNule();
}

void VelikiBroj::ukloniVodeceNule() {
  int brCifara = this->_cifre.size();

  while (brCifara > 0 && this->_cifre[brCifara - 1] == 0) {
    brCifara--;
  }

  this->_cifre.resize(brCifara);

  // Nula nema znak
  if (this->_cifre.empty()) {
    this->_znak = POZITIVAN;
  }
}

string VelikiBroj::uString() const {
  string s;

  // Dodajemo minus ako je potrebno
  if (this->_znak == NEGATIVAN && !this->_cifre.empty()) {
    s += '-';
  }

  // Nula je prazan vektor
  if (this->_cifre.empty()) {
    s += '0';
    return s;
  }

  int i = this->_cifre.size();

  while (i > 0) {
    i--;
    s += (char)(this->_cifre[i] + '0');
  }

  return s;
}

//------------------------------
// Aritmeticke operacije
//------------------------------

VelikiBroj VelikiBroj::saberi(const VelikiBroj& broj) const {
  VelikiBroj rezultat;

  // Ako su brojevi istog znaka, mozemo raditi kao na papiru
  if (this->_znak == broj._znak) {
    rezultat._znak = this->_znak;

    int aVelicina = this->_cifre.size();
    int bVelicina = broj._cifre.size();
    int velicina = max(aVelicina, bVelicina);

    rezultat._cifre.resize(velicina);

    // X pisem, PRENOS pamtim
    char prenos = 0;
    char zbir;

    for (int i = 0; i < velicina; ++i) {
      // Moramo da pazimo ako je jedan broj duzi od drugog
      zbir = ((i < aVelicina) ? this->_cifre[i] : 0)
        + ((i < bVelicina) ? broj._cifre[i] : 0)
        + prenos;
      rezultat._cifre[i] = zbir % 10;
      prenos = zbir / 10;
    }

    if (prenos > 0) {
      rezultat._cifre.push_back(prenos);
    }

    rezultat._znak = this->_znak;
  } else {
    // A ako nisu, radimo komplementiranjem
    rezultat._znak = POZITIVAN;

    const VelikiBroj *prvi, *drugi;

    // Pozitivan broj mora da bude prvi sabirak
    if (this->_znak == POZITIVAN) {
      prvi = this;
      drugi = &broj;
    } else {
      prvi = &broj;
      drugi = this;
    }

    int aVelicina = prvi->_cifre.size();
    int bVelicina = drugi->_cifre.size();
    int velicina = max(aVelicina, bVelicina);

    rezultat._cifre.resize(velicina);

    char prenos = 0;
    char razlika;

    for (int i = 0; i < velicina; ++i) {
      razlika = ((i < aVelicina) ? prvi->_cifre[i] : 0)
        - ((i < bVelicina) ? drugi->_cifre[i] : 0)
        + prenos;
      razlika += 10;
      rezultat._cifre[i] = razlika % 10;
      prenos = razlika / 10 - 1;
    }

    // Obrcemo broj
    if (prenos < 0) {
      prenos = 0;

      for (int i = 0; i < velicina; ++i) {
        razlika = 0 - rezultat._cifre[i] + prenos;
        razlika += 10;
        rezultat._cifre[i] = razlika % 10;
        prenos = razlika / 10 - 1;
      }

      rezultat._znak = NEGATIVAN;
    }
  }

  rezultat.ukloniVodeceNule();

  return rezultat;
}

VelikiBroj VelikiBroj::oduzmi(const VelikiBroj& broj) const {
  // Konstruktor kopije
  VelikiBroj pomocni = broj;

  // Menjamo znak
  if (pomocni._znak == POZITIVAN) {
    pomocni._znak = NEGATIVAN;
  } else {
    pomocni._znak = POZITIVAN;
  }

  return this->saberi(pomocni);
}

VelikiBroj VelikiBroj::pomnozi(const VelikiBroj& broj) const {
  VelikiBroj rezultat;

  // Odredjujemo znak, + ako su oba cinioca istog znaka
  if (this->_znak == broj._znak) {
    rezultat._znak = POZITIVAN;
  } else {
    rezultat._znak = NEGATIVAN;
  }

  // Odredjujemo koliko cifara moze da ima rezultat
  int aVelicina = this->_cifre.size();
  int bVelicina = broj._cifre.size();

  rezultat._cifre.resize(aVelicina + bVelicina);

  // Racunamo rezultat
  char temp;

  for (int i = 0; i < aVelicina; ++i) {
    for (int j = 0; j < bVelicina; ++j) {
      temp = rezultat._cifre[i + j]
        + this->_cifre[i] * broj._cifre[j];
      rezultat._cifre[i + j] = temp % 10;
      rezultat._cifre[i + j + 1] += temp / 10;
    }
  }

  // Uklanjamo vodece nule
  rezultat.ukloniVodeceNule();

  // Vracamo rezultat
  return rezultat;
}

VelikiBroj VelikiBroj::podeli(const VelikiBroj& broj) const {
  VelikiBroj rezultat;

  // Ukoliko je delilac nula, to nije dozvoljeno
  // pa izbacujemo izuzetak
  if (broj.uporedi(rezultat) == 0) {
    throw "Deljenje nulom";
  }

  // Proveravamo da li je deljenik manji ili jednak
  // deliocu po apsolutnoj vrednosti
  int poredjenje = this->uporediApsolutne(broj);

  // Ukoliko je manji, vracamo nulu
  if (poredjenje == -1) {
    return rezultat;
  }

  // Ukoliko su jednaki, vracamo jedinicu sa odgovarajucim znakom
  if (poredjenje == 0) {
    if (this->_znak == broj._znak) {
      rezultat.izIntegera(1);
    } else {
      rezultat.izIntegera(-1);
    }

    return rezultat;
  }

  // Deljenik je veci, pa moramo da oduzimamo
  VelikiBroj pomocni;
  VelikiBroj jedan(1);

  if (this->_znak == broj._znak) {
    rezultat._znak = POZITIVAN;
  } else {
    rezultat._znak = NEGATIVAN;
  }

  jedan._znak = rezultat._znak;

  int brCifA = this->_cifre.size();
  int brCifB = broj._cifre.size();

  // Ukoliko deljenik ima dve ili vise cifara vise od delioca
  if (brCifA - brCifB > 1) {
    for (int i = brCifA - brCifB; i > 2; i--) {
      rezultat._cifre.push_back(0);
    }

    rezultat._cifre.push_back(1);

    pomocni = rezultat.pomnozi(broj);
  }

  // Vrsimo oduzimanje
  while (true) {
    if (this->_znak == broj._znak) {
      pomocni = pomocni.saberi(broj);
    } else {
      pomocni = pomocni.oduzmi(broj);
    }

    if (pomocni.uporediApsolutne(*this) <= 0) {
      rezultat = rezultat.saberi(jedan);
    } else {
      break;
    }
  }

  return rezultat;
}

//------------------------------
// Poredjenje
//------------------------------

int VelikiBroj::uporediApsolutne(const VelikiBroj& b) const {
  int brCifA = this->_cifre.size();
  int brCifB = b._cifre.size();

  // Ako jedan ima vise cifara, ocigledno je veci
  if (brCifA < brCifB) {
    return -1;
  }

  if (brCifA > brCifB) {
    return 1;
  }

  // Imaju isti broj cifara, pa ih redom poredimo
  for (int i = brCifA - 1; i >= 0; i--) {
    if (this->_cifre[i] != b._cifre[i]) {
      return (this->_cifre[i] < b._cifre[i]) ? -1 : 1;
    }
  }

  // Ukoliko su im sve cifre jednake
  return 0;
}

int VelikiBroj::uporedi(const VelikiBroj& b) const {
  // Ako nisu istog znaka, lako je odrediti
  if (this->_znak != b._znak) {
    return (this->_znak == POZITIVAN) ? 1 : -1;
  }

  // Ako su istog znaka, poredimo apsolutne vrednosti
  // i u zavisnosti od znaka vrsimo negaciju rezultata
  int poredjenje = this->uporediApsolutne(b);

  if (this->_znak == NEGATIVAN) {
    poredjenje = -poredjenje;
  }

  return poredjenje;
}

//------------------------------
// Operatori
//------------------------------

ostream& operator << (ostream& o, const VelikiBroj& x) {
  o << x.uString();
  return o;
}

istream& operator >> (istream& i, VelikiBroj& x) {
  string s;
  i >> s;
  x.izStringa(s);

  return i;
}

VelikiBroj VelikiBroj::operator + (const VelikiBroj& desni) const {
  return this->saberi(desni);
}

VelikiBroj VelikiBroj::operator - (const VelikiBroj& desni) const {
  return this->oduzmi(desni);
}

VelikiBroj VelikiBroj::operator * (const VelikiBroj& desni) const {
  return this->pomnozi(desni);
}

VelikiBroj VelikiBroj::operator / (const VelikiBroj& desni) const {
  return this->podeli(desni);
}

bool VelikiBroj::operator == (const VelikiBroj& desni) const {
  return (this->uporedi(desni) == 0);
}

bool VelikiBroj::operator != (const VelikiBroj& desni) const {
  return (this->uporedi(desni) != 0);
}

bool VelikiBroj::operator < (const VelikiBroj& desni) const {
  return (this->uporedi(desni) < 0);
}

bool VelikiBroj::operator <= (const VelikiBroj& desni) const {
  return (this->uporedi(desni) <= 0);
}

bool VelikiBroj::operator > (const VelikiBroj& desni) const {
  return (this->uporedi(desni) > 0);
}

bool VelikiBroj::operator >= (const VelikiBroj& desni) const {
  return (this->uporedi(desni) >= 0);
}

VelikiBroj& VelikiBroj::operator += (const VelikiBroj& desni) {
  return ((*this) = (*this) + desni);
}

VelikiBroj& VelikiBroj::operator -= (const VelikiBroj& desni) {
  return ((*this) = (*this) - desni);
}

VelikiBroj& VelikiBroj::operator *= (const VelikiBroj& desni) {
  return ((*this) = (*this) * desni);
}

VelikiBroj& VelikiBroj::operator /= (const VelikiBroj& desni) {
  return ((*this) = (*this) / desni);
}

#endif
