Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save bossxomlut/e1476a3c7e16845f5510735e84fbc3b7 to your computer and use it in GitHub Desktop.

Select an option

Save bossxomlut/e1476a3c7e16845f5510735e84fbc3b7 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void phepcong(short *A,short nA,short *B,short nB, short *&C, short &nC )
{
short lengtC=0;
short memoryNumber=0;
short SumNode;
if(nA==nB)
{
for(int i=nA;i>0;i--)
{
SumNode=A[i-1]+B[i-1]+memoryNumber;
A[i-1]=SumNode%10;
memoryNumber=SumNode/10;
}
}
// xử lý phép cộng ở trên gán giá trị kết quả
if(memoryNumber==0)
{
nC=nA;
C=(short*)malloc(nC*sizeof(short));
for(int i=0;i<nC;i++)
{
C[i]=A[i];
}
}
else
{
nC=nA+1;
C=(short*)malloc(nC*sizeof(short));
C[0]=memoryNumber;
for(int i=0;i<nA;i++)
{
C[i+1]=A[i];
}
}
}
void outputphepcong(short *C,int nC)
{
for(int i=0;i<nC;i++)
{
printf("%d",C[i]);
}
}
void main()
{
short A[]={1,1,9};
short B[]={1,8,2};
short *C;
short nC;
phepcong(A,3,B,3,C,nC);
printf("Ket qua la: \n");
outputphepcong(C,nC);
free(C);
_getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment