概述
#include <iostream>
using namespace std;
typedef int ElemType;
void SymmetricMatrixStore(int n, ElemType*& s);//存储n行的对称矩阵,将存储结果放入数组s中返回
void TraverseSymmetricMatrix(ElemType* s, int n);//将存储在数组s中的n行对称矩阵遍历
void UpperTriangularMatrixStore(int n, ElemType*& s);//存储n行的上三角矩阵,将存储结果放入数组s中返回
void TraverseUpperTriangularMatrix(ElemType* s, int n);//将存储在数组s中的n行上三角矩阵遍历
void LowerTriangularMatrixStore(int n, ElemType*& s);//存储n行的下三角矩阵,将存储结果放入数组s中返回
void TraverseLowerTriangular(ElemType* s, int n);//将存储在数组s中的n行下三角矩阵遍历
void TridiagonalMatrixStore(int n, ElemType*& s);//存储n行的三对角矩阵,将存储结果放入数组s中返回
void TraverseTridiagonalMatrix(ElemType* s, int n);//将存储在数组s中的n行三对角矩阵遍历
void SymmetricMatrixStore(int n, ElemType*& s) {
s = new ElemType[n * (n + 1) / 2];
if (!s)exit(0);
int length = n * (n + 1) / 2;
cout << "请按行顺序输入对称矩阵的值:" << endl;
for (int i = 0; i < length; i++)
cin >> s[i];
}
void TraverseSymmetricMatrix(ElemType* s, int n) {
int k = 0;
cout << "---------对称矩阵---------" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i >= j)k = i * (i + 1) / 2 + j;
else k = j * (j + 1) / 2 + i;
cout << s[k] << " ";
}
cout << endl;
}
cout << "--------------------------" << endl;
}
void UpperTriangularMatrixStore(int n, ElemType*& s) {
s = new ElemType[n * (n + 1) / 2 + 1];
if (!s)exit(0);
int length = n * (n + 1) / 2;
cout << "请按行顺序输入上三角矩阵的三角部分值:" << endl;
for (int i = 0; i < length; i++)
cin >> s[i];
cout << "请输入一位下三角区元素:";
cin >> s[length];
}
void TraverseUpperTriangularMatrix(ElemType* s, int n) {
int k = 0;
cout << "---------上三角矩阵---------" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i > j)k = n * (n + 1) / 2;
else k = i * (2 * n - i + 1) / 2 + (j - i);
cout << s[k] << " ";
}
cout << endl;
}
cout << "----------------------------" << endl;
}
void LowerTriangularMatrixStore(int n, ElemType*& s) {
s = new ElemType[n * (n + 1) / 2 + 1];
if (!s)exit(0);
int length = n * (n + 1) / 2;
cout << "请按行顺序输入下三角矩阵的三角部分值:" << endl;
for (int i = 0; i < length; i++)
cin >> s[i];
cout << "请输入一位上三角区元素:";
cin >> s[length];
}
void TraverseLowerTriangular(ElemType* s, int n) {
int k = 0;
cout << "---------下三角矩阵---------" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i < j)k = n * (n + 1) / 2;
else k = i * (i + 1) / 2 + j;
cout << s[k] << " ";
}
cout << endl;
}
cout << "----------------------------" << endl;
}
void TridiagonalMatrixStore(int n, ElemType*& s) {
s = new ElemType[3 * n - 2];
if (!s)exit(0);
int length = 3 * n - 2;
cout << "请按行顺序输入三对角矩阵的值:" << endl;
for (int i = 0; i < length; i++)
cin >> s[i];
}
void TraverseTridiagonalMatrix(ElemType* s, int n) {
int k = 0;
cout << "---------下三角矩阵---------" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j - i >= -1 && j - i <= 1) {
cout << s[k] << " ";
k++;
}
else
cout << 0 << " ";
}
cout << endl;
}
cout << "----------------------------" << endl;
}
int main()
{
int n = 0;
cout << "请输入矩阵维数:";
cin >> n;
int* s;
SymmetricMatrixStore(n, s);
TraverseSymmetricMatrix(s, n);
delete[] s;
UpperTriangularMatrixStore(n, s);
TraverseUpperTriangularMatrix(s, n);
delete[] s;
LowerTriangularMatrixStore(n, s);
TraverseLowerTriangular(s, n);
delete[] s;
TridiagonalMatrixStore(n, s);
TraverseTridiagonalMatrix(s, n);
delete[] s;
return 0;
}
实验样例
最后
以上就是独特哑铃为你收集整理的数据结构-特殊矩阵【对称矩阵、上三角下三角矩阵、三对角矩阵】的压缩存储代码实现的全部内容,希望文章能够帮你解决数据结构-特殊矩阵【对称矩阵、上三角下三角矩阵、三对角矩阵】的压缩存储代码实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复