Guest User

Untitled

a guest
May 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4. #include<math.h>
  5. //---------------* KHAI BÁO HÀM *------------------------------------//
  6.  
  7. void addfirt(short *&C, short &lenghtC, short k);
  8. void outputphepcong(short *C, short lenghtC);
  9.  
  10. //---------------* KHAI BÁO HÀM *------------------------------------//
  11. void phepcong(short *A, short lenghtA, short *B, short lenghtB, short *&C, short &lenghtC)
  12. {
  13. short memoryNumber = 0;
  14. short SumNode;
  15. short mark = 1;
  16. if ((A[0] > 0 && B[0] > 0) || (A[0] < 0 && B[0]<0))
  17. {
  18.  
  19. if (A[0]<0)
  20. {
  21. mark = -1;
  22. }
  23. A[0] = abs(A[0]);
  24. B[0] = abs(B[0]);
  25. if (lenghtA == lenghtB)
  26. {
  27. for (short i = lenghtA; i>0; i--)
  28. {
  29. SumNode = A[i - 1] + B[i - 1] + memoryNumber;
  30. addfirt(C, lenghtC, SumNode % 10);
  31. memoryNumber = SumNode / 10;
  32. }
  33.  
  34. if (memoryNumber != 0)
  35. {
  36. addfirt(C, lenghtC, memoryNumber);
  37. }
  38. }
  39. else
  40. {
  41. if (lenghtA < lenghtB)
  42. {
  43. short *tempt = A;
  44. A = B;
  45. B = tempt;
  46. short temptlengt = lenghtA;
  47. lenghtA = lenghtB;
  48. lenghtB = temptlengt;
  49. }
  50. short khoangcachAB = lenghtA - lenghtB;
  51. for (short i = lenghtB; i > 0; i--)
  52. {
  53. SumNode = A[i + khoangcachAB - 1] + B[i - 1] + memoryNumber;
  54. addfirt(C, lenghtC, SumNode % 10);
  55. memoryNumber = SumNode / 10;
  56. }
  57. for (short i = khoangcachAB; i > 0; i--)
  58. {
  59. SumNode = A[i - 1] + memoryNumber;
  60. addfirt(C, lenghtC, SumNode % 10);
  61. memoryNumber = SumNode / 10;
  62. }
  63. if (memoryNumber != 0)
  64. {
  65. addfirt(C, lenghtC, memoryNumber);
  66. }
  67. }
  68. C[0] = C[0] * mark;
  69. }
  70. else
  71. {
  72. // còn trường hợp (1 dương 1 âm)
  73. }
  74.  
  75. }
  76.  
  77. void main()
  78. {
  79. short A[] = { -1, 0, 0, 0 };
  80. short B[] = { -1, 0, 0, 0, 9};
  81. short *C = NULL;
  82. short lenghtA = sizeof(A) / sizeof(short);
  83. short lenghtB = sizeof(B) / sizeof(short);
  84. short lenghtC=0;
  85. phepcong(A,lenghtA, B, lenghtB, C, lenghtC);
  86. printf("Ket qua la: \n");
  87. outputphepcong(C, lenghtC);
  88.  
  89. free(C);
  90. _getch();
  91. }
  92. void addfirt(short *&C, short &lenghtC, short k)
  93. {
  94. lenghtC++;
  95. C = (short*)realloc(C, lenghtC*sizeof(short));
  96. if (lenghtC > 1)
  97. {
  98. for (short i = lenghtC - 1; i >0; i--)
  99. {
  100. C[i] = C[i - 1];
  101. }
  102. }
  103. C[0] = k;
  104. }
  105. void outputphepcong(short *C, short lenghtC)
  106. {
  107.  
  108. for (short i = 0; i<lenghtC; i++)
  109. {
  110. printf("%d", C[i]);
  111. }
  112. }
Add Comment
Please, Sign In to add comment