Advertisement
Sauka1337

Untitled

Nov 10th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.04 KB | None | 0 0
  1. program var_15_task_3_working_2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. function MultiplyMatrices(const matrixA, matrixB: TArray<TArray<Integer>>): TArray<TArray<Integer>>;
  9. var
  10.   rowsA, colsA, colsB, i, j, k: Integer;
  11. begin
  12.   rowsA := Length(matrixA);
  13.   colsA := Length(matrixA[0]);
  14.   colsB := Length(matrixB[0]);
  15.  
  16.   SetLength(Result, rowsA, colsB);
  17.  
  18.   for i := 0 to rowsA - 1 do
  19.     for j := 0 to colsB - 1 do
  20.     begin
  21.       Result[i][j] := 0;
  22.  
  23.       for k := 0 to colsA - 1 do
  24.         Result[i][j] := Result[i][j] + matrixA[i][k] * matrixB[k][j];
  25.     end;
  26. end;
  27.  
  28. procedure DisplayMatrix(const matrix: TArray<TArray<Integer>>);
  29. var
  30.   i, j: Integer;
  31. begin
  32.   for i := 0 to High(matrix) do
  33.   begin
  34.     for j := 0 to High(matrix[i]) do
  35.       Write(matrix[i][j], ' ');
  36.     WriteLn;
  37.   end;
  38. end;
  39.  
  40. procedure SaveMatrixToFile(const matrix: TArray<TArray<Integer>>; const fileName: string);
  41. var
  42.   fileText: TextFile;
  43.   i, j: Integer;
  44. begin
  45.   AssignFile(fileText, fileName);
  46.   Rewrite(fileText);
  47.  
  48.   for i := 0 to High(matrix) do
  49.   begin
  50.     for j := 0 to High(matrix[i]) do
  51.       Write(fileText, matrix[i][j], ' ');
  52.  
  53.     Writeln(fileText);
  54.   end;
  55.  
  56.   CloseFile(fileText);
  57. end;
  58.  
  59. function InputMatrix(const name: string): TArray<TArray<Integer>>;
  60. var
  61.   rows, cols, i, j, value: Integer;
  62. begin
  63.   Write('Enter the number of rows for the ', name, ' matrix: ');
  64.   ReadLn(rows);
  65.  
  66.   Write('Enter the number of columns for the ', name, ' matrix: ');
  67.   ReadLn(cols);
  68.  
  69.   SetLength(Result, rows, cols);
  70.  
  71.   WriteLn('Enter the elements for the ', name, ' matrix:');
  72.   for i := 0 to rows - 1 do
  73.     for j := 0 to cols - 1 do
  74.     begin
  75.       Write('Element at position (', (i + 1), ', ', (j + 1), '): ');
  76.       ReadLn(value);
  77.       Result[i][j] := value;
  78.     end;
  79. end;
  80.  
  81. function ReadMatrixFromFile(const fileName: string): TArray<TArray<Integer>>;
  82. var
  83.   fileText: TextFile;
  84.   i, j, rows, cols, value: Integer;
  85. begin
  86.   AssignFile(fileText, fileName);
  87.   Reset(fileText);
  88.  
  89.   // Determine the size of the matrix
  90.   rows := 0;
  91.   cols := 0;
  92.  
  93.   while not Eof(fileText) do
  94.   begin
  95.     Read(fileText, value);
  96.     Inc(cols);
  97.     if Eoln(fileText) then
  98.     begin
  99.       Inc(rows);
  100.       ReadLn(fileText); // Move to the next line
  101.     end;
  102.   end;
  103.  
  104.   // Reset the file position to the beginning
  105.   Reset(fileText);
  106.  
  107.   SetLength(Result, rows, cols div rows);
  108.  
  109.   // Read the matrix from the file
  110.   for i := 0 to rows - 1 do
  111.     for j := 0 to cols div rows - 1 do
  112.       Read(fileText, Result[i][j]);
  113.  
  114.   CloseFile(fileText);
  115. end;
  116.  
  117. var
  118.   matrixA, matrixB, resultMatrix: TArray<TArray<Integer>>;
  119.   option: Integer;
  120.   fileNameA, fileNameB: string;
  121.  
  122. begin
  123.   // Ask the user whether to input matrices or read from files
  124.   WriteLn('Choose an option:');
  125.   WriteLn('1. Input matrices manually');
  126.   WriteLn('2. Read matrices from text files');
  127.   ReadLn(option);
  128.  
  129.   case option of
  130.     1:
  131.       begin
  132.         matrixA := InputMatrix('first');
  133.         matrixB := InputMatrix('second');
  134.       end;
  135.     2:
  136.       begin
  137.         Write('Enter the file name for matrix A: ');
  138.         ReadLn(fileNameA);
  139.         matrixA := ReadMatrixFromFile(fileNameA);
  140.  
  141.         Write('Enter the file name for matrix B: ');
  142.         ReadLn(fileNameB);
  143.         matrixB := ReadMatrixFromFile(fileNameB);
  144.       end;
  145.   else
  146.     begin
  147.       WriteLn('Invalid option. Exiting program.');
  148.       Exit;
  149.     end;
  150.   end;
  151.  
  152.   if (Length(matrixA) = 0) or (Length(matrixB) = 0) then
  153.   begin
  154.     WriteLn('Error in reading matrices. Exiting program.');
  155.     Exit;
  156.   end;
  157.  
  158.   // Perform matrix multiplication
  159.   resultMatrix := MultiplyMatrices(matrixA, matrixB);
  160.  
  161.   // Display the matrices and result
  162.   WriteLn('Matrix A:');
  163.   DisplayMatrix(matrixA);
  164.  
  165.   WriteLn('Matrix B:');
  166.   DisplayMatrix(matrixB);
  167.  
  168.   WriteLn('Result of matrix multiplication:');
  169.   DisplayMatrix(resultMatrix);
  170.  
  171.   // Save the result to a text file
  172.   SaveMatrixToFile(resultMatrix, 'resultMatrix.txt');
  173.   WriteLn('Result matrix saved to resultMatrix.txt');
  174.  
  175.   ReadLn; // Keep console open
  176. end.
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement