付翔的专栏
在鄙视中成长 记录成长的点滴
posts - 106,  comments - 32,  trackbacks - 0
#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 
= 599while(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 
= 599while(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 
= 599while(A[length]==0 && length>0) length--;
        assert(A[
599]==0);
    }

    
void output()
    
{
        
int u = 599while(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 付翔 阅读(468) 评论(0)  编辑 收藏 引用 所属分类: ACM 数据结构

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2010年9月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

CSDN - 我的blog地址

博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