Advertisement
Alyks

Untitled

Nov 17th, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.69 KB | None | 0 0
  1. program task3;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.     System.SysUtils, Math, Classes;
  8.  
  9. type
  10.     Arr = Array of Double;
  11.     TwoDimArr = Array of Arr;
  12.  
  13. var
  14.     MatrixRows, MatrixCols, VectorLength: Integer;
  15.     VectorType, FilePath, Res: String;
  16.     Vector: Arr;
  17.     Matrix, Product: TwoDimArr;
  18.     InputFile, OutputFile: TextFile;
  19.     NotCorrect: Boolean;
  20.  
  21. procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings);
  22. begin
  23.     ListOfStrings.Clear();
  24.     ListOfStrings.Delimiter := Delimiter;
  25.     ListOfStrings.DelimitedText := Str;
  26. end;
  27.  
  28. function ReadNextLine(const FileToRead: TextFile): String;
  29. var
  30.     Line: String;
  31. begin
  32.     Line := '';
  33.     try
  34.         Readln(FileToRead, Line);
  35.     except
  36.         Writeln('Возникла ошибка при чтении файла.');
  37.     end;
  38.     ReadNextLine := Line;
  39. end;
  40.  
  41. function ShowMatrix(const Matrix: TwoDimArr): String;
  42. var
  43.     i, j: Integer;
  44.     El: Double;
  45.     Res: String;
  46. begin
  47.     for i := 0 to High(Matrix) do
  48.     begin
  49.         for j := 0 to High(Matrix[i]) do
  50.         begin
  51.             El := Matrix[i, j];
  52.             Write(El:5:2, ' ');
  53.             Res := Res + FloatToStr(El) + ' ';
  54.         end;
  55.  
  56.         Writeln;
  57.     end;
  58.     ShowMatrix := Res;
  59. end;
  60.  
  61. procedure ShowVector(const Vector: Arr);
  62. var
  63.     i: Integer;
  64. begin
  65.     for i := 0 to High(Vector) do
  66.         Write(Vector[i]:5:2, ' ');
  67.     Writeln;
  68. end;
  69.  
  70. function TakeRow(Str: String; Size: Integer): Arr;
  71. var
  72.     i, Len, El, errPos: Integer;
  73.     Row: Arr;
  74.     NumsStr: String;
  75.     OutputList: TStringList;
  76. begin
  77.     SetLength(Row, Size);
  78.     OutputList := TStringList.Create();
  79.     Split(' ', Str, OutputList);
  80.     Len := Size - 1;
  81.  
  82.     if (Len = OutputList.Count - 1) then
  83.         for i := 0 to Len do
  84.         begin
  85.             Val(OutputList[i], El, errPos);
  86.             if (errPos = 0) then
  87.                 Row[i] := El;
  88.         end
  89.     else
  90.         Writeln('Количество столбцов, введенных в файле, должно совпадать с количеством столбцов в матрице');
  91.  
  92.     TakeRow := Row;
  93. end;
  94.  
  95. function TakeVector(const InputFile: TextFile; var VectorType: String): Arr;
  96. var
  97.     Vector: Arr;
  98.     VectorLength: Integer;
  99.     CurrentString: String;
  100.     NotCorrect, Stop: Boolean;
  101. begin
  102.     NotCorrect := true;
  103.     Stop := false;
  104.     Reset(InputFile);
  105.     while (NotCorrect and (Not Stop)) do
  106.     begin
  107.         if (EOF(InputFile)) then
  108.         begin
  109.             Writeln('Вы не указали тип вектора');
  110.             Stop := true;
  111.         end
  112.  
  113.         else
  114.         begin
  115.             VectorType := ReadNextLine(InputFile);
  116.             if ((VectorType = 'вектор-столбец') or
  117.                 (VectorType = 'вектор-строка')) then
  118.                 NotCorrect := false;
  119.         end;
  120.     end;
  121.  
  122.     if (Not NotCorrect) then
  123.     begin
  124.         VectorLength := StrToInt(ReadNextLine(InputFile));
  125.         SetLength(Vector, VectorLength);
  126.         CurrentString := ReadNextLine(InputFile);
  127.         Vector := TakeRow(CurrentString, VectorLength);
  128.     end;
  129.     TakeVector := Vector;
  130. end;
  131.  
  132. function TakeMatrixFromFile(const InputFile: TextFile): TwoDimArr;
  133. var
  134.     CurrentString: String;
  135.     MatrixRows, MatrixCols, i: Integer;
  136.     Matrix: TwoDimArr;
  137.     MatrixRow: Arr;
  138. begin
  139.     i := 0;
  140.     Reset(InputFile);
  141.  
  142.     MatrixRows := StrToInt(ReadNextLine(InputFile));
  143.     MatrixCols := StrToInt(ReadNextLine(InputFile));
  144.  
  145.     SetLength(Matrix, MatrixRows, MatrixCols);
  146.  
  147.     while (i < MatrixRows) do
  148.     begin
  149.         CurrentString := ReadNextLine(InputFile);
  150.         MatrixRow := TakeRow(CurrentString, MatrixCols);
  151.         Matrix[i] := MatrixRow;
  152.         i := i + 1;
  153.     end;
  154.  
  155.     CloseFile(InputFile);
  156.     TakeMatrixFromFile := Matrix;
  157. end;
  158.  
  159. function TakeProduct(const Matrix: TwoDimArr; const Vector: Arr;
  160.     VectorLength: Integer): TwoDimArr;
  161. var
  162.     i, j, k: Integer;
  163.     Product: TwoDimArr;
  164. begin
  165.     SetLength(Product, VectorLength, Length(Matrix[0]));
  166.     for i := 0 to High(Product) do
  167.         for j := 0 to High(Matrix) do
  168.             for k := 0 to High(Matrix[j]) do
  169.                 Product[i, k] := Product[i, k] +
  170.                     Vector[IfThen(VectorLength = 1, j, i)] * Matrix[j, k];
  171.  
  172.     TakeProduct := Product;
  173. end;
  174.  
  175. begin
  176.     NotCorrect := true;
  177.     Writeln('Данная программа находит произведение вектора на матрицу');
  178.     Writeln;
  179.     Writeln('Введите путь до файла');
  180.     Readln(FilePath);
  181.     if (FileExists(FilePath)) then
  182.     begin
  183.         AssignFile(InputFile, FilePath);
  184.         Matrix := TakeMatrixFromFile(InputFile);
  185.         Writeln('Матрица:');
  186.         ShowMatrix(Matrix);
  187.         Vector := TakeVector(InputFile, VectorType);
  188.         Writeln('Тип вектора: ', VectorType);
  189.         Writeln('Вектор:');
  190.         ShowVector(Vector);
  191.  
  192.         MatrixRows := Length(Matrix);
  193.         MatrixCols := Length(Matrix[0]);
  194.         VectorLength := Length(Vector);
  195.         NotCorrect := false;
  196.     end
  197.     else
  198.         Writeln('Указанный вами файл не существует');
  199.  
  200.     if (Not NotCorrect) then
  201.     begin
  202.         if (((VectorType = 'вектор-столбец') and (MatrixRows <> 1)) or
  203.             ((VectorType = 'вектор-строка') and (MatrixRows <> VectorLength)))
  204.         then
  205.         begin
  206.             Writeln('Число строк в матрице должно быть равно числу столбцов в векторе');
  207.         end
  208.         else
  209.         begin
  210.             if (VectorType = 'вектор-строка') then
  211.                 VectorLength := 1;
  212.  
  213.             SetLength(Product, VectorLength, MatrixCols);
  214.             Product := TakeProduct(Matrix, Vector, VectorLength);
  215.  
  216.             Writeln('Результат:');
  217.             Res := ShowMatrix(Product);
  218.  
  219.             try
  220.                 AssignFile(OutputFile, 'output.txt');
  221.                 Rewrite(OutputFile);
  222.                 Writeln(OutputFile, 'Результат: ', Res);
  223.                 CloseFile(OutputFile);
  224.                 Writeln('Результат сохранен в файл output.txt');
  225.             except
  226.                 Writeln('Произошла ошибка при сохранении файла');
  227.             end;
  228.         end;
  229.     end;
  230.  
  231.     Readln;
  232. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement