Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project1;
- uses
- System.SysUtils;
- type
- TMatrix = array of array of Shortint;
- TVector = array of Integer;
- TChoiceInput = (Randomly, FromConsole, FromFile);
- TChoiceOutput = (ToFile, ToConsole);
- function Confirm(): Boolean;
- var
- IsCorrect: Boolean;
- Input: Char;
- begin
- Writeln('Enter Y if Yes and N if No');
- repeat
- IsCorrect := True;
- Readln(Input);
- Input := UpCase(Input);
- if not (Input in ['Y', 'N']) then
- begin
- IsCorrect := False;
- Writeln('Invalid input! Please, try again:');
- end;
- until IsCorrect;
- Confirm := Input = 'Y';
- end;
- function GetInputWay(): TChoiceInput;
- var
- Input: Char;
- IsCorrect: Boolean;
- begin
- repeat
- IsCorrect := True;
- Readln(Input);
- if not (Input in ['1'..'3']) then
- begin
- IsCorrect := False;
- Writeln('Invalid answer! Please, try again:');
- end
- else
- case Input of
- '1' : GetInputWay := Randomly;
- '2' : GetInputWay := FromConsole;
- '3' : GetInputWay := FromFile;
- end;
- until IsCorrect;
- end;
- function GetOutputWay(): TChoiceOutput;
- var
- Input: Char;
- IsCorrect: Boolean;
- begin
- repeat
- IsCorrect := True;
- Readln(Input);
- if not (Input in ['1'..'2']) then
- begin
- IsCorrect := False;
- Writeln('Invalid answer! Please, try again:');
- end
- else
- case Input of
- '1' : GetOutputWay := ToFile;
- '2' : GetOutputWay := ToConsole;
- end;
- until IsCorrect;
- end;
- function GetFileName(): string;
- var
- FileName: string;
- IsCorrect: Boolean;
- begin
- Writeln('Enter the name of your file:');
- repeat
- IsCorrect := True;
- Readln(FileName);
- if not FileExists(FileName) then
- begin
- IsCorrect := False;
- Writeln('Invalid input or file doesn''t exist! Please, try again:');
- end;
- until IsCorrect;
- GetFileName := FileName;
- end;
- function GetMatrixDim(): Integer;
- const
- MaxDim = 100;
- MinDim = 1;
- var
- Dim: Integer;
- IsError: Boolean;
- begin
- Writeln('Enter the dimension of your matrix:');
- repeat
- IsError := False;
- try
- Readln(Dim);
- if (Dim > MaxDim) or (Dim < MinDim) then
- begin
- IsError := True;
- Writeln('Invalid range! Please, try again:');
- end;
- except
- IsError := True;
- Writeln('Invalid input! Please, try again:');
- end;
- until not IsError;
- GetMatrixDim := Dim;
- end;
- function RandomMatrix(Matrix: TMatrix): TMatrix;
- var
- i, j: Integer;
- begin
- Randomize;
- for i := 0 to High(Matrix) do
- for j := 0 to High(Matrix) do
- Matrix[i, j] := - 128 + Random(257);
- RandomMatrix := Matrix;
- end;
- function FillMatrixConsole(Matrix: TMatrix): TMatrix;
- const
- MaxValue = 127;
- MinValue = -128;
- var
- i, j: Integer;
- IsCorrect: Boolean;
- begin
- Writeln('Enter the values of matrix''s elements (integer from [', MinValue, '..', MaxValue, ']):');
- for i := 0 to High(Matrix) do
- for j := 0 to High(Matrix) do
- repeat
- IsCorrect := True;
- try
- Readln(Matrix[i, j]);
- if (Matrix[i, j] > MaxValue) or (Matrix[i, j] < MinValue) then
- begin
- IsCorrect := False;
- Writeln('Invalid range! Please, try again:');
- end;
- except
- IsCorrect := False;
- Writeln('Invalid input! Please, repeat:');
- end;
- until IsCorrect;
- FillMatrixConsole := Matrix;
- end;
- function ReadMatrixFile(FileName: string; var Error: Boolean): TMatrix;
- const
- MaxDim = 100;
- MinDim = 1;
- MaxValue = 127;
- MinValue = -128;
- var
- Matrix: TMatrix;
- i, j: Integer;
- DataFile: TextFile;
- Dim: Integer;
- begin
- Error := False;
- try
- AssignFile(DataFile, FileName);
- Reset(DataFile);
- Read(DataFile, Dim);
- if (Dim < MinDim) or (Dim > MaxDim) then
- begin
- Error := True;
- Writeln('Dimension out of range, try again!');
- end
- else
- begin
- i := 0;
- SetLength(Matrix, Dim, Dim);
- while (i < Length(Matrix)) and not Error do
- begin
- j := 0;
- while (j < Length(Matrix)) and not Error do
- begin
- Read(DataFile, Matrix[i, j]);
- if (Matrix[i, j] > MaxValue) or (Matrix[i, j] < MinValue) then
- begin
- Error := True;
- Writeln('Values are out of range, try again:');
- end
- else
- Inc(j);
- end;
- Inc(i);
- end;
- end;
- except
- Error := True;
- Writeln('Invalid file data or file not available! Plese, try again:');
- end;
- CloseFile(DataFile);
- ReadMatrixFile := Matrix;
- end;
- function GetMatrixFile(): TMatrix;
- var
- IsError: Boolean;
- FileName: string;
- begin
- Writeln('Create txt file with the values of matrix dimension and elements.');
- repeat
- FileName := GetFileName;
- GetMatrixFile := ReadMatrixFIle(FileName, IsError);
- until not IsError;
- end;
- function BuiltVector(Matrix: TMatrix): TVector;
- var
- i, j: Integer;
- TempVector: TVector;
- begin
- Setlength(TempVector, Length(Matrix));
- for i := 0 to High(Matrix) do
- begin
- TempVector[i] := Matrix[i, 0];
- for j := 1 to High(Matrix) do
- if Matrix[i, j] > TempVector[i] then
- TempVector[i] := Matrix[i, j];
- end;
- BuiltVector := TempVector;
- end;
- procedure WriteVectorToFile(Vector: TVector; FileName: string; var Error: Boolean);
- var
- i: Byte;
- OutputFile: TextFile;
- begin
- try
- Error := False;
- AssignFile(OutputFile, FileName);
- Rewrite(OutputFile);
- for i := 0 to High(Vector) do
- Writeln(OutputFile, Vector[i]);
- except
- Error := True;
- Writeln('An error occurred, try again:');
- end;
- CloseFile(OutputFile);
- end;
- procedure OutputVectorFile(Vector: TVector);
- var
- IsError: Boolean;
- FileName: string;
- begin
- Writeln('Create file for output and enter it''s name:');
- repeat
- FileName := GetFileName;
- WriteVectorToFile(Vector, FileName, IsError);
- until not IsError;
- Writeln('Answer is successfully saved in your file!');
- end;
- procedure OutputConsole(Vector: TVector);
- var
- i: Byte;
- begin
- Writeln('Vector, built of max values:');
- for i := 0 to High(Vector) do
- Writeln(Vector[i]);
- end;
- procedure Main();
- var
- MainMatrix: TMatrix;
- MainVector: TVector;
- InputWay: TChoiceInput;
- OutputWay: TChoiceOutput;
- MatrixDim: Integer;
- begin
- Writeln('Choose how do you want to fill up the matrix:');
- Writeln('1.Randomly');
- Writeln('2.From console');
- Writeln('3.From file');
- InputWay := GetInputWay;
- if InputWay = Randomly then
- begin
- MatrixDim := GetMatrixDim;
- SetLength(MainMatrix, MatrixDim, MatrixDim);
- MainMatrix := RandomMatrix(MainMatrix);
- end
- else
- if InputWay = FromConsole then
- begin
- MatrixDim := GetMatrixDim;
- SetLength(MainMatrix, MatrixDim, MatrixDim);
- MainMatrix := FillMatrixConsole(MainMatrix);
- end
- else
- if InputWay = FromFile then
- begin
- MainMatrix := GetMatrixFile;
- MatrixDim := Length(MainMatrix);
- end;
- MainVector := BuiltVector(MainMatrix);
- Writeln('Choose how do you want to output data: ');
- Writeln('1.To File');
- Writeln('2.To Console');
- OutputWay := GetOutputWay;
- if OutputWay = ToFile then
- begin
- OutputVectorFile(MainVector);
- OutputConsole(MainVector);
- end
- else
- if OutputWay = ToConsole then
- OutputConsole(MainVector);
- end;
- begin
- Main;
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement