#define _CRT_SECURE_NO_WARNINGS #include#include void Statement(void); //打印出算出的复矩阵 void Print_Array_C(double* _res, const int row, const int colunm_2, double* Im_val); //申请内存 double** _2_Dimension_Array_Buy_Memory(const int* row, const int* column); //初始化数组 void _2_Dimension_Array_Initialise(double** mrx, const int* row, const int* column); //释放内存,并置空 void _2_Dimension_Array_Destory_Memory(double** mrx, const int* row); //第一个矩阵数据的输入 void InputRowAndColumn_1(int* row, int* column); //第二个矩阵数据的输入 void InputRowAndColumn_2(int* column_2); //复矩阵的实现 void C_Martix_XmulY(double* _res, const int* row, const int* column, const int* column_2, double** x_res, double** y_res, double* Im_val); int main(void) { Statement(); int row = 0, column = 0, column_2 = 0; double** x_res = NULL, ** y_res = NULL, * _res = NULL; InputRowAndColumn_1(&row, &column); InputRowAndColumn_2(&column_2); _res = (double*)malloc(sizeof(double) * row * column_2);//重新申请一位数组内存 for (int i = 0; i < row * column_2; i++)//实部置零 _res[i] = 0; x_res = _2_Dimension_Array_Buy_Memory(&row, &column); y_res = _2_Dimension_Array_Buy_Memory(&column, &column_2); double* Im_res = (double*)malloc(sizeof(double) * row * column_2); for (int i = 0; i < row * column_2; i++)//初始化保存虚部的一维数组 Im_res[i] = 0; C_Martix_XmulY(_res, &row, &column, &column_2, x_res, y_res, Im_res); Print_Array_C(_res, row, column_2, Im_res); system("pause"); return 0; } void Statement(void) { printf("这里我虚部是用j表示n"); printf("这个复矩阵的实现并不完美,当虚部为0的时候仍然需要输入Re+0j或者Re-0j这种形式n"); } void Print_Array_C(double* _res, const int row, const int colunm_2, double* Im_val) { printf("n下面是矩阵相乘的结果:n"); for (int i = 0; i < row * colunm_2; i++) { printf("%.4f", *(_res + i)); if (*(Im_val + i) >= 0) { printf("+"); } printf("%.4f", *(Im_val + i)); printf("j "); for (int j = 1; j <= row; j++) { if (i == j * colunm_2 - 1)//判断什么时候该换行打印矩阵 printf("n"); } } printf("n"); } double** _2_Dimension_Array_Buy_Memory(const int* row, const int* column) { double** mrx = (double**)malloc(sizeof(double*) * (*row)); for (int i = 0; i < (*row); i++) { mrx[i] = (double*)malloc(sizeof(double) * (*column)); } return mrx; } void _2_Dimension_Array_Initialise(double** mrx, const int* row, const int* column) { for (int i = 0; i < (*row); i++) for (int j = 0; j < (*column); j++) mrx[i][j] = 0; } void _2_Dimension_Array_Destory_Memory(double** mrx, const int* row) { for (int i = 0; i < (*row); i++) free(mrx[i]); free(mrx); mrx = NULL; } void C_Martix_XmulY(double* _res, const int* row, const int* column, const int* column_2, double** x_res, double** y_res, double* Im_val) { char tmp[65];//保存非实部的数据 double** Im_x_res, ** Im_y_res; Im_x_res = _2_Dimension_Array_Buy_Memory(row, column); Im_y_res = _2_Dimension_Array_Buy_Memory(column, column_2); printf("下面是复矩阵的输入:n"); printf("请输入第一个矩阵的元素:n"); for (int i = 0; i < (*row); i++) for (int j = 0; j < (*column); j++) { scanf("%lf%s", &x_res[i][j], tmp); if (tmp[1] == 'j' && tmp[0] == '+') Im_x_res[i][j] = 1; else if (tmp[1] == 'j' && tmp[0] == '-') Im_x_res[i][j] = -1; else Im_x_res[i][j] = atof(tmp); } printf("请输入第二个矩阵的元素:n"); for (int i = 0; i < (*column); i++) for (int j = 0; j < (*column_2); j++) { scanf("%lf%s", &y_res[i][j], tmp); if (tmp[1] == 'j' && tmp[0] == '+') Im_y_res[i][j] = 1; else if (tmp[1] == 'j' && tmp[0] == '-') Im_y_res[i][j] = -1; else Im_y_res[i][j] = atof(tmp); } int t = 0, i = 0, j = 0; while (j < *row) { //这个i是结果矩阵的每个元素,i++,当i==第二个矩阵的列数时,说明一行乘完了 //i会一直增加,但是t到了每行的边界时候就置零,循环 //矩阵乘法写几项还是挺直观的 for (int k = 0; k < *column; k++) { _res[i] += (x_res[j][k]) * (y_res[k][t]) - (Im_x_res[j][k] * Im_y_res[k][t]); Im_val[i] += (x_res[j][k] * Im_y_res[k][t]) + (y_res[k][t] * Im_x_res[j][k]); } i++; t++; if (t == *column_2) // 这个表明第一个矩阵的一行要乘两次第二个矩阵, 第一个矩阵行才会到下一行与后面矩阵相乘。 { t = 0; j++; } } _2_Dimension_Array_Destory_Memory(Im_x_res, row); _2_Dimension_Array_Destory_Memory(Im_y_res, column); } void InputRowAndColumn_1(int* row, int* column) { printf("请输入第一个矩阵的行数和列数,以空格或者回车作为分隔:n"); scanf("%d%d", row, column); } void InputRowAndColumn_2(int* column_2) { printf("请输入第二个矩阵的列数:n"); scanf("%d", column_2); }
矩阵相乘的数我是用的一个一维数组接受,也就是代码里面的_res。
因为像这样矩阵相乘的二维数组本质上也是可以用一个一维数组解释的。
现在需要做的就是将这个一维数组以正确的矩阵形式打印出即可。
下面这个是一个样例的实现



