参考代码:
#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语言训练-求PI* (C语言代码)浏览:930 |
上车人数 (C语言代码)浏览:1257 |
C语言程序设计教程(第三版)课后习题10.1 (C语言代码)浏览:736 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:615 |
C语言程序设计教程(第三版)课后习题9.1 (Java代码)浏览:481 |
【蟠桃记】 (C语言代码)浏览:711 |
A+B for Input-Output Practice (C++代码)浏览:632 |
淘淘的名单 (C语言代码)答案错误???浏览:624 |
C语言程序设计教程(第三版)课后习题9.4 (C语言代码)浏览:699 |
C语言程序设计教程(第三版)课后习题10.7 (用指针求解)浏览:1542 |