Precision computing-Addition
Syntax:Add (char a [], char B [], char s []);
Parameters:
A []: Multiplier, represented by a string, with unlimited digits
B []: Multiplier, represented by a string, with unlimited digits
T []: Result, represented by a string
Return Value:Null
Note:
The space complexity isO (N ^ 2)
YesString. h
SourceProgram:
Void add (char a [], char B [], char back [])
{
Int I, J, K, up, X, Y, Z, L;
Char * C;
If (strlen (a)> strlen (B) L = strlen (A) + 2; else l = strlen (B) + 2;
C = (char *) malloc (L * sizeof (char ));
I = strlen (a)-1;
J = strlen (B)-1;
K = 0; up = 0;
While (I> = 0 | j> = 0)
{
If (I <0) x = '0'; else x = A [I];
If (j <0) y = '0'; else y = B [J];
Z = x-'0' + Y-'0 ';
If (up) Z + = 1;
If (z> 9) {up = 1; Z % = 10;} else up = 0;
C [k ++] = z + '0 ';
I --; j --;
}
If (up) C [k ++] = '1 ';
I = 0;
C [k] = '\ 0 ';
For (k-= 1; k> = 0; k --)
Back [I ++] = C [k];
Back [I] = '\ 0 ';
}
Void Add ( Char A [], Char B [], Char Back [])
{
Int I, J, K, up, X, Y, Z, L;
Char * C;
If (Strlen () > Strlen (B) L = Strlen () + 2 ; Else L = Strlen (B) + 2 ;
C = ( Char * ) Malloc (L * Sizeof ( Char ));
I = Strlen () - 1 ;
J = Strlen (B) - 1 ;
K = 0 ; Up = 0 ;
While (I > = 0 | J > = 0 )
{
If (I < 0 ) X = ' 0 ' ; Else X = A [I];
If (J < 0 ) Y = ' 0 ' ; Else Y = B [J];
Z = X - ' 0 ' + Y - ' 0 ' ;
If (Up) Z + = 1 ;
If (Z > 9 ) {Up = 1 ; Z % = 10 ;} Else Up = 0 ;
C [K ++ ] = Z + ' 0 ' ;
I -- ; J -- ;
}
If (Up) C [K ++ ] = ' 1 ' ;
I = 0 ;
C [k] = ' \ 0 ' ;
For (K -= 1 ; K > = 0 ; K -- )
Back [I ++ ] = C [k];
Back [I] = ' \ 0 ' ;
}