写一个矩阵类CMatrix,基本要求如下:1.实现构造、拷贝构造与析构函数;2.支持任意大小的矩阵,行数和列数可以动态设置(不用修改代码);3.重载操作符*,实现矩阵相乘;4.重载操作符=,实现矩阵赋值;5.重载操作符++和--,分别实现矩阵元素同时+1和-1;在main函数中对以上功能进行调用。用C语言
时间: 2024-03-06 17:52:11 浏览: 65
好的,下面是一个用C语言实现的简单版本:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int rows;
int cols;
int **data;
} CMatrix;
CMatrix *createMatrix(int m, int n) {
CMatrix *mat = (CMatrix *)malloc(sizeof(CMatrix));
mat->rows = m;
mat->cols = n;
mat->data = (int **)malloc(sizeof(int *) * m);
for (int i = 0; i < m; i++) {
mat->data[i] = (int *)malloc(sizeof(int) * n);
for (int j = 0; j < n; j++) {
mat->data[i][j] = 0;
}
}
return mat;
}
void freeMatrix(CMatrix *mat) {
for (int i = 0; i < mat->rows; i++) {
free(mat->data[i]);
}
free(mat->data);
free(mat);
}
CMatrix *copyMatrix(CMatrix *mat) {
CMatrix *newMat = createMatrix(mat->rows, mat->cols);
for (int i = 0; i < mat->rows; i++) {
for (int j = 0; j < mat->cols; j++) {
newMat->data[i][j] = mat->data[i][j];
}
}
return newMat;
}
CMatrix *multiplyMatrix(CMatrix *mat1, CMatrix *mat2) {
if (mat1->cols != mat2->rows) {
printf("Error: The number of columns in the first matrix must be equal to the number of rows in the second matrix.\n");
return NULL;
}
CMatrix *result = createMatrix(mat1->rows, mat2->cols);
for (int i = 0; i < mat1->rows; i++) {
for (int j = 0; j < mat2->cols; j++) {
for (int k = 0; k < mat1->cols; k++) {
result->data[i][j] += mat1->data[i][k] * mat2->data[k][j];
}
}
}
return result;
}
void assignMatrix(CMatrix *mat1, CMatrix *mat2) {
if (mat1 == mat2) {
return;
}
if (mat1->rows != mat2->rows || mat1->cols != mat2->cols) {
printf("Error: The two matrices must have the same size.\n");
return;
}
for (int i = 0; i < mat1->rows; i++) {
for (int j = 0; j < mat1->cols; j++) {
mat1->data[i][j] = mat2->data[i][j];
}
}
}
CMatrix *incrementMatrix(CMatrix *mat) {
for (int i = 0; i < mat->rows; i++) {
for (int j = 0; j < mat->cols; j++) {
mat->data[i][j]++;
}
}
return mat;
}
CMatrix *decrementMatrix(CMatrix *mat) {
for (int i = 0; i < mat->rows; i++) {
for (int j = 0; j < mat->cols; j++) {
mat->data[i][j]--;
}
}
return mat;
}
void printMatrix(CMatrix *mat) {
for (int i = 0; i < mat->rows; i++) {
for (int j = 0; j < mat->cols; j++) {
printf("%d ", mat->data[i][j]);
}
printf("\n");
}
}
int main() {
CMatrix *mat1 = createMatrix(2, 3);
CMatrix *mat2 = createMatrix(3, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
mat1->data[i][j] = i + j;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
mat2->data[i][j] = i * j;
}
}
printf("mat1:\n");
printMatrix(mat1);
printf("mat2:\n");
printMatrix(mat2);
CMatrix *mat3 = multiplyMatrix(mat1, mat2);
if (mat3 != NULL) {
printf("mat3:\n");
printMatrix(mat3);
}
incrementMatrix(mat1);
printf("mat1++:\n");
printMatrix(mat1);
CMatrix *mat4 = decrementMatrix(copyMatrix(mat2));
printf("mat2--:\n");
printMatrix(mat2);
printf("mat4:\n");
printMatrix(mat4);
freeMatrix(mat1);
freeMatrix(mat2);
freeMatrix(mat3);
freeMatrix(mat4);
return 0;
}
```
上述代码定义了一个矩阵结构体`CMatrix`,其成员包括行数、列数和数据矩阵。该结构体提供了一些函数,如创建矩阵、释放矩阵内存、复制矩阵、矩阵相乘、矩阵赋值、矩阵元素+1和矩阵元素-1等。在main函数中进行了上述功能的调用。
阅读全文