数组及其应用
一、实验目的
1、掌握数组的声明,数组元素的输入,输出和引用;
2、应用数据解决与数组有关的常用算法;
二、实验要求
1、定义以三元组表示的矩阵;
2、要求从键盘输入两个矩阵,并完成该某个矩阵的转置,两个矩阵的加法及减法;
3、实验结果以矩阵的形式输出。
三、实验环境
硬件设备:微型计算机系统
软件环境:操作系统Windows,开发工具VC6.0或VS
四、测试数据
输入:从键盘循环输入整数,建立矩阵。
输出:将转置,两矩阵相加、相减的结果输出。
五、程序代码
#include <iostream>
using namespace std;
constexpr auto MAX = 100;
struct matrix {
int value;
int i, j;
};
void show(matrix A[], int row, int column) {
int count = 0;
for (int it = 1; it <= row; it++) {
for (int jt = 1; jt <= column; jt++) {
if (A[count].j == jt && A[count].i == it) {
printf("%5d", A[count].value);
count++;
}
else
printf("%5d", 0);
}
cout << endl;
}
}
void input(matrix A[], int max, int column) {
int buffer, count = 0;;
for (int i = 1, j = 1, num = 0; num < max; num++) {
cin >> buffer;
if (buffer != 0) {
A[count].value = buffer;
A[count].j = j;
A[count].i = i;
count++;
}
j++;
if (j > column) {
j = 1;
i++;
}
}
}
void Create(matrix A[], int& row, int& column, int& max, char a) {
cout << "...Creating matrix " << a << "..." << endl;
fflush(stdin);
cout << "Enter the number of rows and columns in the matrix (note the order of entries and separate them with spaces):";
cin >> row >> column;
max = row * column;
if (max > MAX) {
cout << "The size of the matrix is out of the default range. Please change the default size of the array or re-enter a smaller matrix!" << endl;
exit(0);
}
input(A, max, column);
}
//矩阵转置
void Transposition(matrix A[], int row, int column, int max) {
int num = row;
row = column;
column = num;
num = 0;
matrix B[MAX];
for (int i = 0; i <= row; ++i)
for (int j = 0; j <= max; ++j) {
if (A[j].j == i) {
B[num].value = A[j].value;
B[num].i = A[j].j;
B[num].j = A[j].i;
++num;
}
}
show(B, row, column);
num = row;
row = column;
column = num;
}
//矩阵加法
void addition(matrix A[], matrix B[], int row, int column) {
matrix C[MAX];
int a = 0, b = 0, c = 0;
for (int i = 0;i <= row;++i)
for (int j = 0; j <= column; ++j) {
if (A[a].i == i && A[a].j == j) {
C[c] = A[a];
if (B[b].i == i && B[b].j == j) {
C[c].value = A[a].value + B[b].value;
b++;
}
a++;
c++;
}
else if (B[b].i == i && B[b].j == j) {
C[c] = B[b];
C[c].value = C[c].value;
b++;
c++;
}
}
show(C, row, column);
}
//矩阵减法
void subtraction(matrix A[], matrix B[], int row, int column) {
matrix C[MAX];
int a = 0, b = 0, c = 0;
for (int i = 0; i <= row; ++i)
for (int j = 0; j <= column; ++j) {
if (A[a].i == i && A[a].j == j) {
C[c] = A[a];
if (B[b].i == i && B[b].j == j) {
C[c].value = A[a].value - B[b].value;
b++;
}
a++;
c++;
}
else if (B[b].i == i && B[b].j == j) {
C[c] = B[b];
C[c].value = -C[c].value;
b++;
c++;
}
}
show(C, row, column);
}
int main()
{
int Arow, Acolumn, Amax, Brow, Bcolumn, Bmax;
matrix A[MAX];
Create(A, Arow, Acolumn, Amax, 'A');
matrix B[MAX];
Create(B, Brow, Bcolumn, Bmax, 'B');
cout << "...Matrix A..." << endl;
show(A, Arow, Acolumn);
cout << "...Matrix B..." << endl;
show(B, Brow, Bcolumn);
while (1) {
cout << "Please enter the matrix you want to transpose:";
fflush(stdin);
char choice;
cin >> choice;
if (choice == 'A') {
cout << "...Matrix A transposition..." << endl;
Transposition(A, Arow, Acolumn, Amax);
break;
}
else if (choice == 'B') {
cout << "...Matrix B transposition..." << endl;
Transposition(B, Brow, Bcolumn, Bmax);
break;
}
else
cout << "Please enter the correct option" << endl;
}
cout << "...Matrix A plus matrix B..." << endl;
if (Arow != Brow || Acolumn != Bcolumn)
cout << "Different matrix types, unable to calculate" << endl;
else
addition(A, B, Arow, Acolumn);
cout << "...Matrix A minus matrix B..." << endl;
if (Arow != Brow || Acolumn != Bcolumn)
cout << "Different matrix types, unable to calculate" << endl;
else
subtraction(A, B, Arow, Acolumn);
}
六、实验结果