TheBulgarianWolf

Letters Change Numbers (Operations)

Mar 21st, 2021
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace LettersChangeNumbers
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Enter your input here: ");
  12.             List<string> inputList = Console.ReadLine()
  13.                                     .Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
  14.                                     .ToList();
  15.             decimal sum = 0;
  16.  
  17.             foreach(var str in inputList)
  18.             {
  19.                 if(str.Length >= 3 && char.IsLetter(str[0]) && char.IsLetter(str.Last()))
  20.                 {
  21.                     long number = int.Parse(str.Substring(1, str.Length - 2));
  22.                     decimal currentResult = 0;
  23.                     if (char.IsUpper(str[0]))
  24.                     {
  25.                         int position = str[0] - 'A' + 1;
  26.                         currentResult += number / (decimal)position;
  27.                     }
  28.                     else
  29.                     {
  30.                         int position = str[0] - 'a' + 1;
  31.                         currentResult += number * (decimal)position;
  32.                     }
  33.  
  34.                     if (char.IsUpper(str.Last()))
  35.                     {
  36.                         int position = str.Last() - 'A' + 1;
  37.                         currentResult -= position;
  38.                     }
  39.                     else
  40.                     {
  41.                         int position = str.Last() - 'a' + 1;
  42.                         currentResult += position;
  43.                     }
  44.  
  45.                     sum += currentResult;
  46.                 }
  47.             }
  48.  
  49.             Console.WriteLine($"{sum:F2}");
  50.  
  51.         }
  52.     }
  53. }
  54.  
Add Comment
Please, Sign In to add comment