算法——大整数乘法

C++语言实现大整数乘法

list<char> long_sub(list<char> a,list<char>b);
list<char> long_add(list<char> a,list<char>b)
{
      list<char> c;
      if(*(a.begin())=='-' && *(b.begin())!='-')
      { a.erase(a.begin());//去掉符号位
            c=long_sub(b,a);
            return c;  
      }
       if(*(a.begin())!='-' && *(b.begin())=='-')
      { b.erase(b.begin());//去掉符号位
            c=long_sub(a,b);
            return c; 
       }
      if(*(a.begin())=='-' && *(b.begin())=='-')
      { a.erase(a.begin());//去掉符号位
            b.erase(b.begin());
            c=long_sub(a,b);
            c.push_front('-');
            return c; 
      }
    if(*(a.begin())!='-' && *(b.begin())!='-')
    {//保证两个数字位数同样
          int len1=a.size();int len2=b.size();
          if(len1<len2)
                for(int i=0;i<len2-len1;i++)
                      a.push_front('0');
          else
                if(len1>len2)
                      for(int i=0;i<len1-len2;i++)
                            b.push_front('0');
    //两个位数同样的正整数相加;
    int carry=0;//进位
    int num;
    list<char>::iterator iter1=a.end();  iter1--;
    list<char>::iterator iter2=b.end(); iter2--;
    for(;iter1!=a.begin() && iter2!=b.begin();iter1--,iter2--)
    {     int num1=*iter1-'0';int num2=*iter2-'0';
          num=(num1+num2+carry)%10;
          carry=(num1+num2+carry)/10;
          c.push_front(num+'0');
    }
    //最高位相加的处理
    int num1=*iter1-'0';int num2=*iter2-'0';
    num=(num1+num2+carry)%10;
    carry=(num1+num2+carry)/10;
    c.push_front(num+'0');
    if(carry!=0)
          c.push_front('0');
    return c;
    }
    return c;
}
list<char> long_sub(list<char> a,list<char>b)
{//a-b
      list<char> c;
      if(*(a.begin())!='-' && *(b.begin())=='-')
      { b.erase(b.begin());//去掉符号位
            c=long_add(a,b);
            return c;
      }
      if(*(a.begin())=='-' && *(b.begin())!='-')
      {    a.erase(a.begin());
            c=long_add(a,b);
            c.push_front('-');
            return c;
      }
      if(*(a.begin())=='-'&&*(b.begin())=='-')
      { a.erase(a.begin());b.erase(b.begin());
            c=long_sub(b,a);
            return c;
      }
      if(*(a.begin())!='-' && *(b.begin())!='-')
      { int sign=-1;//结果的正负
            int len1=a.size();   int len2=b.size();
            if(len1<len2)
            {     sign='-';
                  for(int i=0;i<len2-len1;i++)
                        a.push_front('0');
            }
            else
                  if(len1>len2)
                    for(int i=0;i<len1-len2;i++)
                              b.push_front('0');
                  else
                        if(*(a.begin())<*(b.begin()))
                              sign='-';
            int cc=0;//借位
            int num;
            list<char>::iterator iter1=a.end(); iter1--;
            list<char>::iterator iter2=b.end(); iter2--;
            if(sign!='-')
            {    for(;iter1!=a.begin() && iter2!=b.begin();iter1--,iter2--)
                  {
                        int num1=*iter1-'0';int num2=*iter2-'0';
                        int c_new=0;
                        if(num1-cc<num2)
                        {     c_new=1;
                              num1=num1+10;
                        }
                        num=(num1-num2-cc)%10;
                        cc=c_new;
                        c.push_front(num+'0');
                  }
                  //最高位的处理
                  int num1=*iter1-'0'; int num2=*iter2-'0';
                  num=(num1-num2-cc)%10;
                  if(num!=0)
                        c.push_front(num+'0');
            }
            else
                  if(sign='-')
                  { for(;iter1!=a.begin() && iter2!=b.begin();iter1--,iter2--)
                        {
                              int  num1=*iter1-'0'; int num2=*iter2-'0';
                              int c_new=0;
                              if(num2-cc<num1)
                              { c_new=1;
                                    num2=num2+10;
                              }
                              num=(num2-num1-cc)%10;
                              cc=c_new;
                              c.push_front(num+'0');
                        }
                        //最高位的处理
                        int num1=*iter1-'0'; int num2=*iter2-'0';
                        num=(num2-num1-cc)%10;
                        if(num!=0)
                              c.push_front(num+'0');
                        c.push_front('-');
                  }
                  return c;
      }
      return c;
}
void MultiPly()//大整数乘法;
{//循环法计算,float类型正负号问题都能够间接转换成正整数相乘问题。
      char s1[1000],s2[1000];
      cout<<"输入两个大整数"<<endl;
      cin>>s1>>s2;
      int i=0,j=0;
      bool flag=false;
      int len1=strlen(s1);
      int len2=strlen(s2);
      int len=len1*len2+1;
      int *a=new int [len1];
      int *b=new int [len2];
      int *c=new int [len];
      for( i=0;i<len;i++)
            *(c+i)=0;
      for(i=0;i<len1;i++)
            *(a+i)=*(s1+len1-1-i)-'0';
       for(i=0;i<len2;i++)
            *(b+i)=s2[len2-1-i]-'0';
       for(i=0;i<len1;i++)
             for(j=0;j<len2;j++)
                   c[i+j]+=a[i]*b[j];
       for(i=0;i<len;i++)
       {
             if(c[i]>9)
             {
                   c[i+1]+=c[i]/10;
                   c[i]%=10;
             }
       }
       cout<<"两个大数相乘结果例如以下:"<<endl;
       for(i=len-1;i>=0;i--)
       {
             if(flag)
                   cout<<c[i];
             else
             {
                   if(c[i]!=0)
                        { cout<<c[i];flag=true;}
             }
       }
       delete[]a;delete[]b;delete[]c;
}


posted @ 2017-07-04 16:19  jhcelue  阅读(273)  评论(0编辑  收藏  举报