Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project4;
- {$APPTYPE CONSOLE}
- Uses
- System.SysUtils;
- Type
- TMatrix = Array Of Array Of Integer;
- Function FileConsoleChoice(): Integer;
- Var
- Choice: Integer;
- IsCorrect: Boolean;
- Const
- MIN_NUM = 0;
- MAX_NUM = 1;
- Begin
- Repeat
- IsCorrect := True;
- Try
- Readln(Choice);
- Except
- Writeln('Check data correctness.');
- IsCorrect := False;
- End;
- If (IsCorrect And (Choice < MIN_NUM) And (Choice > MAX_NUM)) Then
- Begin
- Writeln('Invalid input.');
- IsCorrect := False;
- End;
- Until IsCorrect;
- FileConsoleChoice := Choice;
- End;
- Function InputFilePath(): String;
- Var
- IsCorrect: Boolean;
- Path: String;
- Begin
- Writeln('Input path to file: ');
- Repeat
- IsCorrect := True;
- Readln(Path);
- If(Not FileExists(Path)) Then
- Begin
- IsCorrect := False;
- Writeln('Incorrect way to file.');
- End
- Else If (ExtractFileExt(Path) <> '.txt') Then
- Begin
- Writeln('Must have .txt');
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputFilePath := Path;
- End;
- Function InputLengthOfMarix(): Integer;
- Const
- MAX = 7;
- MIN = 2;
- Var
- IsCorrect: Boolean;
- Length: Integer;
- Begin
- Writeln('Enter Size Of Matrix');
- Repeat
- IsCorrect := True;
- Try
- Readln(Length);
- Except
- Writeln('Type Err');
- Iscorrect := False;
- End;
- If (IsCorrect) And ((Length > MAX) Or (Length < MIN)) Then
- Begin
- IsCorrect := False;
- Writeln('RangeErr');
- End;
- Until (Iscorrect);
- InputLengthOfMarix := Length;
- End;
- Function InputLengthFromFile(Path : String): Integer;
- Var
- IsCorrect: Boolean;
- Length: Integer;
- InputFile: TextFile;
- Begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Try
- Readln(InputFile, Length);
- Except
- Writeln('Mistake of reading word from file.');
- End;
- InputLengthFromFile := Length;
- End;
- Function FillInMarixFromFile(Way: String; Length: Integer): TMatrix;
- Var
- I, J, N1: Integer;
- InputFile: TextFile;
- Arr: TMatrix;
- Begin
- SetLength(Arr, Length, Length);
- N1 := Length - 1;
- AssignFile(InputFile, Way);
- Reset(InputFile);
- Readln(InputFile);
- For I := 0 To N1 Do
- Begin
- For J := 0 To N1 Do
- Read(InputFile, Arr[I, J]);
- End;
- CloseFile(InputFile);
- FillInMarixFromFile := Arr;
- End;
- Function FillInMarix(Length: Integer): TMatrix;
- Const
- MAX = 9;
- MIN = 0;
- Var
- IsCorrect: Boolean;
- Arr: TMatrix;
- I, J, N1: Integer;
- Begin
- SetLength(Arr, Length, Length);
- N1 := Length - 1;
- Writeln('Enter Matrix');
- for I := 0 to N1 do
- for J := 0 to N1 do
- begin
- Repeat
- IsCorrect := True;
- Try
- Readln(Arr[i,j]);
- Except
- Writeln('Type Err');
- Iscorrect := False;
- End;
- If (IsCorrect) And ((Length > MAX) Or (Length < MIN)) Then
- Begin
- IsCorrect := False;
- Writeln('RangeErr');
- End;
- Until (Iscorrect);
- end;
- FillInMarix := Arr;
- End;
- Function SortMarix(Length: Integer; Arr: TMatrix): TMatrix;
- const
- FIRST = 0;
- Var
- IsCorrect: Boolean;
- TempArr: array of Integer;
- E, I, N1, J, Min, IMin: Integer;
- Begin
- SetLength(TempArr, Length);
- N1 := Length - 1;
- for E := 0 to N1 do
- begin
- Min := Arr[E, FIRST];
- imin := E;
- for I := E + 1 to N1 do
- begin
- if Arr[I,FIRST] < Min then
- begin
- min := arr[i, FIRST];
- imin := i;
- end;
- end;
- for J := 0 To N1 Do
- begin
- TempArr[J] := Arr[E, j];
- Arr[E, j] := Arr[IMIn, j];
- Arr[Imin,j] := TempArr[j];
- end;
- end;
- SortMarix := Arr;
- End;
- Procedure ConsoleOutput(Arr: TMatrix; Length: Integer);
- Var
- I, J, N1: Integer;
- begin
- N1 := Length - 1;
- for I := 0 to N1 do
- begin
- for J := 0 to N1 do
- Write(Arr[i,j], ' ');
- Writeln;
- end;
- end;
- Procedure FileOutput(Arr: TMatrix; Length: Integer);
- Var
- IsCorrect: Boolean;
- OutputFile: TextFile;
- Path: String;
- I, J, N1: Integer;
- Begin
- Path := InputFilePath();
- AssignFile(OutputFile, Path);
- Repeat
- IsCorrect := True;
- Try
- Rewrite(OutputFile);
- Except
- IsCorrect := False;
- Writeln('Mistake with writing in file. Input another path.');
- Path := InputFilePath();
- End;
- Until IsCorrect;
- N1 := Length - 1;
- for I := 0 to N1 do
- begin
- for J := 0 to N1 do
- Write(OutputFile, Arr[i,j], ' ');
- Writeln(OutputFile);
- end;
- CloseFile(OutputFile);
- Write('Output in file was successful.');
- End;
- Var
- Path: String;
- Length: Integer;
- Arr: TMatrix;
- Begin
- Writeln('The program arranges the rows of this matrix in ascending order of the elements of the first column.');
- Writeln('Type 0 - console input, type 1 - file input.');
- If (FileConsoleChoice()= 0) Then
- begin
- Length := InputLengthOfMarix();
- Arr := FillInMarix(Length);
- End
- Else
- Begin
- Path := InputFilePath();
- Length := InputLengthFromFile(Path);
- Arr := FillInMarixFromFile(Path, Length);
- End;
- Arr := SortMarix(Length, Arr);
- Writeln('Type 0 - console output, type 1 - file output.');
- If (FileConsoleChoice()= 0) Then
- ConsoleOutput(Arr, Length)
- Else
- FileOutput(Arr, Length);
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment