@【数据结构】(矩阵基本函数)
矩阵的基本运算:分别用函数实现矩阵的输入,矩阵的输出,矩阵的转置函数
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#define Max_Row 10
#define Max_Col 10
using namespace std;
typedef struct
{
int row;
int col;
int data[Max_Row][Max_Col];
} Matrix;
void inputMat(Matrix &A,int m,int n)
{
cout << "输入矩阵元素:" << endl;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> A.data[i][j];
A.row = m;
A.col = n;
}
void outputMat(Matrix A)
{
for (int i = 0; i < A.row; i++)
{
for (int j = 0; j < A.col; j++)
cout << A.data[i][j] << " ";
cout << endl;
}
}
void transMat(Matrix A, Matrix &B)
{
int i, j;
for (i = 0; i < A.row; i++)
for (j = 0; j< A.col; j++)
B.data[j][i] = A.data[i][j];
B.col = A.row;
B.row = A.col;
}
void main()
{
Matrix A, B;
int m, n;
cout << "输入矩阵的纬度:";
cin >> m; cin >> n;
inputMat(A, m, n);
cout << "输出矩阵:" << endl;
outputMat(A);
cout << "矩阵转置:" << endl;
transMat(A, B);
outputMat(B);
system("pause");
}
测试: