#include "hugeInt.h"
#include <iostream>
//#include <memory.h>
#include <string.h>
#include <cmath>
using namespace std;
HugeInt::HugeInt()
{
memset(m_num,0,sizeof(char)*MAXLEN);
m_sign=0;
m_len=0;
}
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;
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;
}
}
void 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::operator +(HugeInt &R)
{
HugeInt Result(0);
char *p,*q,*r;
p=q=r=NULL;
int len1,len2; //len1>len2.len1比较长的
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;
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; //12345 123456 1
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;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
HugeInt.zip (10个子文件)
HugeInt
hugeInt.h 1KB
f5000.cpp 396B
HugeInt.cpp 5KB
HugeInt.cbp 1KB
obj
Debug
main.o 12KB
HugeInt.o 27KB
f5000.o 15KB
HugeInt.layout 723B
HugeInt.depend 398B
bin
Debug
HugeInt.exe 62KB
共 10 条
- 1
资源评论
- liguoqiang48882018-04-24多谢分享!!!!!
狂奔的蜗牛Evan
- 粉丝: 414
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功