#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#define MaxLength 100
#define Base 10
#define MaxDivB 1000
//一位一存BIGINT类
class BigInt
{
public:
int* Num;
int Length;
bool NumSingle;
BigInt()
{
Num = new int[MaxLength];
memset(Num,0,sizeof(int)*MaxLength);
Length=0;
NumSingle=1;
}
~BigInt()
{
delete[] Num;
}
void Read(char* strnum);
void Copy(BigInt &source);
void Print(char* strnum);
static BigInt* Add(BigInt &a,BigInt &b);
static BigInt* Sub(BigInt &a,BigInt &b);
static BigInt* Mul(BigInt &a,BigInt &b);
static BigInt* Div(BigInt &a,int b);
static BigInt* Div(BigInt &a,int b,int* modb);
static BigInt* Div(int a,int b,int E);
static BigInt* Div(int a,int b,int ELimit,int* SLen);
};
int CharToNum(char ch)
{
if(ch>='0'&&ch<='9')
return (int)(ch - '0');
else
return -1;
}
char NumToChar(int i)
{
i%=10;
return ((char)i+'0');
}
void BigInt::Read(char* strnum)
{
int len = strlen(strnum);
int start;
if(len<=0)return;
if(strnum[0]=='-')
{
NumSingle=0; //负数
start=1; //推移一位
Length=len-1;
}
else
{
NumSingle=1;
start=0;
Length=len;
}
int i;
for(i=start;i<len;i++)
{
Num[len-i-1]=CharToNum(strnum[i]); //读取
}
}
void BigInt::Print(char* strnum)
{
if(this->Length <=0)
{
strnum[0]='0';
strnum[1]='\0';
return;
}
else if(this->Length == 1) //kill '-0'
{
if(Num[0]==0)
{
strnum[0]='0';
strnum[1]='\0';
return;
}
}
int i,j;
if(this->NumSingle==1)
{
j=0; //正数
}
else
{
strnum[0]='-'; //负数
j=1;
}
for(i=Length-1;i>=0;i--,j++)
{
strnum[j]=NumToChar(Num[i]);
}
strnum[j]='\0';
}
void BigInt::Copy(BigInt& source)
{
this->Length = source.Length;
this->NumSingle = source.NumSingle;
for(int i=0;i<source.Length;i++)
this->Num[i]=source.Num[i];
}
BigInt* BigInt::Add(BigInt &a,BigInt &b)
{
BigInt *c = new BigInt();
c->Length = max(a.Length,b.Length);
int i;
for(i=0;i<c->Length;i++)
{
c->Num[i] += a.Num[i]+b.Num[i];
if(c->Num[i]>=10)
{
c->Num[i+1] += c->Num[i] / 10;
c->Num[i] %= 10;
}
}
if(c->Num[i]>0)
c->Length++;
return c;
}
BigInt* BigInt::Sub(BigInt &a,BigInt &b)
{
BigInt *at;// = a;
BigInt *bt;// = b;
BigInt *c = new BigInt();
c->Length = max(a.Length,b.Length);
//Cmp a and b
bool needswap = false;
if(a.Length<b.Length)
{
needswap = true;
}
else if(a.Length==b.Length)
{
for(int i=a.Length-1;i>=0;i--)
{
if(a.Num[i]<b.Num[i])
{
needswap=true;
break;
}
}
}
if(needswap)
{
c->NumSingle=0;
at=&b;
bt=&a;
}
else
{
c->NumSingle=1;
at=&a;
bt=&b;
}
//work
int i;
for(i=0;i<c->Length;i++)
{
c->Num[i] += at->Num[i]-bt->Num[i];
if(c->Num[i]<0)
{
c->Num[i] += 10;
c->Num[i+1] -= 1;
}
}
while(c->Num[c->Length-1]==0)
{
c->Length--;
}
return c;
}
BigInt* BigInt::Mul(BigInt &a,BigInt &b)
{
BigInt *c = new BigInt();
c->Length = a.Length + b.Length;
c->NumSingle = !(a.NumSingle ^ b.NumSingle);
for(int i=0;i<a.Length;i++)
{
for(int j=0;j<b.Length;j++)
{
c->Num[i+j]+=a.Num[i]*b.Num[j];
}
}
for(int i=0;i<=c->Length;i++)
{
if(c->Num[i]>=10)
{
c->Num[i+1] += c->Num[i]/10;
c->Num[i] %= 10;
}
}
while(c->Num[(c->Length)-1]==0) //缩位
{
(c->Length)--;
}
return c;
}
BigInt* BigInt::Div(BigInt &a,int b,int* modb)
{
if(b==0) return NULL;
//BigInt *At = new BigInt();
BigInt *C = new BigInt();
C->Length = a.Length;
C->NumSingle = !(a.NumSingle ^ (b>0?true:false));
int now,x;
x=0;
for(int i=a.Length-1;i>=0;i--)
{
now = a.Num[i] + 10 * x;
C->Num[i] = now / b;
x = now % b;
}
while(C->Num[(C->Length)-1]==0) //缩位
{
(C->Length)--;
}
if(modb!=NULL)
(*modb)=x;
return C;
}
BigInt* BigInt::Div(BigInt &a,int b)
{
return BigInt::Div(a,b,NULL);
}
BigInt* BigInt::Div(int a,int b,int E)
{
if(b==0) return NULL;
if(a>=Base) a %= b;
//扩大10*E倍
BigInt* A = new BigInt();
A->Length = E+1;
A->Num[E]=a;
return BigInt::Div(BigInt &A,int b);
}
//BigInt* 无符号小数位 Div(int 被除数,int 除数,int 最长小数位限度,int* 循环节长度)
BigInt* BigInt::Div(int a,int b,int ELimit,int* SLen)
{
//指针不空,除数不为0,除数不大于最大除数
if(SLen==NULL||b==0||b>MaxDivB)return NULL;
a = std::abs(a);
b = std::abs(b);
a %= b;
int i=0,j=0;
int x,len;
int st = -1;
int *pos = new int[MaxDivB];
int *num = new int[ELimit];
memset(pos,-1,sizeof(int)*MaxDivB);
memset(num,0,sizeof(int)*ELimit);
while(i<ELimit)
{
a*=10;
x=a%b;
if(x==0) //除尽
{
break;
}
if(pos[x]!=-1) //找到循环节
{
st=i;
break;
}
num[i]=a/b;
pos[x]=i;
i++;
}
len = i;
BigInt* c = new BigInt();
c->Length = len;
c->NumSingle = 1;
for(i=len-1;j<len;i--,j++)
{
c->Num[i]=num[j];
}
if(st==-1) *SLen=0;
else *SLen = len - st;
return c;
}
int main(int argc, char **argv)
{
string stra,strb,strc;
char *a;
char *b;
char *c=new char[MaxLength+2];
char *t=new char[MaxLength+2];
cout<<"Enter first number:"<<endl;
cin>>stra;
cout<<"Enter secend number:"<<endl;
cin>>strb;
a = (char*)stra.c_str();
b = (char*)strb.c_str();
BigInt *A=new BigInt();
BigInt *B=new BigInt();
BigInt *C=new BigInt();
A->Read(a);
B->Read(b);
C=BigInt::Add(*A,*B);
A->Print(c);
cout<<" A="<<c<<endl;
B->Print(c);
cout<<" B="<<c<<endl;
C->Print(c);
cout<<"A+B="<<c<<endl;
delete C;
C=BigInt::Sub(*A,*B);
C->Print(c);
cout<<"A-B="<<c<<endl;
delete C;
C=BigInt::Mul(*A,*B);
C->Print(c);
cout<<"A*B="<<c<<endl;
delete A;
delete B;
delete C;
delete[] c;
int numb,modb;
cout<<"Calc a/b."<<endl<<"Enter number a(as BigInt):"<<endl;
cin>>stra;
c = stra.c_str();
cout<<"Enter number b(as int):"<<endl;
cout>>numb;
A = new BigInt();
A->Read(c);
C = BigInt::Div(*A,numb,&modb);
B = BigInt::Div(modb,b,10);
/*
* 1!+2!+3!+...+n!
*
BigInt *tmp,*bint1;
bint1=new BigInt();
A=new BigInt();
B=new BigInt();
c=new char[MaxLength+2];
int n;
cout<<"Input n:";
cin>>n;
sprintf(c,"%d",n);
BigInt *f = new BigInt[100];
f[1].Read("1");
C->Read("0");
for(int i=2;i<=n;i++)
{
std::memchr(c,0,sizeof(char)*(MaxLength+2));
sprintf(c,"%d",i);
B->Read(c);
f[i]=*BigInt::Mul(f[i-1],*B);
tmp=C;
C=BigInt::Add(f[i],*C);
delete tmp;
}
C->Print(c);
cout<<" 1!+2!+3!+...+"<<n<<"!"<<endl<<"="<<c<<endl;
cin>>strc;
delete A;
delete B;
delete C;
delete[] c;*/
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
C++高精度算法示例
共7个文件
o:1个
exe:1个
d:1个
2星 需积分: 49 28 下载量 25 浏览量
2008-12-14
11:28:35
上传
评论 3
收藏 159KB RAR 举报
温馨提示
C++的高精度算法示例 实现了一位一存的高精度加法 减法 乘法 和高精度除以int
资源推荐
资源详情
资源评论
收起资源包目录
BigInt.rar (7个子文件)
BigInt
main.cpp 6KB
BingInt.mk 2KB
main.cpp.bak 4KB
Debug
BingInt.exe 594KB
main.o 121KB
main.o.d 69B
BingInt.project 2KB
共 7 条
- 1
资源评论
- u0102562752013-04-11问题实在是太多了额,不晓得该说啥了,cout<<都能写成cout>>,debug过没有啊,亲
- 唐门问心2012-04-15注释是乱码。大数除法是错的。
h4x3rotab
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功