ZhivkoPetkov

Strings - Letters Change Numbers

Jun 18th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Numerics;
  6.  
  7. namespace C_
  8. {
  9.  
  10.  
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. var input = Console.ReadLine().Split(new char[] { ' ','\t'},StringSplitOptions.RemoveEmptyEntries).ToList();
  16.  
  17. decimal result = 0;
  18.  
  19. for (int i = 0; i < input.Count; i++)
  20. {
  21. string currentWord = input[i];
  22.  
  23. char firstChar = currentWord[0];
  24. char lastChar = currentWord[currentWord.Length - 1];
  25. var number = currentWord.Substring(1,currentWord.Length-2).ToString();
  26.  
  27. if (isUpper(firstChar))
  28. {
  29. result += decimal.Parse(number) / charPosition(firstChar);
  30. }
  31. else
  32. {
  33. result += decimal.Parse(number) * charPosition(firstChar);
  34. }
  35.  
  36. if (isUpper(lastChar))
  37. {
  38. result -= charPosition(lastChar);
  39. }
  40. else
  41. {
  42. result += charPosition(lastChar);
  43. }
  44. }
  45.  
  46. Console.WriteLine($"{result:F2}");
  47.  
  48. }
  49.  
  50. static bool isUpper (char currentChar)
  51. {
  52. int value = (int)currentChar;
  53.  
  54. if (value >= 65 && value <=90)
  55. {
  56. return true;
  57. }
  58.  
  59. return false;
  60. }
  61.  
  62. static int charPosition (char currentChar)
  63. {
  64. char[] alpha = "%ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
  65. int position = 0;
  66. for (int i = 0; i < alpha.Length; i++)
  67. {
  68. if (currentChar.ToString().ToUpper() == alpha[i].ToString())
  69. {
  70. position = i;
  71. break;
  72. }
  73. }
  74.  
  75. return position;
  76.  
  77. }
  78. }
  79. }
Add Comment
Please, Sign In to add comment