Posted on 2006-04-12 17:43
魔のkyo 阅读(377)
评论(0) 编辑 收藏 引用 所属分类:
Programming
/*
LongInt适合做较短的超长整数加法运算
存储结构是一个MaxSize大小LongNode类型的顺序表
单个结点的最大计数限制是LimitNum
(临时,未完善)
*/
#include
<
stdio.h
>
#include
<
stdlib.h
>
const
int
LimitNum
=
1000000000
;
const
int
MaxSize
=
6
;
typedef unsigned
int
LongNode;
typedef LongNode
*
LongInt;
inline
void
InitLongInt(LongInt
&
I){
/*
初始化I(分配空间,不赋初值!)
*/
I
=
(LongInt)malloc(MaxSize
*
sizeof
(LongNode));
}
void
InitLongInt(LongInt
&
I,
int
n){
/*
用一个整型值初始化I,要求n<LimitNum
*/
I
=
(LongInt)malloc(MaxSize
*
sizeof
(LongNode));
int
i;
for
(i
=
0
;i
<
MaxSize
-
1
;i
++
)I[i]
=
0
;
I[i]
=
n;
}
inline
void
DestroyLongInt(LongInt
&
I){
free(I);
}
void
PlusLongInt(LongInt I1,LongInt I2){
/*
计算I1+I2的值结果放在I1中
*/
LongNode t;
for
(
int
i
=
MaxSize
-
1
;i
>=
0
;i
--
){
t
=
I1[i]
+
I2[i];
I1[i
-
1
]
+=
t
/
LimitNum;
I1[i]
=
t
%
LimitNum;
}
}
void
PrintlnLongInt(LongInt I){
/*
输出I
*/
int
i(
0
);
while
(
!
I[i]
&&
i
<
MaxSize
-
1
)
++
i;
while
(i
<
MaxSize)printf(
"
%d
"
,I[i
++
]);
putchar(
'
\n
'
);
}
int
main()
{
LongInt a,b;
InitLongInt(a,
0
);
InitLongInt(b,
0
);
PlusLongInt(a,b);
PrintlnLongInt(a);
//
system("pause");
return
0
;
}