# 两个任意长度的长整数相乘（C语言、双向链表方法）

/*****************************************************************************

Prototype    : multiply
Description  : 两个任意长度的长整数相乘, 输出结果
Input Param  :
char * strMultiplierA  乘数A
char *  strMultiplierB  乘数B
Output       :
char * strRst            乘法结果
Return Value :
int                       0  正确
-1  异常
*****************************************************************************/
/*

Result = 乘数A *乘数B

Example1:

Example2:

*/

#include <stdio.h>
#include <malloc.h>

typedef struct NODE
{
struct NODE * pUp;
int shuzhi;
struct NODE * pNext;
}*pNode;
int multiply (char * strMultiplierA, char *  strMultiplierB, char * strRst) ;
pNode InitList(char * strMultiplier);
void multiplyA(pNode pHead1, pNode pFlag, int shuzhi );

int main(void)
{
char strMultiplierA[100] = "987654321987654321987654321";
char strMultiplierB[100] = "987654321987654321987654321";
char strRst[20] = "\0";
multiply (strMultiplierA,strMultiplierB,strRst);
puts(strRst);
return 0;
}

int multiply (char * strMultiplierA, char *  strMultiplierB, char * strRst)
{

/* 在这里实现功能 */
pNode p1,p2,TempNode,pFlag;
int carry;
char * pCh;

if(strMultiplierA == NULL || strMultiplierB == NULL || strRst == NULL)
return -1;

while(p2 != NULL)
{

//p2 = p2->pNext;则多一个10n，要补零
TempNode = (pNode)malloc(sizeof(NODE));
TempNode->shuzhi = 0;

while(p1->pNext!=NULL)
{
p1 = p1->pNext;
}
TempNode = (pNode)malloc(sizeof(NODE));
TempNode->shuzhi = 0;
TempNode->pNext = NULL;
TempNode->pUp = p1;
p1->pNext = TempNode;

p2 = p2->pNext;
}

//使p1指向最后一个节点
while(p1->pNext!=NULL)
{
p1 = p1->pNext;
}

//使p1指向最后一个非0节点
while((p1->shuzhi == 0) && (p1 != pHeadstrRst->pNext))
{
p1 = p1->pUp;
}

//赋值给strRst
pCh = strRst;
{
*pCh = p1->shuzhi + '0';
pCh++;
p1 = p1->pUp;
}
*pCh = '\0';

pCh = strRst;

return 0;
}

pNode InitList(char * strMultiplier)
{
/* 在这里实现功能 */
char * pCh;
pNode p,TempNode;

if(strMultiplier == NULL)
return NULL;

pCh = strMultiplier;
while(*pCh!='\0')
{
pCh++;
}
pCh--;

//头指针不使用，只用来作为链表头

while(pCh>=strMultiplier)
{
TempNode = (pNode)malloc(sizeof(NODE));
TempNode->shuzhi = *pCh - '0';
TempNode->pNext = NULL;
TempNode->pUp = p;
p->pNext = TempNode;
p = p->pNext;

pCh--;
}

}

void multiplyA(pNode pHead, pNode pFlag, int shuzhi )
{
pNode p, pTemp;
int carry,temp;

pTemp = pFlag;

carry = 0;
while(p != NULL)
{
temp = (p->shuzhi) * shuzhi + carry;

pTemp->shuzhi = temp % 10;

carry = temp / 10;

pTemp = pTemp->pNext;
p = p->pNext;
}
pTemp->shuzhi = carry;

}

{
int carry,temp;
pNode p1, p2, TempNode;

carry = 0;

while(p1!=NULL)
{
temp = p2->shuzhi + p1->shuzhi + carry;
p2->shuzhi = temp%10;
carry =  temp/10;

p2 = p2->pNext;
p1 = p1->pNext;
}

//printf("求和之后\n");
//printf("carry = %d\n",carry);

while(carry != 0)
{
p2->shuzhi = (p2->shuzhi + carry)%10;
carry =  (p2->shuzhi + carry)/10;

//若carry!=0,且到了最后一个节点了，就增加一个新节点
if(carry != 0 && p2->pNext == NULL)
{
TempNode = (pNode)malloc(sizeof(NODE));
TempNode->shuzhi = 0;
TempNode->pNext = NULL;
TempNode->pUp = p2;
p2->pNext = TempNode;
}

p2 = p2->pNext;
}

}

{

while(p != NULL)
{
TempNode = (pNode)malloc(sizeof(NODE));
TempNode->shuzhi = 0;
TempNode->pNext = NULL;
TempNode->pUp = pTemp;
pTemp->pNext = TempNode;
pTemp = pTemp->pNext;

p = p->pNext;
}

}

{
/* 在这里实现功能 */
pNode p;

while(p!=NULL)
{
printf("%d",p->shuzhi);
p = p->pNext;
}
printf("\n");
}