Advertisement
lithie_oce

lab 3.1 delphi

Nov 30th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. Program Lab3task1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. Uses
  6. SysUtils;
  7.  
  8. Function ChoiceCheck() : Integer;
  9. Var
  10. Choice: Integer;
  11. IsCorrect: Boolean;
  12. Begin
  13. Repeat
  14. IsCorrect:=True;
  15. Try
  16. Readln(Choice);
  17. Except
  18. Writeln('Error! Input a number');
  19. IsCorrect := False;
  20. End;
  21. If (IsCorrect And ((Choice < 1) Or (Choice > 2))) Then
  22. Begin
  23. Writeln('Error! Input 1 or 2');
  24. IsCorrect := False;
  25. End;
  26. Until (IsCorrect);
  27. ChoiceCheck := Choice;
  28. End;
  29.  
  30. Function InputCheck(): String;
  31. Var
  32. IsCorrect: Boolean;
  33. Str: String;
  34. I: Integer;
  35. Begin
  36. Repeat
  37. Writeln ('Input the string consisting of numbers, letters of the English alphabet and symbols "-", "+", "." ');
  38. IsCorrect := True;
  39. Readln(Str);
  40. For I:=1 To Length(Str) Do
  41. If ((Ord(Str[I])<42) Or (Ord(Str[I])=44) Or (Ord(Str[I])=47) Or ((Ord(Str[I])>57) And (Ord(Str[I])<65)) Or ((Ord(Str[I])>90) And (Ord(Str[I])<97)) Or (Ord(Str[I])>122)) Then
  42. IsCorrect := False;
  43. If Not(IsCorrect) Then
  44. Writeln('Incorrect string format');
  45. Until (IsCorrect);
  46. InputCheck := Str;
  47. End;
  48.  
  49. Function CheckInputFilePath(): String;
  50. Var
  51. Path: String;
  52. IsCorrect: Boolean;
  53. InputFile: File;
  54. Begin
  55. Repeat
  56. Writeln('Input path to the file');
  57. IsCorrect := True;
  58. Readln(Path);
  59. If (Not(FileExists(Path))) Then
  60. Begin
  61. IsCorrect := False;
  62. Writeln('Could not find the file');
  63. End;
  64. If (IsCorrect) Then
  65. Begin
  66. Try
  67. AssignFile(InputFile, Path);
  68. ReSet(InputFile);
  69. Except
  70. IsCorrect := False;
  71. Writeln('Could not open the file');
  72. End;
  73. End;
  74. Until (IsCorrect);
  75. CloseFile(InputFile);
  76. CheckInputFilePath := Path;
  77. End;
  78.  
  79. Function FileCheckString() : String;
  80. Var
  81. InputFile: TextFile;
  82. I: Integer;
  83. Path, Str: String;
  84. IsCorrect: Boolean;
  85. Begin
  86. Repeat
  87. Path := CheckInputFilePath();
  88. IsCorrect := True;
  89. AssignFile(InputFile, Path);
  90. Reset(Inputfile);
  91. Read(InputFile, Str);
  92. If (IsCorrect And (Length(Str) < 1)) Then
  93. Begin
  94. IsCorrect := False;
  95. Writeln ('The string is empty');
  96. End;
  97. If ((IsCorrect) And Not(EoF(InputFile))) Then
  98. Begin
  99. IsCorrect := False;
  100. Writeln ('There should be only one string in the file');
  101. End;
  102. If (IsCorrect) Then
  103. Begin
  104. For I:=1 To Length(Str) Do
  105. If ((Ord(Str[I])<42) Or (Ord(Str[I])=44) Or (Ord(Str[I])=47) Or ((Ord(Str[I])>57) And (Ord(Str[I])<65)) Or ((Ord(Str[I])>90) And (Ord(Str[I])<97)) Or (Ord(Str[I])>122)) Then
  106. IsCorrect := False;
  107. If Not(IsCorrect) Then
  108. Writeln('Incorrect string format');
  109. End;
  110.  
  111. Until (IsCorrect);
  112. FileCheckString := Str;
  113. End;
  114.  
  115. Function InputChoice (): String;
  116. Var
  117. Str: String;
  118. Choice: Integer;
  119. Begin
  120. Writeln('Choose input option: ', #10, '1.Input from console', #10,
  121. '2.Input from file');
  122. Choice := ChoiceCheck();
  123. If (Choice = 1) Then
  124. Str := InputCheck()
  125. Else
  126. Str:= FileCheckString();
  127. InputChoice := Str;
  128. End;
  129.  
  130. Function CheckOutputFilePath(): String;
  131. Var
  132. Path: String;
  133. IsCorrect: Boolean;
  134. OutputFile: File;
  135. Begin
  136. Writeln('Input file path and the name of the file for', #10, 'example, Ñ:\Projects\Number\FileName.txt. If the ', #10, 'file does not exist, then it will be created', #10,'automatically in the root folder of the program');
  137. IsCorrect := True;
  138. Readln(Path);
  139. If (Not(FileExists(Path))) Then
  140. Begin
  141. IsCorrect := False;
  142. Writeln('Could not find the file');
  143. End;
  144. If (IsCorrect)Then
  145. Try
  146. AssignFile(OutputFile, Path);
  147. ReSet(OutputFile);
  148. Except
  149. IsCorrect := False;
  150. Writeln('Could not open the file');
  151. End;
  152. If (Not(IsCorrect)) Then
  153. Begin
  154. Writeln ('File will be created in the root folder of the program');
  155. Path := 'Result.txt';
  156. End
  157. Else
  158. CloseFile(OutputFile);
  159. CheckOutputFilePath := Path;
  160. End;
  161.  
  162. Procedure FindConsoleSubStr(Str: String);
  163. Var
  164. SubStr: String;
  165. I: Integer;
  166. IsFound: Boolean;
  167. Begin
  168. IsFound := False;
  169. For I := 1 to Length(Str) Do
  170. Case Ord(Str[I]) Of
  171. 43, 45:
  172. Begin
  173. If (Length(SubStr)>1) Then
  174. Writeln(Substr)
  175. Else
  176. If ((I>1) And (Str[I-1]='0')) Then
  177. Writeln('0');
  178. SubStr := ''+Str[I];
  179. IsFound := True;
  180. End;
  181. 46, 65..90, 97..122:
  182. Begin
  183. IsFound := False;
  184. If (Length(SubStr)>1) Then
  185. Writeln (SubStr)
  186. Else
  187. If ((I>1) And (Str[I-1]='0')) Then
  188. Writeln('0');
  189. SubStr := '';
  190. End;
  191. 48:
  192. If (IsFound) And Not((Substr[Length(Substr)]='+') Or (Substr[Length(Substr)]='-')) Then
  193. SubStr:= SubStr+Str[I];
  194. 49..57:
  195. If (IsFound) Then
  196. SubStr:= SubStr+Str[I];
  197. End;
  198. If (IsFound) And (Length(SubStr)>1) Then
  199. Writeln (SubStr)
  200. Else
  201. If (Str[Length(Str)-1]='0') Then
  202. Writeln ('0');
  203. End;
  204.  
  205. Procedure FindFileSubStr(Str: String);
  206. Var
  207. SubStr, Path: String;
  208. I: Integer;
  209. IsFound: Boolean;
  210. OutputFile: TextFile;
  211. Begin
  212. Path:=CheckOutputFilePath();
  213. AssignFile (OutputFile, Path);
  214. Rewrite(OutputFile);
  215. IsFound := False;
  216. For I := 1 to Length(Str) Do
  217. Case Ord(Str[I]) Of
  218. 43, 45:
  219. Begin
  220. If (Length(SubStr)>1) Then
  221. Writeln (OutputFile, SubStr)
  222. Else
  223. If ((I>1) And (Str[I-1]='0')) Then
  224. Writeln(OutputFile, '0');
  225. SubStr := ''+Str[I];
  226. IsFound := True;
  227. End;
  228. 46, 65..90, 97..122:
  229. Begin
  230. IsFound := False;
  231. If (Length(SubStr)>1) Then
  232. Writeln (OutputFile, SubStr)
  233. Else
  234. If ((I>1) And (Str[I-1]='0')) Then
  235. Writeln(OutputFile, '0');
  236. SubStr := '';
  237. End;
  238. 48:
  239. If (IsFound) And Not((Substr[Length(Substr)]='+') Or (Substr[Length(Substr)]='-')) Then
  240. SubStr:= SubStr+Str[I];
  241. 49..57:
  242. If (IsFound) Then
  243. SubStr:= SubStr+Str[I];
  244. End;
  245. If (IsFound) And (Length(SubStr)>1) Then
  246. Writeln (OutputFile, SubStr)
  247. Else
  248. If (Str[Length(Str)-1]='0') Then
  249. Writeln (OutputFile, '0');
  250. Close(OutputFile);
  251. Writeln('Successful output');
  252. End;
  253.  
  254. Procedure OutputChoice (Str: String);
  255. Var
  256. Choice: Integer;
  257. Begin
  258. Writeln('Choose output option: ', #10, '1.Output through console', #10,
  259. '2.Output through file');
  260. Choice := ChoiceCheck();
  261. If (Choice = 1) Then
  262. FindConsoleSubStr(Str)
  263. Else
  264. FindFileSubStr(Str);
  265. End;
  266.  
  267. Var
  268. Str: String;
  269. Begin
  270. Str := InputChoice();
  271. OutputChoice(Str);
  272. Readln
  273. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement