Class1.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections.ObjectModel;
  2. using System.Runtime.InteropServices;
  3. namespace testirovanie
  4. {
  5. public class REG_MARK_LIB
  6. {
  7. public static List<int> regions = new List<int>([52, 77]);
  8. public static List<String> serias = new List<string>(["А", "В", "Е", "К", "М", "Н", "О", "Р", "С", "Т", "У", "Х"]);
  9. public static Boolean CheckMark(String mark)
  10. {
  11. if (mark.Length == 0) return false;
  12. int number;
  13. try
  14. {
  15. number = int.Parse(mark.Substring(1, 3));
  16. } catch (FormatException)
  17. {
  18. return false;
  19. }
  20. String seria = String.Concat(mark[0], mark.Substring(4, 2));
  21. int region = int.Parse(mark.Substring(6));
  22. Console.WriteLine(number);
  23. Console.WriteLine(seria);
  24. Console.WriteLine(region);
  25. if (!regions.Contains(region)) return false;
  26. if (!(serias.Contains(seria.Substring(0, 1)) && serias.Contains(seria.Substring(1, 1)) && serias.Contains(seria.Substring(2, 1)))) return false;
  27. return true;
  28. }
  29. private static String GetNextLetter(String letter)
  30. {
  31. String ans = String.Empty;
  32. if (!serias.Contains(letter)) return ans;
  33. int index = serias.IndexOf(letter);
  34. if (index == serias.Count - 1) return ans;
  35. return serias[index + 1];
  36. }
  37. public static String GetNextMarkAfter(String mark)
  38. {
  39. String res = String.Empty;
  40. if (!CheckMark(mark)) return res;
  41. int number = int.Parse(mark.Substring(1, 3));
  42. String seria = String.Concat(mark[0], mark.Substring(4, 2));
  43. int region = int.Parse(mark.Substring(6));
  44. if (number + 1 >= 1000)
  45. {
  46. int index = 2;
  47. while (index != -1)
  48. {
  49. String next = GetNextLetter(seria[index].ToString());
  50. if (next != String.Empty)
  51. {
  52. seria = seria.Remove(index).Insert(index, next);
  53. break;
  54. } else
  55. {
  56. index--;
  57. }
  58. }
  59. } else
  60. {
  61. number++;
  62. }
  63. res += seria[0];
  64. res += number.ToString();
  65. res += seria.Substring(1, 2);
  66. res += region.ToString();
  67. return res;
  68. }
  69. public static String GetNextMarkAfterInRange(String prevMark, String rangeStart, String rangeEnd)
  70. {
  71. if (!(CheckMark(prevMark) && CheckMark(rangeStart) && CheckMark(rangeEnd)))
  72. {
  73. return "out of stock";
  74. }
  75. int numberStart = int.Parse(rangeStart.Substring(1, 3));
  76. String seriaStart = String.Concat(rangeStart[0], rangeStart.Substring(4, 2));
  77. int regionStart = int.Parse(rangeStart.Substring(6));
  78. int numberEnd = int.Parse(rangeEnd.Substring(1, 3));
  79. String seriaEnd = String.Concat(rangeEnd[0], rangeEnd.Substring(4, 2));
  80. int regionEnd = int.Parse(rangeEnd.Substring(6));
  81. int numberPrev = int.Parse(prevMark.Substring(1, 3));
  82. String seriaPrev = String.Concat(prevMark[0], prevMark.Substring(4, 2));
  83. int regionPrev = int.Parse(prevMark.Substring(6));
  84. int a = GetSeriaNumber(seriaPrev) * 1000 + numberPrev + 1;
  85. int b = GetSeriaNumber(seriaStart) * 1000 + numberStart + 1;
  86. int c = GetSeriaNumber(seriaEnd) * 1000 + numberEnd + 1;
  87. if (a >= b - 1 && a < c)
  88. {
  89. return GetNextMarkAfter(prevMark);
  90. }
  91. return "out of stock";
  92. }
  93. public static int GetSeriaNumber(String serial)
  94. {
  95. int rez = 0;
  96. int j = 0;
  97. for (int i = 2; i >= 0; i--, j++)
  98. {
  99. rez += (int) (GetLetterNumber(serial[i].ToString()) * Math.Pow(serias.Count(), j));
  100. }
  101. return rez;
  102. }
  103. private static int GetLetterNumber(String letter)
  104. {
  105. if (!serias.Contains(letter)) return 0;
  106. return serias.IndexOf(letter); ;
  107. }
  108. public static int GetCombinationsCountInRange(String mark1, String mark2)
  109. {
  110. if (!CheckMark(mark1) || !CheckMark(mark2)) return 0;
  111. int number1 = int.Parse(mark1.Substring(1, 3));
  112. String seria1 = String.Concat(mark1[0], mark1.Substring(4, 2));
  113. int region1 = int.Parse(mark1.Substring(6));
  114. int number2 = int.Parse(mark2.Substring(1, 3));
  115. String seria2 = String.Concat(mark2[0], mark2.Substring(4, 2));
  116. int region2 = int.Parse(mark2.Substring(6));
  117. int a = GetSeriaNumber(seria1) * 1000 + number1 + 1;
  118. int b = GetSeriaNumber(seria2) * 1000 + number2 + 1;
  119. return Math.Abs(a - b);
  120. }
  121. }
  122. }