Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program lab3_3dproj;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- TArray = array of Integer;
- TAnswers = array of array of Integer;
- TSortStages = array of array of Integer;
- Function TakeInt(Min,Max: Integer): Integer;
- var
- IsCorrect: Boolean;
- Number: Integer;
- begin
- Number := 0;
- repeat
- IsCorrect := True;
- try
- Readln(Number);
- except
- Writeln('Incorrect input!!!');
- IsCorrect := False;
- end;
- if (IsCorrect and (Number < Min) or (Number > Max)) then
- begin
- Writeln('Incorrect input!!!');
- IsCorrect := False;
- end;
- until IsCorrect;
- TakeInt := Number;
- end;
- Function TakeSource: Byte;
- const
- CHOOSING_CONSOLE = 1;
- CHOOSING_FILE = 2;
- var
- Source: Byte;
- IsCorrect: Boolean;
- begin
- Source := 0;
- repeat
- IsCorrect := True;
- try
- Readln(Source);
- except
- Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- IsCorrect := False;
- end;
- if (IsCorrect and (Source <> CHOOSING_CONSOLE) and (Source <> CHOOSING_FILE)) then
- begin
- Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- IsCorrect := False;
- end;
- until IsCorrect;
- TakeSource := Source;
- end;
- Function TakeInPath: String;
- var
- Path: String;
- IsCorrect: Boolean;
- InFile: TextFile;
- begin
- Write('Enter file path: ');
- repeat
- IsCorrect := True;
- Readln(Path);
- if not FileExists(Path) then
- begin
- Write('File is not found' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end
- else
- if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
- (Path[Length(Path)- 2] <> 't') or (Path[Length(Path)- 3] <> '.')) then
- begin
- Write('File is found, but it is not ".txt" type file' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end
- else
- begin
- Assign(InFile, Path);
- try
- Reset(InFile);
- except
- Write('Error!!! Program can''t open file!' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end;
- end;
- until IsCorrect;
- CloseFile(InFile);
- TakeInPath := Path;
- end;
- Function TakeOutPath: String;
- var
- Path: String;
- IsCorrect: Boolean;
- OutFile: TextFile;
- begin
- Write('Enter file path: ');
- repeat
- IsCorrect := True;
- Readln(Path);
- if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
- (Path[Length(Path)- 2] <> 't') or (Path[Length(Path) - 3] <> '.')) then
- begin
- Write('It should be a ".txt" type file' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end
- else
- begin
- AssignFile(OutFile, Path);
- try
- Rewrite(OutFile);
- except
- Write('Error!!! There are problems with file!' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end;
- end;
- until IsCorrect;
- CloseFile(OutFile);
- TakeOutPath := Path;
- end;
- Function TakeArrayFromConsole: TArray;
- const
- MIN_SIZE = 2;
- MAX_SIZE = 200;
- MIN_ELEMENT = -1000;
- MAX_ELEMENT = 1000;
- var
- Size, i: Integer;
- Arr: TArray;
- begin
- Write('Enter size: ');
- Size := TakeInt(MIN_SIZE, MAX_SIZE);
- SetLength(Arr, Size);
- for i := 0 to (Length(Arr) - 1) do
- begin
- Write('Enter element ', (i + 1), ': ');
- Arr[i] := TakeInt(MIN_ELEMENT, MAX_ELEMENT);
- end;
- TakeArrayFromConsole := Arr;
- end;
- Function TakeArrayFromFile(const Path: String): TArray;
- const
- MIN_SIZE = 2;
- var
- InFile: TextFile;
- Size, i: Integer;
- IsCorrect: Boolean;
- Arr: TArray;
- begin
- IsCorrect := True;
- AssignFile(InFile, Path);
- Reset(InFile);
- Size := 0;
- try
- Read(InFile,Size);
- except
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- if (IsCorrect and (Size < MIN_SIZE)) then
- begin
- Write('Incorrect file content!!! The size must be higher than 1');
- IsCorrect := False;
- end;
- i := 0;
- if IsCorrect then
- begin
- SetLength(Arr,Size);
- While (IsCorrect and (i < Size) and (not Eof(InFile))) do
- begin
- try
- Read(InFile, Arr[i]);
- except
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- i := i + 1;
- end;
- end;
- if (IsCorrect and((i < Size) or ((not Eof(InFile))))) then
- begin
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- if not IsCorrect then
- SetLength(Arr, 0);
- TakeArrayFromFile := Arr;
- CloseFile(InFile);
- end;
- Function TakeArray(Source: Byte): TArray;
- var
- InPath: String;
- Arr: TArray;
- begin
- if (Source = 1) then
- Arr := TakeArrayFromConsole
- else
- begin
- InPath := TakeInPath;
- Arr := TakeArrayFromFile(InPath);
- while (Length(Arr) = 0) do
- begin
- InPath := TakeInPath;
- Arr := TakeArrayFromFile(InPath);
- end;
- end;
- TakeArray := Arr;
- end;
- Function FindAnswers(Arr: TArray): TAnswers;
- var
- Stages: TSortStages;
- Answers: TAnswers;
- i, j, k, Counter, LeftBorder, RightBorder: Integer;
- WasThereSwap: Boolean;
- begin
- SetLength(Stages,Length(Arr),Length(Arr));
- for k := 0 to (Length(Arr) - 1) do
- Stages[0][k] := Arr[k];
- Counter := 1;
- LeftBorder := 0;
- RightBorder := Length(Arr) - 1;
- WasThereSwap := True;
- while ((LeftBorder < RightBorder) and WasThereSwap) do
- begin
- WasThereSwap := False;
- for i := LeftBorder to (RightBorder - 1) do
- if (Arr[i] > Arr[i + 1]) then
- begin
- Arr[i] := Arr[i] xor Arr[i + 1];
- Arr[i + 1] := Arr[i + 1] xor Arr[i];
- Arr[i] := Arr[i] xor Arr[i + 1];
- WasThereSwap := True;
- end;
- if(wasThereSwap) then
- begin
- for k := 0 to (Length(Arr) - 1) do
- Stages[Counter][k] := Arr[k];
- Counter := Counter + 1;
- end;
- WasThereSwap := False;
- RightBorder := RightBorder - 1;;
- for i := RightBorder downto (LeftBorder + 1) do
- if (Arr[i - 1] > Arr[i]) then
- begin
- Arr[i - 1] := Arr[i - 1] xor Arr[i];
- Arr[i] := Arr[i] xor Arr[i - 1];
- Arr[i - 1] := Arr[i - 1] xor Arr[i];
- WasThereSwap := True;
- end;
- LeftBorder := LeftBorder + 1;
- if(wasThereSwap) then
- begin
- for k := 0 to (Length(Arr) - 1) do
- Stages[Counter + 1][k] := Arr[k];
- Counter := Counter + 1;
- end;
- end;
- SetLength(Answers,Counter,Length(Arr));
- for i := 0 to (Counter - 1) do
- for j := 0 to (Length(Arr) - 1) do
- Answers[i,j] := Stages[i,j];
- FindAnswers := Answers;
- end;
- Procedure OutputToConsole(Answers: TAnswers);
- var
- i,j: Integer;
- begin
- for i := 0 to (Length(Answers) - 1) do
- begin
- for j := 0 to (Length(Answers[0]) - 1) do
- Write(Answers[i,j], ' ');
- Writeln;
- end;
- end;
- Procedure OutputToFile(const Path: String; Answers: TAnswers);
- var
- OutFile: TextFile;
- i,j: Integer;
- begin
- AssignFile(OutFile, Path);
- Rewrite(OutFile);
- for i := 0 to (Length(Answers) - 1) do
- begin
- for j := 0 to (Length(Answers[0]) - 1) do
- Write(OutFile, Answers[i,j], ' ');
- Writeln(OutFile);
- end;
- CloseFile(OutFile);
- Writeln('Done');
- end;
- Procedure Output(const Source: Byte; Answers: TAnswers);
- var
- OutPath: String;
- begin
- if (Source = 1) then
- OutputToConsole(Answers)
- else
- begin
- OutPath := TakeOutPath;
- OutputToFile(OutPath,Answers);
- end;
- end;
- var
- InputSource, OutputSource: Byte;
- Arr: TArray;
- Answers: TAnswers;
- begin
- Write('Welcome to the program that will sort an array using cocktail sort' + #13#10+ 'Select the source for entering the array:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- InputSource := TakeSource;
- Arr := TakeArray(InputSource);
- Answers := FindAnswers(Arr);
- Write('Select the source for output:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- OutputSource := TakeSource;
- Output(OutputSource, Answers);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement