Guest User

Untitled

a guest
May 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4. void phepcong(short *A,short nA,short *B,short nB, short *&C, short &nC )
  5. {
  6. short lengtC=0;
  7. short memoryNumber=0;
  8. short SumNode;
  9. if(nA==nB)
  10. {
  11. for(int i=nA;i>0;i--)
  12. {
  13. SumNode=A[i-1]+B[i-1]+memoryNumber;
  14. A[i-1]=SumNode%10;
  15. memoryNumber=SumNode/10;
  16. }
  17. }
  18.  
  19. // xử lý phép cộng ở trên gán giá trị kết quả
  20.  
  21. if(memoryNumber==0)
  22. {
  23. nC=nA;
  24. C=(short*)malloc(nC*sizeof(short));
  25. for(int i=0;i<nC;i++)
  26. {
  27. C[i]=A[i];
  28. }
  29. }
  30. else
  31. {
  32. nC=nA+1;
  33. C=(short*)malloc(nC*sizeof(short));
  34. C[0]=memoryNumber;
  35. for(int i=0;i<nA;i++)
  36. {
  37. C[i+1]=A[i];
  38.  
  39. }
  40. }
  41. }
  42. void outputphepcong(short *C,int nC)
  43. {
  44.  
  45. for(int i=0;i<nC;i++)
  46. {
  47. printf("%d",C[i]);
  48. }
  49. }
  50. void main()
  51. {
  52. short A[]={1,1,9};
  53. short B[]={1,8,2};
  54. short *C;
  55. short nC;
  56. phepcong(A,3,B,3,C,nC);
  57. printf("Ket qua la: \n");
  58. outputphepcong(C,nC);
  59. free(C);
  60. _getch();
  61. }
Add Comment
Please, Sign In to add comment