Advertisement
green1ant

3_3 *1

Nov 11th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. program Laba_3_3;
  2. {$APPTYPE CONSOLE}
  3. uses
  4. SysUtils;
  5.  
  6. type
  7. TList = array of Integer;
  8. TInputMode = (WithFile, Console);
  9.  
  10. resourcestring
  11. InstructionMessage
  12. = 'This program sorts your sequence using Insertion Sort';
  13. WayOfInputMessage = 'Where do you want to input data from? [F]ile or [C]onsole';
  14. IncorrectInputFilePathMessage
  15. = 'Incorrect input file path, check if file exists and try again';
  16. IncorrectInputModeMessage = 'Incorrect way of input, choose [F]ile or [C]onsole';
  17. OutputFileExistsErrorMessage
  18. = 'Incorrect output file, such file already exists, try again';
  19. ShouldCreateMessage = 'Do you want to save output data into file? [Y]es or [N]o';
  20. ShouldCreateErrorMessage = 'Incorrect answer, choose [Y]es or [N]o';
  21. EmptySequenceMessage = 'Your sequence doesn''t contain any of assumed symbols';
  22. OutputMessage = 'Composed set based on your sequence';
  23. SuccessfullySavedMessage = 'Output data was successfully saved into ';
  24.  
  25. function GetInputFilePath: string;
  26. var
  27. Path: string;
  28. begin
  29. Writeln('Enter input file path');
  30. Readln(Path);
  31. while not FileExists(Path) do
  32. begin
  33. Writeln(IncorrectInputFilePathMessage);
  34. Readln(Path);
  35. end;
  36. GetInputFilePath := Path;
  37. end;
  38.  
  39. function ShouldCreateOutputFile: Boolean;
  40. var
  41. Answer: string;
  42. begin
  43. Writeln(ShouldCreateMessage);
  44. Readln(Answer);
  45. Answer := LowerCase(Answer);
  46. while (Answer <> 'y') and (Answer <> 'n') do
  47. begin
  48. Writeln(ShouldCreateErrorMessage);
  49. Readln(Answer);
  50. end;
  51. ShouldCreateOutputFile := Answer = 'y';
  52. end;
  53.  
  54. function GetOutputFilePath: string;
  55. var
  56. Path: string;
  57. begin
  58. Writeln('Enter output file path');
  59. Readln(Path);
  60. while FileExists(Path) do
  61. begin
  62. Writeln(OutputFileExistsErrorMessage);
  63. Readln(Path);
  64. end;
  65. GetOutputFilePath := Path;
  66. end;
  67.  
  68. function ChooseInputMode: TInputMode;
  69. var
  70. Mode: string;
  71. begin
  72. Writeln(WayOfInputMessage);
  73. Readln(Mode);
  74. Mode := LowerCase(Mode);
  75. while (Mode <> 'c') and (Mode <> 'f') do
  76. begin
  77. Writeln(IncorrectInputModeMessage);
  78. Readln(Mode);
  79. end;
  80. if Mode = 'f' then
  81. ChooseInputMode := WithFile
  82. else
  83. ChooseInputMode := Console;
  84. end;
  85.  
  86. function ReadFile(InputFilePath: string): TList;
  87. var
  88. InputFile: TextFile;
  89. Sequence: TList;
  90. i, Item: Integer;
  91. begin
  92. AssignFile(InputFile, InputFilePath);
  93. Reset(InputFile);
  94.  
  95. i := 0;
  96. while not EoF(InputFile) do
  97. begin
  98. Read(InputFile, Item);
  99. Inc(i);
  100. end;
  101.  
  102.  
  103. SetLength(Sequence, i);
  104. Reset(InputFile);
  105. i := 0;
  106. while not EoF(InputFile) do
  107. begin
  108. Read(InputFile, Sequence[i]);
  109. Inc(i);
  110. end;
  111. CloseFile(InputFile);
  112. ReadFile := Sequence;
  113. end;
  114.  
  115. function ReadConsole(): TList;
  116. var
  117. N, i: Integer;
  118. IsCorrect: Boolean;
  119. List: TList;
  120. begin
  121. IsCorrect := False;
  122. Writeln('Enter amount of elements of your sequence');
  123. repeat
  124. try
  125. Readln(N);
  126. IsCorrect := True;
  127. except
  128. Writeln('Try again');
  129. end;
  130. until IsCorrect;
  131.  
  132. SetLength(List, N);
  133.  
  134.  
  135. IsCorrect := False;
  136. Writeln('Enter elems');
  137. for i := 0 to N - 1 do
  138.  
  139. repeat
  140. try
  141. Read(List[i]);
  142. IsCorrect := True;
  143. except
  144. Writeln('Try again man');
  145. end;
  146. until IsCorrect;
  147.  
  148. ReadConsole := List;
  149. end;
  150.  
  151. procedure WriteList(var OutputFile: TextFile; List: TList);
  152. var
  153. i, LastIndex: Integer;
  154. begin
  155. LastIndex := High(List);
  156. for i := 0 to LastIndex do
  157. Write(OutputFile, List[i], ' ');
  158.  
  159. Writeln(OutputFile, '');
  160. end;
  161.  
  162. function SortToConsole(List: TList): TList;
  163. var
  164. i, j, Current, LastIndex: Integer;
  165. begin
  166. Writeln('Sorted seqeunce');
  167. LastIndex := High(List) + 1;
  168. for i := 1 to LastIndex do
  169. begin
  170. WriteList(Output, List);
  171. Current := List[i];
  172. j := i - 1;
  173. while (List[j] > Current) and (j >= 0) do
  174. begin
  175. List[j+1] := List[j];
  176. Dec(j);
  177. //WriteList(List);
  178. end;
  179. List[j+1] := Current;
  180. //WriteList('-----------------');
  181. end;
  182. end;
  183.  
  184. function SortToFile(List: TList): TList;
  185. var
  186. i, j, Current, LastIndex: Integer;
  187. OutputFile: TextFile;
  188. begin
  189. Assign(OutputFile, GetOutputFilePath());
  190. Rewrite(OutputFile);
  191. LastIndex := High(List);
  192. Writeln(OutputFile, 'Sorted seqeunce');
  193. for i := 1 to LastIndex do
  194. begin
  195. WriteList(OutputFile, List);
  196. Current := List[i];
  197. j := i - 1;
  198. while (List[j] > Current) and (j >= 0) do
  199. begin
  200. List[j+1] := List[j];
  201. Dec(j);
  202. end;
  203. List[j+1] := Current;
  204. end;
  205. CloseFile(OutputFile);
  206. end;
  207.  
  208. procedure Main;
  209. var
  210. i, j, Current: Integer;
  211. List: TList;
  212. OutputFile: TextFile;
  213. begin
  214. Writeln(InstructionMessage);
  215. case ChooseInputMode() of
  216. WithFile:
  217. List := ReadFile('input.txt'{GetInputFilePath()});
  218. Console:
  219. List := ReadConsole();
  220. end;
  221.  
  222. SortToConsole(List);
  223. if ShouldCreateOutputFile() then
  224. SortToFile(List);
  225. Readln;
  226. end;
  227.  
  228. begin
  229. Main();
  230. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement