这里是源代码。。。。就不解释了。。。心碎
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN 200
#define MAX 40000
#define LEN sizeof(struct string)
struct string
{
int data;
struct string * next;
};
int reverse(int str[],int len)
{
int temp;
int i;
for(i = 0;i < ( len + 1 )/ 2; i++){
temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
return 0;
}
//****************************************************************************/
//这个函数是将数组逆序放在链表中
struct string * creat (int A[], int B)
{
int i;
struct string *head, *p1, *p2;
p1 = p2 = (struct string *) malloc (LEN);
for (i = B; i > 0; i--)
{
p1->data = A[i];
if(i == B)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct string *) malloc (LEN);
}
return head;
}
//****************************************************************************/
//这里是主函数
void main()
{
//定义变量
int i , j , e, f, k , m , n;
int l1, l2;
char AA[MIN] ,BB[MIN];
int aa[MIN] = {0},bb[MIN] = {0},cc[MAX] = {0}, dd[MAX] = {0};//cc用来最后输出加法,dd用来最后输出乘法
struct string *head1, *head2, *head3, *head4;
struct string *p1, *p2, *p3, *p4;
printf("请输入两个大数,用回车结束:\n");
printf("第一个数: ");
gets(AA);
printf("第二个数: ");
gets(BB);
l1 = strlen(AA);
l2 = strlen(BB);
if (l1 > l2)
{
k = l1;
}
else
k = l2;
e = k;
//把字符串转成数组,不逆序存放
for(i = 0,k = e; i < l1; i++,k--)
{
aa[k] = AA[l1 - 1 - i] - '0';
}
for (k = e, i = 0; i < l2; k--, i++)
{
bb[k] = BB[l2 - 1 - i] - '0';
}
printf("\n");
//将刚刚转成的数组 逆序放入两个链表head1,head2
head1 = creat(aa, e);
head2 = creat(bb, e);
p1 = head1, p2 = head2;
//加法!!!,是从后面开始加的,所以要逆序
for (i = 0; i < e; i++)
{
m = p1->data + p2->data;
if (m >= 10)
{
m = m - 10;
(p1->next)->data ++;
}
p3 = p4 = (struct string *)malloc(LEN);
p3->data = m;
cc[i] = m;
f = i;
if (i ==0)
{
head3 = p3;
}
else
{
p4->next = p3;
}
p4 = p3;
p1 = p1->next;
p2 = p2->next;
}
printf("加法的结果是: ");
for (i = f; i >= 0 ; i--)
{
printf("%d", cc[i]);
}
printf("\n");
//乘法
int increase = 0;
i = 0;
j = 0;
int temp = 0;
int add = 0;
for(i = 0; i < MAX - 1; i++)
{
dd[i]= 0 ;
}
p1 = head1;
for (i = 0; i < l1; i++)
{
increase = 0;
p2 = head2;
for (j = 0; j < l2; j++)
{
temp = (p1->data) * (p2->data);
add = (temp + increase + dd[i + j]) % 10;
increase = (temp + increase + dd[i+j]) / 10;
dd[i+j] = add;
if(j == l2 - 1 && increase > 0)
{
dd[i+j+1] = increase;
}
p2 = p2->next;
}
p1 = p1->next;
}
reverse(dd, MAX - 1);
int writeflag = 0;
printf("\n相乘的结果是:");
for(i = 0, n = 0; i < MAX-1; i++)
{
if(dd[i] != 0)
writeflag=1;
if(writeflag == 1)
{
printf("%d",dd[i]);
n++;
}
}
printf("\n");
getchar();
head4 = creat(dd,n);
}