当前位置:首页 > 稀疏矩阵乘法的运算
武汉理工大学《数据结构》课程设计说明书
ElemType data;//存储row行col列的元素的值 }DataNode;
采用结构体存储稀疏二维数组: typedef struct SparseMatrix {
DataType *matrix;//用数组存储非零元 int row;//二维数组的行数 int col;//二维数组的列数
int *rpos;//每行第一个非零元在matrix中的位置 int num;//非零元的个数 int con;//指定常数
};
注:在第二个结构体中num和con这两个变量,在本此系统中con默认为0,num的功能是检验数据是否完整。当二维数组中出现很多非零元素值一样的时候,也可以采用三元组的方法存储,只要让con等于那个非零元素值即可。
7.算法描述:
本系统的主要算法为稀疏矩阵的乘法运算。 算法如下: Q初始化;
if (Q是非零矩阵) {//逐行求积
for(arow=1;arow<=M.row;++arow) {//处理M的每一行
ctemp[]=0;//累加器清零
计算Q中第arow行的积并存入ctemp[]中; 将ctemp[]中的非零元压缩存储到Q.matrix中; } }
3
武汉理工大学《数据结构》课程设计说明书
8.测试结果与分析:
测试1:两个文件数据完全正确
Input the method of getting data(1 is from file,2 is from inputting):1 Input two names of the data file:data1.txt data2.txt Read the first file successfully! Read the second file successfully! The first matrix is: 1 1 3 1 4 5 2 2 -1 3 1 2
The second matrix is: 1 2 2 2 1 1 3 1 -2 3 2 4 The result is: 3 0 3 2 1 2 6 2 1 -1 3 2 4
Input one name of the data file:data5.txt
测试2:一个文件数据完全正确,另一个文件数据不正确 Input the method of getting data(1 is from file,2 is from inputting):1 Input two names of the data file:data1.txt data4.txt Read the first file successfully! The data is wrong! The first data is wrong!
4
武汉理工大学《数据结构》课程设计说明书
Please check it carefully,then input it again: 1 2 2
The data is wrong! The 4th data is wrong!
Please check it carefully,then input it again: 3 2 4
Read the second file successfully! The first matrix is: 1 1 3 1 4 5 2 2 -1 3 1 2
The second matrix is: 1 2 2 2 1 1 3 1 -2 3 2 4 The result is: 3 0 3 2 1 2 6 2 1 -1 3 2 4
Input one name of the data file:data6.txt 测试3:两个数据文件都错误
Input the method of getting data(1 is from file,2 is from inputting):1 Input two names of the data file:data3.txt data4.txt The data is wrong! The second data is wrong!
Please check it carefully,then input it again:
5
武汉理工大学《数据结构》课程设计说明书
1 4 5
The data is wrong! The 4th data is wrong!
Please check it carefully,then input it again: 3 1 2
Read the first file successfully! The data is wrong! The first data is wrong!
Please check it carefully,then input it again: 1 2 2
The data is wrong! The 4th data is wrong!
Please check it carefully,then input it again: 3 2 4
Read the second file successfully! The first matrix is: 1 1 3 1 4 5 2 2 -1 3 1 2
The second matrix is: 1 2 2 2 1 1 3 1 -2 3 2 4 The result is: 3 0 3 2 1 2 6 2 1 -1
6
共分享92篇相关文档