matrix
Megjelenés
Főnév
matrix (tsz. matrices vagy matrixes)
#include <iostream>
#include <vector>
using namespace std;
class Matrix {
private:
vector<vector<double>> mat;
int rows, cols;
public:
// Constructor
Matrix(int r, int c) : rows(r), cols(c) {
mat.resize(rows, vector<double>(cols, 0));
}
// Function to input matrix values
void inputMatrix() {
cout << "Enter matrix values (" << rows << "x" << cols << "):\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> mat[i][j];
}
}
}
// Function to display the matrix
void displayMatrix() {
cout << "Matrix:\n";
for (const auto &row : mat) {
for (double val : row) {
cout << val << "\t";
}
cout << endl;
}
}
// Addition of two matrices
Matrix operator+(const Matrix &other) {
if (rows != other.rows || cols != other.cols) {
cout << "Error: Matrices must have the same dimensions for addition!\n";
return *this;
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.mat[i][j] = mat[i][j] + other.mat[i][j];
}
}
return result;
}
// Subtraction of two matrices
Matrix operator-(const Matrix &other) {
if (rows != other.rows || cols != other.cols) {
cout << "Error: Matrices must have the same dimensions for subtraction!\n";
return *this;
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.mat[i][j] = mat[i][j] - other.mat[i][j];
}
}
return result;
}
// Multiplication of two matrices
Matrix operator*(const Matrix &other) {
if (cols != other.rows) {
cout << "Error: Columns of first matrix must match rows of second matrix for multiplication!\n";
return *this;
}
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.mat[i][j] += mat[i][k] * other.mat[k][j];
}
}
}
return result;
}
// Transpose of the matrix
Matrix transpose() {
Matrix result(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.mat[j][i] = mat[i][j];
}
}
return result;
}
};
int main() {
int r1, c1, r2, c2;
char choice;
cout << "Enter rows and columns for Matrix 1: ";
cin >> r1 >> c1;
Matrix A(r1, c1);
A.inputMatrix();
cout << "Enter rows and columns for Matrix 2: ";
cin >> r2 >> c2;
Matrix B(r2, c2);
B.inputMatrix();
do {
cout << "\n--- Matrix Operations ---\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Transpose\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
int option;
cin >> option;
Matrix result(1, 1); // Placeholder matrix
switch (option) {
case 1:
result = A + B;
result.displayMatrix();
break;
case 2:
result = A - B;
result.displayMatrix();
break;
case 3:
result = A * B;
result.displayMatrix();
break;
case 4:
cout << "Transpose of Matrix 1:\n";
A.transpose().displayMatrix();
cout << "Transpose of Matrix 2:\n";
B.transpose().displayMatrix();
break;
case 5:
cout << "Exiting...\n";
return 0;
default:
cout << "Invalid choice!\n";
}
cout << "\nDo you want to perform another operation? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
cout << "Thank you for using the Matrix Calculator!\n";
return 0;
}