Advertisement
gguuppyy

АИСД, 1.4

Mar 14th, 2024 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. Program Lab1_4;
  2.  
  3. Uses
  4. System.SysUtils, windows;
  5.  
  6. Const
  7. DIGITS = ['0'..'9'];
  8. SERVICE_LENGTH_NUMBER = 3;
  9. SUBSCRIBER_LENGTH_NUMBER = 7;
  10.  
  11. Type
  12. TNumberString = String[SUBSCRIBER_LENGTH_NUMBER];
  13. BiPt = ^BiElem;
  14. BiElem = Record
  15. Data: TNumberString;
  16. Next: BiPt;
  17. Prev: BiPt;
  18. End;
  19. UniPt = ^UniElem;
  20. UniElem = Record
  21. Data: TNumberString;
  22. Next: UniPt;
  23. End;
  24.  
  25. Function IsValidNumber(Number: TNumberString) : Boolean;
  26. Var
  27. IsValid: Boolean;
  28. I: Integer;
  29. Begin
  30. IsValid := True;
  31. I := 1;
  32. While IsValid And (I <= Length(Number)) Do
  33. Begin
  34. IsValid := CharInSet(Number[I], DIGITS);
  35. Inc(I);
  36. End;
  37. IsValid := IsValid And ((Length(Number) = SERVICE_LENGTH_NUMBER) Or (Length(Number) = SUBSCRIBER_LENGTH_NUMBER));
  38. IsValidNumber := IsValid;
  39. End;
  40.  
  41. Procedure ReadNumber(Var BiListOfNumbers: BiPt; IsFirst: Boolean; MessageText: String);
  42. Var
  43. Number: BiPt;
  44. IsValid: Boolean;
  45. Begin
  46. Repeat
  47. IsValid := True;
  48. Number := BiListOfNumbers;
  49. ReadLn(Number^.Data);
  50. If IsFirst Or Not IsFirst And (Number^.Data <> '\0') Then
  51. Begin
  52. If IsValidNumber(Number^.Data) Then
  53. Begin
  54. New(BiListOfNumbers);
  55. Number^.Next := BiListOfNumbers;
  56. BiListOfNumbers^.Prev := Number;
  57. End
  58. Else
  59. Begin
  60. IsValid := False;
  61. WriteLn(MessageText);
  62. End;
  63. End;
  64. Until IsValid;
  65. End;
  66.  
  67. Procedure MakeBiListOfNumbers(Var BiListOfNumbers: BiPt);
  68. Begin
  69. WriteLn('Введите ', SERVICE_LENGTH_NUMBER, ' или ', SUBSCRIBER_LENGTH_NUMBER, ' значные числа (для завершения введите ''\0''):');
  70. ReadNumber(BiListOfNumbers, True, 'Вы ввели некорректное число!');
  71. Repeat
  72. ReadNumber(BiListOfNumbers, False, 'Неверное число!');
  73. Until BiListOfNumbers^.Data = '\0';
  74. BiListOfNumbers := BiListOfNumbers^.Prev;
  75. BiListOfNumbers^.Next := Nil;
  76. End;
  77.  
  78. Procedure WriteBiListOfNumbers(BiListOfNumbers: BiPt);
  79. Begin
  80. WriteLn(#13#10'Список введенных телефонных номеров: ');
  81. While BiListOfNumbers <> Nil Do
  82. Begin
  83. WriteLn(BiListOfNumbers^.Data);
  84. BiListOfNumbers := BiListOfNumbers^.Prev;
  85. End;
  86. End;
  87.  
  88. Procedure DeleteBiListOfNumbers(BiListOfNumbers: BiPt);
  89. Begin
  90. While BiListOfNumbers <> Nil Do
  91. Begin
  92. Dispose(BiListOfNumbers);
  93. BiListOfNumbers := BiListOfNumbers^.Prev;
  94. End;
  95. End;
  96.  
  97. Procedure MakeUniListOfSubscriberNumbers(UniListOfSubscriberNumbers: UniPt; BiListOfNumbers: BiPt);
  98. Begin
  99. While BiListOfNumbers <> Nil Do
  100. Begin
  101. If Length(BiListOfNumbers^.Data) = SUBSCRIBER_LENGTH_NUMBER Then
  102. Begin
  103. New(UniListOfSubscriberNumbers^.Next);
  104. UniListOfSubscriberNumbers := UniListOfSubscriberNumbers^.Next;
  105. UniListOfSubscriberNumbers^.Data := BiListOfNumbers^.Data;
  106. End;
  107. BiListOfNumbers := BiListOfNumbers^.Prev;
  108. End;
  109. UniListOfSubscriberNumbers^.Next := Nil;
  110. End;
  111.  
  112. Procedure SortUniListOfSubscriberNumbers(UniListOfSubscriberNumbers: UniPt);
  113. Var
  114. UniFirstNumber: UniPt;
  115. CountOfSubscriberNumbers, I, J: Integer;
  116. TempNember: TNumberString;
  117. Begin
  118. UniFirstNumber := UniListOfSubscriberNumbers^.Next;
  119. UniListOfSubscriberNumbers := UniFirstNumber;
  120. CountOfSubscriberNumbers := 0;
  121. While UniListOfSubscriberNumbers <> Nil Do
  122. Begin
  123. Inc(CountOfSubscriberNumbers);
  124. UniListOfSubscriberNumbers := UniListOfSubscriberNumbers^.Next;
  125. End;
  126. For I := 1 To CountOfSubscriberNumbers - 1 Do
  127. Begin
  128. UniListOfSubscriberNumbers := UniFirstNumber;
  129. For J := 1 To CountOfSubscriberNumbers - I Do
  130. Begin
  131. If StrToInt(String(UniListOfSubscriberNumbers^.Data)) > StrToInt(String(UniListOfSubscriberNumbers^.Next^.Data)) Then
  132. Begin
  133. TempNember := UniListOfSubscriberNumbers^.Data;
  134. UniListOfSubscriberNumbers^.Data := UniListOfSubscriberNumbers^.Next^.Data;
  135. UniListOfSubscriberNumbers^.Next^.Data := TempNember;
  136. End;
  137. UniListOfSubscriberNumbers := UniListOfSubscriberNumbers^.Next;
  138. End;
  139. End;
  140. End;
  141.  
  142. Procedure WriteUniListOfSubscriberNumbers(UniListOfSubscriberNumbers: UniPt);
  143. Begin
  144. WriteLn(#13#10'Отсортированный список абонентских номеров:');
  145. UniListOfSubscriberNumbers := UniListOfSubscriberNumbers^.Next;
  146. While UniListOfSubscriberNumbers <> Nil Do
  147. Begin
  148. WriteLn(UniListOfSubscriberNumbers^.Data);
  149. UniListOfSubscriberNumbers := UniListOfSubscriberNumbers^.Next;
  150. End;
  151. End;
  152.  
  153. Procedure DeleteUniListOfSubscriberNumbers(UniListOfSubscriberNumbers: UniPt);
  154. Begin
  155. While UniListOfSubscriberNumbers <> Nil Do
  156. Begin
  157. Dispose(UniListOfSubscriberNumbers);
  158. UniListOfSubscriberNumbers := UniListOfSubscriberNumbers^.Next;
  159. End;
  160. End;
  161.  
  162. Var
  163. BiListOfNumbers: BiPt;
  164. UniListOfSubscriberNumbers: UniPt;
  165.  
  166. Begin
  167. SetConsoleCP(1251);
  168. SetConsoleOutputCP(1251);
  169. New(BiListOfNumbers);
  170. New(UniListOfSubscriberNumbers);
  171. BiListOfNumbers^.Next := Nil;
  172. BiListOfNumbers^.Prev := Nil;
  173. UniListOfSubscriberNumbers^.Next := Nil;
  174.  
  175. MakeBiListOfNumbers(BiListOfNumbers);
  176. WriteBiListOfNumbers(BiListOfNumbers);
  177.  
  178. MakeUniListOfSubscriberNumbers(UniListOfSubscriberNumbers, BiListOfNumbers);
  179. SortUniListOfSubscriberNumbers(UniListOfSubscriberNumbers);
  180. WriteUniListOfSubscriberNumbers(UniListOfSubscriberNumbers);
  181.  
  182. DeleteBiListOfNumbers(BiListOfNumbers);
  183. DeleteUniListOfSubscriberNumbers(UniListOfSubscriberNumbers);
  184.  
  185. ReadLn;
  186. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement