Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program laba2_3;
- Uses
- System.SysUtils;
- Type
- TMatrixLine = array [1..40] of real;
- Matr = array[1..40] of TMatrixLine;
- Procedure ReadFile(Var InputFile : Text; Var Matrix: Matr; Var Size: Byte);
- Var
- i, j: Byte;
- NameOfFile: String ;
- CorrectFile: Boolean;
- Begin
- Repeat
- Writeln('Введите имя файла, с которого хотите считать информацию: ');
- Readln(NameOfFile);
- NameOfFile := NameOfFile + '.txt';
- CorrectFile := True;
- Try
- Assign(InputFile,NameOfFile);
- Reset(InputFile);
- except
- CorrectFile := False;
- Writeln('Не удалось найти файл ',NameOfFile);
- End;
- Until CorrectFile;
- Read(InputFile,Size);
- For i := 1 to Size do
- For j := 1 to Size+1 do
- Read(InputFile,Matrix[i,j]);
- Writeln('Исходная матрица коэффициентов и свободных членов уравнения: ');
- For i := 1 to Size do
- Begin
- For j := 1 to Size+1 do
- Write(Matrix[i,j]:7:2);
- Writeln;
- End;
- Writeln;
- Close(InputFile);
- End;
- Procedure Sort(Matrix: Matr; Size: integer);
- Var
- i,j: Byte;
- BufferLine : TMatrixLine;
- Temp: real;
- Begin
- while (i <= Size + 1) do
- Begin
- If((Matrix[i][1]) > (Matrix[i-1][1])) then
- Begin
- BufferLine := Matrix[i-1];
- Matrix[i-1] := Matrix[i];
- Matrix[i] := BufferLine;
- i := 0;
- End;
- i := i + 1;
- End;
- End;
- Procedure MethodGauss(Var Matrix: matr;Var Size: Byte);
- Var
- i,j,k: byte;
- temp: real;
- Begin
- if Matrix[1,1] <> 1 then
- Begin
- for I := 2 to Size + 1 do
- Matrix[1,i] := Matrix[1,i] / Matrix[1,1];
- Matrix[1,1] := Matrix[1,1] / Matrix[1,1];
- End;
- for i := 2 to Size do
- Begin
- temp := Matrix[i,1];
- for j := 1 to Size + 1 do
- Matrix[i,j] := Matrix[i,j] - temp * Matrix[1,j];
- End;
- for i := 2 to Size - 1 do
- Begin
- Temp := Matrix[i,2];
- for j := 2 to Size + 1 do
- Matrix[i,j] := Matrix[i,j] / temp;
- End;
- for I := 2 to Size - 1 do
- Begin
- for j := 1 to Size + 1 do
- Matrix[i,j] := Matrix[i,j] * (-Matrix[i + 1,2]);
- End;
- for i := 2 to Size do
- Begin
- for j := 1 to Size + 1 do
- Matrix[i + 1,j] := Matrix[i + 1,j] + Matrix[i,j];
- End;
- Writeln('Полученная треугольная матрица: ');
- For i := 1 to Size do
- Begin
- for j := 1 to Size + 1 do
- Write(Matrix[i,j]:7:2);
- Writeln;
- End;
- End;
- Procedure OutputInFile(Var OutputFile: Text;Var Matrix: Matr;Size:Byte);
- Var
- i,j: Byte;
- NameOfFile: String;
- Begin
- Writeln('Введите имя файла для записи: ');
- Readln(NameOfFile);
- NameOfFile := NameOfFile + '.txt';
- Assign(OutputFile,NameOfFile);
- Rewrite(OutputFile);
- Writeln(OutputFile,'Полученная треугольная матрица: ');
- for i := 1 to Size do
- Begin
- for j := 1 to Size+1 do
- Write(OutputFile,Matrix[i,j]:6:2);
- Writeln(OutputFile);
- End;
- Close(OutputFile);
- End;
- Var
- Matrix: Matr;
- Size,i,j: Byte;
- InputFile, OutputFile: Text;
- Begin
- Writeln('Данная программа выполняет "прямой ход" в решении',
- 'системы линейных алгебраических уравнений методом Гаусса');
- Writeln;
- ReadFile(InputFile,Matrix,Size);
- Sort(Matrix,Size);
- MethodGauss(Matrix,Size);
- OutputInFile(OutputFile,Matrix,Size);
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement