Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Laba_33;
- Uses
- SysUtils;
- Const
- MIN: Integer = 2;
- MAX: Integer = 100;
- MIN_ELEMENT: Real = -100;
- MAX_ELEMENT: Real = 100;
- Type
- TA = Array Of Array Of Real;
- TB = Array Of Real;
- TC = Array Of Real;
- Function InputAndCheckChoiceOne(IsCorrect: Boolean): Integer;
- Var
- ChoiceOne: Integer;
- Begin
- ChoiceOne := 0;
- Repeat
- IsCorrect := True;
- Try
- Readln(ChoiceOne);
- Except
- IsCorrect := False;
- Writeln('Введите 1 или 2.');
- End;
- If IsCorrect And ((ChoiceOne <> 1) And (ChoiceOne <> 2)) Then
- Begin
- Writeln('Введите 1 или 2.');
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputAndCheckChoiceOne := ChoiceOne;
- End;
- Function InputAndCheckChoiceTwo(IsCorrect: Boolean): Integer;
- Var
- ChoiceTwo: Integer;
- Begin
- ChoiceTwo := 0;
- Repeat
- IsCorrect := True;
- Try
- Readln(ChoiceTwo);
- Except
- IsCorrect := False;
- Writeln('Введите 1 или 2.');
- End;
- If IsCorrect And ((ChoiceTwo <> 1) And (ChoiceTwo <> 2)) Then
- Begin
- Writeln('Введите 1 или 2.');
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputAndCheckChoiceTwo := ChoiceTwo;
- End;
- Function InputAndCheckN(IsCorrect: Boolean): Integer;
- Var
- N: Integer;
- Begin
- N := 0;
- Repeat
- IsCorrect := True;
- Writeln('Введите порядок матрицы: ');
- Try
- Readln(N);
- Except
- IsCorrect := False;
- Writeln('Введите число.');
- End;
- If IsCorrect And ((N < MIN) And (N > MAX)) Then
- Begin
- Writeln('Введите число от ', MIN, ' до ', MAX);
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputAndCheckN := N;
- End;
- Function IsFileExists(Const Path: String): Boolean;
- Begin
- IsFileExists := FileExists(Path);
- If Not FileExists(Path) Then
- Writeln('Файл не существует.');
- End;
- Function IsFileTxt(Const Path: String): Boolean;
- Var
- FileType: String;
- Begin
- If (Length(Path) > 4) Then
- Begin
- FileType := Path.Substring(Path.Length - 4);
- If (FileType = '.txt') Then
- IsFileTxt := True
- Else
- Begin
- WriteLn('Файл не является текстовым. ');
- IsFileTxt := False;
- End;
- End
- Else
- Begin
- WriteLn('Введите корректное имя файла. ');
- IsFileTxt := False;
- End;
- End;
- Function IsFileNotEmpty(Var MyFile: TextFile): Boolean;
- Var
- IsNotEmpty: Boolean;
- Begin
- Try
- Try
- Reset(MyFile);
- IsNotEmpty := Not Eof(MyFile);
- If (Not IsNotEmpty) Then
- Writeln('Этот файл пустой.');
- Except
- Writeln('Непредвиденная ошибка.');
- IsNotEmpty := False;
- End;
- Finally
- CloseFile(MyFile);
- End;
- IsFileNotEmpty := IsNotEmpty;
- End;
- Function IsFileWritable(Var MyFile: TextFile): Boolean;
- Begin
- IsFileWritable := True;
- Try
- Rewrite(MyFile);
- Close(MyFile);
- Except
- IsFileWritable := False;
- Writeln('Этот файл невозможно открыть для записи.');
- End;
- End;
- Function IsFileReadble(Var MyFile: TextFile): Boolean;
- Begin
- IsFileReadble := True;
- Try
- Reset(MyFile);
- Close(MyFile);
- Except
- IsFileReadble := False;
- Writeln('Этот файл невозможно открыть для чтения.');
- End;
- End;
- Function RequestPathForWriting(Var MyFile: TextFile): String;
- Var
- Path: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- Writeln('Введите имя файла с путём: ');
- Readln(Path);
- AssignFile(MyFile, Path);
- IsCorrect := IsFileExists(Path) And IsFileTxt(Path) And IsFileWritable(MyFile);
- Until IsCorrect;
- RequestPathForWriting := Path;
- End;
- Function RequestPathForReading(): String;
- Var
- Path: String;
- IsCorrect: Boolean;
- MyFile: TextFile;
- Begin
- Repeat
- Writeln('Введите имя файла с путём: ');
- Readln(Path);
- AssignFile(MyFile, Path);
- IsCorrect := IsFileExists(Path) And IsFileTxt(Path) And IsFileReadble(MyFile) And IsFileNotEmpty(MyFile);
- Until IsCorrect;
- RequestPathForReading := Path;
- End;
- Procedure CreateMatrix(N: Integer; Var A: TA; IsCorrect: Boolean);
- Var
- I, J: Integer;
- Begin
- SetLength(A, N, N);
- For I := 0 To High(A) Do
- For J := 0 To High(A) Do
- Begin
- Repeat
- IsCorrect := True;
- Writeln('Введите элементы матрицы: ');
- Try
- Readln(A[I][J]);
- Except
- IsCorrect := False;
- Writeln('Введите число.');
- End;
- If IsCorrect And ((A[I][J] < MIN_ELEMENT) And (A[I][J] > MAX_ELEMENT)) Then
- Begin
- Writeln('Введите число от ', MIN_ELEMENT, ' до ', MAX_ELEMENT);
- IsCorrect := False;
- End;
- Until IsCorrect;
- End;
- End;
- Procedure CreateRowVector(N: Integer; Var B: TB; IsCorrect: Boolean);
- Var
- J: Integer;
- Begin
- SetLength(B, N);
- For J := 0 To High(B) Do
- Begin
- Repeat
- IsCorrect := True;
- Writeln('Введите элементы вектора-строки: ');
- Try
- Readln(B[J]);
- Except
- IsCorrect := False;
- Writeln('Введите число.');
- End;
- If IsCorrect And ((B[J] < MIN_ELEMENT) And (B[J] > MAX_ELEMENT)) Then
- Begin
- Writeln('Введите число от ', MIN_ELEMENT, ' до ', MAX_ELEMENT);
- IsCorrect := False;
- End;
- Until IsCorrect;
- End;
- End;
- Procedure MultiplyVectorAndMatrix(N: Integer; Var C: TC; Const A: TA; Const B: TB);
- Var
- I, J: Integer;
- Begin
- SetLength(C, N);
- For J := 0 To High(C) Do
- C[J] := 0;
- For J := 0 To High(C) Do
- For I := 0 To High(B) Do
- C[J] := C[J] + A[I][J] * B[I];
- End;
- Procedure PrintResultToConsole(Const C: TC; Const N: Integer);
- Var
- J: Integer;
- Begin
- For J := 0 To High(C) Do
- Write(C[J]:8:6, ' ');
- Writeln;
- End;
- Function InputSizeFromFile(Var Size: Integer; Var MyFile: TextFile): Boolean;
- Var
- IsCorrect: Boolean;
- Begin
- IsCorrect := True;
- Try
- Readln(MyFile, Size);
- Except
- Writeln('Ошибка в первой строке — неправильный размер матрицы');
- IsCorrect := False;
- End;
- If IsCorrect Then
- Begin
- IsCorrect := (Size > MIN) And (Size < MAX);
- If Not IsCorrect Then
- Begin
- Writeln('Размер не входит в диапазон от ', MIN, ' до ', MAX);
- End;
- End;
- InputSizeFromFile := IsCorrect;
- End;
- Function ReadMatrix(Var MyFile: TextFile; Var A: TA; Const Size: Integer): Boolean;
- Var
- I, J: Integer;
- IsCorrect: Boolean;
- Begin
- SetLength(A, Size, Size);
- IsCorrect := True;
- Try
- For I := 0 To High(A) Do
- For J := 0 To High(A[I]) Do
- Read(MyFile, A[I][J]);
- Except
- Writeln('Неправильный элемент в [', I,'][',J,']');
- IsCorrect := False;
- End;
- ReadMatrix := IsCorrect;
- End;
- Function ReadRowVector(Var MyFile: TextFile; Var B: TB; Const Size: Integer): Boolean;
- Var
- I, J: Integer;
- IsCorrect: Boolean;
- Begin
- SetLength(B, Size);
- IsCorrect := True;
- Try
- For J := 0 To High(B) Do
- Read(MyFile, B[J]);
- Except
- Writeln('Непредвиденная ошибка при чтении вектора в [', J, ']');
- IsCorrect := False;
- End;
- ReadRowVector := IsCorrect;
- End;
- Procedure MultiplyVectorAndMartixFromFile(Var C: TC; Const A: TA; Const B: TB; Const Size: Integer);
- Var
- I, J: Integer;
- Begin
- SetLength(C, Size);
- For J := 0 To High(C) Do
- Begin
- C[J] := 0;
- For I := 0 To High(C) Do
- Begin
- C[J] := C[J] + A[I][J] * B[I];
- End;
- End;
- End;
- Procedure PrintResultFromFileToConsole(Const C: TC; Const Size: Integer);
- Var
- J: Integer;
- Begin
- For J := 0 To High(C) Do
- Writeln(C[J]:8:6);
- End;
- Function PrintResultFromFileToFile(Const C: TC; Const Size: Integer; Var MyFile: TextFile; IsCorrect: Boolean): Boolean;
- Var
- J: Integer;
- Begin
- IsCorrect := True;
- Try
- Try
- Rewrite(MyFile);
- For J := 0 To High(C) Do
- Begin
- Writeln(MyFile, C[J]:8:6);
- End;
- Except
- WriteLn('Непредвиденная ошибка.');
- IsCorrect := False;
- End;
- Finally
- CloseFile(MyFile);
- End;
- PrintResultFromFileToFile := IsCorrect;
- End;
- Function PrintResultToFile(Const C: TC; Var MyFile: TextFile; Const N: Integer; IsCorrect: Boolean): Boolean;
- Var
- J: Integer;
- Begin
- IsCorrect := True;
- Try
- Try
- Rewrite(MyFile);
- For J := 0 To High(C) Do
- Begin
- Writeln(MyFile, C[J]:8:6);
- End;
- Except
- WriteLn('Непредвиденная ошибка.');
- IsCorrect := False;
- End;
- Finally
- CloseFile(MyFile);
- End;
- PrintResultToFile := IsCorrect;
- End;
- Function InputArgumentsFromFile(Const Path: String; Var N: Integer; Var A: TA; Var B: TB; Var C: TC): Boolean;
- Var
- IsCorrect: Boolean;
- MyFile: TextFile;
- Begin
- IsCorrect := True;
- Try
- AssignFile(MyFile, Path);
- Reset(MyFile);
- Except
- IsCorrect := False;
- End;
- If IsCorrect Then
- Begin
- IsCorrect := InputSizeFromFile(N, MyFile);
- If IsCorrect Then
- Begin
- IsCorrect := ReadMatrix(MyFile, A, N);
- If IsCorrect Then
- Begin
- IsCorrect := ReadRowVector(MyFile, B, N);
- If IsCorrect Then
- Begin
- MultiplyVectorAndMartixFromFile(C, A, B, N);
- End;
- End;
- End;
- End;
- CloseFile(MyFile);
- InputArgumentsFromFile := IsCorrect;
- End;
- Var
- N: Integer;
- IsCorrect: Boolean;
- ChoiceOne: Integer;
- ChoiceTwo: Integer;
- A: TA;
- B: TB;
- C: TC;
- MyFile: TextFile;
- Path: String;
- Begin
- N := 0;
- IsCorrect := False;
- Writeln('Данная программа перемножает строку порядка n на матрицу n * n.', #13#10, 'Откуда Вы хотите выводить данные?', #13#10,
- '1 - с консоли, 2 - из файла.');
- ChoiceOne := InputAndCheckChoiceOne(IsCorrect);
- Writeln('Куда Вы хотите записать результат?', #13#10, '1 - в консоль, 2 - в файл.');
- ChoiceTwo := InputAndCheckChoiceTwo(IsCorrect);
- If (ChoiceOne = 1) Then
- Begin
- N := InputAndCheckN(IsCorrect);
- CreateMatrix(N, A, IsCorrect);
- CreateRowVector(N, B, IsCorrect);
- MultiplyVectorAndMatrix(N, C, A, B);
- If (ChoiceTwo = 1) Then
- Begin
- PrintResultToConsole(C, N);
- End
- Else
- Begin
- IsCorrect := False;
- Repeat
- Path := RequestPathForWriting(MyFile);
- PrintResultToFile(C, MyFile, N, IsCorrect);
- Until IsCorrect;
- End;
- End
- Else
- Begin
- IsCorrect := False;
- Repeat
- Path := RequestPathForReading();
- IsCorrect := InputArgumentsFromFile(Path, N, A, B, C);
- If IsCorrect Then
- Begin
- If (ChoiceTwo = 1) Then
- Begin
- PrintResultFromFileToConsole(C, N);
- End
- Else
- Begin
- IsCorrect := False;
- Repeat
- PrintResultFromFileToFile(C, N, MyFile, IsCorrect);
- Until IsCorrect;
- End;
- End;
- Until IsCorrect;
- End;
- Readln;
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement