Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.67 KB | None | 0 0
  1. program Lab3_3;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.   System.SysUtils;
  5.  
  6. type
  7.    TIntArray = Array of Integer;
  8.  
  9. procedure ChooseInOutType(var InOutType: Boolean; MessageType: String);
  10. var
  11.    TypedLetter: Char;
  12.    IsCorrectAnswer: Boolean;
  13. begin
  14.    if (MessageType = 'in') then
  15.       Writeln('В случае, если вы хотите ввести строку из файла, введите F. Если же вы хотите ввести строку из консоли, введите C.')
  16.    else
  17.       if (MessageType = 'out') then
  18.          Writeln('В случае, если вы хотите произвести вывод в файл, введите F. Если же вы хотите произвести вывод только на экран, введите C.');
  19.    IsCorrectAnswer := false;
  20.    repeat
  21.       Readln(TypedLetter);
  22.       TypedLetter := UpCase(TypedLetter);
  23.       if (TypedLetter = 'F') then
  24.       begin
  25.          InOutType := true;
  26.          IsCorrectAnswer := true;
  27.       end
  28.       else
  29.          if (TypedLetter = 'C') then
  30.          begin
  31.             InOutType := false;
  32.             IsCorrectAnswer := true;
  33.          end
  34.       else
  35.       begin
  36.          Writeln('Ошибка. Был введён некорректный ответ. Повторите попытку.');
  37.          IsCorrectAnswer := false;
  38.       end;
  39.    until (IsCorrectAnswer);
  40. end;
  41.  
  42. function ReadFileName(): String;
  43. var
  44.    FileName: String;
  45.    TestNumber: Real;
  46. begin
  47.    Readln(FileName);
  48.    FileName := FileName + '.txt';
  49.    ReadFileName := FileName;
  50. end;
  51.  
  52. function isFileCorrect(FileName: String): Boolean;
  53. var
  54.    TestString: AnsiString;
  55.    TestFile: TextFile;
  56. begin
  57.    try
  58.       AssignFile(TestFile, FileName);
  59.       Reset(TestFile);
  60.       if (FileExists(FileName)) then
  61.       begin
  62.          IsFileCorrect := true;
  63.       end
  64.       else
  65.       begin
  66.          Writeln('Указанный файл не найден');
  67.          IsFileCorrect := false;
  68.       end;
  69.    except
  70.       Writeln('Ощибка. ');
  71.       IsFileCorrect := false;
  72.    end;
  73.    CloseFile(TestFile);
  74. end;
  75.  
  76. function ReadArrayFromFile(SourceFileName: string): TIntArray;
  77. var
  78.    InputSource: TextFile;
  79.    CreatedArray: TIntArray;
  80.    ArrayLength, i: Integer;
  81. begin
  82.    AssignFile(InputSource, SourceFileName);
  83.    Reset(InputSource);
  84.    Readln(InputSource, ArrayLength);
  85.    SetLength(CreatedArray, ArrayLength);
  86.    for i := 0 to high(CreatedArray) do
  87.       Read(InputSource, CreatedArray[i]);
  88.    CloseFile(InputSource);
  89.    ReadArrayFromFile := CreatedArray;
  90. end;
  91.  
  92. function InputCheckedNumber(Min, Max: Integer): Integer;
  93. var
  94.    IsCorrectNumber: Boolean;
  95.    Number: Integer;
  96. begin
  97.    IsCorrectNumber := false;
  98.    Writeln('Введите целое число от ', Min, ' до ', Max);
  99.    repeat
  100.       try
  101.          Readln(Number);
  102.          if (Number > Min) and (Number < Max) then
  103.             IsCorrectNumber := true
  104.          else
  105.          begin
  106.             Writeln('Ошибка ввода. Введите целое число от ', Min,' до ', Max);
  107.          end;
  108.       except
  109.          Writeln('Ошибка ввода. Введите число целое от ', Min, ' до ', Max);
  110.       end;
  111.    until (IsCorrectNumber);
  112.    InputCheckedNumber := number;
  113. end;
  114.  
  115. function ReadArrayFromConsole(): TIntArray;
  116. var
  117.    CreatedArray: TIntArray;
  118.    ArrayLength, i: Integer;
  119. begin
  120.    Writeln('Укажите размер массива. Ввести необходимо натуральное число до 20');
  121.    ArrayLength := InputCheckedNumber(0, 20);
  122.    SetLength(CreatedArray, ArrayLength);
  123.    for i := 0 to High(CreatedArray) do
  124.    begin
  125.       Writeln('Введите элемент А[', i, ']');
  126.       CreatedArray[i] := InputCheckedNumber(-10000, 10000);
  127.    end;
  128.    ReadArrayFromConsole := CreatedArray;
  129. end;
  130.  
  131. function CreateArray(): TIntArray;
  132. var
  133.    CreatedArray: TIntArray;
  134.    InputFileName: String;
  135.    FileInput: Boolean;
  136. begin
  137.    ChooseInOutType(FileInput, 'in');
  138.    if (FileInput) then
  139.    begin
  140.       repeat
  141.          Writeln('Укажите имя файла, из которого хотите считать массив');
  142.          InputFileName := ReadFileName();
  143.       until (IsFileCorrect(InputFileName));
  144.       CreatedArray := ReadArrayFromFile(InputFileName);
  145.    end
  146.    else
  147.    begin
  148.       CreatedArray := ReadArrayFromConsole();
  149.    end;
  150.    CreateArray := CreatedArray;
  151. end;
  152.  
  153. procedure ClearFile(OutputFileName: String);
  154. var
  155.    OutputFile: TextFile;
  156. begin
  157.    AssignFile(OutputFile, OutputFileName);
  158.    Rewrite(OutputFile);
  159.    CloseFile(OutputFile);
  160. end;
  161.  
  162. function ChooseOutputFile(): String;
  163. var
  164.    OutputFileName: String;
  165.    Answer: Char;
  166.    IsCorrectFile, IsCorrectAnswer: Boolean;
  167. begin
  168.    IsCorrectFile := false;
  169.    IsCorrectAnswer := false;
  170.    repeat
  171.       Writeln('Укажите название файла, в который хотите произвести запись.');
  172.       OutputFileName := ReadFileName();
  173.       if (FileExists(OutputFileName)) then
  174.       begin
  175.          ClearFile(OutputFileName);
  176.          IsCorrectFile := true;
  177.       end
  178.       else
  179.       begin
  180.          Writeln('Файл с указанным названием не существует. Если вы желаете создать файл с указанным именем, введите Y, в обратном случае введите N');
  181.          repeat
  182.             Readln(Answer);
  183.             Answer := UpCase(Answer);
  184.             if (Answer = 'Y') then
  185.             begin
  186.                ClearFile(OutputFileName);
  187.                IsCorrectFile := true;
  188.                IsCorrectAnswer := true;
  189.             end
  190.             else
  191.                if (Answer = 'N') then
  192.                begin
  193.                   IsCorrectFile := false;
  194.                   IsCorrectAnswer := true;
  195.                end
  196.             else
  197.             begin
  198.                Writeln('Ошибка. Был введён некорректный ответ. Повторите попытку');
  199.             end;
  200.          until (IsCorrectAnswer);
  201.       end;
  202.    until (IsCorrectFile);
  203.    ChooseOutputFile := OutputFileName;
  204. end;
  205.  
  206. procedure WritelnStrInFile(OutputFileName, Str: String);
  207. var
  208.    OutputFile: TextFile;
  209. begin
  210.    AssignFile(OutputFile, OutputFileName);
  211.    Append(OutputFile);
  212.    Write(OutputFile, Str);
  213.    CloseFile(OutputFile);
  214. end;
  215.  
  216. procedure PrintToFile(PrintedArray: TIntArray);
  217. var
  218.    OutputFileName: String;
  219.    i: Integer;
  220. begin
  221.    OutputFileName := ChooseOutputFile();
  222.    WritelnStrInFile(OutputFileName, 'Отсортированный массив: ');
  223.    WritelnStrInFile(OutputFileName, #10#13);
  224.    for i := 0 to High(PrintedArray) do
  225.    begin
  226.       WritelnStrInFile(OutputFileName, IntToStr(PrintedArray[i]));
  227.       WritelnStrInFile(OutputFileName, '  ');
  228.    end;
  229.    Writeln('Запись в файл произведена успешно');
  230. end;
  231.  
  232. procedure PrintResult(PrintedArray: TIntArray);
  233. var
  234.    FileOutput: Boolean;
  235.   i: Integer;
  236. begin
  237.    Write('Отсортированный массив: ');
  238.    for i := 0 to high(PrintedArray) do
  239.       Write(PrintedArray[i], '  ');
  240.    Writeln;
  241.    ChooseInOutType(FileOutput, 'out');
  242.    if (FileOutput) then
  243.       PrintToFile(PrintedArray);
  244. end;
  245.  
  246. procedure InsertionSort(var A: TIntArray);
  247. var i, j, temp: Integer;
  248. begin
  249.    for i := 1 to High(A) do
  250.    begin
  251.       j := i;
  252.       temp := A[i];
  253.       while ((j > 0)and(A[j - 1] > tmp)) do
  254.       begin
  255.          A[j] := A[j - 1];
  256.          dec(j);
  257.       end;
  258.       A[j] := temp;
  259.    end;
  260. end;
  261.  
  262. procedure Main();
  263. var
  264.    NumberArray: TIntArray;
  265.    i: Integer;
  266. begin
  267.    Writeln('Данная программа создаёт массив целых чисел и сортирует его, используя метод сортировки простыми вставками.');
  268.    NumberArray := CreateArray();
  269.    InsertionSort(NumberArray);
  270.    PrintResult(NumberArray);
  271.    Readln;
  272. end;
  273.  
  274. begin
  275.    Main();
  276. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement