#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<algorithm>
using namespace std;
const long base = 10000;
long max(long a,long b)
{
return a>b?a:b;
}
struct Integer
{
long A[600];
int length;
Integer()
{
memset(A,0,sizeof(A));
A[0] = 0; length = 0;
}
Integer(int num)//构造函数
{
int i = 0;
memset(A,0,sizeof(A));
while(num)
{
A[i] = num%base;
i ++;
num/=base;
}
length = i;
}
void operator+=(Integer x)
{
long t = 0;
length = max(length, x.length) + 5;
for(int i=0;i<length;i++)
{
A[i] += x.A[i] + t;
if(A[i] >= base) A[i] -= base, t = 1;
else t = 0;
}
length = 599; while(A[length]==0 && length>0) length--;
assert(A[599]==0);
}
void operator-=(Integer x)
{
long t = 0;
length = max(length, x.length) + 5;
for(int i=0;i<length;i++)
{
A[i] -= x.A[i] - t;
if(A[i] < 0) A[i] += base, t = 1;
else t = 0;
}
length = 599; while(A[length]==0 && length>0) length--;
assert(A[599]==0);
}
void operator*=(Integer x)
{
long res[600], t = 0;
memset(res,0,sizeof(res));
for(int i=0;i<=length;i++)
{
t = 0;
for(int j=0;j<=x.length || t;j++)
{
res[i+j] += A[i] * x.A[j] + t;//这里会溢出 然后将base 减少至相乘也不会溢出的大小
t = res[i+j] / base;
res[i+j] %= base;
}
}
memcpy(A,res,sizeof(res));
length = 599; while(A[length]==0 && length>0) length--;
assert(A[599]==0);
}
void output()
{
int u = 599; while(A[u]==0 && u>0) u--;
printf("%ld",A[u]); u--;
while(u>=0) printf("%04ld",A[u]), u--;
printf("\n");
}
} A[10];
void init()
{
}
void process()
{
int i,j,k;
struct Integer tmp;
for(i = 0;i < 10; i ++)
A[i]= 123123123;
A[1]*=A[2];
A[1].output();
A[1]*=A[2];
A[1].output();
A[1]-=A[2];
A[1].output();
}
int main()
{
int x,y;
init();
process();
return 0;
}
posted on 2010-09-05 19:56
付翔 阅读(485)
评论(0) 编辑 收藏 引用 所属分类:
ACM 数据结构