用模板技术,实现一个矩阵类CMatrixT,基本要求如下:1.实现构造、拷贝构造与析构函数;2.支持任意大小的矩阵,行数和列数可以动态设置(不用修改代码);3.重载操作符*,实现矩阵相乘;4.重载操作符=,实现矩阵赋值;5.重载操作符++和--,分别实现矩阵元素同时+1和-1;在main函数中对以上功能进行调用。用C语言
时间: 2024-03-06 13:52:21 浏览: 129
很抱歉,模板技术是C++的特性,而不是C语言的特性。在C语言中,无法实现泛型编程。不过,我们可以使用结构体和函数指针来达到类似的效果。以下是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int rows;
int cols;
int **data;
} CMatrixT;
void init_matrix(CMatrixT *mat, int rows, int cols) {
mat->rows = rows;
mat->cols = cols;
mat->data = (int **)malloc(sizeof(int *) * rows);
for (int i = 0; i < rows; i++) {
mat->data[i] = (int *)malloc(sizeof(int) * cols);
for (int j = 0; j < cols; j++) {
mat->data[i][j] = 0;
}
}
}
void copy_matrix(CMatrixT *dst, CMatrixT *src) {
init_matrix(dst, src->rows, src->cols);
for (int i = 0; i < src->rows; i++) {
for (int j = 0; j < src->cols; j++) {
dst->data[i][j] = src->data[i][j];
}
}
}
void print_matrix(CMatrixT *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");
}
}
void multiply_matrix(CMatrixT *dst, CMatrixT *mat1, CMatrixT *mat2) {
if (mat1->cols != mat2->rows) {
printf("Error: matrix sizes do not match.\n");
return;
}
init_matrix(dst, 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++) {
dst->data[i][j] += mat1->data[i][k] * mat2->data[k][j];
}
}
}
}
void assign_matrix(CMatrixT *dst, CMatrixT *src) {
if (dst->rows != src->rows || dst->cols != src->cols) {
printf("Error: matrix sizes do not match.\n");
return;
}
for (int i = 0; i < dst->rows; i++) {
for (int j = 0; j < dst->cols; j++) {
dst->data[i][j] = src->data[i][j];
}
}
}
void inc_matrix(CMatrixT *mat) {
for (int i = 0; i < mat->rows; i++) {
for (int j = 0; j < mat->cols; j++) {
mat->data[i][j]++;
}
}
}
void dec_matrix(CMatrixT *mat) {
for (int i = 0; i < mat->rows; i++) {
for (int j = 0; j < mat->cols; j++) {
mat->data[i][j]--;
}
}
}
void destroy_matrix(CMatrixT *mat) {
for (int i = 0; i < mat->rows; i++) {
free(mat->data[i]);
}
free(mat->data);
}
int main() {
CMatrixT mat1, mat2, mat3, mat4;
init_matrix(&mat1, 2, 3);
mat1.data[0][0] = 1;
mat1.data[0][1] = 2;
mat1.data[0][2] = 3;
mat1.data[1][0] = 4;
mat1.data[1][1] = 5;
mat1.data[1][2] = 6;
printf("Matrix 1:\n");
print_matrix(&mat1);
copy_matrix(&mat2, &mat1);
printf("Matrix 2 (copy of Matrix 1):\n");
print_matrix(&mat2);
init_matrix(&mat3, 3, 2);
mat3.data[0][0] = 1;
mat3.data[0][1] = 4;
mat3.data[1][0] = 2;
mat3.data[1][1] = 5;
mat3.data[2][0] = 3;
mat3.data[2][1] = 6;
printf("Matrix 3:\n");
print_matrix(&mat3);
multiply_matrix(&mat4, &mat1, &mat3);
printf("Matrix 4 (Matrix 1 x Matrix 3):\n");
print_matrix(&mat4);
assign_matrix(&mat1, &mat3);
printf("Matrix 1 (assigned from Matrix 3):\n");
print_matrix(&mat1);
inc_matrix(&mat1);
printf("Matrix 1 (incremented):\n");
print_matrix(&mat1);
dec_matrix(&mat1);
printf("Matrix 1 (decremented):\n");
print_matrix(&mat1);
destroy_matrix(&mat1);
destroy_matrix(&mat2);
destroy_matrix(&mat3);
destroy_matrix(&mat4);
return 0;
}
```
在该程序中,我们使用了结构体来存储矩阵的行数、列数和数据。我们还定义了一些函数来实现矩阵的各种操作,例如初始化、拷贝、打印、相乘、赋值、加1和减1。这些函数可以通过函数指针来传递给其他函数使用。在main函数中,我们演示了如何使用这些函数来创建、操作和销毁矩阵。
阅读全文