📄 bigint.cpp
字号:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct node{
struct node *high;
struct node *link;
int data;
}Node;
Node* getint(Node **head,char str[],int len)
{
int i;
Node *NEW,*low,*p;
p=(Node*)malloc(sizeof(Node));
p->high=NULL;
p->data=str[0]-'0';
low=p;
for(i=1;i<len;i++)
{
NEW=(Node*)malloc(sizeof(Node));
NEW->data=str[i]-'0';
NEW->high=low;
low->link=NEW;
low=NEW;
}
low->link=NULL;
*head=p;
return low;
}
Node* add(Node* low1,Node* low2)
{
Node *head,*NEW,*p,*q,*low;
int m;
low=(Node*)malloc(sizeof(Node));
low->link=NULL;
head=low;
m=(low1->data+low2->data);
low->data=m%10;
m/=10;
for(p=low1->high,q=low2->high;p!=NULL&&q!=NULL;p=p->high,q=q->high)
{
m+=p->data+q->data;
NEW=(Node*)malloc(sizeof(Node));
NEW->data=m%10;
NEW->link=head;
head->high=NEW;
head=NEW;
m/=10;
}
if(q!=NULL)p=q;
for(;p!=NULL;p=p->high)
{
m+=p->data;
NEW=(Node*)malloc(sizeof(Node));
NEW->data=m%10;
NEW->link=head;
head->high=NEW;
head=NEW;
m/=10;
}
if(m!=0)
{
NEW=(Node*)malloc(sizeof(Node));
NEW->data=m;
NEW->link=head;
head->high=NEW;
head=NEW;
}
head->high=NULL;
return head;
}
void print(Node *head)
{ Node* p;
for(p=head;p!=NULL;p=p->link)printf("%d",p->data);
}
int main()
{
char str1[1001],str2[1001];
int n,i;
Node *head1,*head2,*head,*low1,*low2;
while((scanf("%s%s",str1,str2))!=EOF)
{low1=getint(&head1,str1,strlen(str1));
low2=getint(&head2,str2,strlen(str2));
head=add(low1,low2);
print(head);
printf("\n");
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -