• Bignum类的程序提交


    日期:2018.7.19

    星期四

    博客期:002

      这之前赶着做一个单机游戏的修改器忘了时间,不好意思啊!今天我就把Bignum类的源代码发出来,文件的话,我不知道怎样发,待我好好研究研究这个网站哈!因为内容实在是太大了!我就不排版了啊!

    //===================》如下为 main.cpp 文件

      1 #include<iostream>
      2 using namespace std;
      3 #include<Windows.h>
      4 #include"Basic.h"
      5 #include"Bignum.h"
      6 Bignum touch1,touch2;
      7 void win0()
      8 {
      9  system("cls");
     10  Basic::pend();
     11  Basic::make_an_char("Basic Class Practise",'*',1,2,2);
     12  Basic::pend();
     13  Basic::pend(' ');
     14  Basic::pend('=',28);
     15  Basic::pend();
     16  Basic::pend(' ');
     17 }
     18 void win1()
     19 {
     20  win0();
     21  Basic::pend();
     22  cout<<"     Please choice these:"<<endl<<endl;;
     23  cout<<"       S:设置第一个大数"<<endl<<endl;
     24  cout<<"       M:设置第二个大数"<<endl<<endl;
     25  cout<<"         D:选择运算"<<endl<<endl;
     26  cout<<"         I:查看说明"<<endl<<endl;
     27  cout<<"         Q:退出程序"<<endl<<endl;
     28  cout<<"              ";
     29 }
     30 //设置第一个大数
     31 void S()
     32 {
     33  char printin[100];
     34  system("cls");
     35  cout<<endl<<endl;
     36  Basic::make_an_char("set Bignum",'-',0,2,11);
     37  Basic::pend(2);
     38  cout<<" --------------------------------------"<<endl<<endl;
     39  cout<<"        请输入第一个大数的值:"<<endl<<endl<<"                  ";
     40  cin>>printin;
     41  touch1 = Bignum(printin);
     42  cout<<endl<<"           数据存储完成"<<endl;
     43  Basic::pend(2);
     44  cout<<"           ";
     45  system("PAUSE");
     46 }
     47 //设置第二个大数
     48 void M()
     49 {
     50  char printin[100];
     51  system("cls");
     52  cout<<endl<<endl;
     53  Basic::make_an_char("set Bignum",'-',0,2,11);
     54  Basic::pend(2);
     55  cout<<" --------------------------------------"<<endl<<endl;
     56  cout<<"        请输入第二个大数的值:"<<endl<<endl<<"                ";
     57  cin>>printin;
     58  touch2 = Bignum(printin);
     59  cout<<endl<<"               数据存储完成"<<endl;
     60  Basic::pend(2);
     61  cout<<"           ";
     62  system("PAUSE");
     63 }
     64 //选择运算
     65 void D()
     66 {
     67  system("cls");
     68  Basic::make_an_char("选择运算",'-',0,2,9);
     69  char x='0';
     70  Bignum touch3;
     71  cout<<" --------------------------------"<<endl;
     72  cout<<"          请输入运算符:"<<endl;
     73  while(x!='+'&&x!='-'&&x!='*'&&x!='/'&&x!='%')
     74  {
     75   cout<<"                ";
     76   cin>>x;
     77  }
     78  cout<<endl<<" ********************************"<<endl<<endl;
     79  cout<<" "<<touch1<<endl;
     80  cout<<" "<<x<<endl;
     81  cout<<" "<<touch2<<endl;
     82  cout<<" ="<<endl;
     83  switch(x)
     84  {
     85  case '+':touch3=touch1+touch2;break;
     86  case '-':touch3=touch1-touch2;break;
     87  case '*':Bignum::multiplication(touch1,touch2);break;
     88  case '/':touch3=touch1/touch2;break;
     89  case '%':touch3=touch1%touch2;break;
     90  default:break;
     91  }
     92  cout<<" "<<touch3<<endl<<endl<<" ********************************"<<endl;
     93  cout<<endl;
     94  system("PAUSE");
     95 }
     96 //查看说明
     97 void I()
     98 {
     99  system("cls");
    100  Basic::pend(2);
    101  Basic::make_an_char("Instruction",'*',1,2,2);
    102  cout<<endl<<endl;
    103  cout<<"  Maker:    Master"<<endl<<endl;
    104  cout<<"  Name:     Bignum's Program"<<endl<<endl;
    105  cout<<"  Date:     2018.7.19"<<endl<<endl;
    106  cout<<"  Version:  1.023"<<endl<<endl;
    107  cout<<"  ";
    108  system("PAUSE");
    109 }
    110 //退出程序
    111 void Q()
    112 {
    113  system("cls");
    114  Basic::pend(2);
    115  Basic::make_an_char("Welcome to the next time !",'*',1,2,2);
    116  cout<<endl<<endl<<"        ";
    117 }
    118 int main()
    119 {
    120  Bignum tar[20];
    121  char x='c';
    122  while(x!='q'&&x!='Q')
    123  {
    124   win1();
    125   cin>>x;
    126   getchar();
    127   switch(x)
    128   {
    129   case 's':
    130   case 'S':S();break;
    131   case 'm':
    132   case 'M':M();break;
    133   case 'd':
    134   case 'D':D();break;
    135   case 'i':
    136   case 'I':I();break;
    137   case 'q':
    138   case 'Q':Q();break;
    139   default:break;
    140   }
    141  }
    142  return 0;
    143 }
    main.cpp

    //=============================》如下为 Basic.h 文件

      1 #include<iostream>
      2 using namespace std;
      3 #include<string>
      4 class Basic
      5 {
      6 public:
      7  //====================<输入输出>=======================//
      8  /*
      9  /-----------<pend函数>
     10  /-# 
     11  /----(1)默认输出"
    ",可直接输入int类型变量或常量给形参n,得到输出n次的换行(默认输出一次)
     12  /----(2)可指定字符p,输入n次(默认输出一次)
     13  /----(3)可连续输出对间隔输出
     14  */ 
     15  static void pend(const int n=1)
     16  {
     17   for(int i=0;i<n;i++)
     18    cout<<endl;
     19  }
     20  static void pend(const char p,const int n=1)
     21  {
     22   for(int i=0;i<n;i++)
     23    cout<<p;
     24  }
     25  static void pend(const char p,const char q,const int n=1,const int m=1,const bool s=true)
     26  {
     27   if(s)
     28   {
     29    Basic::pend(p,n);
     30    Basic::pend(q,m);
     31   }
     32   else
     33   {
     34    if(m==n)
     35     for(int i=0;i<n;i++)
     36     {
     37      Basic::pend(p);
     38      Basic::pend(q);
     39     }
     40   }
     41  }
     42  static void pend(const char o,const char p,const char q,const int l=1,const int m=1,const int n=1)
     43  {
     44   Basic::pend(o,l);
     45   Basic::pend(p,m);
     46   Basic::pend(q,n);
     47  }
     48  /*
     49  /-----------<printarray函数>
     50  /-# 
     51  /----(1)用于打印一维数组或二维数组
     52  /----(2)可确定初始位置(begin)
     53  /----(3)可设定列间距(lie) 
     54  /----(4)使用时必须设定数组长度 
     55  /----(5)数据中从du开始读,并结束于 n(即 n可以小于 p的实际长度)
     56  /----(6)数据的列从da开始读,并结束于 m(同上)
     57  */ 
     58  template <typename T>
     59  static void printarray(T *p,int n=0,const int lie=1,const int begin=1,int du=0)
     60  {
     61   if(n==0)
     62    return;
     63   Basic::pend(' ',begin);
     64   for(int i=du;i<n;i++)
     65   {
     66    cout<<p[i];
     67    if(i!=n-1)
     68     Basic::pend(' ',lie);
     69   }
     70   Basic::pend();
     71  }
     72  template <typename T>
     73  static void printarray(T **q,int n=0,int m=0,const int hang=0,const int lie=1,const int begin=1,int du=0,int da=0)
     74  {
     75   if(n==0||m==0)
     76    return;
     77   for(int t=da;t<m;t++)
     78   {
     79    Basic::pend(' ',begin);
     80    for(int i=du;i<n;i++)
     81    {
     82     cout<<q[t][i];
     83     if(i!=n-1)
     84      Basic::pend(' ',lie);
     85    }
     86    Basic::pend(hang+1);
     87   }
     88  }
     89  //====================<窗口>=======================//
     90  /*
     91  /-----------<make_an_char函数>
     92  /-# 
     93  /----(1)窗口制定 
     94  /----(2)可设定长宽
     95  /----(3)可确定初始位置  
     96  */ 
     97  static void make_an_char(char *q,const char p='*',const int hang=0,const int lie=1,const int begin=1)
     98  {
     99   //最初行 
    100   Basic::pend(' ',begin);
    101   Basic::pend(p,lie+1);
    102   for(int i=0;*(q+i)!='';i++)
    103    Basic::pend(p);
    104   Basic::pend(p,lie+1);
    105   //最初行 
    106   //-------------------------
    107   //hang的行 
    108   Basic::pend();
    109   Basic::pend(' ',begin);
    110   for(int j=0;j<hang;j++)
    111   {
    112    Basic::pend(p);
    113    Basic::pend(' ',2*lie);
    114    for(int i=0;*(q+i)!='';i++)
    115     Basic::pend(' ');
    116    Basic::pend(p);
    117    Basic::pend();
    118    Basic::pend(' ',begin);
    119   } 
    120   //hang的行
    121   //----------------------
    122   //内容行 
    123   Basic::pend(p);
    124   Basic::pend(' ',lie);
    125   printf("%s",q);
    126   Basic::pend(' ',lie);
    127   Basic::pend(p);
    128   //内容行 
    129   //------------------------
    130   //hang的行 
    131   Basic::pend();
    132   Basic::pend(' ',begin);
    133   for(int j=0;j<hang;j++)
    134   {
    135    Basic::pend(p);
    136    Basic::pend(' ',2*lie);
    137    for(int i=0;*(q+i)!='';i++)
    138     Basic::pend(' ');
    139    Basic::pend(p);
    140    Basic::pend();
    141    Basic::pend(' ',begin);
    142   } 
    143   //hang的行
    144   //---------------------
    145   //最末行
    146   Basic::pend(p,lie+1);
    147   for(int i=0;*(q+i)!='';i++)
    148    Basic::pend(p);
    149   Basic::pend(p,lie+1);
    150   //最末行
    151   Basic::pend();
    152  } 
    153 };
    Basic.h

    //=============================》如下为 Bignum.h 文件

      1 #pragma once
      2 #include<iostream>
      3 #include<string>
      4 using namespace std;
      5 //----<<大数类>>----//
      6 class Bignum
      7 {
      8 protected:
      9  //--------------<内部数据存储>--------------//
     10  //---
     11  //数据存储
     12  char num[100];
     13  //正负存储
     14  bool ifhavef; 
     15 public:
     16  //--------------<附加小函数>--------------//
     17  //---
     18  //数据类型转换[直接使用]
     19  //===int和char表示变换
     20  friend int change(char x);
     21  friend char change(int x);
     22  //===int和char*的转换
     23  friend int change_s(char x[100]); 
     24  friend void change_s(int x,char y[100]);
     25  //===int和Bignum的类型转换
     26  friend int change_ib(Bignum &x);
     27  friend Bignum change_ib(int x);
     28  //===char转为char[100]
     29  friend void change(char x,char y[100]);
     30  //===char和Bignum的类型转换
     31  friend char change_cb(Bignum &x);
     32  friend Bignum change_cb(char x);
     33  //===char*和Bignum的类型转换
     34  friend void change_cbs(Bignum &x,char y[100]);
     35  friend Bignum change_cbs(char x[100]);
     36  //---
     37  //---
     38  //排除-0的情况(将-0转为+0) 
     39  void test();
     40  //排除内部 num 有‘ - ’字符 
     41  void test_s();
     42  //读取第first个到第last个成员组成新的字符串 [直接使用] 
     43  friend void strf(char n[100],char ios[100],int first,int last);
     44  //---
     45  //--------------<静态小函数>--------------//
     46  //---
     47  //位数函数 
     48  //===[长度前移更新 ]
     49  void static runformer(char x[100]);
     50  //===[字符串位数升高 [后面补 0 ](默认升高 1 位) ]
     51  void static run0(char x[100]);
     52  void static run0(char x[100],int ct);
     53  //===[长度后移 x位(默认后移 1 位)] 
     54  void static runlatter(char x[100]);
     55  void static runlatter(char x[100],int t);
     56  //---
     57  //--- 
     58  //相反数
     59  Bignum static anti(Bignum &x);
     60  //交换函数
     61  void static exchange(Bignum &x,Bignum &y);
     62  //返回 10 的 i 次幂 
     63  Bignum static run1(const int i);
     64  //---
     65  //--- 
     66  //长度函数 
     67  //===[测量整形、大数或字符串的长度]
     68  int static litude(int x);
     69  int static litude(char x[100]);
     70  int static litude(Bignum &z);
     71  //===[判断是否 x和 y相同长度 ]
     72  bool static ifsamelong(Bignum &x,Bignum &y);
     73  //---
     74  //--------------<output、intput函数>--------------//
     75  //--- 
     76  //set函数
     77  //===[设置ifhavef]
     78  void setf(const bool ifhavef_s);
     79  //===[设置num]
     80  void setnum(const char num_s[100]);
     81  void setnum(const char x);
     82  //===[设置num和ifhavef] 
     83  void set(const char num_s[100],const bool ifhavef_s);
     84  void set(const bool ifhavef_s,const char num_s[100]);
     85  //---
     86  //--- 
     87  //input函数
     88  void input();
     89  void static input(Bignum &x);
     90  //output函数
     91  void output();
     92  void static output(Bignum &x); 
     93  //---
     94  //--------------<运算函数>--------------//
     95  //--- 
     96  //加法
     97  //===[int置前]
     98  Bignum static addition(int x,int y);
     99  Bignum static addition(int x,char y);
    100  Bignum static addition(int x,char y[100]);
    101  Bignum static addition(int x,Bignum &y);
    102  //===[char置前]
    103  Bignum static addition(char x,int y);  
    104  Bignum static addition(char x,char y);
    105  Bignum static addition(char x,char y[100]);
    106  Bignum static addition(char x,Bignum &y);
    107  //===[char*置前]
    108  Bignum static addition(char x[100],int y); 
    109  Bignum static addition(char x[100],char y);
    110  Bignum static addition(char x[100],char y[100]); 
    111  Bignum static addition(char x[100],Bignum &y); 
    112  //===[Bignum置前]
    113  Bignum static addition(Bignum &x,int y); 
    114  Bignum static addition(Bignum &x,char y); 
    115  Bignum static addition(Bignum &x,char y[100]); 
    116  Bignum static addition(Bignum &x,Bignum &y);
    117  //---
    118  //---                
    119  //减法
    120  //===[int置前]
    121  Bignum static subtraction(int x,int y);
    122  Bignum static subtraction(int x,char y);
    123  Bignum static subtraction(int x,char y[100]);
    124  Bignum static subtraction(int x,Bignum &y);
    125  //===[char置前]
    126  Bignum static subtraction(char x,int y);  
    127  Bignum static subtraction(char x,char y);
    128  Bignum static subtraction(char x,char y[100]);
    129  Bignum static subtraction(char x,Bignum &y);
    130  //===[char*置前]
    131  Bignum static subtraction(char x[100],int y);
    132  Bignum static subtraction(char x[100],char y);
    133  Bignum static subtraction(char x[100],char y[100]);
    134  Bignum static subtraction(char x[100],Bignum &y); 
    135  //===[Bignum置前]
    136  Bignum static subtraction(Bignum &x,int y); 
    137  Bignum static subtraction(Bignum &x,char y); 
    138  Bignum static subtraction(Bignum &x,char y[100]);
    139  Bignum static subtraction(Bignum &x,Bignum &y);
    140  //---
    141  //---
    142  //乘法
    143  //===[int置前]
    144  Bignum static multiplication(int x,int y);//未写
    145  Bignum static multiplication(int x,char y);//未写
    146  Bignum static multiplication(int x,char y[100]);//未写
    147  Bignum static multiplication(int x,Bignum &y);
    148  //===[char置前]
    149  Bignum static multiplication(char x,int y);//未写 
    150  Bignum static multiplication(char x,char y);//未写
    151  Bignum static multiplication(char x,char y[100]);//未写 
    152  Bignum static multiplication(char x,Bignum &y);
    153  //===[char*置前]
    154  Bignum static multiplication(char x[100],int y);//未写
    155  Bignum static multiplication(char x[100],char y);//未写
    156  Bignum static multiplication(char x[100],char y[100]);//未写
    157  Bignum static multiplication(char x[100],Bignum y);//未写
    158  //===[Bignum置前]
    159  Bignum static multiplication(Bignum &x,int y);
    160  Bignum static multiplication(Bignum &x,char y);
    161  Bignum static multiplication(Bignum &x,char y[100]);
    162  Bignum static multiplication(Bignum &x,Bignum &y); 
    163  //---
    164  //--- 
    165  //除法
    166  //===[char*置前]
    167  Bignum static division(char x[100],int y);
    168  Bignum static division(char x[100],char y);
    169  Bignum static division(char x[100],char y[100]);
    170  Bignum static division(char x[100],Bignum &y);
    171  //===[Bignum置前]
    172  Bignum static division(Bignum &x,int y);
    173  Bignum static division(Bignum &x,char y);
    174  Bignum static division(Bignum &x,char y[100]);
    175  Bignum static division(Bignum &x,Bignum &y);
    176  //---
    177  //--- 
    178  //求余数
    179  //===[char*置前]
    180  Bignum static rest(char x[100],int y);
    181  Bignum static rest(char x[100],char y);
    182  Bignum static rest(char x[100],char y[100]);
    183  Bignum static rest(char x[100],Bignum &y);
    184  //===[Bignum置前]
    185  Bignum static rest(Bignum &x,Bignum &y);
    186  Bignum static rest(Bignum &x,int y);
    187  Bignum static rest(Bignum &x,char y);
    188  Bignum static rest(Bignum &x,char y[100]);
    189  //---
    190  //---
    191  //求幂数 
    192  //===[int置前]
    193  Bignum static pxp(int x,int y);
    194  Bignum static pxp(int x,char y);
    195  Bignum static pxp(int x,char y[100]);
    196  Bignum static pxp(int x,Bignum &y);
    197  //===[char置前]
    198  Bignum static pxp(char x,int y);
    199  Bignum static pxp(char x,char y);
    200  Bignum static pxp(char x,char y[100]);
    201  Bignum static pxp(char x,Bignum &y);
    202  //===[char[100]置前]
    203  Bignum static pxp(char x[100],int y);
    204  Bignum static pxp(char x[100],char y);
    205  Bignum static pxp(char x[100],char y[100]);
    206  Bignum static pxp(char x[100],Bignum &y);
    207  //===[Bignum置前]
    208  Bignum static pxp(Bignum &x,int y); 
    209  Bignum static pxp(Bignum &x,char y); 
    210  Bignum static pxp(Bignum &x,char y[100]);
    211  Bignum static pxp(Bignum &x,Bignum &y);
    212  //---
    213  //--------------<运算符重载>--------------//
    214  //--- 
    215  //加法
    216  //===[Bignum置前]
    217  friend Bignum operator +(Bignum &x,int y);
    218  friend Bignum operator +(Bignum &x,char y); 
    219  friend Bignum operator +(Bignum &x,char y[100]); 
    220  friend Bignum operator +(Bignum &x,Bignum &y);
    221  //===[Bignum置后]
    222  friend Bignum operator +(int x,Bignum &y); 
    223  friend Bignum operator +(char x,Bignum &y); 
    224  friend Bignum operator +(char x[100],Bignum &y);
    225  //---
    226  //---
    227  //减法
    228  //===[Bignum置前]
    229  friend Bignum operator -(Bignum &x,int y); 
    230  friend Bignum operator -(Bignum &x,char y);
    231  friend Bignum operator -(Bignum &x,char y[100]);
    232  friend Bignum operator -(Bignum &x,Bignum &y);
    233  //===[Bignum置后]
    234  friend Bignum operator -(int x,Bignum &y); 
    235  friend Bignum operator -(char x,Bignum &y);
    236  friend Bignum operator -(char x[100],Bignum &y);
    237  //---
    238  //---
    239  //除法
    240  //===[Bignum置前]
    241  friend Bignum operator /(Bignum &x,int y);
    242  friend Bignum operator /(Bignum &x,char y);
    243  friend Bignum operator /(Bignum &x,char y[100]);
    244  friend Bignum operator /(Bignum &x,Bignum &y);
    245  //===[Bignum置后]
    246  friend Bignum operator /(char x[100],Bignum &y);
    247  //---
    248  //---
    249  //求余数
    250  //===[Bignum置前] 
    251  friend Bignum operator %(Bignum &x,int y); 
    252  friend Bignum operator %(Bignum &x,char y);
    253  friend Bignum operator %(Bignum &x,char y[100]); 
    254  friend Bignum operator %(Bignum &x,Bignum &y);
    255  //===[Bignum置后] 
    256  friend Bignum operator %(char x[100],Bignum &y); 
    257  //---
    258  //---
    259  //单目运算符
    260  //===[++运算]
    261  //---
    262  //======[前置++](绝对值) 
    263  Bignum operator ++(); 
    264  //--- 
    265  //======[后置++](数值运算) 
    266  Bignum operator ++(int x);
    267  //===[--运算]
    268  //---
    269  //======[前置--](相反数)
    270  Bignum operator --();
    271  //--- 
    272  //======[后置--](数值运算)
    273  Bignum operator --(int x);
    274  //---
    275  //---
    276  //比较函数
    277  //===[小于]
    278  //---
    279  //======[Bignum置前] 
    280  friend bool operator <(Bignum &x,int y); 
    281  friend bool operator <(Bignum &x,char y);
    282  friend bool operator <(Bignum &x,char y[100]); 
    283  friend bool operator <(Bignum &x,Bignum &y);
    284  //======[Bignum置后]
    285  friend bool operator <(int x,Bignum &y); 
    286  friend bool operator <(char x,Bignum &y); 
    287  friend bool operator <(char x[100],Bignum &y); 
    288  //---
    289  //===[大于] 
    290  //---
    291  //======[Bignum置前] 
    292  friend bool operator >(Bignum &x,int y);
    293  friend bool operator >(Bignum &x,char y); 
    294  friend bool operator >(Bignum &x,char y[100]); 
    295  friend bool operator >(Bignum &x,Bignum &y);
    296  //======[Bignum置后]
    297  friend bool operator >(int x,Bignum &y); 
    298  friend bool operator >(char x,Bignum &y); 
    299  friend bool operator >(char x[100],Bignum &y);
    300  //---
    301  //===[等于]
    302  //---
    303  //======[Bignum置前] 
    304  friend bool operator ==(Bignum &x,int y); 
    305  friend bool operator ==(Bignum &x,char y); 
    306  friend bool operator ==(Bignum &x,char y[100]);
    307  friend bool operator ==(Bignum &x,Bignum &y);
    308  //======[Bignum置后]
    309  friend bool operator ==(int x,Bignum &y);
    310  friend bool operator ==(char x,Bignum &y); 
    311  friend bool operator ==(char x[100],Bignum &y);
    312  //---
    313  //===[不等于] 
    314  //---
    315  //======[Bignum置前] 
    316  friend bool operator !=(Bignum &x,int y);
    317  friend bool operator !=(Bignum &x,char y); 
    318  friend bool operator !=(Bignum &x,char y[100]); 
    319  friend bool operator !=(Bignum &x,Bignum &y);
    320  //======[Bignum置后]
    321  friend bool operator !=(int x,Bignum &y); 
    322  friend bool operator !=(char x,Bignum &y);
    323  friend bool operator !=(char x[100],Bignum &y);
    324  //---
    325  //===[小于或等于] 
    326  //---
    327  //======[Bignum置前] 
    328  friend bool operator <=(Bignum &x,int y); 
    329  friend bool operator <=(Bignum &x,char y); 
    330  friend bool operator <=(Bignum &x,char y[100]);
    331  friend bool operator <=(Bignum &x,Bignum &y);
    332  //======[Bignum置后]
    333  friend bool operator <=(int x,Bignum &y); 
    334  friend bool operator <=(char x,Bignum &y);
    335  friend bool operator <=(char x[100],Bignum &y);
    336  //---
    337  //===[大于或等于]
    338  //---
    339  //======[Bignum置前] 
    340  friend bool operator >=(Bignum &x,int y); 
    341  friend bool operator >=(Bignum &x,char y); 
    342  friend bool operator >=(Bignum &x,char y[100]); 
    343  friend bool operator >=(Bignum &x,Bignum &y);
    344  //======[Bignum置后]
    345  friend bool operator >=(int x,Bignum &y); 
    346  friend bool operator >=(char x,Bignum &y); 
    347  friend bool operator >=(char x[100],Bignum &y);
    348  //---
    349  //---
    350  //输入输出流 
    351  //===[输入]
    352  friend istream & operator >>(istream &in,Bignum &x);
    353  //===[输出]
    354  friend ostream & operator <<(ostream &out,Bignum &x);
    355  //---
    356  //--------------<构造和析构函数>--------------//
    357  //--- 
    358  //构造函数
    359  //===无参构造函数 
    360  Bignum();
    361  //===转换构造函数 
    362  Bignum(int num);
    363  Bignum(char p);
    364  Bignum(char p[100]);
    365  //===拷贝构造函数 
    366  Bignum(Bignum &x);
    367  //---
    368  //--- 
    369  //析构函数 
    370  ~Bignum();
    371 };
    Bignum.h

    //========================================》如下为 Bignum.cpp 文件

       1 #include "Bignum.h"
       2 //--------------<附加小函数>--------------//
       3 //---
       4 //数据类型转换[直接使用]
       5 //===int和char表示变换
       6 int change(char x)
       7 {
       8  return (int)(x-'0');
       9 }
      10 char change(int x)
      11 {
      12  return (char)(x+'0');
      13 }
      14 //===int和char*的转换
      15 int change_s(char x[100])
      16 {
      17  int z=0,k=1;
      18  bool havef=x[0]=='-';
      19  char y[100];
      20  strcpy(y,x);
      21  if(havef)
      22  {
      23   y[0]='0';
      24   Bignum::runformer(y);
      25  }
      26  int l=Bignum::litude(y);
      27  for(int i=l-1;i>=0;i--)
      28  {
      29   z=z+k*change(y[i]);
      30   k=k*10;
      31  }
      32  if(havef)
      33   z=-z;
      34  return z;
      35 }
      36 void change_s(int x,char y[100])
      37 {
      38  bool havef=x<0;
      39  if(havef)
      40   x=-x;
      41  char p[100]="0";
      42  int l=Bignum::litude(x);
      43  int u=1;
      44  for(int i=1;i<l;i++)
      45   u=u*10;
      46  for(int i=0;i<l;i++)
      47  {
      48   p[i]=change((x/u)%10);
      49   u=u/10;
      50  }
      51  p[l]='';
      52  Bignum::runformer(p);
      53  if(havef)
      54  {
      55   Bignum::runlatter(p);
      56   p[0]='-';
      57  }
      58  strcpy(y,p);
      59 } 
      60 //===int和Bignum的类型转换
      61 int change_ib(Bignum &x)
      62 {
      63  return x.ifhavef?-change_s(x.num):change_s(x.num);
      64 }
      65 Bignum change_ib(int x)
      66 {
      67  Bignum z;
      68  bool havef=x<0;
      69  if(havef)
      70   x=-x;
      71  char p[100];
      72  change_s(x,p);
      73  z.set(p,havef);
      74  return z;
      75 }
      76 //===char*转为char[100]
      77 void change(char x,char y[100])
      78 {
      79  y[0]=x;
      80  y[1]='';
      81 }
      82 //===char和Bignum的类型转换
      83 char change_cb(Bignum &x)
      84 {
      85  x.test();
      86  return x.num[0];
      87 }
      88 Bignum change_cb(char x)
      89 {
      90  Bignum y(x);
      91  return y;
      92 }
      93 //===char*和Bignum的类型转换
      94 void change_cbs(Bignum &x,char y[100])
      95 {
      96  Bignum::runformer(x.num);
      97  x.test();
      98  strcpy(y,x.num);
      99  if(x.ifhavef)
     100  {
     101   Bignum::runlatter(y);
     102   y[0]='-';
     103  }
     104 }
     105 Bignum change_cbs(char x[100])
     106 {
     107  Bignum z(x);
     108  return z;
     109 }
     110 //---
     111 //---
     112 //排除-0的情况(将-0转为+0) 
     113 void Bignum::test()
     114 {
     115  if(ifhavef&&strcmp(num,"0")==0)
     116   ifhavef=false;
     117 }
     118 //排除内部 num 有‘ - ’字符 
     119 void Bignum::test_s()
     120 {
     121  if(num[0]=='-')
     122  {
     123   num[0]='0';
     124   Bignum::runformer(num);
     125   ifhavef=!ifhavef;
     126  }
     127 }
     128 //读取第first个到第last个成员组成新的字符串 [直接使用] 
     129 void strf(char pr[100],char save[100],int first,int last)
     130 {
     131  int i,j;
     132  for(i=0,j=first-1;j!=last&&pr[j]!='';j++,i++)
     133   save[i]=pr[j];
     134  save[i]='';
     135 }
     136 //---
     137 //--------------<静态小函数>--------------//
     138 //---
     139 //位数函数 
     140 //===[长度前移更新 ]
     141 void Bignum::runformer(char x[100])
     142 {
     143  int m=0;//执行前移的次数 
     144  for(int i=0;i<100;i++)//测定m的值 
     145  {
     146   if(x[i]!='0')
     147    break;
     148   m++;
     149  }
     150  if(m==0)
     151   return ;
     152  for(int i=0;x[i+m-1]!='';i++)//前移 
     153   x[i]=x[i+m];
     154  if(x[0]=='')
     155  {
     156   x[1]=x[0];
     157   x[0]='0';
     158  }
     159 } 
     160 //===[字符串位数升高 [后面补 0 ](默认升高 1 位) ]
     161 void Bignum::run0(char x[100])
     162 {
     163  int zz=Bignum::litude(x);
     164  x[zz]='0';
     165  x[zz+1]='';
     166 }
     167 void Bignum::run0(char x[100],int ct)
     168 {
     169  for(int i=1;i<=ct;i++)
     170   Bignum::run0(x);
     171 }
     172 //===[长度后移 x位(默认后移 1 位)] 
     173 void Bignum::runlatter(char x[100])
     174 {
     175  char s[100];
     176  int i;
     177  for(i=0;i<100&&x[i]!='';i++)
     178   s[i+1]=x[i];
     179  s[i+1]='';
     180  s[0]='0';
     181  strcpy(x,s);
     182 }
     183 void Bignum::runlatter(char x[100],int t)
     184 {
     185  char s[100];
     186  s[1]=x[0];
     187  int i;
     188  for(i=0;i<100&&x[i]!='';i++)//后移一次 
     189  {
     190   s[i+1]=x[i];
     191  }
     192  s[i+1]='';
     193  s[0]='0';
     194  strcpy(x,s);
     195  if(t!=1)//递归 
     196   Bignum::runlatter(x,t-1);
     197 }
     198 //---
     199 //--- 
     200 //相反数
     201 Bignum Bignum::anti(Bignum &x)
     202 {
     203  Bignum z(x.num);
     204  z.setf(!x.ifhavef);
     205  z.test();
     206  return z;
     207 }
     208 //交换函数
     209 void Bignum::exchange(Bignum &x,Bignum &y)
     210 {
     211  Bignum z(x);
     212  x.set(y.num,y.ifhavef);
     213  y.set(z.num,z.ifhavef);
     214  x.test();
     215  y.test();
     216 }
     217 //返回 10 的 i 次幂 
     218 Bignum Bignum::run1(int i)
     219 {
     220  Bignum z;
     221  z.set("1",false);
     222  Bignum::run0(z.num,i);
     223  z.test();//无用
     224  return z;
     225 }
     226 //---
     227 //--- 
     228 //长度函数 
     229 //===[测量整形、大数或字符串的长度]
     230 //------准确的长度(不含'') 
     231 //------int最大长度为10
     232 int Bignum::litude(int x)
     233 {
     234  int num=10;
     235  int z=1000000000;
     236  if(x==0)
     237   return 1;
     238  while(num-1)
     239  {
     240   if((x/z)!=0)
     241    break;
     242   num--;
     243   z=z/10;
     244  }
     245  return num;
     246 }
     247 int Bignum::litude(char x[100])
     248 {
     249  int m;
     250  Bignum::runformer(x);
     251  for(int i=0;i<100;i++)
     252   if(x[i]=='')
     253   {
     254    m=i;
     255    break;
     256   }
     257  return m;
     258 }
     259 int Bignum::litude(Bignum &x)
     260 {
     261  return Bignum::litude(x.num);
     262 }
     263 //===[判断是否 x和 y相同长度 ]
     264 bool Bignum::ifsamelong(Bignum &x,Bignum &y)
     265 {
     266  return(Bignum::litude(x)==Bignum::litude(y));
     267 }
     268 //---
     269 //--------------<output、intput函数>--------------//
     270 //--- 
     271 //set函数
     272 //===[设置ifhavef]
     273 void Bignum::setf(const bool ifhavef_s)
     274 {
     275  ifhavef=ifhavef_s;
     276 }
     277 //===[设置num]
     278 void Bignum::setnum(const char x)
     279 {
     280  char arrays[100];
     281  arrays[1]='';
     282  arrays[0]=x;
     283  setnum(arrays);
     284 }
     285 void Bignum::setnum(const char num_s[100])
     286 {
     287  strcpy(num,num_s);
     288 }
     289 //===[设置num和ifhavef] 
     290 void Bignum::set(const char num_s[100],const bool ifhavef_s)
     291 {
     292  setf(ifhavef_s);
     293  setnum(num_s);
     294  test();
     295 }
     296 void Bignum::set(const bool ifhavef_s,const char num_s[100])
     297 {
     298  set(num_s,ifhavef_s);
     299 }
     300 //---
     301 //--- 
     302 //input函数 
     303 void Bignum::input()
     304 {
     305  char x[100];
     306  cin>>x;
     307  if(x[0]=='-')
     308  {
     309   x[0]='0';
     310   Bignum::runformer(x);
     311  }
     312  strcpy(num,x);
     313 } 
     314 void Bignum::input(Bignum &x)
     315 {
     316  x.input();
     317 }
     318 //output函数
     319 void Bignum::output()
     320 {
     321  cout<<*this;
     322 }
     323 void Bignum::output(Bignum &x)
     324 {
     325  x.output();
     326 }
     327 //---
     328 //--------------<运算函数>--------------//
     329 //--- 
     330 //加法
     331 //===[int置前] 
     332 Bignum Bignum::addition(int x,int y)
     333 {
     334  return change_ib(x+y);
     335 }
     336 Bignum Bignum::addition(int x,char y)
     337 {
     338  return change_ib(x+change(y));
     339 }
     340 Bignum Bignum::addition(int x,char y[100])
     341 {
     342  Bignum z1(y);
     343  Bignum z2(x);
     344  return z1+z2;
     345 }
     346 Bignum Bignum::addition(int x,Bignum &y)
     347 {
     348  return x+y;
     349 }
     350 //===[char置前]
     351 Bignum Bignum::addition(char x,int y)
     352 {
     353  return Bignum::addition(y,x);
     354 }
     355 Bignum Bignum::addition(char x,char y)
     356 {
     357  Bignum z(change(x)+change(y));
     358  return z;
     359 }
     360 Bignum Bignum::addition(char x,char y[100])
     361 {
     362  Bignum z(y);
     363  return z+x;
     364 }
     365 Bignum Bignum::addition(char x,Bignum &y)
     366 {
     367  Bignum z(x);
     368  return x+y;
     369 }
     370 //===[char*置前]
     371 Bignum Bignum::addition(char x[100],int y)
     372 {
     373  return Bignum::addition(y,x);
     374 }
     375 Bignum Bignum::addition(char x[100],char y)
     376 {
     377  return Bignum::addition(y,x);
     378 }
     379 Bignum Bignum::addition(char x[100],char y[100])
     380 {
     381  Bignum t(x);
     382  return t+y;
     383 }
     384 Bignum Bignum::addition(char x[100],Bignum &y)
     385 {
     386  return x+y;
     387 }
     388 //===[Bignum置前]
     389 Bignum Bignum::addition(Bignum &x,int y)
     390 {
     391  return Bignum::addition(y,x);
     392 }
     393 Bignum Bignum::addition(Bignum &x,char y)
     394 {
     395  return Bignum::addition(y,x);
     396 }
     397 Bignum Bignum::addition(Bignum &x,char y[100])
     398 {
     399  return Bignum::addition(y,x);
     400 }
     401 Bignum Bignum::addition(Bignum &x,Bignum &y)
     402 {
     403  Bignum z=x+y;
     404  return z;
     405 }
     406 //---
     407 //---                
     408 //减法
     409 //===[int置前]
     410 Bignum Bignum::subtraction(int x,int y)
     411 {
     412  Bignum z(x-y);
     413  return z;
     414 } 
     415 Bignum Bignum::subtraction(int x,char y)
     416 {
     417  Bignum t1(x),t2(y);
     418  return t1-t2;
     419 } 
     420 Bignum Bignum::subtraction(int x,char y[100])
     421 {
     422  Bignum t1(x),t2(y);
     423  return t1-t2;
     424 } 
     425 Bignum Bignum::subtraction(int x,Bignum &y)
     426 {
     427  return x-y;
     428 } 
     429 //===[char置前]
     430 Bignum Bignum::subtraction(char x,int y)
     431 {
     432  Bignum t1(x),t2(y);
     433  return t1-t2;
     434 } 
     435 Bignum Bignum::subtraction(char x,char y)
     436 {
     437  Bignum t1(x),t2(y);
     438  return t1-t2;
     439 } 
     440 Bignum Bignum::subtraction(char x,char y[100])
     441 {
     442  Bignum t1(x),t2(y);
     443  return t1-t2;
     444 } 
     445 Bignum Bignum::subtraction(char x,Bignum &y)
     446 {
     447  return x-y;
     448 } 
     449 //===[char*置前]
     450 Bignum Bignum::subtraction(char x[100],int y)
     451 {
     452  Bignum t1(x),t2(y);
     453  return t1-t2;
     454 } 
     455 Bignum Bignum::subtraction(char x[100],char y)
     456 {
     457  Bignum t1(x),t2(y);
     458  return t1-t2;
     459 } 
     460 Bignum Bignum::subtraction(char x[100],char y[100])
     461 {
     462  Bignum t1(x),t2(y);
     463  return t1-t2;
     464 } 
     465 Bignum Bignum::subtraction(char x[100],Bignum &y)
     466 {
     467  return x-y;
     468 } 
     469 //===[Bignum置前]
     470 Bignum Bignum::subtraction(Bignum &x,int y)
     471 {
     472  return x-y;
     473 }
     474 Bignum Bignum::subtraction(Bignum &x,char y)
     475 {
     476  return x-y;
     477 }
     478 Bignum Bignum::subtraction(Bignum &x,char y[100])
     479 {
     480  return x-y;
     481 }
     482 Bignum Bignum::subtraction(Bignum &x,Bignum &y)
     483 {
     484  return x-y;
     485 }
     486 //---
     487 //---
     488 //乘法
     489 //===[int置前]
     490 Bignum Bignum::multiplication(int x,int y)
     491 {
     492  return change_ib(x*y); 
     493 }
     494 Bignum Bignum::multiplication(int x,char y)
     495 {
     496  return change_ib(x*change(y));
     497 }
     498 Bignum Bignum::multiplication(int x,char y[100])
     499 {
     500  return Bignum::subtraction(x,change_cbs(y));
     501 }
     502 Bignum Bignum::multiplication(int x,Bignum &y)
     503 {
     504  Bignum z;
     505  z=Bignum::multiplication(y,x);
     506  return z;
     507 }
     508 //===[char置前]
     509 Bignum Bignum::multiplication(char x,int y)
     510 { 
     511  return Bignum::multiplication(y,x);
     512 }
     513 Bignum Bignum::multiplication(char x,char y)
     514 {
     515   return change_ib(change(x)*change(y));
     516 }
     517 Bignum Bignum::multiplication(char x,char y[100])
     518 { 
     519  return Bignum::multiplication(x,change_cbs(y));
     520 }
     521 Bignum Bignum::multiplication(char x,Bignum &y)
     522 {
     523  Bignum z;
     524  z=Bignum::multiplication(y,change(x));
     525  return z;
     526 }
     527 //===[char*置前]
     528 Bignum Bignum::multiplication(char x[100],int y)
     529 { 
     530  return Bignum::multiplication(y,x);
     531 }
     532 Bignum Bignum::multiplication(char x[100],char y)
     533 { 
     534  return Bignum::multiplication(y,x);
     535 }
     536 Bignum Bignum::multiplication(char x[100],char y[100])
     537 { 
     538  Bignum t1(x),t2(y);
     539  return Bignum::multiplication(t1,t2);
     540 }
     541 //===[Bignum置前]
     542 Bignum Bignum::multiplication(Bignum &x,int y)
     543 {
     544  Bignum t(x);
     545  if(y>=0&&y<=9)
     546  {
     547   char arrays[100];
     548   Bignum::runlatter(t.num);
     549   arrays[Bignum::litude(t)]='';
     550   for(int i=Bignum::litude(t)-1,k=0;i>=0;i--)
     551   {
     552    int answer=k+change(t.num[i])*y;
     553    k=answer/10;
     554    answer=answer%10;
     555    arrays[i]=change(answer);
     556   }
     557   Bignum::runformer(arrays);
     558   Bignum z(arrays);
     559   return z;
     560  }
     561  else
     562   return t;
     563 }
     564 Bignum Bignum::multiplication(Bignum &x,char y)
     565 {
     566  Bignum z;
     567  z=Bignum::multiplication(x,change(y));
     568  return z;
     569 }
     570 Bignum Bignum::multiplication(Bignum &x,char y[100])
     571 {
     572  Bignum z(y);
     573  return Bignum::multiplication(x,z); 
     574 }
     575 Bignum Bignum::multiplication(Bignum &x,Bignum &y)
     576 {
     577  Bignum z("0");
     578  if(!x.ifhavef&&!y.ifhavef)
     579  {
     580   Bignum t1(x),t2(y);
     581   int l=Bignum::litude(t2);
     582   for(int t=l-1;t>=0;t--)
     583   {
     584    Bignum t3=Bignum::multiplication(t1,change(t2.num[t]));
     585    Bignum::run0(t1.num);
     586    z=z+t3;
     587   }
     588   Bignum::runformer(z.num);
     589   z.setf(false);
     590  }
     591  else if(x.ifhavef&&y.ifhavef)
     592   z=Bignum::multiplication(Bignum::anti(x),Bignum::anti(y));
     593  else if(x.ifhavef&&!y.ifhavef)
     594  {
     595   z=Bignum::multiplication(Bignum::anti(x),y);
     596   z.setf(true);
     597  }
     598  else
     599   z=Bignum::multiplication(y,x);
     600  z.test();
     601  return z;
     602 }
     603 //---
     604 //---
     605 //除法
     606 //===[char[100]置前]
     607 Bignum Bignum::division(char x[100],int y)
     608 {
     609  Bignum z(x);
     610  return z/y;
     611 }
     612 Bignum Bignum::division(char x[100],char y)
     613 {
     614  Bignum z(x);
     615  return z/y;
     616 }
     617 Bignum Bignum::division(char x[100],char y[100])
     618 {
     619  Bignum z(x);
     620  return z/y;
     621 }
     622 Bignum Bignum::division(char x[100],Bignum &y)
     623 {
     624  return x/y;
     625 }
     626 //===[Bignum置前] 
     627 Bignum Bignum::division(Bignum &x,int y)
     628 {
     629  Bignum z("0");
     630  if(!(y>0&&y<=9))
     631   return z;
     632  char arrays[100];
     633  Bignum t(x);
     634  int l=Bignum::litude(x);
     635  arrays[l]='';
     636  for(int i=0,k=0;i<l;i++)
     637  {
     638   int answer=k*10+change(x.num[i]);
     639   k=answer%y;
     640   answer=answer/y;
     641   arrays[i]=change(answer);
     642  }
     643  Bignum::runformer(arrays);
     644  z.set(arrays,false);
     645  z.test();
     646  return z;
     647 }
     648 Bignum Bignum::division(Bignum &x,char y)
     649 {
     650  Bignum z(Bignum::division(x,change(y)));
     651  return z;
     652 }
     653 Bignum Bignum::division(Bignum &x,char y[100])
     654 {
     655  return x/y;
     656 }
     657 Bignum Bignum::division(Bignum &x,Bignum &y)
     658 {
     659  Bignum z;
     660  if(!x.ifhavef&&!y.ifhavef)
     661  {
     662   z.setf(false);
     663   Bignum t1(x),t2(y);
     664   Bignum::runformer(t1.num);
     665   Bignum::runformer(t2.num);
     666   int l1,l2;
     667   l1=Bignum::litude(t1);
     668   l2=Bignum::litude(t2);
     669   if(l1<l2||strcmp(t1.num,t2.num)<0)
     670    z.setnum('0');
     671   else if(strcmp(t1.num,t2.num)==0)
     672    z.setnum('1');
     673   else if(l1==l2)
     674   {
     675    char f;
     676    f='2';
     677    while(Bignum::multiplication(t2,f)<t1)
     678     f++;
     679    f--;
     680    z.setnum(f);
     681   }
     682   else
     683   {
     684    z.setnum('0');
     685    for(int i=l1-l2;i>=0;i--)
     686    {
     687     Bignum t3(t2);
     688     Bignum::run0(t3.num,i);
     689     int k=0;
     690     while(t1>t3)
     691     {
     692      t1=t1-t3;
     693      k++;
     694     }
     695     z=z+Bignum::multiplication(k,Bignum::run1(i));
     696    }
     697   }
     698  }
     699  else if(x.ifhavef&&y.ifhavef)
     700   z=Bignum::division(Bignum::anti(x),Bignum::anti(y));
     701  else if(x.ifhavef&&!y.ifhavef)
     702   z=Bignum::anti(Bignum::division(Bignum::anti(x),y));
     703  else
     704   z=Bignum::anti(Bignum::division(x,Bignum::anti(y)));
     705  z.test();
     706  return z;
     707 }
     708 //---
     709 //---
     710 //求余数
     711 //===[char*置前]
     712 Bignum Bignum::rest(char x[100],int y)
     713 {
     714  Bignum z(x);
     715  return z%y;
     716 }
     717 Bignum Bignum::rest(char x[100],char y)
     718 {
     719  Bignum z(x);
     720  return z%y;
     721 }
     722 Bignum Bignum::rest(char x[100],char y[100])
     723 {
     724  Bignum z(x);
     725  return z%y;
     726 }
     727 Bignum Bignum::rest(char x[100],Bignum &y)
     728 {
     729  Bignum z(x);
     730  return z%y;
     731 }
     732 //===[Bignum置前]
     733 Bignum Bignum::rest(Bignum &x,int y)
     734 {
     735  return x%y;
     736 }
     737 Bignum Bignum::rest(Bignum &x,char y)
     738 {
     739  return x%y;
     740 }
     741 Bignum Bignum::rest(Bignum &x,char y[100])
     742 {
     743  return x%y;
     744 }
     745 Bignum Bignum::rest(Bignum &x,Bignum &y)
     746 {
     747  return x%y;
     748 }
     749 //---
     750 //---
     751 //求幂数 
     752 //===[int置前]
     753 Bignum Bignum::pxp(int x,int y)
     754 {
     755  return Bignum::pxp(change_ib(x),y);
     756 }
     757 Bignum Bignum::pxp(int x,char y)
     758 {
     759  return Bignum::pxp(change_ib(x),y);
     760 }
     761 Bignum Bignum::pxp(int x,char y[100])
     762 {
     763  return Bignum::pxp(change_ib(x),y);
     764 }
     765 Bignum Bignum::pxp(int x,Bignum &y)
     766 {
     767  return Bignum::pxp(change_ib(x),y);
     768 }
     769 //===[char置前]
     770 Bignum Bignum::pxp(char x,int y)
     771 {
     772  return Bignum::pxp(change_cb(x),y);
     773 }
     774 Bignum Bignum::pxp(char x,char y)
     775 {
     776  return Bignum::pxp(change_cb(x),y);
     777 }
     778 Bignum Bignum::pxp(char x,char y[100])
     779 {
     780  return Bignum::pxp(change_cb(x),y);
     781 }
     782 Bignum Bignum::pxp(char x,Bignum &y)
     783 {
     784  return Bignum::pxp(change_cb(x),y);
     785 }
     786 //===[char[100]置前]
     787 Bignum Bignum::pxp(char x[100],int y)
     788 {
     789  return Bignum::pxp(change_cbs(x),y);
     790 }
     791 Bignum Bignum::pxp(char x[100],char y)
     792 {
     793  return Bignum::pxp(change_cbs(x),y);
     794 }
     795 Bignum Bignum::pxp(char x[100],char y[100])
     796 {
     797  return Bignum::pxp(change_cbs(x),y);
     798 }
     799 Bignum Bignum::pxp(char x[100],Bignum &y)
     800 {
     801  return Bignum::pxp(change_cbs(x),y);
     802 }
     803 //===[Bignum置前]
     804 Bignum Bignum::pxp(Bignum &x,int y)
     805 {
     806  return Bignum::pxp(x,change_ib(y));
     807 }
     808 Bignum Bignum::pxp(Bignum &x,char y)
     809 {
     810  return Bignum::pxp(x,change_cb(y));
     811 }
     812 Bignum Bignum::pxp(Bignum &x,char y[100])
     813 {
     814  return Bignum::pxp(x,change_cbs(y));
     815 }
     816 Bignum Bignum::pxp(Bignum &x,Bignum &y)
     817 {
     818  x.test();x.test_s();
     819  y.test();y.test_s();
     820  Bignum::runformer(x.num);
     821  Bignum::runformer(y.num);
     822  Bignum t1(x),t2(y);
     823  if(!t1.ifhavef)
     824  {
     825   if(t1=='0')
     826    return change_ib(0);
     827   else if(t1=='1')
     828    return change_ib(1);
     829   if(t2=='0')
     830    return change_ib(1);
     831   else if(t2>0)
     832   {
     833    Bignum z(1);
     834    while(t2>0)
     835    {
     836     z=Bignum::multiplication(t1,z);
     837     t2--;
     838    }
     839    return z;
     840   }
     841   else
     842    return change_ib(0);
     843  }
     844  else if(t1.ifhavef)
     845  {
     846   if(t2%2==0)
     847    return Bignum::pxp(Bignum::anti(t1),t2);
     848   else
     849    return Bignum::anti(Bignum::pxp(Bignum::anti(t1),t2)); 
     850  }
     851  return t1;
     852 }
     853 //---
     854 //--------------<运算符重载>--------------//
     855 //--- 
     856 //加法
     857 //===[Bignum置前]
     858 Bignum operator +(Bignum &x,int y)
     859 {
     860  return x+change_ib(y);
     861 }
     862 Bignum operator +(Bignum &x,char y)
     863 {
     864  return x+change_cb(y);
     865 }
     866 Bignum operator +(Bignum &x,char y[100])
     867 {
     868  return x+change_cbs(y);
     869 }
     870 Bignum operator +(Bignum &x,Bignum &y)
     871 {
     872  Bignum zz;
     873  if(!x.ifhavef&&!y.ifhavef)
     874  {
     875   Bignum t1(x),t2(y);
     876   int l1,l2;
     877   l1=Bignum::litude(x);
     878   l2=Bignum::litude(y);
     879   //以上-----初步计算 
     880   if(l1==l2)
     881   {
     882    Bignum::runlatter(t1.num);
     883    Bignum::runlatter(t2.num);
     884    l1++;
     885    l2++;
     886   }
     887   else if(l1<l2)
     888   {
     889    Bignum::runlatter(t2.num);
     890    Bignum::runlatter(t1.num,l2-l1+1);
     891    l2++;
     892    l1=l2;
     893   }
     894   else if(l1>l2)
     895   {
     896    Bignum::runlatter(t1.num);
     897    Bignum::runlatter(t2.num,l1-l2+1);
     898    l1++;
     899    l2=l1;
     900   }
     901   char arrays[100];
     902   arrays[l1]='';
     903   //以上为规划
     904   for(int j=l1-1,k=0;j>=0;j--)
     905   {
     906    int answer=change(t1.num[j])+change(t2.num[j])+k;
     907    k=answer/10;
     908    arrays[j]=change(answer%10);
     909   } 
     910   Bignum::runformer(arrays);
     911   zz.setnum(arrays);
     912  }
     913  else if(x.ifhavef&&y.ifhavef)
     914   zz=Bignum::anti(Bignum::anti(x)+Bignum::anti(y));
     915  else if(x.ifhavef&&!y.ifhavef)
     916   zz=y-Bignum::anti(x);
     917  else
     918   Bignum zz=x-Bignum::anti(y);
     919  zz.test();
     920  return zz;
     921 }
     922 //===[Bignum置后]
     923 Bignum operator +(int x,Bignum &y)
     924 {
     925  return y+x;
     926 }
     927 Bignum operator +(char x,Bignum &y)
     928 {
     929  return y+x;
     930 }
     931 Bignum operator +(char x[100],Bignum &y)
     932 {
     933  return y+x;
     934 }
     935 //--- 
     936 //--- 
     937 //减法
     938 //===[Bignum置前]
     939 Bignum operator -(Bignum &x,int y)
     940 {
     941  return x-change_ib(y);
     942 }
     943 Bignum operator -(Bignum &x,char y)
     944 {
     945  return x-change_cb(y);
     946 }
     947 Bignum operator -(Bignum &x,char y[100])
     948 {
     949  return x-change_cbs(y);
     950 }
     951 Bignum operator -(Bignum &x,Bignum &y)
     952 {
     953  Bignum result;
     954  if(!x.ifhavef&&!y.ifhavef)
     955  {
     956   char arrays[100];
     957   bool resulthavef=false;
     958   Bignum t1(x),t2(y);
     959   int l1=Bignum::litude(x),l2=Bignum::litude(y);
     960   if(l1==l2)
     961   {
     962    Bignum::runlatter(t1.num);
     963    Bignum::runlatter(t2.num);
     964    l1++;
     965    l2++;
     966   }
     967   else if(l1<l2)
     968   {
     969    Bignum::runlatter(t2.num);
     970    Bignum::runlatter(t1.num,l2-l1+1);
     971    l2++;
     972    l1=l2;
     973   }
     974   else if(l1>l2)
     975   {
     976    Bignum::runlatter(t1.num);
     977    Bignum::runlatter(t2.num,l1-l2+1);
     978    l1++;
     979    l2=l1;
     980   }
     981   //统一排位
     982   Bignum t3(t1),t4(t2);
     983   if(strcmp(t1.num,t2.num)<0)
     984   {
     985    Bignum::exchange(t3,t4);
     986    result.ifhavef=true;
     987   }
     988  
     989   if(strcmp(t1.num,t2.num)==0)
     990   {
     991    arrays[0]='0';
     992    arrays[1]='';
     993   }
     994   else
     995   {
     996    arrays[l2]='';
     997    for(int j=l2-1,k=0;j>=0;j--)
     998    {
     999     int answer=change(t3.num[j])-change(t4.num[j])-k;
    1000     if(answer<0)
    1001     {
    1002      k=1;
    1003      answer=answer+10;
    1004     }
    1005     else
    1006      k=0;
    1007     arrays[j]=change(answer);
    1008    } 
    1009   }
    1010   Bignum::runformer(arrays);
    1011   result.setnum(arrays);
    1012   result.setf(resulthavef);
    1013  }
    1014  else if(x.ifhavef&&y.ifhavef)
    1015   result=Bignum::anti(y)-Bignum::anti(x);
    1016  else if(x.ifhavef&&!y.ifhavef)
    1017   result=Bignum::anti(Bignum::anti(x)+y);
    1018  else
    1019   result=Bignum::anti(y)+x;
    1020  result.test();
    1021  return result;
    1022 }
    1023 //===[Bignum置后]
    1024 Bignum operator -(int x,Bignum &y)
    1025 {
    1026  return y-x;
    1027 } 
    1028 Bignum operator -(char x,Bignum &y)
    1029 {
    1030  return y-x;
    1031 } 
    1032 Bignum operator -(char x[100],Bignum &y)
    1033 {
    1034  return y-x;
    1035 } 
    1036 //---
    1037 //---
    1038 //除法
    1039 //===[Bignum置前]
    1040 Bignum operator /(Bignum &x,int y)
    1041 {
    1042  return x/change_ib(y);
    1043 }
    1044 Bignum operator /(Bignum &x,char y)
    1045 {
    1046  return x/change_cb(y);
    1047 }
    1048 Bignum operator /(Bignum &x,char y[100])
    1049 {
    1050  return x/change_cbs(y);
    1051 }
    1052 Bignum operator /(Bignum &x,Bignum &y)
    1053 {
    1054  Bignum z=Bignum::division(x,y);
    1055  return z;
    1056 }
    1057 //===[Bignum置后]
    1058 Bignum operator /(char x[100],Bignum &y)
    1059 {
    1060  return change_cbs(x)/y;
    1061 }
    1062 //---
    1063 //---
    1064 //求余数
    1065 //===[Bignum置前]
    1066 Bignum operator %(Bignum &x,int y)
    1067 {
    1068  return x%change_ib(y);
    1069 }
    1070 Bignum operator %(Bignum &x,char y)
    1071 {
    1072  return x%change_cb(y);
    1073 }
    1074 Bignum operator %(Bignum &x,char y[100])
    1075 {
    1076  return x%change_cbs(y);
    1077 }
    1078 Bignum operator %(Bignum &x,Bignum &y)
    1079 {
    1080  Bignum answer;
    1081  answer.setf(false);
    1082  Bignum t1(x),t2(y);
    1083  if(t2.ifhavef||strcmp(t2.num,"0")==0)//除数不是正数
    1084  {
    1085   answer.setnum("0");
    1086   return answer;
    1087  }
    1088  int l1,l2;
    1089  Bignum::runformer(t1.num);
    1090  Bignum::runformer(t2.num);
    1091  l1=Bignum::litude(t1);
    1092  l2=Bignum::litude(t2);
    1093  if(!t1.ifhavef)
    1094  {
    1095   if(t1==t2)
    1096    answer.setnum("0");
    1097   else if(t1>t2)
    1098   {
    1099    if(l1==l2||l1==l2+1)
    1100    {
    1101     for(int i=1;i>=0;i--)
    1102     {
    1103      Bignum t3(t2);     
    1104      Bignum::run0(t3.num,i);
    1105      while(t1>t3)
    1106       t1=t1-t3;
    1107     }
    1108     answer.setnum(t1.num);
    1109    }
    1110    else
    1111    {
    1112     int i;
    1113     for(i=l1-l2;i>=0;i--)
    1114     {
    1115      Bignum t3(t2);
    1116      Bignum::run0(t3.num,i);
    1117      while(t1>t3)
    1118       t1=t1-t3;
    1119     }
    1120     answer.setnum(t1.num);
    1121    }
    1122   }
    1123   else
    1124    answer=t2;
    1125  }
    1126  else if(t1.ifhavef)
    1127  {
    1128   if(Bignum::anti(t1)==t2)
    1129    answer.setnum("0");
    1130   else if(Bignum::anti(t1)>t2)
    1131    answer=t2-(Bignum::anti(t1)%t2);
    1132   else
    1133    answer=t1+t2;
    1134  }
    1135  answer.test();
    1136  return answer;
    1137 }
    1138 //===[Bignum置后]
    1139 Bignum operator %(char x[100],Bignum &y)
    1140 {
    1141  return change_cbs(x)%y;
    1142 }
    1143 //---
    1144 //---
    1145 //单目运算符
    1146 //===[++运算]
    1147 //---
    1148 //======[前置++](绝对值) 
    1149 Bignum Bignum::operator ++()
    1150 {
    1151  test();
    1152  Bignum::runformer(this->num); 
    1153  ifhavef=false;
    1154  return *this;
    1155 }
    1156 //--- 
    1157 //======[后置++](数值运算) 
    1158 Bignum Bignum::operator ++(int x)
    1159 {
    1160  test();
    1161  Bignum::runformer(this->num);
    1162  *this=*this+1;
    1163  return *this;
    1164  
    1165 }
    1166 //===[--运算]
    1167 //---
    1168 //======[前置--](相反数)
    1169 Bignum Bignum::operator --()
    1170 {
    1171  test();
    1172  Bignum::runformer(this->num);
    1173  ifhavef=!ifhavef;
    1174  return *this;
    1175 }
    1176 //--- 
    1177 //======[后置--](数值运算)
    1178 Bignum Bignum::operator --(int x)
    1179 {
    1180  test();
    1181  Bignum::runformer(this->num);
    1182  *this=*this-1; 
    1183  return *this;
    1184 }
    1185 //---
    1186 //---
    1187 //比较函数
    1188 //===[小于]
    1189 //---
    1190 //======[Bignum置前]
    1191 bool operator <(Bignum &x,int y)
    1192 {
    1193  return change_ib(y)<x;
    1194 }
    1195 bool operator <(Bignum &x,char y)
    1196 {
    1197  return change_cb(y)<x;
    1198 }
    1199 bool operator <(Bignum &x,char y[100])
    1200 {
    1201  return change_cbs(y)<x;
    1202 } 
    1203 bool operator <(Bignum &x,Bignum &y)
    1204 {
    1205  x.test();
    1206  Bignum::runformer(x.num);
    1207  y.test();
    1208  Bignum::runformer(y.num);
    1209  int l1,l2;
    1210  l1=Bignum::litude(x);
    1211  l2=Bignum::litude(y);
    1212  bool it=false;
    1213  if(x.ifhavef&&y.ifhavef)
    1214  {
    1215   if(l1==l2)
    1216    it=strcmp(x.num,y.num)>0;
    1217   else if(l1>l2)
    1218    it=!it;
    1219  }
    1220  else if(!x.ifhavef&&!y.ifhavef)
    1221  {
    1222   if(l1==l2)
    1223    it=strcmp(x.num,y.num)<0;
    1224   else if(l2>l1)
    1225    it=!it;
    1226  }
    1227  else if(!x.ifhavef&&y.ifhavef)
    1228   it=!it;
    1229  return it;
    1230 }
    1231 //======[Bignum置后]
    1232 bool operator <(int x,Bignum &y)
    1233 {
    1234  return change_ib(x)<y;
    1235 }
    1236 bool operator <(char x,Bignum &y)
    1237 {
    1238  return change_cb(x)<y;
    1239 }
    1240 bool operator <(char x[100],Bignum &y)
    1241 {
    1242  return change_cbs(x)<y;
    1243 }
    1244 //---
    1245 //===[大于]
    1246 //---
    1247 //======[Bignum置前] 
    1248 bool operator >(Bignum &x,int y)
    1249 {
    1250  return change_ib(y)>x;
    1251 }
    1252 bool operator >(Bignum &x,char y)
    1253 {
    1254  return change_cb(y)>x;
    1255 }
    1256 bool operator >(Bignum &x,char y[100])
    1257 {
    1258  return change_cbs(y)>x;
    1259 } 
    1260 bool operator >(Bignum &x,Bignum &y)
    1261 {
    1262  return y<x;
    1263 }
    1264 //======[Bignum置后]
    1265 bool operator >(int x,Bignum &y)
    1266 {
    1267  return change_ib(x)>y;
    1268 }
    1269 bool operator >(char x,Bignum &y)
    1270 {
    1271  return change_cb(x)>y;
    1272 }
    1273 bool operator >(char x[100],Bignum &y)
    1274 {
    1275  return change_cbs(x)>y;
    1276 }
    1277 //---
    1278 //===[等于]
    1279 //---
    1280 //======[Bignum置前] 
    1281 bool operator ==(Bignum &x,int y)
    1282 {
    1283  return change_ib(y)==x;
    1284 }
    1285 bool operator ==(Bignum &x,char y)
    1286 {
    1287  return change_cb(y)==x;
    1288 }
    1289 bool operator ==(Bignum &x,char y[100])
    1290 {
    1291  return change_cbs(y)==x;
    1292 }
    1293 bool operator ==(Bignum &x,Bignum &y)
    1294 {
    1295  return strcmp(x.num,y.num)!=0&&Bignum::ifsamelong(x,y);
    1296 }
    1297 //======[Bignum置后]
    1298 bool operator ==(int x,Bignum &y)
    1299 {
    1300  return change_ib(x)==y;
    1301 }
    1302 bool operator ==(char x,Bignum &y)
    1303 {
    1304  return change_cb(x)==y;
    1305 }
    1306 bool operator ==(char x[100],Bignum &y)
    1307 {
    1308  return change_cbs(x)==y;
    1309 }
    1310 //---
    1311 //===[不等于]
    1312 //---
    1313 //======[Bignum置前] 
    1314 bool operator !=(Bignum &x,int y)
    1315 {
    1316  return change_ib(y)!=x;
    1317 }
    1318 bool operator !=(Bignum &x,char y)
    1319 {
    1320  return change_cb(y)!=x;
    1321 }
    1322 bool operator !=(Bignum &x,char y[100])
    1323 {
    1324  return change_cbs(y)!=x;
    1325 }
    1326 bool operator !=(Bignum &x,Bignum &y)
    1327 {
    1328  return !(x==y);
    1329 }
    1330 //======[Bignum置后]
    1331 bool operator !=(int x,Bignum &y)
    1332 {
    1333  return change_ib(x)!=y;
    1334 }
    1335 bool operator !=(char x,Bignum &y)
    1336 {
    1337  return change_cb(x)!=y;
    1338 }
    1339 bool operator !=(char x[100],Bignum &y)
    1340 {
    1341  return change_cbs(x)!=y;
    1342 }
    1343 //---
    1344 //===[小于或等于]
    1345 //---
    1346 //======[Bignum置前]
    1347 bool operator <=(Bignum &x,int y)
    1348 {
    1349  return change_ib(y)<=x;
    1350 }
    1351 bool operator <=(Bignum &x,char y)
    1352 {
    1353  return change_cb(y)<=x;
    1354 }
    1355 bool operator <=(Bignum &x,char y[100])
    1356 {
    1357  return change_cbs(y)<=x;
    1358 }
    1359 bool operator <=(Bignum &x,Bignum &y)
    1360 {
    1361  return (x<y)||(x==y);
    1362 }
    1363 //======[Bignum置后]
    1364 bool operator <=(int x,Bignum &y)
    1365 {
    1366  return change_ib(x)<=y;
    1367 }
    1368 bool operator <=(char x,Bignum &y)
    1369 {
    1370  return change_cb(x)<=y;
    1371 }
    1372 bool operator <=(char x[100],Bignum &y)
    1373 {
    1374  return change_cbs(x)<=y;
    1375 }
    1376 //---
    1377 //===[大于或等于]
    1378 //---
    1379 //======[Bignum置前] 
    1380 bool operator >=(Bignum &x,int y)
    1381 {
    1382  return change_ib(y)>=x;
    1383 }
    1384 bool operator >=(Bignum &x,char y)
    1385 {
    1386  return change_cb(y)>=x;
    1387 }
    1388 bool operator >=(Bignum &x,char y[100])
    1389 {
    1390  return change_cbs(y)>=x;
    1391 }
    1392 bool operator >=(Bignum &x,Bignum &y)
    1393 {
    1394  return (y<x)||(x==y);
    1395 }
    1396 //======[Bignum置后]
    1397 bool operator >=(int x,Bignum &y)
    1398 {
    1399  return change_ib(x)>=y;
    1400 }
    1401 bool operator >=(char x,Bignum &y)
    1402 {
    1403  return change_cb(x)>=y;
    1404 }
    1405 bool operator >=(char x[100],Bignum &y)
    1406 {
    1407  return change_cbs(x)>=y;
    1408 }
    1409 //---
    1410 //--- 
    1411 //输入输出流 
    1412 //===[输入]
    1413 istream & operator >>(istream &in,Bignum &x)
    1414 {
    1415  char str[100];
    1416  in>>str;
    1417  x.setf(false);
    1418  if(str[0]=='-')
    1419  {
    1420   str[0]='0';
    1421   x.setf(true);
    1422   Bignum::runformer(str);
    1423  }
    1424  x.setnum(str);
    1425  x.test();
    1426  return in;
    1427 }
    1428 //===[输出] 
    1429 ostream & operator <<(ostream &out,Bignum &x)
    1430 {
    1431  x.test();
    1432  if(x.ifhavef)
    1433   out<<'-';
    1434  out<<x.num;
    1435  return out;
    1436 }
    1437 //---
    1438 //--------------<构造和析构函数>--------------//
    1439 //--- 
    1440 //构造函数
    1441 //===无参构造函数 
    1442 Bignum::Bignum()
    1443 {
    1444  ifhavef=false;
    1445  num[0]='1';
    1446  num[1]='0';
    1447  num[2]='0';
    1448  num[3]='';
    1449 }
    1450 //===转换构造函数
    1451 Bignum::Bignum(int x)
    1452 {
    1453  Bignum(change_ib(x));
    1454 }
    1455 Bignum::Bignum(char p)
    1456 {
    1457  ifhavef=false;
    1458  num[0]=p;
    1459  num[1]='';
    1460 }
    1461 Bignum::Bignum(char p[100])
    1462 {
    1463  ifhavef=false;
    1464  if(p[0]!='-')
    1465   strcpy(num,p);
    1466  else
    1467  {
    1468   char temp[100];
    1469   for(int i=0;i<99;i++)
    1470    temp[i]=p[i+1];
    1471   temp[99]='';
    1472   strcpy(num,temp);
    1473   ifhavef=true; 
    1474  }
    1475  test();
    1476 }
    1477 //===拷贝构造函数 
    1478 Bignum::Bignum(Bignum &x)
    1479 {
    1480  ifhavef=x.ifhavef;
    1481  strcpy(num,x.num);
    1482  test();
    1483 }
    1484 //---
    1485 //--- 
    1486 //析构函数 
    1487 Bignum::~Bignum()
    1488 {
    1489  
    1490 }
    Bignum.cpp
  • 相关阅读:
    MySQL 元数据
    MySQL 复制表
    MySQL 临时表
    MySQL 索引
    MySQL ALTER
    MySQL 事务
    MySQL 正则表达式
    Mysql Join
    Python(数据库之表操作)
    Python知识点复习之__call__
  • 原文地址:https://www.cnblogs.com/onepersonwholive/p/9336510.html
Copyright © 2020-2023  润新知