Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Lab3_3;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils;
- type
- TIntArray = Array of Integer;
- procedure ChooseInOutType(var InOutType: Boolean; MessageType: String);
- var
- TypedLetter: Char;
- IsCorrectAnswer: Boolean;
- begin
- if (MessageType = 'in') then
- Writeln('В случае, если вы хотите ввести строку из файла, введите F. Если же вы хотите ввести строку из консоли, введите C.')
- else
- if (MessageType = 'out') then
- Writeln('В случае, если вы хотите произвести вывод в файл, введите F. Если же вы хотите произвести вывод только на экран, введите C.');
- IsCorrectAnswer := false;
- repeat
- Readln(TypedLetter);
- TypedLetter := UpCase(TypedLetter);
- if (TypedLetter = 'F') then
- begin
- InOutType := true;
- IsCorrectAnswer := true;
- end
- else
- if (TypedLetter = 'C') then
- begin
- InOutType := false;
- IsCorrectAnswer := true;
- end
- else
- begin
- Writeln('Ошибка. Был введён некорректный ответ. Повторите попытку.');
- IsCorrectAnswer := false;
- end;
- until (IsCorrectAnswer);
- end;
- function ReadFileName(): String;
- var
- FileName: String;
- TestNumber: Real;
- begin
- Readln(FileName);
- FileName := FileName + '.txt';
- ReadFileName := FileName;
- end;
- function isFileCorrect(FileName: String): Boolean;
- var
- TestString: AnsiString;
- TestFile: TextFile;
- begin
- try
- AssignFile(TestFile, FileName);
- Reset(TestFile);
- if (FileExists(FileName)) then
- begin
- IsFileCorrect := true;
- end
- else
- begin
- Writeln('Указанный файл не найден');
- IsFileCorrect := false;
- end;
- except
- Writeln('Ощибка. ');
- IsFileCorrect := false;
- end;
- CloseFile(TestFile);
- end;
- function ReadArrayFromFile(SourceFileName: string): TIntArray;
- var
- InputSource: TextFile;
- CreatedArray: TIntArray;
- ArrayLength, i: Integer;
- begin
- AssignFile(InputSource, SourceFileName);
- Reset(InputSource);
- Readln(InputSource, ArrayLength);
- SetLength(CreatedArray, ArrayLength);
- for i := 0 to high(CreatedArray) do
- Read(InputSource, CreatedArray[i]);
- CloseFile(InputSource);
- ReadArrayFromFile := CreatedArray;
- end;
- function InputCheckedNumber(Min, Max: Integer): Integer;
- var
- IsCorrectNumber: Boolean;
- Number: Integer;
- begin
- IsCorrectNumber := false;
- Writeln('Введите целое число от ', Min, ' до ', Max);
- repeat
- try
- Readln(Number);
- if (Number > Min) and (Number < Max) then
- IsCorrectNumber := true
- else
- begin
- Writeln('Ошибка ввода. Введите целое число от ', Min,' до ', Max);
- end;
- except
- Writeln('Ошибка ввода. Введите число целое от ', Min, ' до ', Max);
- end;
- until (IsCorrectNumber);
- InputCheckedNumber := number;
- end;
- function ReadArrayFromConsole(): TIntArray;
- var
- CreatedArray: TIntArray;
- ArrayLength, i: Integer;
- begin
- Writeln('Укажите размер массива. Ввести необходимо натуральное число до 20');
- ArrayLength := InputCheckedNumber(0, 20);
- SetLength(CreatedArray, ArrayLength);
- for i := 0 to High(CreatedArray) do
- begin
- Writeln('Введите элемент А[', i, ']');
- CreatedArray[i] := InputCheckedNumber(-10000, 10000);
- end;
- ReadArrayFromConsole := CreatedArray;
- end;
- function CreateArray(): TIntArray;
- var
- CreatedArray: TIntArray;
- InputFileName: String;
- FileInput: Boolean;
- begin
- ChooseInOutType(FileInput, 'in');
- if (FileInput) then
- begin
- repeat
- Writeln('Укажите имя файла, из которого хотите считать массив');
- InputFileName := ReadFileName();
- until (IsFileCorrect(InputFileName));
- CreatedArray := ReadArrayFromFile(InputFileName);
- end
- else
- begin
- CreatedArray := ReadArrayFromConsole();
- end;
- CreateArray := CreatedArray;
- end;
- procedure ClearFile(OutputFileName: String);
- var
- OutputFile: TextFile;
- begin
- AssignFile(OutputFile, OutputFileName);
- Rewrite(OutputFile);
- CloseFile(OutputFile);
- end;
- function ChooseOutputFile(): String;
- var
- OutputFileName: String;
- Answer: Char;
- IsCorrectFile, IsCorrectAnswer: Boolean;
- begin
- IsCorrectFile := false;
- IsCorrectAnswer := false;
- repeat
- Writeln('Укажите название файла, в который хотите произвести запись.');
- OutputFileName := ReadFileName();
- if (FileExists(OutputFileName)) then
- begin
- ClearFile(OutputFileName);
- IsCorrectFile := true;
- end
- else
- begin
- Writeln('Файл с указанным названием не существует. Если вы желаете создать файл с указанным именем, введите Y, в обратном случае введите N');
- repeat
- Readln(Answer);
- Answer := UpCase(Answer);
- if (Answer = 'Y') then
- begin
- ClearFile(OutputFileName);
- IsCorrectFile := true;
- IsCorrectAnswer := true;
- end
- else
- if (Answer = 'N') then
- begin
- IsCorrectFile := false;
- IsCorrectAnswer := true;
- end
- else
- begin
- Writeln('Ошибка. Был введён некорректный ответ. Повторите попытку');
- end;
- until (IsCorrectAnswer);
- end;
- until (IsCorrectFile);
- ChooseOutputFile := OutputFileName;
- end;
- procedure WritelnStrInFile(OutputFileName, Str: String);
- var
- OutputFile: TextFile;
- begin
- AssignFile(OutputFile, OutputFileName);
- Append(OutputFile);
- Write(OutputFile, Str);
- CloseFile(OutputFile);
- end;
- procedure PrintToFile(PrintedArray: TIntArray);
- var
- OutputFileName: String;
- i: Integer;
- begin
- OutputFileName := ChooseOutputFile();
- WritelnStrInFile(OutputFileName, 'Отсортированный массив: ');
- WritelnStrInFile(OutputFileName, #10#13);
- for i := 0 to High(PrintedArray) do
- begin
- WritelnStrInFile(OutputFileName, IntToStr(PrintedArray[i]));
- WritelnStrInFile(OutputFileName, ' ');
- end;
- Writeln('Запись в файл произведена успешно');
- end;
- procedure PrintResult(PrintedArray: TIntArray);
- var
- FileOutput: Boolean;
- i: Integer;
- begin
- Write('Отсортированный массив: ');
- for i := 0 to high(PrintedArray) do
- Write(PrintedArray[i], ' ');
- Writeln;
- ChooseInOutType(FileOutput, 'out');
- if (FileOutput) then
- PrintToFile(PrintedArray);
- end;
- procedure InsertionSort(var A: TIntArray);
- var i, j, temp: Integer;
- begin
- for i := 1 to High(A) do
- begin
- j := i;
- temp := A[i];
- while ((j > 0)and(A[j - 1] > tmp)) do
- begin
- A[j] := A[j - 1];
- dec(j);
- end;
- A[j] := temp;
- end;
- end;
- procedure Main();
- var
- NumberArray: TIntArray;
- i: Integer;
- begin
- Writeln('Данная программа создаёт массив целых чисел и сортирует его, используя метод сортировки простыми вставками.');
- NumberArray := CreateArray();
- InsertionSort(NumberArray);
- PrintResult(NumberArray);
- Readln;
- end;
- begin
- Main();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement