Advertisement
Guest User

Untitled

a guest
Oct 19th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace _08_LettersChangeNumbers
  6. {
  7.     class LettersChangeNumbers
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<char, byte> alphabet = new Dictionary<char, byte>();
  12.             byte position = 1;
  13.             for (byte i = 65; i <= 90; i++)
  14.             {
  15.                 alphabet.Add((char)i,position);
  16.                 position++;
  17.             }
  18.             decimal sum = 0.0m;
  19.             string pattern = @"([^\d]?)([\d]+)([^\d]?)";
  20.             Regex regex = new Regex(pattern);
  21.             string[] input = Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  22.  
  23.             for (int i = 0; i < input.Length; i++)
  24.             {
  25.                 Match matc = regex.Match(input[i]);
  26.                 if (matc.Groups[1].ToString() == matc.Groups[1].ToString().ToUpper())
  27.                 {
  28.                     decimal n = decimal.Parse(matc.Groups[2].ToString());
  29.                     sum += n / alphabet[char.Parse(matc.Groups[1].ToString().ToUpper())];
  30.                     if (matc.Groups[3].ToString() == matc.Groups[3].ToString().ToUpper())
  31.                     {
  32.                         sum -= alphabet[char.Parse(matc.Groups[3].ToString().ToUpper())];
  33.                     }
  34.                     else
  35.                     {
  36.                         sum += alphabet[char.Parse(matc.Groups[3].ToString().ToUpper())];
  37.                     }
  38.                 }
  39.                 else
  40.                 {
  41.                     decimal n = decimal.Parse(matc.Groups[2].ToString());
  42.                     sum += n * alphabet[char.Parse(matc.Groups[1].ToString().ToUpper())];
  43.                     if (matc.Groups[3].ToString() == matc.Groups[3].ToString().ToUpper())
  44.                     {
  45.                         sum -= alphabet[char.Parse(matc.Groups[3].ToString().ToUpper())];
  46.                     }
  47.                     else
  48.                     {
  49.                         sum += alphabet[char.Parse(matc.Groups[3].ToString().ToUpper())];
  50.                     }
  51.                 }
  52.             }
  53.             Console.WriteLine($"{sum:f2}");
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement