原题链接:大整数乘法
参考代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct lennum{
int num;
struct lennum *next;
struct lennum *back;
};
struct lennum* create(char *num);
struct lennum* add(struct lennum *head1,struct lennum *head2);
struct lennum* times(struct lennum *head1,struct lennum *head2);
struct lennum *copylennum(struct lennum *head,int l);
void adddigitalprocess(struct lennum *head);
void printflennum(struct lennum *head);
int main()
{
char strnum1[300],strnum2[300];
scanf("%s\n",strnum1);
scanf("%s",strnum2);
// 记录大整数 从个位数开始存储
struct lennum *head1=NULL,*head2=NULL;
head1=create(strnum1);
head2=create(strnum2);
// 乘法
struct lennum *timesnum=NULL;
timesnum = times(head1,head2);
// 输出
printflennum(timesnum);
}
struct lennum *create(char *num)
{
// 将字符串数字转成一维数组进行存储 下标0对应个位数 下标1对应十位数
struct lennum *head=NULL,*p=NULL,*q=NULL;
for(int i=strlen(num)-1;i>=0;i--)
if(i==strlen(num)-1)
{
q = (struct lennum *)malloc(sizeof(struct lennum));
q->num = num[i]-'0';
q->back=NULL;
q->next=NULL;
head=p=q;
}
else
{
q = (struct lennum *)malloc(sizeof(struct lennum));
q->num = num[i]-'0';
q->back=NULL;
q->next=NULL;
p->next=q;
q->back=p;
p=q;
}
return head;
}
struct lennum *add(struct lennum *head1,struct lennum *head2)
{
int l1=0,l2=0;
struct lennum *p=NULL,*q=NULL;
// 计算长度
p=head1;
while(p!=NULL)
{
l1++;
p=p->next;
}
p=head2;
while(p!=NULL)
{
l2++;
p=p->next;
}
// 短的整数加到长的整数上去 temphead复制最长的链表
struct lennum *temphead=NULL;
if(l1>l2)
{
temphead=copylennum(head1,0);
q=head2;
}
else
{
temphead=copylennum(head2,0);
q=head1;
}
p=temphead;
while(q!=NULL)
{
p->num=p->num+q->num;
p=p->next;
q=q->next;
}
return temphead;
}
struct lennum *times(struct lennum *head1,struct lennum *head2)
{
// 用head1的数字去乘head2
char beginnum[2]={'0','\0'};
struct lennum *p=NULL,*q=NULL;
struct lennum *temphead=NULL,*sum=create(beginnum); // 用sum存储结果
int l=0;
while(head1!=NULL)
{
// 在head2链表前面生成l个0
temphead = copylennum(head2,l);
// 乘法
p=temphead;
while(p!=NULL)
{
p->num=p->num*head1->num;
p=p->next;
}
// 加法
p=sum; // 用于释放原sum
sum = add(sum,temphead);
while(p->next!=NULL)
p=p->next;
while(p->back!=NULL)
{
q=p->back;
free(p);
p=q;
}
free(p);
// 进位
adddigitalprocess(sum);
// 释放temphead
p=temphead;
while(p->next!=NULL)
p=p->next;
while(p->back!=NULL)
{
q=p->back;
free(p);
p=q;
}
free(p);
temphead=NULL;
// 下一位数进行乘法
head1=head1->next;
l++;
}
return sum;
}
struct lennum *copylennum(struct lennum *head,int l)
{
// 复制链表head
struct lennum *p=NULL,*q=NULL,*temphead=NULL;
while(head!=NULL)
{
if(temphead==NULL)
{
q = (struct lennum *)malloc(sizeof(struct lennum));
q->num = head->num;
q->back=NULL;
q->next=NULL;
temphead=p=q;
}
else
{
q = (struct lennum *)malloc(sizeof(struct lennum));
q->num = head->num;
q->back=NULL;
q->next=NULL;
p->next=q;
q->back=p;
p=q;
}
head=head->next;
}
// 在链表temphead的前方添加l个0
for(int i=1;i<=l;i++)
{
q = (struct lennum *)malloc(sizeof(struct lennum));
q->num = 0;
q->back=NULL;
q->next=NULL;
q->next=temphead;
temphead->back=q;
temphead=q;
}
return temphead;
}
void adddigitalprocess(struct lennum *head)
{
// 加法进位处理
struct lennum *p=NULL,*q=NULL;
p=head;
while(p->next!=NULL)
{
p->next->num=p->next->num + p->num/10;
p->num=p->num%10;
p=p->next;
}
// 最高位进位
while(p->num>=10)
{
q = (struct lennum *)malloc(sizeof(struct lennum));
q->num = p->num/10;
q->back=NULL;
q->next=NULL;
p->next=q;
q->back=p;
p->num=p->num%10;
p=q;
}
}
void printflennum(struct lennum *head)
{
// 输出
struct lennum *p=NULL;
p=head;
while(p->next!=NULL)
p=p->next;
while(p!=NULL)
{
printf("%d",p->num);
p=p->back;
}
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复