Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program lab3_3;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils;
- Type
- TArray = array of array of array of Integer;
- Function InputFileLocation(): string;
- Var
- IsCorrect: Boolean;
- Location: String;
- Begin
- Repeat
- IsCorrect := false;
- WriteLn('Enter file location:');
- ReadLn(Location);
- If FileExists(Location) then
- IsCorrect := true
- Else
- Begin
- WriteLn('Please enter the correct location');
- WriteLn('');
- End;
- Until IsCorrect;
- InputFileLocation := Location;
- End;
- Procedure GetArrayFromConsole(Arrays: TArray; Size: Integer);
- Var
- I, J, Temp: Integer;
- IsCorrect: Boolean;
- K: Integer;
- Begin
- WriteLn('Enter the matrix');
- Temp := (Size - 1);
- For I := 0 to Temp do
- For J := 0 to Temp do
- for K := 0 to Temp do
- Repeat
- IsCorrect := false;
- WriteLn('Enter element number [', I + 1, '|' , J + 1, '|', K + 1, ']');
- Try
- ReadLn(Arrays[I, J, K]);
- IsCorrect := true;
- Except
- WriteLn('Enter the Number')
- End;
- Until IsCorrect;
- End;
- Function ChooseInput(): Integer;
- Var
- Line: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := true;
- WriteLn('Do you want to input from file? (y/n)');
- ReadLn(line);
- Line := Line.ToLower();
- If(Line <> '') and (Line <> 'y') and (Line <> 'n') then
- Begin
- IsCorrect := false;
- WriteLn('Enter valid answer');
- End;
- Until IsCorrect;
- If (Line = '') or (Line = 'y') then
- ChooseInput := 0
- Else
- ChooseInput := 1;
- End;
- Procedure GetArrayFromFile(Size: Integer; Arrays: TArray);
- Var
- TxtFile: TextFile;
- I, J, Temp: Integer;
- K: Integer;
- Begin
- Temp := Size - 1;
- AssignFile(TxtFile, InputFileLocation());
- Reset(TxtFile);
- For I := 0 to Temp do
- Begin
- For J := 0 to Temp do
- Begin
- For K := 0 to Temp do
- Read(TxtFile, Arrays[I, J, K]);
- ReadLn(TxtFile);
- End;
- Readln(TxtFile);
- End;
- SetLength(Arrays, Temp);
- CloseFile(TxtFile);
- End;
- Procedure ResultPrint(Arrays: TArray; Time: Integer);
- Var
- I, J, K, Temp: Integer;
- Begin
- Writeln('Iteraration number', Time);
- Writeln('__________________');
- for I := 0 to High(Arrays) do
- Begin
- Writeln('Slay number ', I + 1);
- for J := 0 to High(Arrays) do
- Begin
- For K := 0 to High(Arrays) do
- Begin
- Write(arrays[I, J, K], ' ');
- End;
- Writeln;
- End;
- Writeln;
- End;
- End;
- Procedure OutputToFile(Arrays: TArray; Size: Integer);
- Var
- I, J, K, Temp: Integer;
- TxtFile: TextFile;
- Line: String;
- Begin
- Temp := Size - 1;
- AssignFile(TxtFile, InputFileLocation());
- Rewrite(TxtFile);
- Begin
- For I := 0 to Temp do
- Begin
- For J := 0 to Temp do
- Begin
- for K := 0 to Temp do
- Write(TxtFile, Arrays[I, J, K], ' ');
- writeln;
- End;
- Writeln;
- End;
- CloseFile(TxtFile);
- End;
- End;
- Procedure SortMatrix(Arrays: TArray; Size: Integer);
- Var
- I, J, K, I1, J1, K1, Temp, Up, Time:Integer;
- Begin
- Up := Size - 1;
- Time := 0;
- for I := 0 to Up do
- for J := 0 to Up do
- for K := 0 to Up do
- for I1 := 0 to Up do
- for J1 := 0 to Up do
- for K1 := 0 to Up do
- if Arrays[I1, J1, K1] > Arrays[I, J, K] then
- Begin
- Temp := Arrays[I1, J1, K1];
- Arrays[I1, J1, K1] := Arrays[I, J, K];
- Arrays[I, J, K] := Temp;
- Inc(Time);
- ResultPrint(Arrays, Time);
- End;
- End;
- Function DimensionInput(IsNatural: Boolean = false): Integer;
- Var
- IsCorrect: Boolean;
- Size: Integer;
- Begin
- Size := 0;
- WriteLn('Enter the matrix size');
- Repeat
- IsCorrect := false;
- Try
- ReadLn(Size);
- IsCorrect := true;
- Except
- WriteLn('Enter the Number')
- End;
- If IsCorrect and IsNatural and (Size < 1) then
- Begin
- IsCorrect := false;
- WriteLn('Please enter a natural value');
- End;
- Until IsCorrect ;
- DimensionInput := Size;
- End;
- Procedure Main();
- Var
- ChosenInput: Integer;
- MatrixArray: TArray;
- Size : Integer;
- Begin
- WriteLn('This program sorts a three-dimensional array in ascending order.');
- Size := DimensionInput(true);
- ChosenInput := ChooseInput();
- SetLength(MatrixArray, Size, Size, Size);
- If(ChosenInput = 0) then
- GetArrayFromFile(Size, MatrixArray)
- Else
- GetArrayFromConsole(MatrixArray, Size);
- WriteLn;
- Writeln('Sorted Matrix:');
- SortMatrix(MatrixArray, Size);
- OutputToFile(MatrixArray, Size);
- ReadLn;
- End;
- Begin
- Main();
- End.
Add Comment
Please, Sign In to add comment