Advertisement
KrestininVladislav

Untitled

Nov 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. program FinalLaba3zad3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8. SysUtils;
  9.  
  10. const
  11. CMaxNameSize = 30;
  12. CMaxNumPart = 15;
  13. CMinNumPart = 2;
  14. CMinScore = 0;
  15. CMaxScore = 1000;
  16. type
  17. TGrade = Integer;
  18. TRec = record
  19. ParticipantName : string[CMaxNameSize];
  20. Score : TGrade;
  21. end;
  22. TArrOfRec = array of TRec;
  23.  
  24. function GetUserAcception: Boolean;
  25. type
  26. TCharSet = Set of Char;
  27. var
  28. UserChoice: Char;
  29. UserCommandForAcception: TCharSet;
  30. UserCommandForRejection: TCharSet;
  31. begin
  32. repeat
  33. Write('Enter ''Y'' / 1 for acception or ''N'' / 0 for rejection: ');
  34. UserCommandForAcception := ['Y','y','1'];
  35. UserCommandForRejection := ['N','n','0'];
  36.  
  37.  
  38.  
  39. ReadLn(UserChoice);
  40. until UserChoice in (UserCommandForAcception + UserCommandForRejection);
  41. GetUserAcception := UserChoice in UserCommandForAcception;
  42. end;
  43.  
  44. function GetRightInputPath(): string;
  45. var
  46. PathToFile: string;
  47. FileIsAvailable: Boolean;
  48. TextFile: Text;
  49. begin
  50. Write('Enter path to input file: ');
  51. repeat
  52. FileIsAvailable := True;
  53. ReadLn(PathToFile);//C:\ucheba\Лабы\Win32\Debug\laba3zad3In.txt
  54. if not FileExists(PathToFile) then
  55. Write('File doesnt exists or specified path is wrong.Try again: ')
  56. else
  57. try
  58. Assign(TextFile, PathToFile);
  59. Reset(TextFile);
  60. Close(TextFile);
  61. except
  62. FileIsAvailable := False;
  63. Write('File is unavailable.Unclock it for reading or choose',
  64. 'another file: ');
  65. end;
  66. until FileExists(PathToFile) and FileIsAvailable;
  67. GetRightInputPath := PathToFile;
  68. end;
  69.  
  70. function GetValidNumPart(): Integer;
  71. var
  72. NumPart: Integer;
  73. NumPartIsCorr: Boolean;
  74. begin
  75. NumPart := 0;
  76. Write('Enter number of participants: ');
  77. repeat
  78. try
  79. NumPartIsCorr := True;
  80. Read(NumPart);
  81. if (NumPart > CMaxNumPart) or (NumPart < CMinNumPart) then
  82. begin
  83. NumPartIsCorr := False;
  84. Write('Number of participants can be from ', CMinNumPart,' to ',
  85. CMaxNumPart,'.Try again: ');
  86. end;
  87. except
  88. NumPartIsCorr := False;
  89. Write ('Enter integer number from ', CMinNumPart,' to ',
  90. CMaxNumPart,'.Try again: ');
  91. end;
  92. until NumPartIsCorr;
  93. GetValidNumPart := NumPart;
  94. end;
  95.  
  96. function InPutFromFile(PathToFile: string;
  97. var DataOfFileIsCorr: Boolean): TArrOfRec;
  98. var
  99. TextFile: Text;
  100. List: TArrOfRec;
  101. i, NumPart: Integer;
  102. TempCh: Char;
  103. DistChSet: set of Char;
  104. begin
  105. DistChSet := [':','|'];
  106. Write('Enter number of participants: ');
  107. NumPart := GetValidNumPart();
  108. SetLength(List, NumPart);
  109. repeat
  110. try
  111. DataOfFileIsCorr := True;
  112. Assign(TextFile, PathToFile);
  113. Reset(TextFile);
  114. for i := 0 to Length(List) - 1 do
  115. begin
  116. with List[i] do
  117. begin
  118. TempCh := #0;
  119. repeat
  120. ParticipantName := ParticipantName + TempCh;
  121. Read(TextFile, TempCh);
  122. until EOLn(TextFile) or (TempCh in DistChSet);
  123. while Length(ParticipantName) < CMaxNameSize do
  124. ParticipantName := ParticipantName + ' ';
  125. ReadLn(TextFile, Score);
  126. if (Score < CMinScore) or (Score > CMaxScore) then
  127. DataOfFileIsCorr := False;
  128. end;
  129. end;
  130. except
  131. WriteLn('File data is not valid!');
  132. DataOfFileIsCorr := False;
  133. end;
  134. until DataOfFileIsCorr;
  135. Close(TextFile);
  136. InPutFromFile := List;
  137. end;
  138.  
  139. function InPutConsole(): TArrOfRec;
  140. var
  141. NumOfPart: Integer;
  142. ListIsCorrect: Boolean;
  143. List: TArrOfRec;
  144. i: Integer;
  145. begin
  146. i := 0;
  147. NumOfPart := GetValidNumPart();
  148. SetLength(List, NumOfPart);
  149. repeat
  150. try
  151. ListIsCorrect := True;
  152. while i < Length(List) do
  153. begin
  154. with List[i] do
  155. begin
  156. Write('Enter name of ', i + 1,' participant: ');
  157.  
  158.  
  159. Flush(Input);
  160. ReadLn(ParticipantName);
  161. while Length(ParticipantName) < CMaxNameSize do
  162. ParticipantName := ParticipantName + ' ';
  163. Write('Enter score of ', i + 1,' participant: ');
  164. ReadLn(Score);
  165. Inc(i);
  166. end;
  167. end;
  168. except
  169. begin
  170. ListIsCorrect := False;
  171. WriteLn('You have entered not valid data. Try again!');
  172. end;
  173. end;
  174. until ListIsCorrect;
  175. InPutConsole := List;
  176. end;
  177.  
  178. function GetAvailableOutputPath(): string;
  179. var
  180. OutPutPath: string;
  181. OutPutFile: Text;
  182. OutPutFileIsAvailable: Boolean;
  183. begin
  184. WriteLn('Warning!!!Choosed file will be rewriten.You may lose yor''s data.');
  185. repeat
  186. OutPutFileIsAvailable := True;
  187. Write('Enter output path: ');
  188. try
  189. Read(OutPutPath);
  190. Assign(OutPutFile, OutputPath);
  191. ReWrite(OutPutFile);
  192. Close(OutPutFile);
  193. except
  194. OutPutFileIsAvailable := False;
  195. Write('File is unavailable!try again.');
  196. end;
  197. until OutPutFileIsAvailable;
  198. GetAvailableOutputPath := OutputPath
  199. end;
  200.  
  201. procedure OutPutFile(List: TArrOfRec; PathToOutputFile: String);
  202. var
  203. OutPutFile: Text;
  204. i: Integer;
  205. begin
  206. Assign(OutPutFile, PathToOutputFile);
  207. Rewrite(OutPutFile);
  208. WriteLn(OutPutFile, 'Result List: ');
  209. for i := 0 to Length(List) - 1 do
  210. begin
  211. with List[i] do
  212. begin
  213. Write(OutPutFile, ParticipantName, ' | ');
  214. WriteLn(OutPutFile, Score);
  215. end;
  216. end;
  217. WriteLn('Result has been successfully saved!');
  218. Close(OutPutFile);
  219. end;
  220.  
  221. procedure OutPutArrOfRec(List: TArrOfRec);
  222. var
  223. i: Integer;
  224. begin
  225. for i := 0 to Length(List) - 1 do
  226. begin
  227. with List[i] do
  228. begin
  229. Write(ParticipantName, ' | ');
  230. WriteLn(Score);
  231. end;
  232. end;
  233. end;
  234.  
  235. function Sort(List: TArrOfRec): TArrOfRec;
  236. const
  237. CFirstSymbolPos = 1;
  238. var
  239. i, j: Integer;
  240. TempRec: TRec;
  241. begin
  242. for i := Length(List) - 1 downto 0 do
  243. for j:= i downto 0 do
  244. begin
  245. if List[j].Score < List[i].Score then
  246. begin
  247. TempRec := List[i];
  248. List[i] := List[j];
  249. List[j] := TempRec;
  250. end;
  251. if (List[j].Score = List[i].Score) and
  252. (Ord(List[i].ParticipantName[CFirstSymbolPos]) <
  253. Ord(List[j].ParticipantName[CFirstSymbolPos])) then
  254. begin
  255. TempRec := List[i];
  256. List[i] := List[j];
  257. List[j] := TempRec;
  258. end;
  259. end;
  260. Sort := List;
  261. end;
  262.  
  263. procedure main;
  264. var
  265. List: TArrOfRec;
  266. PathToInputFile, PathToOutputFile: string;
  267. DataOfFileIsCorr: Boolean;
  268. begin
  269. repeat
  270. Write('Do you want to get list of participans from file?');
  271. if GetUserAcception then
  272. repeat
  273. PathToInputFile := GetRightInputPath;
  274. DataOfFileIsCorr := True;
  275. List := InPutFromFile(PathToInputFile, DataOfFileIsCorr)
  276. until DataOfFileIsCorr
  277. else
  278. List := InPutConsole();
  279. List := Sort(List);
  280. OutPutArrOfRec(List);
  281. Write('Save results to file?');
  282. if GetUserAcception then
  283. begin
  284. PathToOutputFile := GetAvailableOutputPath();
  285. OutPutFile(List, PathToOutputFile);
  286. end;
  287. Write('Restart program?');
  288. until not GetUserAcception;
  289. end;
  290.  
  291.  
  292. begin
  293. Main;
  294. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement