Advertisement
Alyks

Untitled

Nov 3rd, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. program task2;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7. System.SysUtils,
  8. Classes,
  9. StrUtils,
  10. Math;
  11.  
  12. type
  13. NumsArr = Array [0 .. 1] of String;
  14. SeqArr = Array of String;
  15.  
  16. procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings);
  17. begin
  18. ListOfStrings.Clear();
  19. ListOfStrings.Delimiter := Delimiter;
  20. ListOfStrings.DelimitedText := Str;
  21. end;
  22.  
  23. function MakeSameLength(Seq1, Seq2: String): NumsArr;
  24. var
  25. ZeroesCount, i: Integer;
  26. Arr: NumsArr;
  27. Num1, Num2: String;
  28. begin
  29. Num1 := Seq1;
  30. Num2 := Seq2;
  31. ZeroesCount := Abs(Length(Num1) - Length(Num2));
  32. for i := 0 to ZeroesCount - 1 do
  33. begin
  34. if (Length(Num1) < Length(Num2)) then
  35. Num1 := '0' + Num1
  36. else
  37. Num2 := '0' + Num2;
  38. end;
  39. Arr[0] := Num1;
  40. Arr[1] := Num2;
  41. MakeSameLength := Arr;
  42. end;
  43.  
  44. function Sum(Seq1, Seq2: String): String;
  45. var
  46. Nums: NumsArr;
  47. i: Integer;
  48. Num1, Num2, Sequence: String;
  49. Memory, El1, El2, Res, Units: Byte;
  50. begin
  51. Nums := MakeSameLength(Seq1, Seq2);
  52. Num1 := Nums[0];
  53. Num2 := Nums[1];
  54. Memory := 0;
  55. Sequence := '';
  56. for i := Length(Num1) downto 1 do
  57. begin
  58. El1 := StrToInt(Num1[i]);
  59. El2 := StrToInt(Num2[i]);
  60. Res := El1 + El2 + Memory;
  61. Units := Res mod 10;
  62. if (i <> 1) then
  63. begin
  64. Memory := Res div 10;
  65. Sequence := IntToStr(Units) + Sequence;
  66. end
  67. else
  68. Sequence := IntToStr(Res) + Sequence;
  69. end;
  70. Sum := Sequence;
  71. end;
  72.  
  73. procedure ShowSequence(Seq: String);
  74. var
  75. i: Integer;
  76. begin
  77. for i := 1 to Length(Seq) do
  78. Write(Seq[i], ' ');
  79. Writeln;
  80. end;
  81.  
  82. function ReadNextLine(const FileToRead: TextFile): String;
  83. var
  84. CurrLine: String;
  85. Exception: Boolean;
  86. begin
  87. Exception := false;
  88. try
  89. Readln(FileToRead, CurrLine);
  90. except
  91. Exception := true;
  92. end;
  93. if (Exception) then
  94. ReadNextLine := '-1'
  95. else
  96. ReadNextLine := CurrLine;
  97. end;
  98.  
  99. function Input(): String;
  100. var
  101. Value: String;
  102. begin
  103. try
  104. Readln(Value);
  105. except
  106. Writeln('Введите корректный путь к файлу!');
  107. end;
  108. Input := Value;
  109. end;
  110.  
  111. function ExtractNums(Str: String): String;
  112. var
  113. i, Len, El: Integer;
  114. Num: String;
  115. OutputList: TStringList;
  116. begin
  117. OutputList := TStringList.Create;
  118. Split(' ', Str, OutputList);
  119. Len := OutputList.Count - 1;
  120. for i := 0 to Len do
  121. begin
  122. El := StrToInt(Trim(OutputList[i]));
  123. if (El in [0 .. 9]) then
  124. Num := Num + IntToStr(El);
  125. end;
  126. ExtractNums := Num;
  127. end;
  128.  
  129. procedure Main();
  130. var
  131. NotCorrect, NoException: Boolean;
  132. FilePath, Sequence: String;
  133. Seq1, Seq2: String;
  134. InputFile, OutputFile: TextFile;
  135. begin
  136. Writeln('Данная программа находит последовательность, представляющую сумму двух натуральных чисел, заданных своими последовательностями');
  137. Writeln;
  138. NotCorrect := true;
  139. while (NotCorrect) do
  140. begin
  141. NoException := true;
  142. Writeln('Введите путь до файла, в котором находятся последовательности чисел, либо его имя, если файл находится в одной директории с программой');
  143. FilePath := Input();
  144. if (not ContainsText(FilePath, '.txt')) then
  145. FilePath := FilePath + '.txt';
  146. if (FileExists(FilePath)) then
  147. AssignFile(InputFile, FilePath)
  148. else
  149. begin
  150. Writeln('Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла.');
  151. NoException := false;
  152. end;
  153.  
  154. if (NoException) then
  155. begin
  156. Reset(InputFile);
  157. Seq1 := ExtractNums(ReadNextLine(InputFile));
  158. Seq2 := ExtractNums(ReadNextLine(InputFile));
  159. Close(InputFile);
  160. Writeln('Числа, записанные в файле:');
  161. ShowSequence(Seq1);
  162. ShowSequence(Seq2);
  163.  
  164. if ((StrToInt(Seq1) > 0) and (StrToInt(Seq2) > 0)) then
  165. begin
  166. Sequence := Sum(Seq1, Seq2);
  167. Writeln('Результат: ', Sequence);
  168. AssignFile(OutputFile, 'output.txt');
  169. Rewrite(OutputFile);
  170. Writeln(OutputFile, 'Результат: ', Sequence);
  171. Close(OutputFile);
  172. Writeln('Результат сохранен в файл output.txt');
  173. NotCorrect := false;
  174. end
  175. else
  176. Writeln('Файл должен состоять из последовательности натуральных чисел');
  177. end;
  178. end;
  179. Readln;
  180. end;
  181.  
  182. begin
  183. Main();
  184. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement