Red de conocimiento informático - Computadora portátil - ¿Cómo sumar dos números de decenas de dígitos en lenguaje C?

¿Cómo sumar dos números de decenas de dígitos en lenguaje C?

Utilice una matriz o una lista vinculada, implementada mediante el algoritmo de suma de números grandes

Enfoque estándar:

typedef struct

{

int * NumHead; //encabezado de matriz

int size;//tamaño de matriz

}BigNumber

const int BigBase = 1000000000;//Byte

const int PerBitLen = 9;

const int BigSize=sizeof(BigNumber);

//Inicializar BigNumber desde la cadena

int BaseChange(char * str ,int bajo,int arriba)

{

int i=arriba

int suma=0

for(; i >=bajo;--i)

{

if(str[i]=='0') continuar

if(str[i); ] <'0'||str[i]>'9')return -1

suma+=(str[i]-'0')*((int)pow((double)10; , (double)up-i));

}

retorno suma

}

BigNumber *Str_BigInit(char * str) )

{

int strlenth,bitlen;

int i=0,k=0;

Número Grande * Grande

p >

assert(str!=NULL);

mientras(*str++=='0');

strlenth=strlen(--str); >

bitlen=strlenth/PerBitLen;

if( strlenth%PerBitLen!=0)

++bitlen

Grande=(GranNúmero *) malloc (Tamaño grande);

Grande->NumHead=(int *)malloc(4*(bitlen));

Grande->tamaño=bitlen; - >NumHead! =NULL);

for(;bitlen>0;--bitlen,strlenth-=PerBitLen)

{

int max=strlenth-1; /p>

int min=strlenth-PerBitLen;

min=min<0?0:min

Grande->NumHead[bitlen-1]=BaseChange(str; ,mín,máx);

}

return(;bitlen>0;--bitlen,strlenth-=PerBitLen)

{

int max=strlenth-1;

int min=strlenth-PerBitLen;

min=min<0?

return Grande;

}

Número grande *add(Número grande*a,

BigNumber*b,int offset)//El desplazamiento se puede utilizar para calcular varias sumas, O(N)/*El desplazamiento es a, que es una operación de amplificación, solo escriba 0 para el desplazamiento de suma simple*/

{

int nsize=a-> tamaño;

char mark=-1

Número grande *resultado=(Número grande *)malloc(Tamaño grande)

if( desplazamiento==0)

{

if(a->tamaño>b->tamaño)

{

nsize=a->tamaño;

{

Número Grande *newb=(Número Grande *) malloc(Tamaño Grande

newb->NumHead=(int *)malloc(4*nsize);

newb-> tamaño=nsize; nsize -b->tamaño));

memcpy(newb->NumHead+nsize->tamaño,b->NumHead,4*b->tamaño

b=); newb;

}

marca=1;

}

else if(a->tamañotamaño)

{

nsize=b->tamaño;

{

Número Grande *newa=(Número Grande *)malloc( Tamaño Grande); /p>

newa->NumHead=(int *)malloc(4*nsize);

newa->size=nsize

memset(newa->NumHead, 0,4*(nsize-b-> tamaño));

memcpy(newa->NumHead+nsize-a->size,a->NumHead,a->size*4); p> p>

a=newa;

}

marca=0

}

}

;

else

{

if(a->tamaño+desplazamiento>b->tamaño)

{

nsize= a- >tamaño+desplazamiento;

{

Número Grande * newa=(Número Grande *)malloc(Tamaño Grande

Número Grande *newb=(Número Grande *) malloc( Tamaño grande);

newb->NumHead=(int *)malloc(4*nsize);

newb->tamaño=nsize;

memset(newb->NumHead,0,4*(nsize-b->tamaño));

memcpy(newb->NumHead+nsize-b->tamaño,b ->NumHead ,4 *b->tamaño);

b=newb;

newa->NumHead=(int *)malloc(4*nsize);<

/p>

newa->tamaño=nsize;

memcet( newa->NumHead+a->tamaño,0,4*offset;

memcpy(newa-); >NumHead,a->NumHead,a->tamaño*4);

a=newa

}

marca=2;

}

else if(a->tamaño+desplazamiento<=b->tamaño)

{

nsize=b->tamaño; /p>

{

Número Grande *newa=( Número Grande *)malloc(Tamaño Grande

newa->NumHead=(int *)malloc(4*nsize);

newa->tamaño=nsize;

memset(newa->NumHead ,0,4*nsize);

memcpy(newa->NumHead+nsize); -a->tamaño-offset,a->NumHead,a->tamaño*4

a=newa

}

mark=0);

}

}

resultado->NumHead=(int *)malloc(4*nsize); tamaño=ntamaño;

while(--nsize>=0)

resultado->NumHead[nsize]=a->NumHead[nsize]+b->NumHead[nsize] ];

if(marca==0)

{

libre(a->NumHead

gratis(a)

}

else if(mark==1)

{

gratis(b->NumHead

gratis(b);

}

.

else if(mark==2)

{

gratis(a->NumHead);

gratis(b->NumHead);

gratis(a);

}

return(a->NumHead).

devuelve resultado; /////// //////////////////////////////////////////// /// /////////////////////////////

void BigPrint(BigNumber * resultado,ARCHIVO *fp)

{

int i=0;

assert(result!=NULL&&result->NumHead! =NULL);

mientras(resultado->NumHead[i++]==0);

fprintf(fp,"% d",resultado->NumHead[i++]);

for(;itamaño;++i)

for(;itamaño;++i)

p>

{

fprintf(fp,"%09d",resultado->NumHead[i]

}

BigNumber *b=); Str_Init("123456789876543412351725371253712537152371523");

BigNumber *c=add(a,b,0);

BigPrint(c, stdout); ;

}