Advertisement
AnhVan1712

Tính tổng 2 số nguyên lớn

Apr 2nd, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define SIZE 100
  4. void chen (char S[], int k);
  5. void chuanHoa (char S1[], char S2[]);
  6. void tinhTong(char S1[], char S2[], char S[]);
  7. void main()
  8. {
  9. char S1[SIZE], S2[SIZE], S[SIZE];
  10. printf ("Nhap lan luot 2 so !\n");
  11. gets(S1);
  12. gets(S2);
  13. chuanHoa (S1,S2);
  14. tinhTong(S1, S2, S);
  15. puts(S);
  16. }
  17. void chen (char S[], int k)
  18. {
  19. strrev (S);
  20. int l=strlen(S);
  21. for (int i=0;i<k;i++)
  22. {
  23. S[l+i]='0';
  24. }
  25. S[l+k]='\0';
  26. strrev (S);
  27. }
  28. void chuanHoa (char S1[], char S2[])
  29. {
  30. int l1=strlen (S1);
  31. int l2=strlen (S2);
  32. if (l1>l2)
  33. {
  34. chen (S2,l1-l2);
  35. } else
  36. {
  37. chen (S1,l2-l1);
  38. }
  39. }
  40. void tinhTong(char S1[], char S2[], char S[])
  41. {
  42. //chuanHoa (S1,S2);
  43. strrev(S1);
  44. strrev(S2);
  45. int l = strlen(S1);
  46. int nho = 0;
  47. for (int i = 0; i < l; i++)
  48. {
  49. int t = (S1[i] - '0') + (S2[i] - '0') + nho;
  50. if (t < 10)
  51. {
  52. S[i] = t + '0';
  53. nho=0;
  54. } else
  55. {
  56. S[i] = t - 10 + '0';
  57. nho = 1;
  58. }
  59. }
  60. if (nho==1)
  61. {
  62. S[l++]='1';
  63. }
  64. S[l] = '\0';
  65. strrev(S);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement