⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 int.cpp

📁 一个高精度库,重载了一些操作符
💻 CPP
字号:
#include <iostream>
#include <string>
using namespace std;

typedef   long   long  _BigInt;
 const   int  BITSIZE  =   7000 ;
 const   int  EACHBITNUM  =   1000000000 ;
 class  BIGINT
  {
 public :
    _BigInt size;
    _BigInt data[BITSIZE];
    BIGINT();
    BIGINT( string );
    BIGINT  operator = ( const  BIGINT & );
     bool   operator < ( const  BIGINT  & );
     bool   operator == ( const  BIGINT  & );
     bool   operator > ( const  BIGINT  & );
     bool   operator <= ( const  BIGINT  & );
     bool   operator >= ( const  BIGINT  & );
     bool   operator != ( const  BIGINT  & );
    friend BIGINT  operator + ( const  BIGINT & ,  const  BIGINT & );
    friend BIGINT  operator - (BIGINT, BIGINT);
    friend BIGINT  operator * ( const  BIGINT  & ,  const  BIGINT  & );
     // friend BIGINT operator/(const BIGINT &, const BIGINT &); 
      void  print();
} ;

BIGINT::BIGINT()
  {
    size  =   0 ;
    memset(data,  0 ,  sizeof (data));
} 
BIGINT::BIGINT( string  s)
  {
     int  i;
     int  len  =  s.length();
     int  exp  =   1 ;
    _BigInt tmp  =   0 ;
    size  =   0 ;
    memset(data,  0 ,  sizeof (data));
     for  (i = len; i >= 1 ; i -- )
      {
        tmp  +=  (s[i - 1 ]  -   '0' )  *  exp;
         if  (exp  ==  EACHBITNUM / 10 )
          {    
            data[ ++ size]  =  tmp;
            tmp  =   0 ;
            exp  =   1 ;
        } 
         else 
            exp  *=   10 ;
    } 
     if  (tmp  !=   0 )
        data[ ++ size]  =  tmp;
} 
BIGINT BIGINT:: operator = ( const  BIGINT  & a)
  {
     int  i;
    memset(data,  0 ,  sizeof (data));
    size  =  a.size;
     for  (i = 1 ; i <= size; i ++ )
        data[i]  =  a.data[i];
     return   * this ;
} 
 void  BIGINT::print()
  {
     int  i;
     for  (i = size; i >= 1 ; i -- )
         if  (i  !=  size)
           printf( " %.9lld " , data[i]);
         else 
           printf( " %lld " , data[i]);
    printf( " \n " );
} 
 bool  BIGINT:: operator < ( const  BIGINT  & a)
  {
      int  i;
      if  (size  <  a.size)  return   true ;
      if  (size  >  a.size)  return   false ;
      for  (i = 1 ; i <= size; i ++ )
          if  (data[i]  >=  a.data[i])  return   false ;
      return   true ;     
} 
 bool  BIGINT:: operator == ( const  BIGINT  & a)
  {
      int  i;
      if  (size  !=  a.size)  return   false ;
      for  (i = 1 ; i <= size; i ++ )
          if  (data[i]  !=  a.data[i])  return   false ;
      return   true ;
} 
 bool  BIGINT:: operator > ( const  BIGINT  & a)
  {
      int  i;
      if  (size  >  a.size)  return   true ;
      if  (size  <  a.size)  return   false ;
      for  (i = 1 ; i <= size; i ++ )
          if  (data[i]  <=  a.data[i])  return   false ;
      return   true ;  
} 
 bool  BIGINT:: operator >= ( const  BIGINT  & a)
  {
      return   ! (( * this )  <  a);
} 
 bool  BIGINT:: operator <= ( const  BIGINT  & a)
  {
      return   ! (( * this )  >  a);
} 
 bool  BIGINT:: operator != ( const  BIGINT  & a)
  {
      return   ! (( * this )  ==  a);
} 
BIGINT  operator + ( const  BIGINT  & a,  const  BIGINT  & b)
  {
    BIGINT rnt;
     int  i;
     bool  t  =   false ;
    _BigInt tmp;
     int  len  =  a.size  >  b.size  ?  a.size : b.size;
     for  (i = 1 ; i <= len; i ++ )
      {
        tmp  =  a.data[i]  +  b.data[i];
         if  (t) tmp  +=   1 ;
        t  =   false ;
         if  (tmp  >=  EACHBITNUM)
          {
            tmp  -=  EACHBITNUM;
            t  =   true ;
        } 
        rnt.data[ ++ rnt.size]  =  tmp;
    } 
     if  (t) rnt.data[ ++ rnt.size]  +=   1 ; 
     return  rnt;
} 
BIGINT  operator - (BIGINT a, BIGINT b)
  {
    BIGINT rnt;
     int  i;
       //  if (a < b) return rnt; 
      for  (i = 1 ; i <= a.size; i ++ )
      {
         if  (a.data[i]  <  b.data[i])
          {
            a.data[i + 1 ] -- ;
            a.data[i]  +=  EACHBITNUM;
        } 
        rnt.data[ ++ rnt.size]  =  a.data[i]  -  b.data[i];
    } 
     return  rnt;
} 
BIGINT  operator * ( const  BIGINT  & a,  const  BIGINT  & b)
  {
     int  i, j;
    BIGINT rnt;
    _BigInt tmp;
     for  (i = 1 ; i <= a.size; i ++ )
         for  (j = 1 ; j <= b.size; j ++ )
          {
            tmp  =  a.data[i]  *  b.data[j];
            rnt.data[i + j - 1 ]  +=  tmp;
            rnt.data[i + j]  +=   rnt.data[i + j - 1 ]  /  EACHBITNUM;
            rnt.data[i + j - 1 ]  %=  EACHBITNUM;
        } 
     for  (i = BITSIZE - 1 ; i >= 1 ; i -- )
         if  (rnt.data[i]  >   0   ||  i  ==   1 )
          {
            rnt.size  =  i;
             break ;
        } 
     return  rnt;
} 
 
 int  main()
  {    
     int  n;
     int  i;
    BIGINT one("1");
    BIGINT y("1");
    BIGINT a=one+y;
    a.print();
      
    system( " pause " );
     return   0 ;
} 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -