Advertisement
Sauka1337

Untitled

Oct 11th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.67 KB | None | 0 0
  1. program MatrixMultiplication;
  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: Integer;
  62.   inputValueStr: string;
  63. begin
  64.   repeat
  65.     Write('Enter the number of rows for the ', name, ' matrix: ');
  66.     ReadLn(rows);
  67.  
  68.     Write('Enter the number of columns for the ', name, ' matrix: ');
  69.     ReadLn(cols);
  70.  
  71.     if (rows <= 0) or (cols <= 0) then
  72.       WriteLn('Error: Matrix dimensions must be positive.');
  73.   until (rows > 0) and (cols > 0);
  74.  
  75.   SetLength(Result, rows, cols);
  76.  
  77.   WriteLn('Enter the elements for the ', name, ' matrix:');
  78.   for i := 0 to rows - 1 do
  79.     for j := 0 to cols - 1 do
  80.     begin
  81.       repeat
  82.         Write('Element at position (', (i + 1), ', ', (j + 1), '): ');
  83.         ReadLn(inputValueStr);
  84.  
  85.         if not TryStrToInt(inputValueStr, Result[i][j]) then
  86.           WriteLn('Error: Invalid input. Please enter a valid integer.');
  87.       until TryStrToInt(inputValueStr, Result[i][j]);
  88.     end;
  89.   WriteLn;
  90. end;
  91.  
  92. function ReadMatrixFromFile(const fileName: string): TArray<TArray<Integer>>;
  93. var
  94.   fileText: TextFile;
  95.   i, j, rows, cols, value: Integer;
  96. begin
  97.   if not FileExists(fileName) then
  98.   begin
  99.     WriteLn('Error: File not found or cannot be read.');
  100.     Exit(nil);
  101.   end;
  102.  
  103.   AssignFile(fileText, fileName);
  104.   Reset(fileText);
  105.  
  106.   // Determine the size of the matrix
  107.   rows := 0;
  108.   cols := 0;
  109.  
  110.   while not Eof(fileText) do
  111.   begin
  112.     Read(fileText, value);
  113.     Inc(cols);
  114.     if Eoln(fileText) then
  115.     begin
  116.       Inc(rows);
  117.       ReadLn(fileText); // Move to the next line
  118.     end;
  119.   end;
  120.  
  121.   // Reset the file position to the beginning
  122.   Reset(fileText);
  123.  
  124.   if (rows <= 0) or (cols mod rows <> 0) then
  125.   begin
  126.     WriteLn('Error: Invalid matrix dimensions in the file.');
  127.     Exit(nil);
  128.   end;
  129.  
  130.   SetLength(Result, rows, cols div rows);
  131.  
  132.   // Read the matrix from the file
  133.   for i := 0 to rows - 1 do
  134.     for j := 0 to cols div rows - 1 do
  135.       Read(fileText, Result[i][j]);
  136.  
  137.   CloseFile(fileText);
  138. end;
  139.  
  140. var
  141.   matrixA, matrixB, resultMatrix: TArray<TArray<Integer>>;
  142.   option: Integer;
  143.   fileNameA, fileNameB: string;
  144.  
  145. begin
  146.   // Ask the user whether to input matrices or read from files
  147.   WriteLn('Choose an option:');
  148.   WriteLn('1. Input matrices manually');
  149.   WriteLn('2. Read matrices from text files');
  150.   ReadLn(option);
  151.  
  152.   case option of
  153.     1:
  154.       begin
  155.         matrixA := InputMatrix('first');
  156.         matrixB := InputMatrix('second');
  157.       end;
  158.     2:
  159.       begin
  160.         Write('Enter the file name for matrix A: ');
  161.         ReadLn(fileNameA);
  162.         matrixA := ReadMatrixFromFile(fileNameA);
  163.  
  164.         Write('Enter the file name for matrix B: ');
  165.         ReadLn(fileNameB);
  166.         matrixB := ReadMatrixFromFile(fileNameB);
  167.       end;
  168.   else
  169.     begin
  170.       WriteLn('Invalid option. Exiting program.');
  171.       Exit;
  172.     end;
  173.   end;
  174.  
  175.   if (Length(matrixA) = 0) or (Length(matrixB) = 0) then
  176.   begin
  177.     WriteLn('Error in reading matrices. Exiting program.');
  178.     Exit;
  179.   end;
  180.  
  181.   // Perform matrix multiplication
  182.   resultMatrix := MultiplyMatrices(matrixA, matrixB);
  183.  
  184.   // Display the matrices and result
  185.   WriteLn('Matrix A:');
  186.   DisplayMatrix(matrixA);
  187.  
  188.   WriteLn('Matrix B:');
  189.   DisplayMatrix(matrixB);
  190.  
  191.   WriteLn('Result of matrix multiplication:');
  192.   DisplayMatrix(resultMatrix);
  193.  
  194.   // Save the result to a text file
  195.   SaveMatrixToFile(resultMatrix, 'resultMatrix.txt');
  196.   WriteLn('Result matrix saved to resultMatrix.txt');
  197.  
  198.   ReadLn; // Keep console open
  199. end.
  200.  
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement