Advertisement
Berindei

HugeN v2

Oct 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. template <int MAXSIZE>
  2. class HugeN: public bitset<MAXSIZE>{
  3. private:
  4.     int nr;
  5.     void negate(){
  6.         (*this).flip();
  7.         (*this)+=1;
  8.     }
  9. public:
  10.     HugeN(int nr){
  11.         if (nr<0){
  12.             bitset<MAXSIZE>(-nr);
  13.             (*this).negate();
  14.         }
  15.         else{
  16.             bitset<MAXSIZE>(nr);
  17.         }
  18.     }
  19.     HugeN(HugeN& other){
  20.         (*this)=other;
  21.     }
  22.     HugeN(string S, int base){
  23.         if (base==2)
  24.             bitset<MAXSIZE>(S);
  25.         else{
  26.             int size=0;
  27.             while(S!="0"){
  28.                 if (S[0]=='0') S=S.substr(1);
  29.                 int carry=0;
  30.                 for (auto &x : S){
  31.                     carry=carry*10+x-'0';
  32.                     x='0'+carry/2;
  33.                     carry&=1;
  34.                 }
  35.                 (*this)[size++]=carry;
  36.             }
  37.         }
  38.     }
  39.     HugeN(){}
  40.     void setnr(int nr){
  41.         this->nr=nr;
  42.     }
  43.     string print(){
  44.         string toadd="1";
  45.         string ans="";
  46.         int i;
  47.         goto exec;
  48.  
  49.         forward:{
  50.             int carry=0;
  51.             for(auto& x: toadd){
  52.                 carry=carry+2*(x-'0');
  53.                 x=carry%10+'0';
  54.                 carry/=10;
  55.             }
  56.             if (carry!=0)
  57.                 toadd.push_back(carry+'0');
  58.         }goto back;
  59.  
  60.         exec:
  61.         for (int i=1; i<=MAXSIZE/8*3; i++) ans+='0';
  62.         for (i=0; i<MAXSIZE; i++){
  63.             if ((*this)[i]){
  64.                 int carry=0;
  65.                 for (int i=0; i<ans.length(); i++){
  66.                     carry+=ans[i]-'0';
  67.                     if (i<toadd.length()) carry+=toadd[i]-'0';
  68.                     ans[i]=carry%10+'0';
  69.                     carry/=10;
  70.                 }
  71.             }
  72.             goto forward;
  73.             back:;
  74.         }
  75.         while (ans.back()=='0') ans.pop_back();
  76.         reverse(ans.begin(), ans.end());
  77.         return ans;
  78.     }
  79.     HugeN& operator-(){
  80.         this->negate();
  81.         return *this;
  82.     }
  83.     HugeN operator+(HugeN& other){
  84.         HugeN<MAXSIZE> ans;
  85.         bool carry=0;
  86.         bool rez=0;
  87.         for (int i=0; i<MAXSIZE; i++){
  88.             rez=carry^(*this)[i]^other[i];
  89.             carry=(carry&other[i])|(*this)[i]&(carry|other[i]);
  90.             ans[i]=rez;
  91.         }
  92.         return ans;
  93.     }
  94.     HugeN& operator+=(HugeN& other){
  95.         bool carry=0;
  96.         bool rez=0;
  97.         for (int i=0; i<MAXSIZE; i++){
  98.             rez=carry^(*this)[i]^other[i];
  99.             carry=(carry&other[i])|(*this)[i]&(carry|other[i]);
  100.             (*this)[i]=rez;
  101.         }
  102.         return *this;
  103.     }
  104.     HugeN operator+(int other){
  105.         HugeN ans(other);
  106.         ans+=(*this);
  107.         return ans;
  108.     }
  109.     void operator+=(int other){
  110.         HugeN otherH(other);
  111.         (*this)+=otherH;
  112.     }
  113.     HugeN operator-(HugeN& other){
  114.         other=-other;
  115.         return (*this)+other;
  116.     }
  117.     HugeN operator-(int other){
  118.         HugeN ans(other);
  119.         return ans+(*this);
  120.     }
  121.     void operator-=(HugeN& other){
  122.         (*this)+=-other;
  123.     }
  124.     void operator -=(int other){
  125.         (*this)+=-other;
  126.     }
  127. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement