banner
Hello!

三元组矩阵的运算

Scroll down

基本的三元组数据结构:

matrix.h如下。

1
2
3
4
5
6
7
8
9
10
11
#pragma once//防止多次引用
//每一个三元组的内容
typedef struct {
int i, j;
int val;
}tri;
//用于表示整个矩阵的三元组
typedef struct {
tri val[20];
int n, m, num;
}tsmat;

在此之中包含了两个结构体。第一个tri为三元组结构,以[i,j,val]形式存储一个三元组。而第二个tsmat即为tri的集合,最大二十个。

输入/输出函数:

io.h如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "matrix.h"
#include <iostream>
#include <vector>
using namespace std;
//输入函数
tsmat *input_mat() {
int m, n;
cout << "请输入矩阵的行数" << endl;
cin >> m;
cout << "请输入矩阵的列数" << endl;
cin >> n;
tsmat* matrix=new tsmat;
matrix->m = m;
matrix->n = n;
matrix->num = 0;
int no = 0;
for (int i = 0; i < matrix->m; i++) {
cout << "请输入第" << i+1 << "列内容" << endl;
for (int j = 0; j < matrix->n; j++) {
int in;
cin >> in;
if (in) {
matrix->val[no].i = i;
matrix->val[no].j = j;
matrix->val[no].val = in;
matrix->num++;
no++;
}
}
}
return matrix;
}
//输出函数
void output_mat(tsmat *t) {
vector<vector<int>> mat(t->m,vector<int>(t->n));
//先初始化一个vector容器,再用三元组中的值替换其中的对应值
for (int i = 0; i < t->m; i++) {
for (int j = 0; j < t->n; j++) {
mat[i][j]=0;
}
}
for (int n = 0; n < t->num; n++) {
mat[t->val[n].i][t->val[n].j] = t->val[n].val;
}
for (int p = 0; p < t->m; p++) {
for (int q = 0; q < t->n; q++) {
cout << mat[p][q];
if (q == t->n - 1) { cout << endl; }
else { cout << " "; };
}
}
}

通过将三元组逐个填入vector空位的方式输出。

运算实现函数

