Created
May 21, 2018 10:12
-
-
Save bossxomlut/a04e5a65739dac59bd498630ea3d29b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<conio.h> | |
| #include<malloc.h> | |
| #include<math.h> | |
| //---------------* KHAI BÁO HÀM *------------------------------------// | |
| void addfirt(short *&C, short &lenghtC, short k); | |
| void outputphepcong(short *C, short lenghtC); | |
| //---------------* KHAI BÁO HÀM *------------------------------------// | |
| void phepcong(short *A, short lenghtA, short *B, short lenghtB, short *&C, short &lenghtC) | |
| { | |
| short memoryNumber = 0; | |
| short SumNode; | |
| short mark = 1; | |
| if ((A[0] > 0 && B[0] > 0) || (A[0] < 0 && B[0]<0)) | |
| { | |
| if (A[0]<0) | |
| { | |
| mark = -1; | |
| } | |
| A[0] = abs(A[0]); | |
| B[0] = abs(B[0]); | |
| if (lenghtA == lenghtB) | |
| { | |
| for (short i = lenghtA; i>0; i--) | |
| { | |
| SumNode = A[i - 1] + B[i - 1] + memoryNumber; | |
| addfirt(C, lenghtC, SumNode % 10); | |
| memoryNumber = SumNode / 10; | |
| } | |
| if (memoryNumber != 0) | |
| { | |
| addfirt(C, lenghtC, memoryNumber); | |
| } | |
| } | |
| else | |
| { | |
| if (lenghtA < lenghtB) | |
| { | |
| short *tempt = A; | |
| A = B; | |
| B = tempt; | |
| short temptlengt = lenghtA; | |
| lenghtA = lenghtB; | |
| lenghtB = temptlengt; | |
| } | |
| short khoangcachAB = lenghtA - lenghtB; | |
| for (short i = lenghtB; i > 0; i--) | |
| { | |
| SumNode = A[i + khoangcachAB - 1] + B[i - 1] + memoryNumber; | |
| addfirt(C, lenghtC, SumNode % 10); | |
| memoryNumber = SumNode / 10; | |
| } | |
| for (short i = khoangcachAB; i > 0; i--) | |
| { | |
| SumNode = A[i - 1] + memoryNumber; | |
| addfirt(C, lenghtC, SumNode % 10); | |
| memoryNumber = SumNode / 10; | |
| } | |
| if (memoryNumber != 0) | |
| { | |
| addfirt(C, lenghtC, memoryNumber); | |
| } | |
| } | |
| C[0] = C[0] * mark; | |
| } | |
| else | |
| { | |
| // còn trường hợp (1 dương 1 âm) | |
| } | |
| } | |
| void main() | |
| { | |
| short A[] = { -1, 0, 0, 0 }; | |
| short B[] = { -1, 0, 0, 0, 9}; | |
| short *C = NULL; | |
| short lenghtA = sizeof(A) / sizeof(short); | |
| short lenghtB = sizeof(B) / sizeof(short); | |
| short lenghtC=0; | |
| phepcong(A,lenghtA, B, lenghtB, C, lenghtC); | |
| printf("Ket qua la: \n"); | |
| outputphepcong(C, lenghtC); | |
| free(C); | |
| _getch(); | |
| } | |
| void addfirt(short *&C, short &lenghtC, short k) | |
| { | |
| lenghtC++; | |
| C = (short*)realloc(C, lenghtC*sizeof(short)); | |
| if (lenghtC > 1) | |
| { | |
| for (short i = lenghtC - 1; i >0; i--) | |
| { | |
| C[i] = C[i - 1]; | |
| } | |
| } | |
| C[0] = k; | |
| } | |
| void outputphepcong(short *C, short lenghtC) | |
| { | |
| for (short i = 0; i<lenghtC; i++) | |
| { | |
| printf("%d", C[i]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment