Super Large multiplication Program
Last Update:2018-12-03
Source: Internet
Author: User
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <cassert>
# Include <cstdlib>
Typedef int et;
Typedef struct node {
Et data;
Struct node * P;
Struct node * N;
} Node, * nodeh;
Typedef struct list {
Nodeh head;
Nodeh end;
Int length;
} List, * listh;
////////////////////////
////////////////////////
Inline bool initlist (listh & L)
{
// Process the header node l
L = (listh) malloc (sizeof (list ));
Assert (L );
L-> head = L-> end = NULL;
L-> length = 0;
Return true;
}
Inline nodeh nmalloc (const et & ELEM)
{
// Return a new node whose data is ELEM
Nodeh temp = (nodeh) malloc (sizeof (node ));
Assert (temp );
Temp-> DATA = ELEM;
Return temp;
}
Inline bool preadd (listh & L, const et & ELEM)
{
// Add a new ELEM node to the header
Nodeh temp = nmalloc (ELEM );
++ (L-> length );
If (null = L-> head & null = L-> end ){
Temp-> N = temp-> P = NULL;
L-> head = L-> end = temp;
Return true;
}
Temp-> P = NULL;
Temp-> N = L-> head;
L-> head-> P = temp;
L-> head = temp;
Return true;
}
Inline bool endadd (listh & L, const et & ELEM)
{
// Add a new ELEM node to the end
Nodeh temp = nmalloc (ELEM );
++ (L-> length );
If (null = L-> head & null = L-> end ){
Temp-> N = temp-> P = NULL;
L-> head = L-> end = temp;
Return true;
}
Temp-> N = NULL;
Temp-> P = L-> end;
L-> end-> N = temp;
L-> end = temp;
Return true;
}
Inline bool destorylist (listh & L)
{
// Release table L
Nodeh temp = L-> head, it;
If (temp = NULL ){
Free (L );
Return true;
}
While (temp! = L-> end ){
It = temp;
Temp = temp-> N;
Free (it );
}
Free (temp );
Free (L );
Return true;
}
Inline void printlist (listh & L)
{
// Print
Nodeh it = L-> head, end = L-> end;
If (IT = NULL) return;
For (; it! = End; it = it-> N)
Putchar (IT-> Data + '0 ');
Putchar (IT-> Data + '0 ');
}
Inline bool addzero (listh & L, int count)
{
// Add count 0 at the end of L
While (count --) endadd (L, 0 );
Return true;
}
Inline bool bitmul (listh & L, const int number, listh & num)
{
// Store the result after multiplying L and number in num
// Num should be an empty table structure !!!!!!!!!
Nodeh l_end = L-> end;
Int status = 0, temp;
For (; l_end! = L-> head; l_end = l_end-> P ){
Temp = l_end-> data * Number + status;
Preadd (Num, TEMP % 10 );
Status = temp/10;
}
Temp = L-> head-> data * Number + status;
If (temp> 10 ){
Preadd (Num, TEMP % 10 );
Preadd (Num, temp/10 );
}
Else preadd (Num, temp );
// One more time :)
Return true;
}
Inline bool add (const listh & la, listh & Lb)
{
// LB = La + lB
Nodeh Pa = La-> end, Pb = LB-> end;
Int status = 0;
For (; pa! = La-> head; Pa = pa-> P, Pb = Pb-> P ){
If (PB = NULL ){
Preadd (LB, 0 );
PB = LB-> head;
}
Int sum = pa-> Data + Pb-> Data + status;
If (sum> = 10 ){
Pb-> DATA = sum % 10;
Status = 1;
}
Else {
Pb-> DATA = sum;
Status = 0;
}
}
If (PB = NULL ){
Preadd (LB, 0 );
PB = LB-> head;
}
Int sum = pa-> Data + Pb-> Data + status;
If (sum> = 10 ){
Pb-> DATA = sum % 10;
If (Pb-> P = NULL ){
Preadd (LB, 0 );
PB = LB-> head;
Pb-> DATA = sum/10;
Return true;
}
PB = Pb-> P;
If (PB = NULL ){
Preadd (LB, 0 );
PB = LB-> head;
}
Pb-> DATA = sum/10;
Return true;
}
Else // sum <10
Pb-> DATA = sum;
Return true;
}
//////////////////////////////////////// ///////////////////////////////////////
Inline void MUL (const char * a, const char * B)
{
// Init
Listh cs = NULL; // Multiplier
Listh BCS = NULL; // Multiplier
Listh num = NULL; // result
Assert (A & B );
Assert (initlist (Num ));
Assert (initlist (CS) & initlist (BCS ));
{
// Symbol processing
Int Sig = 1;
If ('-' = * ){
++;
SIG * =-1;
}
Else if ('+' = * A) ++;
If ('-' = * B ){
+ + B;
SIG * =-1;
}
Else if ('+' = * B) ++ B;
If (SIG <0) putchar ('-');
}
If (strcmp (a, B)> 0 ){
Const char * pA = A, * pb = B;
For (; '/0 '! = * PA; ++ Pa ){
Int temp = * pa-'0 ';
Assert (endadd (BCS, temp ));
}
For (; '/0 '! = * Pb; ++ Pb ){
Int temp = * pb-'0 ';
Assert (endadd (CS, temp ));
}
}
Else {
Const char * pA = A, * pb = B;
For (; '/0 '! = * PA; ++ Pa ){
Int temp = * pa-'0 ';
Assert (endadd (CS, temp ));
}
For (; '/0 '! = * Pb; ++ Pb ){
Int temp = * pb-'0 ';
Assert (endadd (BCS, temp ));
}
}
// Init end !!!!
Nodeh cs_p = cs-> end;
Int bit = 0; // number of digits
For (; cs_p! = Cs-> head; cs_p = cs_p-> P ){
Listh temp;
Initlist (temp );
Bitmul (BCS, cs_p-> data, temp );
//////////////////////////////////////// ///////////////////////////////
Puts ("========================================== ===========================================/N ");
Printlist (BCS ); //////////////////////////////////////// ///////
Printf ("X % d =", cs_p-> data );
Printlist (temp );
//////////////////////////////////////// ///////////////////////////////
// <================================== Here !!!!!! !!!!!!!!!!!!!!!!!!!!!!!!
Addzero (temp, bit );
Add (temp, num );
/////////////////
Puts ("/nnum :");
Printlist (Num );
Printf ("/nbit: % d/N", bit );
//////////////////////////////////////// ////////////////////////////
++ Bit;
Destorylist (temp );
}
Listh temp;
Initlist (temp );
Bitmul (BCS, cs_p-> data, temp );
//////////////////////////////////////// ///////////////////////////////
Puts ("========================================== ===========================================/N ");
Printlist (BCS ); //////////////////////////////////////// ///////
Printf ("X % d =", cs_p-> data );
Printlist (temp );
//////////////////////////////////////// ///////////////////////////////
// <================================== Here !!!!!! !!!!!!!!!!!!!!!!!!!!!!!!
Addzero (temp, bit );
Add (temp, num );
/////////////////
Puts ("/nnum :");
Printlist (Num );
Printf ("/nbit: % d/N", bit );
//////////////////////////////////////// ////////////////////////////
//////////////////////////////////////// ///////////////////////////////
STD: cout <'/N' <"result = ";
Printlist (Num );
Putchar ('/N ');
///////// Cleaning
Destorylist (temp );
Destorylist (BCS );
Destorylist (CS );
Destorylist (Num );
}
Int main (void)
{
STD: String A, B;
STD: CIN>;
STD: CIN> B;
Mul (A. c_str (), B. c_str ());
System ("pause ");
Return 0;
}