无限精度运算

 

花费了一个寒假而写出来的神奇代码。

在七月大热天想与大家分享。

觉得我的代码丑的

勿喷

 

 

   1 #include<typeinfo>
   2 #include<vector>
   3 #include<string>
   4 #include<iostream>
   5 #include<cstdio>
   6 #include<cstdlib>
   7 namespace tool{
   8 #ifndef __terminate__
   9 #define __terminate__(X) (printf(X),exit(-1))
  10 #endif
  11 #define EQUAL_TYPENAME(x,y) (typeid(x)==typeid(y))
  12   template<typename __Tp>
  13     inline bool is_integer(const __Tp& expr)
  14     {
  15         return
  16 #ifdef _INT128_DEFINED
  17             EQUAL_TYPENAME(__Tp,__int128)|| 
  18 #endif               
  19             EQUAL_TYPENAME(__Tp,short) || 
  20             EQUAL_TYPENAME(__Tp,int) || 
  21             EQUAL_TYPENAME(__Tp,long) || 
  22             EQUAL_TYPENAME(__Tp,long long) ||
  23 #if __cplusplus>=201103L
  24             EQUAL_TYPENAME(__Tp,char16_t) ||
  25             EQUAL_TYPENAME(__Tp,char32_t) ||
  26 #endif/*cplusplus*/
  27             EQUAL_TYPENAME(__Tp,char) ||
  28             EQUAL_TYPENAME(__Tp,wchar_t);
  29     }
  30   template<typename __Tp>
  31     inline bool is_decimal(const __Tp& expr)
  32     {
  33         return
  34 #ifdef _FLOAT128_DEFINED
  35             EQUAL_TYPENAME(__Tp,__float128)|| 
  36 #endif               
  37             EQUAL_TYPENAME(__Tp,float) || 
  38             EQUAL_TYPENAME(__Tp,double) || 
  39             EQUAL_TYPENAME(__Tp,long double);
  40     }
  41   template<typename __Tp>
  42     inline bool is_boolean(const __Tp& expr)
  43     {
  44         return 
  45             EQUAL_TYPENAME(__Tp,bool);
  46     }
  47   template<typename __Tp>
  48     inline bool is_character(const __Tp& expr)
  49     {
  50         return
  51 #if __cplusplus>=201103L
  52             EQUAL_TYPENAME(__Tp,char16_t) ||
  53             EQUAL_TYPENAME(__Tp,char32_t) ||
  54 #endif/*cplusplus*/
  55             EQUAL_TYPENAME(__Tp,char) ||
  56             EQUAL_TYPENAME(__Tp,wchar_t);
  57     }
  58     struct integer_list;
  59     struct decimal_list;
  60     struct bit_of_dec
  61     {
  62         signed char data;
  63         bit_of_dec(){data=(signed char)(0);}
  64         ~bit_of_dec(){}
  65         
  66         operator char()
  67             {return data;}
  68         bit_of_dec(const bit_of_dec&other)
  69             {data=other.data;return;}
  70         bit_of_dec(const char&other)
  71             {data=other;return;}
  72         bit_of_dec&operator=(const bit_of_dec&other)
  73             {data=other.data;return *this;}
  74         template<typename __Tp>
  75         bit_of_dec(const __Tp&other)
  76             {data=other;return;}
  77         template<typename __Tp>
  78         bit_of_dec&operator=(const __Tp&other)
  79             {data=other;return *this;}
  80 
  81 #define bitop(op)\
  82     friend bit_of_dec operator op \
  83     (const bit_of_dec&t,const bit_of_dec&other) \
  84     { \
  85         return t.data op other.data; \
  86     }
  87 #define bitopself(op) \
  88     bit_of_dec&operator op \
  89     (const bit_of_dec&other) \
  90     { \
  91         data op other.data; \
  92         return *this; \
  93     }
  94 #define bitboolop(op) \
  95     friend bool operator op \
  96     (const bit_of_dec&t,const bit_of_dec&other) \
  97     { \
  98         return t.data op other.data; \
  99     }
 100 #define bitopt(op) \
 101     template<typename __Tp> \
 102     friend bit_of_dec operator op \
 103     (const bit_of_dec&t,const __Tp&other) \
 104     { \
 105         static char c; \
 106         c=other; \
 107         return t.data op c; \
 108     }        
 109 #define bitopselft(op) \
 110     template<typename __Tp> \
 111     bit_of_dec&operator op \
 112     (const __Tp&other) \
 113     { \
 114         data op other; \
 115         return *this; \
 116     }
 117 #define bitboolopt(op) \
 118     template<typename __Tp> \
 119     friend bool operator op \
 120     (const bit_of_dec&t,const __Tp&other) \
 121     { \
 122         return t.data op other; \
 123     }
 124         bitop(+)
 125         bitop(-)
 126         bitop(*)
 127         bitop(/)
 128         bitop(%)
 129         bitopself(+=)
 130         bitopself(-=)
 131         bitopself(*=)
 132         bitopself(/=)
 133         bitopself(%=)
 134         bitboolop(<)
 135         bitboolop(<=)
 136         bitboolop(>=)
 137         bitboolop(>)
 138         bitboolop(!=)
 139         bitboolop(==)
 140         bitopt(+)
 141         bitopt(-)
 142         bitopt(*)
 143         bitopt(/)
 144         bitopt(%)
 145         bitopselft(+=)
 146         bitopselft(-=)
 147         bitopselft(*=)
 148         bitopselft(/=)
 149         bitopselft(%=)
 150         bitboolopt(<)
 151         bitboolopt(<=)
 152         bitboolopt(>=)
 153         bitboolopt(>)
 154         bitboolopt(!=)
 155         bitboolopt(==)
 156 #undef bitop
 157 #undef bitopt
 158 #undef bitopself
 159 #undef bitopselft
 160 #undef bitboolop
 161 #undef bitboolopt
 162     };
 163     struct integer_list
 164     {
 165         friend struct decimal_list;
 166         
 167         typedef bit_of_dec                         bit_type;
 168         typedef std::vector<bit_type>             list;
 169         typedef typename list::reference        reference;
 170         typedef typename list::const_reference    const_reference;
 171         typedef typename list::difference_type    ctrl_type;
 172         typedef integer_list                    self;
 173         
 174         list l;
 175         
 176         integer_list(){}
 177         ~integer_list(){}
 178         
 179         void clear()
 180         {
 181             l.clear();
 182         }
 183         self&operator=(const self&other)
 184         {
 185             l=other.l;
 186             return *this;
 187         }
 188         self&operator=(const decimal_list&other);
 189         reference operator[](const ctrl_type&subsize)
 190         {
 191             return l[subsize];
 192         }
 193         const_reference operator[](const ctrl_type&subsize)const
 194         {
 195             return l[subsize];
 196         }
 197         ctrl_type minnum()const
 198         {
 199             return 0;
 200         }
 201         ctrl_type maxnum()const
 202         {
 203             return l.size()-1;
 204         }
 205         
 206         void resize(const ctrl_type&max_subsize)
 207         {
 208             l.resize(max_subsize+1);
 209         }
 210         reference build(const ctrl_type&subsize)
 211         {
 212             if(subsize>maxnum())l.resize(subsize+1);
 213             return l[subsize];
 214         }
 215     };
 216     struct decimal_list
 217     {
 218         friend struct integer_list;
 219         
 220         typedef bit_of_dec                         bit_type;
 221         typedef std::vector<bit_type>             list;
 222         typedef typename list::reference        reference;
 223         typedef typename list::const_reference    const_reference;
 224         typedef typename list::difference_type    ctrl_type;
 225         typedef decimal_list                    self;
 226         
 227         list front,back;
 228         
 229         decimal_list(){}
 230         ~decimal_list(){}
 231         
 232         void clear()
 233         {
 234             front.clear();
 235             back.clear();
 236         }
 237         self&operator=(const self&other)
 238         {
 239             front=other.front;
 240             back=other.back;
 241             return *this;
 242         }
 243         self&operator=(const integer_list&other);
 244 
 245         reference operator[](const ctrl_type&subsize)
 246         {
 247             return subsize>=0?back[subsize]:front[-subsize-1];
 248         }
 249         const_reference operator[](const ctrl_type&subsize)const
 250         {
 251             return subsize>=0?back[subsize]:front[-subsize-1];
 252         }
 253         ctrl_type minnum()const
 254         {
 255             return -1*front.size();
 256         }
 257         ctrl_type maxnum()const
 258         {
 259             return back.size()-1;
 260         }
 261         
 262         void resize(const ctrl_type&max_or_min_subsize)
 263         {
 264             if(max_or_min_subsize>0)back.resize(max_or_min_subsize+1);
 265             else if(max_or_min_subsize==0)back.clear(),front.clear();
 266             else front.resize(-max_or_min_subsize);
 267         }
 268         reference build(const ctrl_type&subsize)
 269         {
 270             if(subsize>=0)
 271             {
 272                 if(subsize>maxnum())back.resize(subsize+1);
 273                 return back[subsize];
 274             }
 275             else
 276             {
 277                 if(subsize<minnum())front.resize(-subsize);
 278                 return front[-subsize-1];
 279             }
 280         }
 281     };
 282     integer_list& integer_list::operator=(const decimal_list&other)
 283         {l=other.back;return *this;}        
 284     decimal_list& decimal_list::operator=(const integer_list&other) 
 285         {back=other.l;front.clear();return *this;}
 286 #define str_num_base(X) #X
 287 #define str_num(X) str_num_base(X)
 288     class integer;
 289     class decimal;
 290     class integer:private integer_list
 291     {
 292         private:
 293             friend class decimal;
 294             integer_list l;
 295             bool sign;    
 296             void clear()
 297             {
 298                 sign=false;
 299                 l.clear();
 300             }
 301             void del_zero()
 302             {
 303                 while(l.l.back()==bit_of_dec(0)&&l.l.size()>0)l.l.pop_back();
 304                 if(l.l.size()==0)sign=false;
 305             }
 306             integer abs()const
 307             {
 308                 static integer res;
 309                 res=*this;
 310                 res.sign=false;
 311                 return res;
 312             }
 313             integer oppo()const
 314             {
 315                 static integer res;
 316                 res=*this;
 317                 res.sign=!res.sign;
 318                 return res;
 319             }
 320             void move(ctrl_type num)//->+INF
 321             {
 322                 if(num==0)return ;
 323                 ctrl_type smax=l.maxnum(),smin=l.minnum(); 
 324                 if(num>0||num+l.maxnum()>=0)l.build(num+l.maxnum());
 325                 else if(num+l.maxnum()<0)
 326                 {
 327                     l.clear();
 328                     return;
 329                 }
 330                 if(num>0)
 331                     for(ctrl_type i=smax;i>=smin;--i)l.build(num+i)=l.build(i),l.build(i)=0;
 332                 else if(num<0)
 333                     for(ctrl_type i=-num;i<=smax;++i)l.build(i+num)=l.build(i),l.build(i)=0;
 334                 if(num>0)l.resize(num+smax);
 335                 del_zero();
 336             }
 337             void get()
 338             {
 339                 std::string buffer;
 340                 std::cin>>buffer;
 341                 operator=(buffer);
 342             }
 343             void put()const
 344             {
 345                 if(l.maxnum()<0)putchar('0');
 346                 if(sign)putchar('-');
 347                 for(integer_list::ctrl_type i=l.maxnum();i>=0;i--)putchar(l[i]+48);
 348                 return;
 349             }
 350         public:
 351             //let operator
 352             integer&operator=(const integer&other);
 353             integer&operator=(const decimal&other);
 354             integer&operator=(const std::string& str);
 355             integer&operator=(const std::wstring& str);
 356             integer&operator=(const char* str);
 357             integer&operator=(const wchar_t* str);
 358           template<typename __Tp>
 359             integer&operator=(__Tp expr);        
 360             //construct,copy_struct,destruct
 361           template<typename __Tp>
 362             integer(const __Tp&expr){operator=(expr);}
 363             integer(){sign=false;}
 364             ~integer(){} 
 365             
 366             integer_list::ctrl_type maxinum()
 367             {
 368                 return l.maxnum() ;
 369             }
 370             integer_list::ctrl_type mininum()
 371             {
 372                 return l.minnum() ;
 373             }
 374             //relational operator
 375             friend bool operator<(const integer&a,const integer&b);
 376             friend bool operator==(const integer&a,const integer&b);
 377             friend bool operator>(const integer&a,const integer&b);
 378             friend bool operator<=(const integer&a,const integer&b);
 379             friend bool operator>=(const integer&a,const integer&b);
 380             friend bool operator!=(const integer&a,const integer&b);
 381             //arithmetic operator
 382             friend integer operator+(const integer&a,const integer&b);
 383             friend integer operator-(const integer&a,const integer&b);
 384             friend integer operator*(const integer&a,const integer&b);
 385             friend integer operator/(const integer&a,const integer&b);
 386             friend integer operator%(const integer&a,const integer&b);
 387             friend integer operator<<(const integer&t,const ctrl_type&s);
 388             friend integer operator>>(const integer&t,const ctrl_type&s);
 389             
 390             integer& operator+=(const integer&b);
 391             integer& operator-=(const integer&b);
 392             integer& operator*=(const integer&b);
 393             integer& operator/=(const integer&b);
 394             integer& operator%=(const integer&b);
 395             integer& operator<<=(const ctrl_type&b);
 396             integer& operator>>=(const ctrl_type&b);
 397             
 398             integer&operator++();
 399             integer operator++(int);
 400             integer&operator--();
 401             integer operator--(int);
 402             
 403             integer operator+()const;
 404             integer operator-()const; 
 405             //io operator
 406             friend std::istream&operator>>(std::istream&,integer&);
 407             friend std::ostream&operator<<(std::ostream&,const integer&);
 408     };
 409     class decimal:private decimal_list
 410     {
 411         private:
 412             friend class integer;
 413             decimal_list l;
 414             bool sign;
 415             void clear()
 416             {
 417                 sign=false;
 418                 l.clear();
 419             }
 420             void del_zero()
 421             {
 422                 while(l.back.size()>0&&l.back.back()==bit_of_dec(0))l.back.pop_back();
 423                 while(l.front.size()>0&&l.front.back()==bit_of_dec(0))l.front.pop_back();
 424                 if(l.back.empty()&&l.front.empty())sign=false;
 425             }
 426             decimal abs()const
 427             {
 428                 static decimal res;
 429                 res=*this;
 430                 res.sign=false;
 431                 return res;
 432             }
 433             decimal oppo()const
 434             {
 435                 static decimal res;
 436                 res=*this;
 437                 res.sign=!res.sign;
 438                 return res;
 439             }
 440             void move(ctrl_type num)//->+INF
 441             {
 442                 ctrl_type smax=l.maxnum(),smin=l.minnum(); 
 443                 l.build(num+l.maxnum());
 444                 l.build(num+l.minnum());
 445                 if(num>0)
 446                     for(ctrl_type i=smax;i>=smin;--i)l.build(num+i)=l.build(i),l.build(i)=0;
 447                 else if(num<0)
 448                     for(ctrl_type i=smin;i<=smax;++i)l.build(i+num)=l.build(i),l.build(i)=0;
 449                 
 450                 if(num>0)l.resize(num+smax);
 451                 if(num<0)l.resize(num+smin);
 452                 del_zero();
 453             }
 454             void get()
 455             {
 456                 std::string buffer;
 457                 std::cin>>buffer;
 458                 operator=(buffer);
 459                 return;
 460             }
 461             void put()const
 462             {
 463                 if(sign)putchar('-');
 464                 if(l.maxnum()<0)
 465                 {
 466                     putchar('0');
 467                 }
 468                 for(decimal_list::ctrl_type i=l.maxnum();i>=0;i--)putchar(l[i]+48);
 469                 if(l.minnum()<0)putchar('.');
 470                 for(decimal_list::ctrl_type i=-1;i>=l.minnum();i--)putchar(l[i]+48);
 471                 return;
 472             }
 473         public:
 474             //let operator
 475             decimal&operator=(const integer&other);
 476             decimal&operator=(const decimal&other);
 477             decimal&operator=(const std::string& str);
 478             decimal&operator=(const std::wstring& str);
 479             decimal&operator=(const char* str);
 480             decimal&operator=(const wchar_t* str);
 481           template<typename __Tp>
 482             decimal&operator=(__Tp expr);
 483             //construct,copy_struct,destruct            
 484           template<typename __Tp>
 485               decimal(const __Tp&expr){operator=(expr);}
 486             decimal(){sign=false;}
 487             ~decimal(){}
 488             
 489             decimal_list::ctrl_type maxinum()
 490             {
 491                 return l.maxnum() ;
 492             }
 493             decimal_list::ctrl_type mininum()
 494             {
 495                 return l.minnum() ;
 496             }
 497             //relational operator
 498             friend bool operator<(const decimal&a,const decimal&b);
 499             friend bool operator==(const decimal&a,const decimal&b);
 500             friend bool operator>(const decimal&a,const decimal&b);
 501             friend bool operator<=(const decimal&a,const decimal&b);
 502             friend bool operator>=(const decimal&a,const decimal&b);
 503             friend bool operator!=(const decimal&a,const decimal&b);
 504             //arithmetic operator
 505             friend decimal operator+(const decimal&a,const decimal&b);
 506             friend decimal operator-(const decimal&a,const decimal&b);
 507             friend decimal operator*(const decimal&a,const decimal&b);
 508             friend decimal operator/(const decimal&a,const decimal&b);
 509             friend decimal operator<<(const decimal&t,const ctrl_type&s);
 510             friend decimal operator>>(const decimal&t,const ctrl_type&s);
 511             
 512             decimal& operator+=(const decimal&b);
 513             decimal& operator-=(const decimal&b);
 514             decimal& operator*=(const decimal&b);
 515             decimal& operator/=(const decimal&b);
 516             decimal& operator<<=(const ctrl_type&b);
 517             decimal& operator>>=(const ctrl_type&b);
 518             
 519             decimal&operator++();
 520             decimal operator++(int);
 521             decimal&operator--();
 522             decimal operator--(int);
 523             
 524             decimal operator+()const;
 525             decimal operator-()const;
 526             //io operator
 527             friend std::istream&operator>>(std::istream&,decimal&);
 528             friend std::ostream&operator<<(std::ostream&,const decimal&);
 529     };
 530 //function
 531     integer& integer::operator=(const integer&other)
 532         {l=other.l;sign=other.sign;return *this;}
 533     integer& integer::operator=(const decimal&other)
 534         {l=other.l;sign=other.sign;return *this;}
 535     decimal& decimal::operator=(const integer&other)
 536         {l=other.l;sign=other.sign;return *this;}
 537     decimal& decimal::operator=(const decimal&other)
 538         {l=other.l;sign=other.sign;return *this;}
 539     integer& integer::operator=(const std::string& str)
 540     {
 541         clear();
 542         ctrl_type i=0;
 543         std::vector<unsigned char>vec;
 544         while(str[i]==' '&&i!=(ctrl_type)str.size())++i;
 545         if(i==(ctrl_type)str.size())__terminate__("No data!");
 546         if(str.size()>1&&(str[i]=='+'||str[i]=='-'))sign=(str[i]=='+')?0:1,++i;
 547         else if(str.size()==1&&(str[i]=='+'||str[i]=='-'))
 548         {
 549             clear();
 550             __terminate__("Data error!");
 551         }
 552         while(i<(ctrl_type)str.size())
 553         {
 554             while(str[i]==' ')++i;
 555             if(str[i]>='0'&&str[i]<='9')vec.push_back(str[i]-'0');
 556             else if(str[i]=='.')
 557             {
 558                 ++i;
 559                 bool err=true;
 560                 while(i<(ctrl_type)str.size())
 561                 {
 562                     if(str[i]==' '||(str[i]>='0'&&str[i]<='9'))++i,err=(str[i]==' '?err:false);
 563                     else 
 564                     {
 565                         clear();
 566                         __terminate__("Data error!");
 567                     }
 568                 }
 569                 if(err)
 570                 {
 571                     clear();
 572                     __terminate__("Data error!");
 573                 }
 574             }
 575             else
 576             {
 577                 clear();
 578                 __terminate__("Data error!");
 579             }
 580             ++i;
 581         }
 582         for(i=0;i!=(ctrl_type)vec.size();++i)l.build(vec.size()-1-i)=vec[i];
 583         del_zero();
 584         return *this;
 585     }
 586     integer& integer::operator=(const std::wstring& str)
 587     {
 588         clear();
 589         ctrl_type i=0;
 590         std::vector<unsigned char>vec;
 591         while(str[i]==L' '&&i!=(ctrl_type)str.size())++i;
 592         if(i==(ctrl_type)str.size())__terminate__("No data!");
 593         if(str.size()>1&&(str[i]==L'+'||str[i]==L'-'))sign=(str[i]==L'+')?0:1,++i;
 594         else if(str.size()==1&&(str[i]==L'+'||str[i]==L'-'))
 595         {
 596             clear();
 597             __terminate__("Data error!");
 598         }
 599         while(i<(ctrl_type)str.size())
 600         {
 601             while(str[i]==L' ')++i;
 602             if(str[i]>=L'0'&&str[i]<=L'9')vec.push_back(str[i]-L'0');
 603             else if(str[i]==L'.')
 604             {
 605                 ++i;
 606                 bool err=true;
 607                 while(i<(ctrl_type)str.size())
 608                 {
 609                     if(str[i]==L' '||(str[i]>=L'0'&&str[i]<=L'9'))++i,err=(str[i]==L' '?err:false);
 610                     else 
 611                     {
 612                         clear();
 613                         __terminate__("Data error!");
 614                     }
 615                 }
 616                 if(err)
 617                 {
 618                     clear();
 619                     __terminate__("Data error!");
 620                 }
 621             }
 622             else
 623             {
 624                 clear();
 625                 __terminate__("Data error!");
 626             }
 627             ++i;
 628         }
 629         for(i=0;i!=(ctrl_type)vec.size();++i)l.build(vec.size()-1-i)=vec[i];
 630         del_zero();
 631         return *this;
 632     }
 633     decimal& decimal::operator=(const std::string& str)
 634     {
 635         clear();
 636         ctrl_type i=0;
 637         std::vector<unsigned char>vec;
 638         while(str[i]==' '&&i!=(ctrl_type)str.size())++i;
 639         if(i==(ctrl_type)str.size())__terminate__("No data!");
 640         if(str.size()>1&&(str[i]=='+'||str[i]=='-'))sign=(str[i]=='+')?0:1,++i;
 641         else if(str.size()==1&&(str[i]=='+'||str[i]=='-'))
 642         {
 643             clear();
 644             __terminate__("Data error!");
 645         }
 646         while(i!=(ctrl_type)str.size())
 647         {
 648             while(str[i]==' ')++i;
 649             if(str[i]>='0'&&str[i]<='9')vec.push_back(str[i]-'0');
 650             else if(str[i]=='.')
 651             {
 652                 ++i;
 653                 break;
 654             }
 655             else
 656             {
 657                 clear();
 658                 __terminate__("Data error!");
 659             }
 660             ++i;
 661         }
 662         while(i!=(ctrl_type)str.size())
 663         {
 664             while(str[i]==' ')++i;
 665             if(str[i]>='0'&&str[i]<='9')l.build(l.minnum()-1)=str[i]-'0';
 666             else{
 667                 clear();
 668                 __terminate__("Data error!");
 669             }
 670             ++i;
 671         }
 672         for(i=0;i!=(ctrl_type)vec.size();++i)l.build(vec.size()-1-i)=vec[i];
 673         del_zero();
 674         return *this;
 675     }
 676     decimal& decimal::operator=(const std::wstring& str)
 677     {
 678         clear();
 679         ctrl_type i=0;
 680         std::vector<unsigned char>vec;
 681         while(str[i]==L' '&&i!=(ctrl_type)str.size())++i;
 682         if(i==(ctrl_type)str.size())__terminate__("No data!");
 683         if(str.size()>1&&(str[i]==L'+'||str[i]==L'-'))sign=(str[i]==L'+')?0:1,++i;
 684         else if(str.size()==1&&(str[i]==L'+'||str[i]==L'-'))
 685         {
 686             clear();
 687             __terminate__("Data error!");
 688         }
 689         while(i!=(ctrl_type)str.size())
 690         {
 691             while(str[i]==L' ')++i;
 692             if(str[i]>=L'0'&&str[i]<=L'9')vec.push_back(str[i]-L'0');
 693             else if(str[i]==L'.')
 694             {
 695                 ++i;
 696                 break;
 697             }
 698             else
 699             {
 700                 clear();
 701                 __terminate__("Data error!");
 702             }
 703             ++i;
 704         }
 705         while(i!=(ctrl_type)str.size())
 706         {
 707             while(str[i]==' ')++i;
 708             if(str[i]>=L'0'&&str[i]<=L'9')l.build(l.minnum()-1)=str[i]-L'0';
 709             else{
 710                 clear();
 711                 __terminate__("Data error!");
 712             }
 713             ++i;
 714         }
 715         for(i=0;i!=(ctrl_type)vec.size();++i)l.build(vec.size()-1-i)=vec[i];
 716         del_zero();
 717         return *this;
 718     }
 719     integer& integer::operator=(const char* str)
 720         {return operator=(std::string(str));}
 721     integer& integer::operator=(const wchar_t* str)
 722         {return operator=(std::wstring(str));}
 723     decimal& decimal::operator=(const char* str)
 724         {return operator=(std::string(str));}
 725     decimal& decimal::operator=(const wchar_t* str)
 726         {return operator=(std::wstring(str));}
 727   template<typename __Tp>
 728     integer& integer::operator=(__Tp expr)
 729     {
 730         clear();
 731         if(is_character(expr))
 732         {
 733             l.build(0)=expr-48;
 734         }
 735         else if(is_integer(expr))
 736         {
 737             if(expr<0)expr=-expr,sign=1;
 738             unsigned long long temp=expr;
 739             if(temp==0)l.build(0)=0;
 740             while(temp)
 741             {
 742                 l.build(l.maxnum()+1)=temp%10;
 743                 temp/=10;
 744             }
 745         }
 746         else if(is_decimal(expr))
 747         {
 748             if(expr<0)expr=-expr,sign=1;
 749             unsigned long long temp=expr;
 750             if(temp==0)l.build(0)=0;
 751             while(temp)
 752             {
 753                 l.build(l.maxnum()+1)=temp%10;
 754                 temp/=10;
 755             }
 756         }
 757         else if(is_boolean(expr))
 758         {
 759             l.build(0)=expr==false?0:1;
 760         }
 761         del_zero();
 762         return *this;
 763     }
 764   template<typename __Tp>
 765     decimal& decimal::operator=(__Tp expr)
 766     {
 767         clear();
 768         if(is_character(expr))
 769         {
 770             l.build(0)=expr-48;
 771         }
 772         else if(is_integer(expr))
 773         {
 774             if(expr<0)expr=-expr,sign=1;
 775             unsigned long long temp=expr;
 776             if(temp==0)l.build(0)=0;
 777             while(temp)
 778             {
 779                 l.build(l.maxnum()+1)=temp%10;
 780                 temp/=10;
 781             }
 782         }
 783         else if(is_decimal(expr))
 784         {
 785             if(expr<0)expr=-expr,sign=1;
 786             unsigned long long temp=expr;
 787             if(temp==0)l.build(0)=0;
 788             expr-=temp;
 789             while(temp)
 790             {
 791                 l.build(l.maxnum()+1)=temp%10;
 792                 temp/=10;
 793             }
 794             while(expr)
 795             {
 796                 expr*=10;
 797                 temp=expr;
 798                 l.build(l.minnum()-1)=temp;
 799                 expr-=temp;
 800             }
 801         }
 802         else if(is_boolean(expr))
 803         {
 804             l.build(0)=expr==false?0:1;
 805         }
 806         del_zero();
 807         return *this;
 808     }
 809     bool operator<(const integer&a,const integer&b)
 810     {
 811         if(a.sign==0&&b.sign==1)return false;
 812         else if(a.sign==1&&b.sign==0)return true;
 813         else if(a.sign==0&&b.sign==0)
 814         {
 815             if(a.l.maxnum()>b.l.maxnum())return false;
 816             else if(a.l.maxnum()<b.l.maxnum())return true;
 817             for(integer::ctrl_type i=a.l.maxnum();i>=0;--i)
 818             {
 819                 if(a.l[i]<b.l[i])return true;
 820                 else if(a.l[i]>b.l[i])return false;
 821             }
 822             return false;
 823         }
 824         else {
 825             if(a.l.maxnum()>b.l.maxnum())return true;
 826             else if(a.l.maxnum()<b.l.maxnum())return false;
 827             for(integer::ctrl_type i=a.l.maxnum();i>=0;--i)
 828             {
 829                 if(a.l[i]<b.l[i])return false;
 830                 else if(a.l[i]>b.l[i])return true;
 831             }
 832             return false;
 833         }
 834     }
 835     bool operator==(const integer&a,const integer&b)
 836     {
 837         return a.sign==b.sign&&a.l.l==b.l.l;
 838     } 
 839     bool operator>(const integer&a,const integer&b)
 840     {
 841         return b<a;
 842     }
 843     bool operator<=(const integer&a,const integer&b)
 844     {
 845         return !(b<a);
 846     }
 847     bool operator>=(const integer&a,const integer&b)
 848     {
 849         return !(a<b);
 850     }
 851     bool operator!=(const integer&a,const integer&b)
 852     {
 853         return !(a==b);
 854     }
 855     
 856       bool operator<(const decimal&a,const decimal&b)
 857       {
 858         if(a.sign==0&&b.sign==1)return false;
 859         else if(a.sign==1&&b.sign==0)return true;
 860         else if(a.sign==0&&b.sign==0)
 861         {
 862             decimal::ctrl_type mini=
 863                 a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
 864             if(a.l.maxnum()>b.l.maxnum())return false;
 865             else if(a.l.maxnum()<b.l.maxnum())return true;
 866             for(decimal::ctrl_type i=a.l.maxnum();i>=0;--i)
 867             {
 868                 if(a.l[i]<b.l[i])return true;
 869                 else if(a.l[i]>b.l[i])return false;
 870             }
 871             for(decimal::ctrl_type i=-1;i>=mini;--i)
 872             {
 873                 if(a.l[i]<b.l[i])return true;
 874                 else if(a.l[i]>b.l[i])return false;
 875             }
 876             if(a.l.minnum()<mini)return false;
 877             if(b.l.minnum()<mini)return true;
 878             return false;
 879         }
 880         else {
 881             decimal::ctrl_type mini=
 882                 a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
 883             if(a.l.maxnum()>b.l.maxnum())return true;
 884             else if(a.l.maxnum()<b.l.maxnum())return false;
 885             for(decimal::ctrl_type i=a.l.maxnum();i>=0;--i)
 886             {
 887                 if(a.l[i]<b.l[i])return false;
 888                 else if(a.l[i]>b.l[i])return true;
 889             }
 890             for(decimal::ctrl_type i=-1;i>=mini;--i)
 891             {
 892                 if(a.l[i]<b.l[i])return false;
 893                 else if(a.l[i]>b.l[i])return true;
 894             }
 895             if(a.l.minnum()<mini)return true;
 896             if(b.l.minnum()<mini)return false;
 897             return false;
 898         }
 899     }
 900     bool operator==(const decimal&a,const decimal&b)
 901     {
 902         return a.sign==b.sign&&a.l.front==b.l.front&&a.l.back==b.l.back;
 903     }
 904     bool operator>(const decimal&a,const decimal&b)
 905     {
 906         return b<a;
 907     }
 908     bool operator<=(const decimal&a,const decimal&b)
 909     {
 910         return !(b<a);
 911     }
 912     bool operator>=(const decimal&a,const decimal&b)
 913     {
 914         return !(a<b);
 915     }
 916     bool operator!=(const decimal&a,const decimal&b)
 917     {
 918         return !(a==b);
 919     }
 920 #define OTHER_RELAT(SELF,OP)\
 921   template<typename __Tp>\
 922       bool operator OP(const SELF&self,const __Tp&other)\
 923       {\
 924           static SELF temp;\
 925           temp=other;\
 926           return self OP temp;\
 927     }
 928     OTHER_RELAT(integer,<)
 929     OTHER_RELAT(integer,==)
 930     OTHER_RELAT(integer,>)
 931     OTHER_RELAT(integer,<=)
 932     OTHER_RELAT(integer,>=)
 933     OTHER_RELAT(integer,!=)
 934     
 935     OTHER_RELAT(decimal,<)
 936     OTHER_RELAT(decimal,==)
 937     OTHER_RELAT(decimal,>)
 938     OTHER_RELAT(decimal,<=)
 939     OTHER_RELAT(decimal,>=)
 940     OTHER_RELAT(decimal,!=)
 941 #undef OTHER_RELAT
 942     std::ptrdiff_t max_dec_digit=6;
 943     integer operator+(const integer&a,const integer&b)
 944     {
 945         static integer res;
 946         res=0;
 947         if(a.sign==b.sign)
 948         {
 949             res.sign=a.sign;
 950             
 951             integer::ctrl_type stop_num=
 952                 a.l.maxnum()>b.l.maxnum()?b.l.maxnum():a.l.maxnum();
 953             res.l.build((a.l.maxnum()<b.l.maxnum()?b.l.maxnum():a.l.maxnum())+1);
 954             for(integer::ctrl_type i=0;i<=stop_num;++i)
 955             {
 956                 res.l[i]+=a.l[i]+b.l[i];
 957                 res.l[i+1]=res.l[i]/10;
 958                 res.l[i]%=10;
 959             }
 960             if(a.l.maxnum()>stop_num)
 961             {
 962                 res.build(a.l.maxnum()+1);
 963                 for(integer::ctrl_type i=stop_num+1;i<=a.l.maxnum();++i)
 964                 {
 965                     res.l[i]+=a.l[i];
 966                     res.l[i+1]=res.l[i]/10;
 967                     res.l[i]%=10;
 968                 }
 969             }
 970             if(b.l.maxnum()>stop_num)
 971             {
 972                 res.build(b.l.maxnum()+1);
 973                 for(integer::ctrl_type i=stop_num+1;i<=b.l.maxnum();++i)
 974                 {
 975                     res.l[i]+=b.l[i];
 976                     res.l[i+1]=res.l[i]/10;
 977                     res.l[i]%=10;
 978                 }
 979             }
 980             res.del_zero();
 981             return res;
 982         }
 983         else{
 984             if(a.sign)return b-a.abs();
 985             else return a-b.abs();
 986         }
 987     }
 988     integer operator-(const integer&a,const integer&b)
 989     {
 990         static integer res;
 991         res=0;
 992         if(a.sign==b.sign)
 993         {
 994             signed int buf=0; 
 995             if(a.abs()>b.abs())
 996             {
 997                 res.sign=a.sign;
 998                 integer::ctrl_type stop_num=b.l.maxnum();
 999                 res.l.build(stop_num+1);
1000                 for(integer::ctrl_type i=0;i<=stop_num;++i)
1001                 {
1002                     res.l[i]+=a.l[i]-b.l[i]+buf,buf=0;
1003                     if(res.l[i]<0)
1004                     buf-=1,res.l[i]+=10;
1005                 }
1006                 res.build(a.l.maxnum()+1);
1007                 for(integer::ctrl_type i=stop_num+1;i<=a.l.maxnum();++i)
1008                 {
1009                     res.l.build(i)+=a.l[i]+buf,buf=0;
1010                     if(res.l[i]<0)
1011                     buf-=1,res.l[i]+=10;
1012                 }
1013             }
1014             else 
1015             {
1016                 res.sign=!a.sign;
1017                 integer::ctrl_type stop_num=a.l.maxnum();
1018                 res.l.build(stop_num+1);
1019                 for(integer::ctrl_type i=0;i<=stop_num;++i)
1020                 {
1021                     res.l[i]+=b.l[i]-a.l[i]+buf,buf=0;
1022                     if(res.l[i]<0)
1023                     buf-=1,res.l[i]+=10;
1024                 }
1025                 res.build(b.l.maxnum()+1);
1026                 for(integer::ctrl_type i=stop_num+1;i<=b.l.maxnum();++i)
1027                 {
1028                     res.l.build(i)+=b.l[i]+buf,buf=0;
1029                     if(res.l[i]<0)
1030                     buf-=1,res.l[i]+=10;
1031                 }
1032             }
1033             res.del_zero();
1034             return res;
1035         }
1036         else{
1037             if(a.sign)return (a.abs()+b).oppo();
1038             else return (b.abs()+a);
1039         }
1040     } 
1041     integer operator*(const integer&a,const integer&b)
1042     {
1043         static integer res;
1044         res=0;
1045         res.sign=a.sign==b.sign?0:1;
1046         res.l.build(a.maxnum()+b.maxnum()+1);
1047         for(integer::ctrl_type i=0;i<=a.l.maxnum();++i)
1048         {
1049             for(integer::ctrl_type j=0;j<=b.l.maxnum();++j)
1050             {
1051                 res.l.build(i+j)+=a.l[i]*b.l[j];
1052                 res.l.build(i+j+1)+=res.l[i+j]/10;
1053                 res.l[i+j]%=10;
1054             }
1055         }
1056         res.del_zero();
1057         return res;
1058     }
1059     integer operator/(const integer&a,const integer&b)
1060     {
1061         static integer tempa;
1062         static integer tempb;
1063         static integer res;
1064         res=0;
1065         tempa=a;
1066         tempb=b;
1067         tempa.sign=false;
1068         tempb.sign=false;
1069         if(tempb==0)__terminate__("Divided by zero!");
1070         if(tempa==0)return res;
1071         if(tempa<tempb)return res;
1072         res.sign=a.sign==b.sign?0:1;
1073         integer::ctrl_type subsize=a.l.maxnum()-b.l.maxnum();
1074         res.l.build(subsize);
1075         tempb.move(tempa.l.maxnum()-tempb.l.maxnum());
1076         while(!(tempa<b.abs()))
1077         {    
1078             while(tempa>=tempb)tempa=tempa-tempb,res.l.build(subsize)=res.l.build(subsize)+1;
1079             --subsize;
1080             tempb.move(-1);
1081         }
1082         res.del_zero();
1083         return res;
1084     }
1085     integer operator%(const integer&a,const integer&b)
1086     {
1087         static integer res;
1088         res=a-a/b*b;
1089         return res;
1090     }
1091     
1092     decimal operator+(const decimal&a,const decimal&b)
1093     {
1094         static decimal res;
1095         res=0;
1096         if(a.sign==b.sign)
1097         {
1098             res.sign=a.sign;
1099             decimal::ctrl_type stop_num=
1100                 a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
1101             if(a.l.minnum()<b.l.minnum())
1102             {
1103                 int buf=0;
1104                 res.l.build(a.l.minnum());
1105                 for(decimal::ctrl_type i=a.l.minnum();i<b.l.minnum();++i)
1106                 res.l.build(i)=a.l[i];
1107                 for(decimal::ctrl_type i=b.l.minnum();i<0;++i)
1108                 {
1109                     res.l.build(i)=a.l[i]+b.l[i]+buf;
1110                     buf=res.l.build(i)/10;
1111                     res.l.build(i)%=10;
1112                 }
1113                 res.l.build(0)=buf; 
1114             }
1115             else {
1116                 int buf=0;
1117                 res.l.build(b.l.minnum());
1118                 for(decimal::ctrl_type i=b.l.minnum();i<a.l.minnum();++i)
1119                 res.l.build(i)=b.l[i];
1120                 for(decimal::ctrl_type i=a.l.minnum();i<0;++i)
1121                 {
1122                     res.l.build(i)=a.l[i]+b.l[i]+buf;
1123                     buf=res.l.build(i)/10;
1124                     res.l.build(i)%=10;
1125                 }
1126                 res.l.build(0)=buf; 
1127             }
1128             stop_num=a.l.maxnum()>b.l.maxnum()?b.l.maxnum():a.l.maxnum();
1129             res.l.build((a.l.maxnum()<b.l.maxnum()?b.l.maxnum():a.l.maxnum())+1);
1130             for(decimal::ctrl_type i=0;i<=stop_num;++i)
1131             {
1132                 res.l[i]+=a.l[i]+b.l[i];
1133                 res.l[i+1]=res.l[i]/10;
1134                 res.l[i]%=10;
1135             }
1136             if(a.l.maxnum()>stop_num)
1137             {
1138                 res.build(a.l.maxnum()+1);
1139                 for(decimal::ctrl_type i=stop_num+1;i<=a.l.maxnum();++i)
1140                 {
1141                     res.l[i]+=a.l[i];
1142                     res.l[i+1]=res.l[i]/10;
1143                     res.l[i]%=10;
1144                 }
1145             }
1146             if(b.l.maxnum()>stop_num)
1147             {
1148                 res.build(b.l.maxnum()+1);
1149                 for(decimal::ctrl_type i=stop_num+1;i<=b.l.maxnum();++i)
1150                 {
1151                     res.l[i]+=b.l[i];
1152                     res.l[i+1]=res.l[i]/10;
1153                     res.l[i]%=10;
1154                 }
1155             }
1156             res.del_zero();
1157             return res;
1158         }
1159         else{
1160             if(a.sign)return b-a.abs();
1161             else return a-b.abs();
1162         }
1163     }
1164     decimal operator-(const decimal&a,const decimal&b)
1165     {
1166         static decimal res;
1167         res=0;
1168         if(a.sign==b.sign)
1169         {
1170             signed int buf=0; 
1171             if(a.abs()>b.abs())
1172             {
1173                 res.sign=a.sign;
1174                 decimal::ctrl_type stop_num=a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
1175                 res.l.build(a.l.minnum()<b.l.minnum()?a.l.minnum():b.l.minnum()); 
1176                 if(b.l.minnum()<a.l.minnum())
1177                 {
1178                     for(decimal::ctrl_type i=b.l.minnum();i<a.l.minnum();++i)
1179                     {
1180                         res.l[i]=res.l[i]-b.l[i]+buf,buf=0;
1181                         if(res.l[i]<0)
1182                         buf-=1,res.l[i]+=10;
1183                     }
1184                     res.l.build(a.l.minnum())=buf;
1185                     buf=0;
1186                 }
1187                 else
1188                 {
1189                     for(decimal::ctrl_type i=a.l.minnum();i<b.l.minnum();++i)
1190                     {
1191                         res.l[i]=res.l[i]+a.l[i]+buf,buf=0;
1192                         if(res.l[i]<0)
1193                         buf-=1,res.l[i]+=10;
1194                     }
1195                     res.l.build(b.l.minnum())=buf;
1196                     buf=0;
1197                 }
1198                 for(decimal::ctrl_type i=stop_num;i<0;++i)
1199                 {
1200                     res.l[i]+=a.l[i]-b.l[i]+buf,buf=0;
1201                     if(res.l[i]<0)
1202                     buf-=1,res.l[i]+=10;
1203                 }
1204                 res.l.build(0)+=buf;
1205                 buf=0; 
1206                 stop_num=b.l.maxnum();
1207                 res.l.build(stop_num+1);
1208                 for(decimal::ctrl_type i=0;i<=stop_num;++i)
1209                 {
1210                     res.l[i]+=a.l[i]-b.l[i]+buf,buf=0;
1211                     if(res.l[i]<0)
1212                     buf-=1,res.l[i]+=10;
1213                 }
1214                 res.build(a.l.maxnum()+1);
1215                 for(decimal::ctrl_type i=stop_num+1;i<=a.l.maxnum();++i)
1216                 {
1217                     res.l.build(i)+=a.l[i]+buf,buf=0;
1218                     if(res.l[i]<0)
1219                     buf-=1,res.l[i]+=10;
1220                 }
1221             }
1222             else 
1223             {
1224                 res.sign=!a.sign;
1225                 decimal::ctrl_type stop_num=a.l.minnum()>b.l.minnum()?a.l.minnum():b.l.minnum();
1226                 res.l.build(a.l.minnum()<b.l.minnum()?a.l.minnum():b.l.minnum()); 
1227                 if(b.l.minnum()<a.l.minnum())
1228                 {
1229                     for(decimal::ctrl_type i=b.l.minnum();i<a.l.minnum();++i)
1230                     {
1231                         res.l[i]=res.l[i]-a.l[i]+buf,buf=0;
1232                         if(res.l[i]<0)
1233                         buf-=1,res.l[i]+=10;
1234                     }
1235                     res.l.build(a.l.minnum())=buf;
1236                     buf=0;
1237                 }
1238                 else
1239                 {
1240                     for(decimal::ctrl_type i=a.l.minnum();i<b.l.minnum();++i)
1241                     {
1242                         res.l[i]=res.l[i]+b.l[i]+buf,buf=0;
1243                         if(res.l[i]<0)
1244                         buf-=1,res.l[i]+=10;
1245                     }
1246                     res.l.build(b.l.minnum())=buf;
1247                     buf=0;
1248                 }
1249                 for(decimal::ctrl_type i=stop_num;i<0;++i)
1250                 {
1251                     res.l[i]+=b.l[i]-a.l[i]+buf,buf=0;
1252                     if(res.l[i]<0)
1253                     buf-=1,res.l[i]+=10;
1254                 }
1255                 res.l.build(0)+=buf;
1256                 buf=0; 
1257                 stop_num=a.l.maxnum();
1258                 res.l.build(stop_num+1);
1259                 for(decimal::ctrl_type i=0;i<=stop_num;++i)
1260                 {
1261                     res.l[i]+=b.l[i]-a.l[i]+buf,buf=0;
1262                     if(res.l[i]<0)
1263                     buf-=1,res.l[i]+=10;
1264                 }
1265                 res.build(b.l.maxnum()+1);
1266                 for(decimal::ctrl_type i=stop_num+1;i<=b.l.maxnum();++i)
1267                 {
1268                     res.l.build(i)+=b.l[i]+buf,buf=0;
1269                     if(res.l[i]<0)
1270                     buf-=1,res.l[i]+=10;
1271                 }
1272             }
1273             res.del_zero();
1274             return res;
1275         }
1276         else{
1277             if(a.sign)return (a.abs()+b).oppo();
1278             else return (b.abs()+a);
1279         }
1280     }
1281     decimal operator*(const decimal&a,const decimal&b)
1282     {
1283         static decimal res;
1284         res=0;
1285         res.sign=a.sign==b.sign?0:1;
1286         res.l.build(a.maxnum()+b.maxnum()+1);
1287         res.l.build(a.minnum()+b.minnum()-1);
1288         for(decimal::ctrl_type i=a.l.minnum();i<=a.l.maxnum();++i)
1289         {
1290             for(decimal::ctrl_type j=b.l.minnum();j<=b.l.maxnum();++j)
1291             {
1292                 res.l.build(i+j)+=a.l[i]*b.l[j];
1293                 res.l.build(i+j+1)+=res.l.build(i+j)/10;
1294                 res.l.build(i+j)%=10;
1295             }
1296         }
1297         res.del_zero();
1298         return res;
1299     } 
1300     decimal operator/(const decimal&a,const decimal&b)
1301     {
1302         static decimal tempa;
1303         static decimal tempb;
1304         static decimal res;
1305         res=0;
1306         tempa=a;
1307         tempb=b;
1308         tempa.sign=false;
1309         tempb.sign=false;
1310         if(tempb==0)__terminate__("Divided by zero!");
1311         if(tempa==0)return res;
1312         res.sign=a.sign==b.sign?0:1;
1313         decimal::ctrl_type subsize=a.l.maxnum()-b.l.maxnum();
1314         res.l.build(subsize);
1315         res.l.build(-max_dec_digit-1);
1316         tempb.move(tempa.l.maxnum()-tempb.l.maxnum());
1317         while(subsize>=-max_dec_digit)
1318         {    
1319             if(tempa==0)break;
1320             while(tempa>=tempb)tempa=tempa-tempb,
1321             res.l.build(subsize)=res.l.build(subsize)+1;
1322             --subsize;
1323             tempb.move(-1);
1324         }
1325         res.del_zero();
1326         return res;
1327     }
1328 #define OTHER_ARITH(SELF,OP)\
1329   template<typename __Tp>\
1330       SELF operator OP(const SELF&self,const __Tp&other)\
1331       {\
1332           return self OP SELF(other);\
1333     }
1334 #define ARITH_TYPE(SELF,OPE,OPC)\
1335       SELF& SELF::operator OPE(const SELF&other)\
1336       {\
1337           *this=*this OPC other;\
1338           return *this;\
1339     }
1340 #define OTHER_ARITH_TYPE(SELF,OPE,OPC)\
1341   template<typename __Tp>\
1342     SELF& operator OPE(SELF&t,const __Tp&other)\
1343       {\
1344           t=t OPC other;\
1345           return t;\
1346     }
1347 #define ADD_SUB(SELF)\
1348     SELF& SELF::operator ++()\
1349       {\
1350           *this=*this+1;\
1351           return *this;\
1352     }\
1353     SELF SELF::operator ++(int)\
1354       {\
1355           static SELF res;\
1356           res=*this;\
1357           ++*this;\
1358         return res;\
1359     }\
1360     SELF& SELF::operator --()\
1361       {\
1362           *this=*this-1;\
1363           return *this;\
1364     }\
1365     SELF SELF::operator --(int)\
1366       {\
1367           static SELF res;\
1368           res=*this;\
1369           --*this;\
1370         return res;\
1371     }
1372 #define POSI_NEGA(SELF)\
1373     SELF SELF::operator+()const{return *this;}\
1374     SELF SELF::operator-()const{return this->oppo();}
1375 #define ROTATE(SELF)\
1376     SELF operator>>(const SELF&t,const std::ptrdiff_t&s)\
1377     {\
1378         static SELF res;\
1379         res=t; \
1380         res.move(-s);\
1381         return res;\
1382     }\
1383     SELF operator<<(const SELF&t,const std::ptrdiff_t&s)\
1384     {\
1385         static SELF res;\
1386         res=t; \
1387         res.move(s);\
1388         return res;\
1389     }
1390 #define OTHER_ROTATE(SELF)\
1391     SELF& SELF::operator>>=(const std::ptrdiff_t&s)\
1392     {\
1393         move(-s);\
1394         return *this;\
1395     }\
1396     SELF& SELF::operator<<=(const std::ptrdiff_t&s)\
1397     {\
1398         move(s);\
1399         return *this;\
1400     }    
1401     OTHER_ARITH(integer,+)
1402     OTHER_ARITH(integer,-)
1403     OTHER_ARITH(integer,*)
1404     OTHER_ARITH(integer,/)
1405     OTHER_ARITH(integer,%)
1406     
1407     OTHER_ARITH(decimal,+)
1408     OTHER_ARITH(decimal,-)
1409     OTHER_ARITH(decimal,*)
1410     OTHER_ARITH(decimal,/)
1411 
1412     ARITH_TYPE(integer,+=,+)
1413     ARITH_TYPE(integer,-=,-)
1414     ARITH_TYPE(integer,*=,*)
1415     ARITH_TYPE(integer,/=,/)
1416     ARITH_TYPE(integer,%=,%)
1417     
1418     ARITH_TYPE(decimal,+=,+)
1419     ARITH_TYPE(decimal,-=,-)
1420     ARITH_TYPE(decimal,*=,*)
1421     ARITH_TYPE(decimal,/=,/)
1422 
1423     OTHER_ARITH_TYPE(integer,+=,+)
1424     OTHER_ARITH_TYPE(integer,-=,-)
1425     OTHER_ARITH_TYPE(integer,*=,*)
1426     OTHER_ARITH_TYPE(integer,/=,/)
1427     OTHER_ARITH_TYPE(integer,%=,%)
1428     
1429     OTHER_ARITH_TYPE(decimal,+=,+)
1430     OTHER_ARITH_TYPE(decimal,-=,-)
1431     OTHER_ARITH_TYPE(decimal,*=,*)
1432     OTHER_ARITH_TYPE(decimal,/=,/)
1433 
1434     ADD_SUB(integer)
1435     ADD_SUB(decimal)
1436 
1437     POSI_NEGA(integer)
1438     POSI_NEGA(decimal)
1439 
1440     ROTATE(integer)
1441     ROTATE(decimal)
1442 
1443     OTHER_ROTATE(integer)
1444     OTHER_ROTATE(decimal)
1445 #undef OTHER_ARITH
1446 #undef OTHER_ARITH_TYPE
1447 #undef ARITH_TYPE
1448 #undef ADD_SUB
1449 #undef POSI_NEGA
1450 #undef ROTATE
1451 #undef OTHER_ROTATE
1452     std::istream&operator>>(std::istream&s,integer&t)
1453     {
1454         t.get();
1455         return s;
1456     }
1457     std::ostream&operator<<(std::ostream&s,const integer&t)
1458     {
1459         t.put();
1460         return s;
1461     }
1462     std::istream&operator>>(std::istream&s,decimal&t)
1463     {
1464         t.get();
1465         return s;
1466     }
1467     std::ostream&operator<<(std::ostream&s,const decimal&t)
1468     {
1469         t.put();
1470         return s;
1471     }
1472       integer pow(integer x,size_t y)
1473       {
1474           integer res=1;
1475         while(y)
1476         {
1477             if(y&1)res=res*x;
1478             if(y>>=1)x*=x;
1479         }
1480         return res;
1481     }
1482     decimal pow(decimal x,size_t y)
1483       {
1484           decimal res=1;
1485         while(y)
1486         {
1487             if(y&1)res=res*x;
1488             if(y>>=1)x*=x;
1489         }
1490         return res;
1491     }
1492     integer sqrt(const integer&x)
1493     {
1494         static integer left;
1495         static integer right;
1496         static integer middle;
1497         if(x<0)
1498         {
1499             printf("The number is less than zero!\n");
1500             exit(-1);
1501         }
1502         left=0;
1503         right=x;
1504         middle=(left+right)/2;
1505         while((right-left)>1)
1506         {
1507             middle=(left+right)/2;
1508             const decimal&y=middle*middle;
1509             if(y<x)left=middle;
1510             else if(y>x)right=middle;
1511             else return middle;    
1512         }
1513         return middle;
1514     }
1515     decimal sqrt(const decimal&x)
1516     {
1517         static decimal left;
1518         static decimal right;
1519         static decimal middle;
1520         static decimal temp;
1521         if(x<0)
1522         {
1523             printf("The number is less than zero!\n");
1524             exit(-1);
1525         }
1526         left=0;
1527         middle=right=x<1?1:x;
1528         do
1529         {
1530             //std::cout<<"left="<<left<<" "<<"right="<<right<<" "<<"middle="<<middle<<" square="<<middle*middle<<" x="<<x<<std::endl;
1531             if(middle*middle<x)left=middle;
1532             else if(middle*middle>x)right=middle;
1533             else return middle;
1534             temp=(left+right)/2;
1535             if(middle==temp)break;
1536             middle=temp;
1537         }while(middle.mininum()>=-max_dec_digit);
1538         return middle;
1539     }
1540     integer gcd(integer x,integer y);
1541     integer lcm(integer x,integer y);
1542 #undef __terminate__
1543 }

 

解说敬请期待!!

 

转载于:https://www.cnblogs.com/ColinWang-OIer/p/11248660.html

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注