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"
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) { for (int p = 0; p < t1->num; p++) { for (int q = 0; q < t2->num; q++) { if (t1->val[p].j == t2->val[q].i) { 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; } }
|