哪位大侠能给一个超大整数运算的类?或者有算法就行~
谢谢了~
小弟新手出道,大侠照顾一下~
[解决办法]
http://o.thec.cn/maths/hugecalc/
[解决办法]
#include "stdio.h "
#include <string.h>
#define MAX 10000
struct Node{
int data;
Node *next;
};
void output(Node *head)
{
if(!head-> next&&!head-> data)
return;
output(head-> next);
//if(head-> data!=1||head-> data!=0)
//return;
printf( "%d ",head-> data);
}
//两数相乘
void Mul(char *a,char *b)
{
char *ap = a , *bp = b;
Node *head = 0;
head = new Node;
head-> data = 0,head-> next = 0; //头
Node *p , *q = head , *p1;
int temp=0,temp1,bbit;
while(*bp)//若乘数不为空 ,继续.
{
p=q-> next;p1=q;
bbit=*bp-48;//把当前位转为整型
while(*ap||temp)//若被乘数不空,继续
{
if(!p)//若要操作的结点为空,申请之
{
p = new Node;
p-> data=0;
p-> next=0;
p1-> next=p;
}
if(*ap==0)
temp1=temp;
else
{
temp1=(p1-> data)+(*ap-48)*bbit+temp;
ap++;
}
p1-> data=temp1%10;//留当前位
temp=temp1/10;//进位以int的形式留下.
p1=p;
p=p-> next;//被乘数到下一位
}
ap=a;
bp++;
q=q-> next;//q进下一位
}
p=head;
output(p);//显示
while(head)//释放空间
{
p=head-> next;
delete head;
head=p;
}
}
//两数相加
void MaxSum(char *a,char *b )
{
int nt = strlen(a);
a[nt] = '\0 ';
nt = strlen(b);
b[nt] = '\0 ';
char *ap = a , *bp = b;
Node *head = 0;
head = new Node;
head-> data = 0,head-> next = 0; //头
Node *p = head ;
Node *p1 = 0;
int nsum = 0 ,temp = 0;
int na,nb;
while(*ap||*bp||temp) // 只要有一个数还没有结束,就执行循环
{
if(!p1)//若要操作的结点为空,申请之
{
p1 = new Node;
p1-> data=0;
p1-> next=0;
p-> next=p1;
}
if(*ap)
na = *ap-48 ;
else
na = 0;
if(*bp)
nb = *bp-48 ;
else
nb = 0;
if(*ap== '\0 '&&*bp== '\0 ')
{
nsum = temp;
}
else
{
nsum = p-> data + na + nb + temp;
}
p-> data = nsum%10;//留当前位
temp = nsum/10;//进位以int的形式留下.
p = p1;
p1 = p1-> next;
p1 = 0;
if(*ap!= '\0 ')
ap++;
if(*bp!= '\0 ')
bp++;
}
p = head;
output(p);//显示
printf( "\n ");
while(head)//释放空间
{
p=head-> next;
delete head;
head=p;
}
}
void main()
{
printf( "请输入两个数 ");
char test1[MAX],test2[MAX];
scanf( "%s ",test1);
scanf( "%s ",test2);
MaxSum(strrev(test1),strrev(test2));
}