func.h如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include<iostream>
#include "matrix.h"
//输出至t3
void inline append(tsmat* ti, tsmat* to, int& n, int& c) {
to->val[n].i = ti->val[c].i;
to->val[n].j = ti->val[c].j;
to->val[n].val = ti->val[c].val;
n++;
c++;
}
void inline appendminus(tsmat* ti, tsmat* to, int& n, int& c) {
to->val[n].i = ti->val[c].i;
to->val[n].j = ti->val[c].j;
to->val[n].val = -ti->val[c].val;
n++;
c++;
}
//矩阵的转置
void trans(tsmat* t) {
cout << "你输入的矩阵为" << endl;
output_mat(t);
int swap;
for (int n = 0; n < t->num; n++) {
swap = t->val[n].i;
t->val[n].i = t->val[n].j;
t->val[n].j = swap;
}
cout << "转置后的矩阵为" << endl;
output_mat(t);
}
//矩阵相加
void matadd(tsmat* t1, tsmat* t2) {
cout << "你执行的操作为" << endl;
output_mat(t1);
cout << "加上" << endl;
output_mat(t2);
tsmat* t3 = new tsmat;
int a = 0, b = 0, n = 0;
bool flag=true;
if(t1->m==t2->m&&t1->n==t2->n){
while (flag) {
//一方输出完后直接输出另一方全部数值
if (a > t1->num) {
while (b <= t2->num) {
append(t2, t3, n, b);
}
break;
}
else if (b > t2->num) {
while (a <= t1->num) {
append(t1, t2, n, a);
}
break;
}
else {
//首先比较行号,行号不等输出较小者
if (t1->val[a].i < t2->val[b].i) {
append(t1, t3, n, a);
}
else if (t1->val[a].i > t2->val[b].i) {
append(t2, t3, n, b);
}
else if (t1->val[a].i == t2->val[b].i) {
//行号相等比较列号,列号不等输出较小者
if (t1->val[a].j < t2->val[b].j) {
append(t1, t3, n, a);
}
else if (t1->val[a].j > t2->val[b].j) {
append(t2, t3, n, b);
}
//两者都相等输出和
else if (t1->val[a].j == t2->val[b].j) {
t3->val[n].i = t1->val[a].i;
t3->val[n].j = t1->val[a].j;
t3->val[n].val = t1->val[a].val + t2->val[b].val;
a++;
b++;
n++;
}
}
}

}
t3->m = t1->m; t3->n = t1->n; t3->num = n-1;
cout << "相加得到的矩阵为" << endl;
output_mat(t3);
}
else {
cout << "这两个矩阵不能相加" << endl;
}
}
//矩阵相减
void matminus(tsmat*t1,tsmat*t2) {
cout << "你执行的操作为" << endl;
output_mat(t1);
cout << "减去" << endl;
output_mat(t2);
tsmat* t3 = new tsmat;
int a = 0, b = 0, n = 0;
bool flag = true;
if (t1->m == t2->m && t1->n == t2->n) {
while (flag) {
//一方输出完后直接输出另一方全部数值
if (a > t1->num) {
while (b <= t2->num) {
append(t2, t3, n, b);
}
break;
}
else if (b > t2->num) {
while (a <= t1->num) {
appendminus(t1, t2, n, a);
}
break;
}
else {
//首先比较行号,行号不等输出较小者
if (t1->val[a].i < t2->val[b].i) {
append(t1, t3, n, a);
}
else if (t1->val[a].i > t2->val[b].i) {
appendminus(t2, t3, n, b);
}
else if (t1->val[a].i == t2->val[b].i) {
//行号相等比较列号,列号不等输出较小者
if (t1->val[a].j < t2->val[b].j) {
append(t1, t3, n, a);
}
else if (t1->val[a].j > t2->val[b].j) {
appendminus(t2, t3, n, b);
}
//两者都相等输出差
else if (t1->val[a].j == t2->val[b].j) {
t3->val[n].i = t1->val[a].i;
t3->val[n].j = t1->val[a].j;
t3->val[n].val = t1->val[a].val - t2->val[b].val;
a++;
b++;
n++;
}
}
}
}
t3->m = t1->m; t3->n = t1->n; t3->num = n - 1;
cout << "相减得到的矩阵为" << endl;
output_mat(t3);
}
else {
cout << "这两个矩阵不能相减" << endl;
}
}
//矩阵相乘
void matmult(tsmat* t1, tsmat* t2) {
cout << "你执行的操作为" << endl;
output_mat(t1);
cout << "乘上" << endl;
output_mat(t2);
tsmat *t3 = new tsmat;
t3->num = 0;
bool b = true;
//前者行数等于后者列数才能相乘
if (t1->m == t2->n) {
//遍历t1序号
for (int p = 0; p < t1->num; p++) {
//遍历t2,查找是否有行号与t1现三元组列号相等的三元组
for (int q = 0; q < t2->num; q++) {
if (t1->val[p].j == t2->val[q].i) {
//若t3中存在行列都相等的三元组,将原值与得到的值相加
for (int s = 0; s < t3->num+1; s++) {
if (t3->val[s].i == t1->val[p].i && t3->val[s].j == t2->val[q].j) {
b = false;
t3->val[s].val += t1->val[p].val * t2->val[q].val;
}
}
//没有则添入此值
if(b) {
t3->val[t3->num].i = t1->val[p].i;
t3->val[t3->num].j = t2->val[q].j;
t3->val[t3->num].val = t1->val[p].val * t2->val[q].val;
t3->num++;
}
b = true;
}
}
}
t3->m = t1->m;
t3->n = t2->n;
cout << "结果为" << endl;
output_mat(t3);
}
else {
cout << "这两个矩阵不能相乘" << endl;
}
}

加法逻辑:

加法

乘法逻辑:

乘法

主函数:

main.cpp如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include "io.h"
#include "func.h"
using namespace std;
int main() {
bool flag = true;
while (flag) {
cout << "请输入你需要做的操作的编号" << endl;
cout << 1 << ". 矩阵的转置" << endl
<< 2 << ". 求矩阵的和" << endl
<< 3 << ". 求矩阵的差" << endl
<< 4 << ". 求矩阵的积" << endl
<< 0 << ". 退出" << endl;
int ch;
cin >> ch;
switch (ch) {
case 1: {
cout << "准备进行矩阵的转置,请输入要转置的矩阵" << endl;
trans(input_mat());
break;
}
case 2:{
cout << "准备求矩阵的和,请输入第一个矩阵" << endl;
tsmat* t1 = input_mat();
cout << "请输入第二个矩阵" << endl;
tsmat* t2 = input_mat();
matadd(t1, t2);
delete(t1); delete(t2);
break;
}
case 3: {
cout << "准备求矩阵的差,请输入被减矩阵" << endl;
tsmat* t1 = input_mat();
cout << "请输入减去的矩阵" << endl;
tsmat* t2 = input_mat();
matminus(t1, t2);
delete(t1); delete(t2);
break;
}
case 4: {
cout << "准备求矩阵的积,请输入前矩阵" << endl;
tsmat* t1 = input_mat();
cout << "请输入后矩阵" << endl;
tsmat* t2 = input_mat();
matmult(t1, t2);
break;
}
case 0: {
flag = false;
break;
}
default: {
cout << "请输入正确的编号" << endl;
break;
}
}
}
}

以上。

其他文章
cover
【自设架空】前一
  • 22/12/04
  • 12:40
  • 架空
cover
高数作业一
  • 22/11/08
  • 15:10
  • 数学
目录导航 置顶
  1. 1. 基本的三元组数据结构:
  2. 2. 输入/输出函数:
  3. 3. 运算实现函数
    1. 3.1. 加法逻辑:
    2. 3.2. 乘法逻辑:
  4. 4. 主函数: