elena1234

Reverse String and Count sum whithout Parse to int

May 1st, 2021 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace SumOfTwoStrings
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             TestSum();
  12.         }
  13.  
  14.         private static void TestSum()
  15.         {
  16.             string num1 = "123456";
  17.             string num2 = "12";
  18.             string result = CalculateSumWithoutParsing(num1, num2);
  19.             Console.WriteLine(result);
  20.  
  21.             string num3 = "12";
  22.             string num4 = "123456";
  23.             string secondResult = CalculateSumWithoutParsing(num3, num4);
  24.             Console.WriteLine(secondResult);
  25.  
  26.             string num5 = "123456";
  27.             string num6 = "123456";
  28.             string thirdResult = CalculateSumWithoutParsing(num5, num6);
  29.             Console.WriteLine(thirdResult);
  30.  
  31.             string num7 = "123454";
  32.             string num8 = "123454";
  33.             string fourthResult = CalculateSumWithoutParsing(num7, num8);
  34.             Console.WriteLine(fourthResult);
  35.  
  36.             string num9 = "0";
  37.             string num10 = "0";
  38.             string fifthResult = CalculateSumWithoutParsing(num9, num10);
  39.             Console.WriteLine(fifthResult);
  40.         }
  41.  
  42.         private static string CalculateSumWithoutParsing(string num1, string num2)
  43.         {
  44.             var firstStack = new Stack<char>(num1);
  45.             var secondStack = new Stack<char>(num2);
  46.             string result = string.Empty;
  47.             int carry = 0;
  48.             while (firstStack.Count > 0 && secondStack.Count > 0)
  49.             {
  50.                 char currentDigitFromFirstStack = firstStack.Pop();
  51.                 char currentDigitFromSecondStack = secondStack.Pop();
  52.                 int currentSum = int.Parse(currentDigitFromFirstStack.ToString()) + int.Parse(currentDigitFromSecondStack.ToString());
  53.  
  54.                 int newCurrentResult = currentSum + carry; // if already have carry added it
  55.                 carry = 0;
  56.                 if (newCurrentResult / 10 > 0) // check for the carry after newCurrentResult
  57.                 {
  58.                     carry += newCurrentResult / 10;
  59.                 }
  60.  
  61.                 result += (newCurrentResult % 10).ToString();
  62.             }
  63.  
  64.             string finalResult = string.Empty;
  65.             if (firstStack.Count == 0 && secondStack.Count > 0)
  66.             {
  67.                 finalResult = GenereteFinalResult(secondStack, result, carry);
  68.             }
  69.             else if (secondStack.Count == 0 && firstStack.Count > 0)
  70.             {
  71.                 finalResult = GenereteFinalResult(firstStack, result, carry);
  72.             }
  73.             else // the count of the both stecks is equal
  74.             {
  75.                 finalResult = new String(result.ToCharArray().Reverse().ToArray());
  76.             }
  77.  
  78.             return finalResult.Trim();
  79.         }
  80.  
  81.         private static string GenereteFinalResult(Stack<char> secondStack, string result, int carry)
  82.         {
  83.             string finalResult;
  84.             char currentDigitFromSecondStack = secondStack.Pop();
  85.             int newCurrentResult = int.Parse(currentDigitFromSecondStack.ToString()) + carry;
  86.             string temporaryResult = result + newCurrentResult;
  87.             string reverseTemporaryResult = new String(temporaryResult.ToCharArray().Reverse().ToArray());
  88.             finalResult = string.Join("", secondStack.ToArray().Reverse()) + reverseTemporaryResult; // how to reverse string in a one line
  89.             return finalResult;
  90.         }
  91.     }
  92. }
  93.  
  94.  
Add Comment
Please, Sign In to add comment