HugeInt类 大数运算加法、乘法 c++实现

晚上写出来的乘法,现在整理出来。

我的相关日志:

2007-03-23 | HugeInt类-无限位长度的大数加法 c++实现

h

/*-------------------------------------------------------*
* CopyRight 2007 北京交通大学           
*              
* 日期:2007-3-23                                        
*              
* 文件名:HugeInt.h          
*              
* 描述:HugeInt类的定义文件,定义了加法、乘法接口           
*              
---------------------------------------------------------*/
#include <iostream.h>
//using namespace std;
const int MAXLEN=20000;
class HugeInt
{
public:
 //构造函数
 HugeInt();
 HugeInt(int R);

 //IO接口

 friend ostream& operator <<(ostream& out,HugeInt &R);

 //关系运算接口
 //bool HugeInt& operator ==(HugeInt &R);
 bool operator !=(HugeInt &R);
 bool operator !=(const int &R);
 bool operator <=(HugeInt &R);
 bool operator <=(const int &R);

 //加法接口
 HugeInt& operator =(int R);
 HugeInt operator +(HugeInt &R);
 HugeInt operator +(int R);
 HugeInt& operator +=(HugeInt &R);
 HugeInt& operator +=(int R);
 HugeInt& operator ++();
 
 //HugeInt& operator =(const HugeInt &R);

 //乘法接口
 HugeInt operator *(HugeInt &R);
 HugeInt operator *(int R);
 HugeInt& operator *=(HugeInt &R);
 HugeInt& operator *=(int R);
 
 //输出接口
 Print();

 //公用接口
 int Len(){return m_len;}
private:
 int m_sign;    //符号
 int m_len;    //长度
 char m_num[MAXLEN];  //存储空间
};

cpp

/*-------------------------------------------------------*
* CopyRight 2007 北京交通大学       
*              
* 日期:2007-3-23                                        
*              
* 文件名:HugeInt.cpp         
*                                                       
* 描述:HugeInt.cpp    类的实现文件,实现了加法、乘法接口
*                                                       
---------------------------------------------------------*/
 
#include "HugeInt.h"
#include <iostream.h>
#include <memory>
#include <cmath>
//using namespace std;

HugeInt::HugeInt()
{
 memset(m_num,0,sizeof(char)*MAXLEN);
 m_sign=0;
 m_len=0;
}

HugeInt::Print()
{
 int i;
 if(m_sign==-1)
  cout<<"-";
 for(i=m_len;i!=0;i--)
  cout<<m_num[i]+0;
 cout<<endl;
}

HugeInt::HugeInt(int R)
{
 memset(m_num,0,sizeof(char)*MAXLEN);
 if(R!=0)
 {
  if(R>0)
   m_sign=1;
  else
   m_sign=-1;
  int i=0,k=1;
  int abs_R=abs(R);
  do
  {
  i++;
  m_num[i]=abs_R%10;
  abs_R/=10;
  }while(abs_R);
  m_len=i;
 }
 else
 {
  m_num[1]=0;
  m_len=1;
  m_sign=0;
 }
}

HugeInt HugeInt::operator +(HugeInt &R)
{
 HugeInt Result(0);
 char *p,*q,*r;
 p=q=r=NULL;
 int len1,len2;
 if(Len()>R.Len())
 {
  p=this->m_num;
  q=R.m_num;
  r=Result.m_num;
  len1=Len();
  len2=R.Len();
 }
 else
 {
  p=R.m_num;
  q=this->m_num;
  r=Result.m_num;
  len1=R.Len();
  len2=Len();
 }
 int i=1,j=1,k=1,carry=0;

 while(j<=len2)
 {
  r[k]=p[i++]+q[j++]+carry;
  carry=r[k]/10;
  r[k]%=10;
  k++;
 }
 while(i<=len1)
 {
  r[k]=p[i++]+carry;
  carry=r[k]/10;
  r[k]%=10;
  k++;
 }
 if(carry>0)
 {
  r[k]=carry;
  Result.m_len=k;
 }
 else
 {
  Result.m_len=k-1;
 }
 Result.m_sign=1;
 return Result;
}

HugeInt &HugeInt::operator +=(HugeInt &R)
{
 *this=*this+R;
 return *this;
}

HugeInt& HugeInt::operator ++()
{
 *this=*this+1;
 return *this;
}

HugeInt HugeInt::operator +(int R)
{
 HugeInt hInt(R);
 return (*this)+hInt;
}

HugeInt& HugeInt::operator =(int R)
{
 if(R!=0)
 {
  if(R>0)
   m_sign=1;
  else
   m_sign=-1;
  int i=0,k=1;
  int abs_R=abs(R);
  do
  {
  i++;
  m_num[i]=abs_R%10;
  abs_R/=10;
  }while(abs_R);
  m_len=i;
 }
 else
 {
  m_num[1]=0;
  m_len=1;
  m_sign=1;
 }
 return *this;
}

HugeInt& HugeInt::operator +=(int R)
{
 HugeInt hInt=R;
 *this=*this+hInt;
 return *this;
}

HugeInt HugeInt::operator *(HugeInt &R)
{
 HugeInt Result=0;
 Result.m_sign=this->m_sign*R.m_sign;
 char *muti1,*muti2,*result=Result.m_num;
 int len1,len2;
 if(this->m_len>R.Len())
 {
  muti1=this->m_num;
  muti2=R.m_num;
  len1=this->m_len;
  len2=R.m_len;
 }
 else
 {
  muti1=R.m_num;
  muti2=this->m_num;
  len2=this->m_len;
  len1=R.m_len;
 }
 

 int i=1,j=1,k=1,carry=0;
 
 
 while(j<=len2)
 {
  i=1;
  k=j;
  while(i<=len1)
  {
   result[k]+=muti1[i++]*muti2[j]+carry;
   carry=result[k]/10;
   result[k]%=10;
   k++;
  }
  if(carry!=0)
  {
   result[k]+=carry;
   Result.m_len=k;
   carry=0;
  }
  else
  {
   Result.m_len=k-1;
  }
  j++;
 }
 return Result;
}

HugeInt HugeInt::operator *(int R)
{
 HugeInt hInt=R;
 return (*this)*hInt;
}
bool HugeInt::operator !=(HugeInt &R)
{
 if(R.Len()==this->Len())
 {
  int i;
  for(i=1;i<this->Len();i++)
   if(m_num[i]!=R.m_num[i])
    return true;
 }
 else
  return true;
 return false;
}

bool HugeInt::operator !=(const int &R)
{
 HugeInt hInt(R);
 if(hInt.Len()==this->Len())
 {
  int i;
  for(i=1;i<=this->Len();i++)
   if(m_num[i]!=hInt.m_num[i])
    return true;
 }
 else
  return true;
 return false;
}

bool HugeInt::operator <=(HugeInt &R)
{
 int i=1;
 if(Len()==R.Len())
 {
  for(i=R.Len();i!=0;i--)
   if(R.m_num[i]<m_num[i])
    return false;
   return true;
 }
 else
  if(Len()<R.Len())
   return true;
  else
   return false;
}

bool HugeInt::operator <=(const int &R)
{
 HugeInt hInt=R;
 return (*this)<=hInt;
}

HugeInt& HugeInt::operator *=(HugeInt &R)
{
 *this=(*this)*R;
 return *this;
}

HugeInt& HugeInt::operator *=(int R)
{
 HugeInt hInt=R;
 *this=*this*hInt;
 return *this;
}

ostream& operator <<(ostream &out,HugeInt &R)
{
 int i;
 if(R.m_sign==-1)
  out<<"-";
 for(i=R.m_len;i!=0;i--)
  out<<R.m_num[i]+0;
 out<<endl;
 return out;
}

main

#include <iostream.h>
#include "HugeInt.h"
//using namespace std;

int main()
{
 HugeInt result=1,b=100;
 int i=1;
 
 for(i=1;i<=100;i++)
  result*=i;
 cout<<"100!=";
 cout<<result;
 

 return 0;
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *