Java编程之前的复习和练习

日期:2018.7.14

星期六

博客期:001

  今天先是试着写一下博客,最近去青海旅游了,学习时间有点少,但空余时间还是有学习的,不管怎么样吧!先说一下我的这几天的成果——“Bignum”类,虽然很费劲写了不少,但还是没有用很多硬性要求!Bignum是用C++写的,我想着多少先用C++写出来,因为java没有运算符重载,写起来是相当麻烦!先声明一下:如下是还未优化,且未整理的Bignum类,所以明天再把具体文件拉入。

  先来一部分源代码吧!

/*---------------/<如下为Bignum.cpp>/--------------------*/

//减法
Bignum operator -(Bignum &x,Bignum &y)
{
   Bignum result;
   if(!x.ifhavef&&!y.ifhavef)
   {
      char arrays[100];
      bool resulthavef=false;
      Bignum t1(x),t2(y);
      int l1=Bignum::litude(x),l2=Bignum::litude(y);
      if(l1==l2)
      {
         Bignum::runlatter(t1.num);
         Bignum::runlatter(t2.num);
         l1++;
         l2++;
      }
      else if(l1<l2)
      {
         Bignum::runlatter(t2.num);
         Bignum::runlatter(t1.num,l2-l1+1);
         l2++;
         l1=l2;
      }
      else if(l1>l2)
      {
         Bignum::runlatter(t1.num);
         Bignum::runlatter(t2.num,l1-l2+1);
         l1++;
         l2=l1;
      }
      //统一排位
      Bignum t3(t1),t4(t2);
      if(strcmp(t1.num,t2.num)<0)
      {
         Bignum::exchange(t3,t4);
         resulthavef=true;
      }
      if(strcmp(t1.num,t2.num)==0)
      {
         arrays[0]='0';
         arrays[1]='';
      }
      else
      {
         arrays[l2]='';
         for(int j=l2-1,k=0;j>=0;j--)
         {
            int answer=change(t3.num[j])-change(t4.num[j])-k;
            if(answer<0)
            {
               k=1;
               answer=answer+10;
            }
            else
               k=0;
            arrays[j]=change(answer);
         }
      }
      Bignum::runformer(arrays);
      result.setnum(arrays);
      result.setf(resulthavef);
   }
   else if(x.ifhavef&&y.ifhavef)
     result=Bignum::anti(y)-Bignum::anti(x);
   else if(x.ifhavef&&!y.ifhavef)
     result=Bignum::anti(Bignum::anti(x)+y);
   else
     result=Bignum::anti(y)+x;
   result.test();
   return result;
}

原文地址:https://www.cnblogs.com/onepersonwholive/p/9311208.html