编写矩阵系统程序可以根据所使用的编程语言的不同而有所差异。以下是几种常见编程语言编写矩阵系统程序的示例:
C++ 示例
```cpp
include include using namespace std; class Matrix { private: int rows; int cols; vector public: Matrix(int r, int c) : rows(r), cols(c), data(r, vector void input() { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cin >> data[i][j]; } } } void display() const { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cout << data[i][j] << " "; } cout << endl; } } Matrix operator+(const Matrix& other) const { if (rows != other.rows || cols != other.cols) { throw invalid_argument("Matrix dimensions must match for addition."); } Matrix result(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { result.data[i][j] = data[i][j] + other.data[i][j]; } } return result; } Matrix operator*(const Matrix& other) const { if (cols != other.rows) { throw invalid_argument("Matrix dimensions are not compatible for multiplication."); } Matrix result(rows, other.cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < other.cols; ++j) { for (int k = 0; k < cols; ++k) { result.data[i][j] += data[i][k] * other.data[k][j]; } } } return result; } }; int main() { Matrix A(3, 3); Matrix B(3, 3); A.input(); B.input(); Matrix C = A + B; Matrix D = A * B; cout << "Matrix A:" << endl; A.display(); cout << "Matrix B:" << endl; B.display(); cout << "Matrix A + B:" << endl; C.display(); cout << "Matrix A * B:" << endl; D.display(); return 0; } ``` Python 示例