Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Polina;
- Uses
- SysUtils;
- Type
- Matrix = Array Of Array Of Real;
- Procedure PrintTask();
- Begin
- Writeln('This program counts the number of columns in a given matrix whose elements are arranged in ascending order.');
- End;
- Function UserChoice(): Byte;
- Var
- Choice: Byte;
- IsCorrect: Boolean;
- Begin Choice := 0;
- Writeln('Select the input method : 1 - console, 2 - file: ');
- Repeat
- Try
- IsCorrect := True;
- Readln(Choice);
- Except
- IsCorrect := False;
- End;
- If (Choice < 1) Or (Choice > 2) Or Not IsCorrect Then
- Begin
- Writeln('Select the input method : 1 - console, 2 - file: ');
- IsCorrect := False;
- End;
- Until (IsCorrect);
- UserChoice := Choice;
- End;
- Function EnterSizeFromConsole(): Integer;
- Const
- MIN_N = 2;
- Var
- N: Integer;
- IsCorrect: Boolean;
- Begin
- N := 2;
- Writeln('Enter matrix order N: ');
- Repeat
- IsCorrect := True;
- Try
- Readln(N);
- Except
- IsCorrect := False;
- Writeln('Enter correct value: ');
- End;
- If (IsCorrect) And (N < MIN_N) Then
- Begin
- IsCorrect := False;
- Writeln('Enter correct value: ');
- End;
- Until IsCorrect;
- EnterSizeFromConsole := N;
- End;
- Function EnterSizeFromFile(Var PathToFile : String): Integer;
- Const
- MIN_N = 2;
- Var
- N: Integer;
- IsCorrect: Boolean;
- T : TextFile;
- Begin
- Assign(T, PathToFile);
- Reset(T);
- N := 2;
- IsCorrect := True;
- Try
- Readln(T, N);
- Except
- IsCorrect := False;
- End;
- If (IsCorrect) And (N < MIN_N) Then
- Begin
- IsCorrect := False;
- Writeln('Размер матрицы в файле не корректен');
- N := EnterSizeFromConsole();
- End;
- CloseFile(T);
- EnterSizeFromFile := N;
- End;
- Function ReadPathToFile() : String;
- Var
- PathToFile: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Write('Введите путь к файлу с расширением.txt с количеством точек и координатами точек: ');
- Readln(PathToFile);
- If ExtractFileExt(PathToFile) <> '.txt' Then
- Begin
- Writeln('Расширение файла не .txt!');
- IsCorrect := False;
- End;
- Until(IsCorrect);
- ReadPathToFile := PathToFile;
- End;
- Function IsExists(PathToFile : String) : Boolean;
- Var
- IsRight: Boolean;
- Begin
- IsRight := False;
- If FileExists(PathToFile) Then
- IsRight := True;
- IsExists := IsRight;
- End;
- Function IsNotAbleToReading(Var T: TextFile) : Boolean;
- Var
- IsRight: Boolean;
- Begin
- IsRight := False;
- Try
- Reset(T);
- CloseFile(T);
- Except
- IsRight := True;
- End;
- IsNotAbleToReading := IsRight;
- End;
- Function IsNotAbleToWriting(PathToFile: String) : Boolean;
- Var
- IsRight: Boolean;
- Begin
- IsRight := False;
- If FileIsReadOnly(PathToFile) Then
- IsRight := True;
- IsNotAbleToWriting := IsRight;
- End;
- Function IsEmpty(Var T: TextFile) : Boolean;
- Var
- IsRight: Boolean;
- Begin
- IsRight := False;
- Reset(T);
- If EOF(T) Then
- IsRight := True;
- CloseFile(T);
- IsEmpty := IsRight;
- End;
- Function GetFileNormalReading() : String;
- Var
- IsCorrect: Boolean;
- T : TextFile;
- PathToFile: String;
- Begin
- Repeat
- IsCorrect := True;
- PathToFile := ReadPathToFile();
- If Not IsExists(PathToFile) Then
- Begin
- IsCorrect := False;
- Writeln('Проверьте корректность ввода пути к файлу!');
- End;
- If IsCorrect Then
- AssignFile(T, PathToFile);
- If IsCorrect And IsNotAbleToReading(T) Then
- Begin
- IsCorrect := False;
- Writeln('Файл закрыт для чтения!');
- End;
- If IsCorrect And IsEmpty(T) Then
- Begin
- IsCorrect := False;
- WriteLn('Файл пуст!');
- End;
- Until IsCorrect;
- GetFileNormalReading := PathToFile;
- End;
- Function GetFileNormalWriting() : String;
- Var
- T : TextFile;
- IsCorrect: Boolean;
- PathToFile: String;
- Begin
- Repeat
- IsCorrect := True;
- PathToFile := ReadPathToFile();
- If Not IsExists(PathToFile) Then
- Begin
- IsCorrect := False;
- Writeln('Проверьте корректность ввода пути к файлу!');
- End;
- If IsCorrect Then
- AssignFile(T, PathToFile);
- If IsCorrect And IsNotAbleToWriting(PathToFile) Then
- Begin
- IsCorrect := False;
- WriteLn('Файл закрыт для записи!');
- End;
- Until IsCorrect;
- GetFileNormalWriting := PathToFile;
- End;
- Function EnterMatrixFromConsole(N: Integer) : Matrix;
- Var
- IsCorrect: Boolean;
- OutputMatrix: Matrix;
- Begin
- SetLength(OutputMatrix, N, N);
- Dec(N);
- For Var I := 0 To N Do
- For Var J := 0 To N Do
- Begin
- Write('Введите элемент матрицы Matrix[', I + 1, ',', J + 1, ']: ');
- Repeat
- IsCorrect := True;
- Try
- Read(OutputMatrix[I][J]);
- Except
- IsCorrect := False;
- Writeln('Ошибка! Введите корректное значение: ');
- End;
- Until IsCorrect;
- End;
- EnterMatrixfromConsole := OutputMatrix;
- End;
- Function FindTheSolution(MatrixForSolution : Matrix; N : Integer) : Integer;
- Var
- Increasing : Boolean;
- IncreasingCount : Integer;
- Begin
- IncreasingCount := 0;
- Dec(N);
- For Var J := 0 to N do
- Begin
- Increasing := true;
- For Var I := 1 to N do
- If MatrixForSolution[I-1, j] > MatrixForSolution[I, J] then
- Increasing := False;
- If Increasing then
- Inc(IncreasingCount);
- End;
- FindTheSolution := IncreasingCount;
- End;
- Function ReadMatrix(WayToFile: String; N: Integer) : Matrix;
- Var
- MyFile: TextFile;
- NullElement : Char;
- InputMatrix : Matrix;
- IsEndOfFile, IsCorrect : Boolean;
- Begin
- Assign(MyFile, WayToFile);
- Reset(MyFile);
- Read(MyFile, NullElement);
- SetLength(InputMatrix, N, N);
- Dec(N);
- For Var I := 0 To N Do
- Begin
- If Not EOF(MyFile) Then
- Begin
- For Var J := 0 To N Do
- Begin
- If Not EOF(MyFile) Then
- Begin
- Try
- Read(MyFile, InputMatrix[I][J]);
- Except
- IsCorrect := False;
- Writeln('Ошибка! Введите корректное значение: ');
- End;
- Write(Format('%8.1f', [InputMatrix[I][J]]));
- End
- Else
- Begin
- Repeat
- IsCorrect := True;
- Try
- Read(InputMatrix[I][J]);
- Except
- IsCorrect := False;
- Writeln('Ошибка! Введите корректное значение: ');
- End;
- Until IsCorrect;
- End;
- End;
- Writeln;
- End;
- End;
- CloseFile(MyFile);
- ReadMatrix := InputMatrix;
- End;
- Procedure Input(Var Size : Integer; Var InputMatrix : Matrix);
- Var
- Choose : Integer;
- PathToFile : String;
- Begin
- Choose := UserChoice();
- If (Choose = 1) Then
- Begin
- Size := EnterSizeFromConsole();
- InputMatrix := EnterMatrixFromConsole(Size);
- End
- Else
- Begin
- PathToFile := GetFileNormalReading();
- Size := EnterSizeFromFile(PathToFile);
- InputMatrix := ReadMatrix(PathToFile, Size);
- End;
- End;
- Procedure OutputInFile(PathToFile : String; Answer : Integer);
- Var
- T : TextFile;
- Begin
- AssignFile(T, PathToFile);
- Rewrite(T);
- Write(T, 'Какой-то текст: ');
- Write(T, Answer);
- CloseFile(T);
- End;
- Procedure Output(Answer : Integer);
- Var
- Choose : Integer;
- PathToFile : String;
- Begin
- Choose := UserChoice();
- If (Choose = 1) Then
- Writeln('Какой-то текст: ', Answer)
- Else
- Begin
- PathToFile := GetFileNormalWriting();
- Writeln('Какой-то текст: ', Answer);
- OutputInFile(PathToFile, Answer);
- End;
- Readln;
- End;
- Var
- Size, Answer : Integer;
- MatrixForSolution : Matrix;
- Begin
- PrintTask();
- Input(Size, MatrixForSolution);
- Answer := FindTheSolution(MatrixForSolution, Size);
- Output(Answer);
- End.
Advertisement
Add Comment
Please, Sign In to add comment