Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.54 KB | None | 0 0
  1. Program laba2_3;
  2. Uses
  3.   System.SysUtils;
  4. Type
  5.   TMatrixLine = array [1..40] of real;
  6.   Matr = array[1..40] of TMatrixLine;
  7.  
  8. Procedure ReadFile(Var InputFile : Text; Var Matrix: Matr; Var Size: Byte);
  9. Var
  10.   i, j: Byte;
  11.   NameOfFile: String ;
  12.   CorrectFile: Boolean;
  13. Begin
  14.   Repeat
  15.     Writeln('Введите имя файла, с которого хотите считать информацию: ');
  16.     Readln(NameOfFile);
  17.     NameOfFile := NameOfFile + '.txt';
  18.     CorrectFile := True;
  19.     Try
  20.     Assign(InputFile,NameOfFile);
  21.     Reset(InputFile);
  22.     except
  23.       CorrectFile := False;
  24.       Writeln('Не удалось найти файл ',NameOfFile);
  25.     End;
  26.   Until CorrectFile;
  27.   Read(InputFile,Size);
  28.   For i := 1 to Size do
  29.     For j := 1 to Size+1 do
  30.       Read(InputFile,Matrix[i,j]);
  31.   Writeln('Исходная матрица коэффициентов и свободных членов уравнения: ');
  32.   For i := 1 to Size do
  33.     Begin
  34.       For j := 1 to Size+1 do
  35.         Write(Matrix[i,j]:7:2);
  36.         Writeln;
  37.     End;
  38.   Writeln;
  39.   Close(InputFile);
  40. End;
  41.  
  42.  
  43.  
  44. Procedure Sort(Matrix: Matr; Size: integer);
  45. Var
  46.   i,j: Byte;
  47.   BufferLine : TMatrixLine;
  48.   Temp: real;
  49. Begin
  50.   while (i <= Size + 1) do
  51.   Begin
  52.     If((Matrix[i][1]) > (Matrix[i-1][1])) then
  53.     Begin
  54.       BufferLine := Matrix[i-1];
  55.       Matrix[i-1] := Matrix[i];
  56.       Matrix[i] := BufferLine;
  57.       i := 0;
  58.     End;
  59.       i := i + 1;
  60.   End;
  61.  
  62. End;
  63.  
  64.  
  65.  
  66. Procedure MethodGauss(Var Matrix: matr;Var Size: Byte);
  67. Var
  68.   i,j,k: byte;
  69.   temp: real;
  70. Begin
  71.    if Matrix[1,1] <> 1 then
  72.   Begin
  73.     for I := 2 to Size + 1  do
  74.       Matrix[1,i] := Matrix[1,i] / Matrix[1,1];
  75.       Matrix[1,1] := Matrix[1,1] / Matrix[1,1];
  76.   End;
  77.  
  78.   for i := 2 to Size do
  79.   Begin
  80.     temp := Matrix[i,1];
  81.     for j := 1 to Size + 1 do
  82.       Matrix[i,j] := Matrix[i,j] - temp * Matrix[1,j];
  83.   End;
  84.  
  85.   for i := 2 to Size - 1 do
  86.   Begin
  87.     Temp := Matrix[i,2];
  88.     for j := 2 to Size + 1 do
  89.       Matrix[i,j] := Matrix[i,j] / temp;
  90.   End;
  91.  
  92.   for I := 2 to Size - 1 do
  93.   Begin
  94.      for j := 1 to Size + 1 do
  95.         Matrix[i,j] := Matrix[i,j] * (-Matrix[i + 1,2]);
  96.   End;
  97.  
  98.   for i := 2 to Size do
  99.   Begin
  100.      for j := 1 to Size + 1 do
  101.         Matrix[i + 1,j] := Matrix[i + 1,j] + Matrix[i,j];
  102.   End;
  103.  
  104.   Writeln('Полученная треугольная матрица: ');
  105.   For i := 1 to Size do
  106.   Begin
  107.     for j := 1 to Size + 1 do
  108.       Write(Matrix[i,j]:7:2);
  109.       Writeln;
  110.   End;
  111.  
  112. End;
  113.  
  114.  
  115.  
  116.  
  117. Procedure OutputInFile(Var OutputFile: Text;Var Matrix: Matr;Size:Byte);
  118. Var
  119.   i,j: Byte;
  120.   NameOfFile: String;
  121. Begin
  122.   Writeln('Введите имя файла для записи: ');
  123.   Readln(NameOfFile);
  124.   NameOfFile := NameOfFile + '.txt';
  125.   Assign(OutputFile,NameOfFile);
  126.   Rewrite(OutputFile);
  127.   Writeln(OutputFile,'Полученная треугольная матрица: ');
  128.   for i := 1 to Size do
  129.   Begin
  130.     for j := 1 to Size+1 do
  131.       Write(OutputFile,Matrix[i,j]:6:2);
  132.       Writeln(OutputFile);
  133.   End;
  134.   Close(OutputFile);
  135. End;
  136.  
  137.  
  138.  
  139. Var
  140.   Matrix: Matr;
  141.   Size,i,j: Byte;
  142.   InputFile, OutputFile: Text;
  143. Begin
  144.   Writeln('Данная программа выполняет "прямой ход" в решении',
  145.               'системы линейных алгебраических  уравнений методом Гаусса');
  146.   Writeln;
  147.   ReadFile(InputFile,Matrix,Size);
  148.   Sort(Matrix,Size);
  149.   MethodGauss(Matrix,Size);
  150.   OutputInFile(OutputFile,Matrix,Size);
  151.   Readln;
  152. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement