Advertisement
simonses

Method08

Jan 27th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _08.SumOfArraysInteger
  5. {
  6.     class SumOfArraysInteger
  7.     {
  8.         static int[] ArrayInput()
  9.         {
  10.             Console.WriteLine("Enter the length of the array: ");
  11.             int n = int.Parse(Console.ReadLine());
  12.  
  13.             int[] array = new int[n];
  14.  
  15.             //Reverse
  16.             for (int i = n - 1; i >= 0; i--)
  17.             {
  18.                 Console.Write("Enter the digit: ");
  19.                 array[i] = int.Parse(Console.ReadLine());
  20.             }
  21.             return array;
  22.         }
  23.  
  24.         static void PrintFormated(int[] array)
  25.         {
  26.             for (int i = array.Length - 1; i >= 0; i--)
  27.             {
  28.                 Console.Write(array[i]);
  29.             }
  30.         }
  31.  
  32.         static List<int> SumOfIntegerArrays(int[] arrayX, int[] arrayY)
  33.         {
  34.             List<int> sumList = new List<int>();
  35.  
  36.             int currentSum = 0;
  37.             byte special = 0;
  38.  
  39.             int loopLengthMax = Math.Max(arrayX.Length, arrayY.Length);
  40.             int loopLengthMin = Math.Min(arrayX.Length, arrayY.Length);
  41.            
  42.             for (int i = 0; i < loopLengthMax; i++)
  43.             {
  44.                 if (i < loopLengthMin)
  45.                 {
  46.                     currentSum = (arrayX[i] + arrayY[i] + special);
  47.                     if (currentSum >= 10)
  48.                     {
  49.                         currentSum = currentSum % 10;
  50.                         special = 1;
  51.                     }
  52.                     else
  53.                     {
  54.                         special = 0;
  55.                     }
  56.                     sumList.Add(currentSum);
  57.                 }
  58.                 if (i >= loopLengthMin)
  59.                 {
  60.                     if (arrayX.Length > arrayY.Length)
  61.                     {
  62.                         currentSum = arrayX[i] + special;
  63.                         if (currentSum >= 10)
  64.                         {
  65.                             currentSum = currentSum % 10;
  66.                             special = 1;
  67.                         }
  68.                         else
  69.                         {
  70.                             special = 0;
  71.                         }
  72.                         sumList.Add(currentSum);
  73.                     }
  74.                     if (arrayX.Length < arrayY.Length)
  75.                     {
  76.                         currentSum = arrayY[i] + special;
  77.                         if (currentSum >= 10)
  78.                         {
  79.                             currentSum = currentSum % 10;
  80.                             special = 1;
  81.                         }
  82.                         else
  83.                         {
  84.                             special = 0;
  85.                         }
  86.                         sumList.Add(currentSum);
  87.                     }
  88.                 }
  89.             }
  90.             if (special > 0) sumList.Add(special);
  91.            
  92.             sumList.Reverse();
  93.             return sumList;
  94.         }
  95.  
  96.         static void Main()
  97.         {
  98.             int[] firstArray = ArrayInput();
  99.             int[] secondArray = ArrayInput();
  100.  
  101.             PrintFormated(firstArray);
  102.             Console.Write(" + ");
  103.             PrintFormated(secondArray);
  104.             Console.Write(" = ");
  105.  
  106.             foreach (var item in SumOfIntegerArrays(firstArray, secondArray))
  107.             {
  108.                 Console.Write(item);
  109.             }
  110.             Console.WriteLine();
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement