Advertisement
denislava

RealMess1

Jan 3rd, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _08.AddNumbersInCharArrays
  8. {
  9. class Program
  10. {
  11. /*
  12. Write a method that adds two positive integer numbers
  13. represented as arrays of digits
  14. (each array element arr[i] contains a digit;
  15. the last digit is kept in arr[0]).
  16. Each of the numbers that will be added could have up to 10 000 digits.
  17. */
  18.  
  19. static void Main()
  20. {
  21. char [] firstNum = new char [3]{'9', '9', '9'};
  22. char[] secondNum = new char[2] { '9', '9' };
  23. Array.Reverse(firstNum);
  24. Array.Reverse(secondNum);
  25. int[] newArray = addNumbers(firstNum, secondNum);
  26. foreach (var charche in newArray)
  27. {
  28. Console.Write(charche + " ");
  29. }
  30. }
  31.  
  32. static int[] addNumbers(char[] fisrtNum, char[] secondNum)
  33. {
  34. if (fisrtNum.Length > secondNum.Length)
  35. {
  36. int[] result = new int[fisrtNum.Length + 1];
  37. bool inMind = false;
  38. for (int i = 0; i < secondNum.Length; i++)
  39. {
  40.  
  41. if (inMind == false)
  42. {
  43. if ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48 > 9)
  44. {
  45. inMind = true;
  46. result[i] = ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48) - 10;
  47. }
  48. else
  49. {
  50. inMind = false;
  51. result[i] = ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48);
  52. }
  53. }
  54. else
  55. {
  56. if ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48 + 1 > 9)
  57. {
  58. inMind = true;
  59. result[i] = ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48) + 1 - 10;
  60. }
  61. else
  62. {
  63. inMind = false;
  64. result[i] = ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48 + 1);
  65. }
  66. }
  67.  
  68.  
  69. }
  70. return result;
  71. }
  72. else
  73. {
  74. int[] nullArr = new int[0];
  75. return nullArr;
  76. }
  77. }
  78.  
  79.  
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement