#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define MaxNumber 30;
typedef double ElemType;
typedef struct matrix {
int col;
int column;
ElemType data[10][10];
}Matrix, * FuckMatrix;
FuckMatrix InitMatrix(FuckMatrix M, int m, int n) {
M->col = m;
M->column = n;
return M;
}
void PrintMatrix(FuckMatrix M) {
for (int i = 0; i < M->col; i++) {
for (int j = 0; j < M->column; j++) {
printf("%-5.1lf ", M->data[i][j]);
}
printf("\n");
}
}
FuckMatrix zhuanzhi(FuckMatrix N) {
FuckMatrix M;
M = (FuckMatrix)malloc(sizeof(Matrix));
InitMatrix(M, N->column, N->col);
for (int i = 0; i < N->column; i++) {
for (int j = 0; j < N->col; j++) {
M->data[i][j] = N->data[j][i];
}
}
return M;
}
void suiji_zhuanzhi() {
FuckMatrix M, N;
int x[2];
srand(time(NULL));
system("CLS");
N = (FuckMatrix)malloc(sizeof(Matrix));
for (int i = 0; i < 2; i++) {
x[i] = (int)(rand() % 10) + 1;
}
InitMatrix(N, x[0], x[1]);
for (int i = 0; i < N->col; i++) {
for (int j = 0; j < N->column; j++) {
N->data[i][j] = rand() % MaxNumber;
}
}
printf("\nThe Matrix:\n");
PrintMatrix(N);
printf("\nThe Result:\n");
N = zhuanzhi(N);
PrintMatrix(N);
system("pause");
}
void danwei_zhuanzhi() {
FuckMatrix M, N;
int x = 4;
srand(time(NULL));
system("CLS");
N = (FuckMatrix)malloc(sizeof(Matrix));
InitMatrix(N, x, x);
for (int i = 0; i < N->col; i++) {
for (int j = 0; j < N->column; j++) {
if (i == j)
N->data[i][j] = 1;
else
N->data[i][j] = 0;
}
}
printf("\nThe Matrix:\n");
PrintMatrix(N);
printf("\nThe Result:\n");
N = zhuanzhi(N);
PrintMatrix(N);
system("pause");
}
void main() {
int choice;
while (1) {
system("CLS");
system("color 11");
printf("\t\t\t---------------------------------------------------\n");
printf("\t\t\t--------------------1.矩阵转置---------------------\n");
printf("\t\t\t--------------------2.单位矩阵转置-----------------\n");
printf("\t\t\t--------------------3.结束程序---------------------\n");
printf("\t\t\t---------------------------------------------------\n");
printf("\t\t\tEnter Your Choice:");
scanf_s("%d", &choice);
switch (choice) {
case 1:
suiji_zhuanzhi(); break;
case 2:
danwei_zhuanzhi(); break;
case 3:
exit(0);
default:printf("Wrong!\n");
}
}
}
如代码,实现矩阵转置,和单位矩阵转置