ellapt

T9.8.AddBigNumbers

Jan 19th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. using System;
  2.  
  3. class AddBigNumbers
  4. {
  5. static void AddBigInt(int c1, int c2, string[][] bigIntArray)
  6. {
  7. int max=Math.Max(c1,c2);
  8. int min=Math.Min(c1,c2);
  9. int temp = 0;
  10. int carry = 0;
  11.  
  12. for (int i = 0; i < min; i++)
  13. {
  14. temp=Convert.ToInt32(bigIntArray[0][i])+Convert.ToInt32(bigIntArray[1][i])+carry;
  15. carry=temp/10;
  16. bigIntArray[2][i]=(temp%10).ToString();
  17. }
  18. if (c1 == c2 && carry != 0)
  19. {
  20. bigIntArray[2][min] = "1";
  21. carry = 0;
  22. }
  23. else
  24. {
  25. if (c1 == max)
  26. {
  27. for (int i = min; i <= max; i++)
  28. {
  29. temp = Convert.ToInt32(bigIntArray[0][i]) + carry;
  30. carry = temp / 10;
  31. bigIntArray[2][i] = (temp % 10).ToString();
  32. }
  33. }
  34. else if (c2 == max)
  35. {
  36. for (int i = min; i <= max; i++)
  37. {
  38. temp = Convert.ToInt32(bigIntArray[1][i]) + carry;
  39. carry = temp / 10;
  40. bigIntArray[2][i] = (temp % 10).ToString();
  41. }
  42. }
  43. }
  44. return;
  45. }
  46.  
  47. static void Main()
  48. {
  49. Console.WriteLine("Add two positive integer numbers as arrays of digits \n(each arr[i] contains a digit, i<10 000, last digit in arr[0]).");
  50.  
  51. Console.Write("Enter first positive integer number with up to 10 000 digits: ");
  52. int num1=int.Parse(Console.ReadLine());
  53. Console.Write("Enter second positive integer number with up to 10 000 digits: ");
  54. int num2=int.Parse(Console.ReadLine());
  55.  
  56. // Allocate a jagged array with aray1,array2 and result, each with 1 more element for keeping the carry
  57.  
  58. string[][] jagged = new string [3][];
  59.  
  60. int n1=0; // convert first number to the first row of the string array
  61. int work=num1;
  62. string[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  63. while (work>0)
  64. {
  65. n1++;
  66. work=work/10;
  67. }
  68. jagged[0] = new string[n1 + 1]; // first number row
  69. work = num1;
  70. for (int i = 0; i < n1; i++)
  71. {
  72. jagged[0][i] = digits[work%10];
  73. work = work / 10;
  74. }
  75. jagged[0][n1]="0";
  76.  
  77. int n2=0; // convert second number to the second row of the string array
  78. work=num2;
  79. while (work > 0)
  80. {
  81. n2++;
  82. work = work / 10;
  83. }
  84. jagged[1] = new string[n2 + 1]; // first number row
  85. work = num2;
  86. for (int i = 0; i < n2; i++)
  87. {
  88. jagged[1][i] = digits[work % 10];
  89. work = work / 10;
  90. }
  91. jagged[1][n2]="0";
  92.  
  93. int max = Math.Max(n1,n2);
  94. int min= Math.Min(n1,n2);
  95.  
  96. jagged[2] = new string[max + 1]; // third (result) row
  97.  
  98. //*************** Call the method to sum the big integers' arrays **************************
  99.  
  100. AddBigInt(n1, n2, jagged); // call the method to sum the arrays
  101.  
  102. //******* Print both input arrays and the result (arrays reversed, represented as integer numbers) **********
  103.  
  104. Console.WriteLine("The result is (reversed):");
  105. Console.WriteLine();
  106.  
  107. Array.Reverse(jagged[0], 0, n1);
  108. for (int i = 0; i < n1; i++)
  109. {
  110. Console.Write(jagged[0][i]);
  111. }
  112. Console.Write(" + ");
  113.  
  114. Array.Reverse(jagged[1], 0, n2);
  115. for (int i = 0; i < n2; i++)
  116. {
  117. Console.Write(jagged[1][i]);
  118. }
  119. Console.Write(" = ");
  120.  
  121. Array.Reverse(jagged[2], 0, max+1);
  122.  
  123. if (jagged[2][0] != "0")
  124. {
  125. Console.Write(jagged[2][0]);
  126. }
  127. for (int i = 1; i <= max; i++)
  128. {
  129. Console.Write(jagged[2][i]);
  130. }
  131. Console.WriteLine();
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment