数据结构课程设计 报告(十字链表实现稀疏矩阵的加法)(2)

2019-08-01 22:39

tmp->right=p;

InsertInCol(tmp); //在列表中插入tmp pre=tmp; //当前指针p的前驱变为tmp pa=pa->right; }

else { //列标相同,则做加法 p->data +=pa->data;

if(!p->data) { //和为0,则删除之 (行、列都要删除) tmp=p;p=p->right;pre->right=p;//在行链表中将tmp摘下 DeleteInCol(tmp); //在列链表中将tmp删除 }

pre=p;p=p->right;pa=pa->right; } }

h=h->next; ah=ah->next; //处理下一行 }

return *this; }

9、重载+

template //重载加法运算符+

LinkMatrix operator +(const LinkMatrix &a,const LinkMatrix &b){ LinkMatrix c(a); //复制构造函数得到被加矩阵A的一个副本放在矩阵C中 c+=b;return c; }

五、测试与讨论 1、编译环境 Visual C++ 6.0 2、main函数 int main() { LinkMatrixa,b,c; cin>>a; cout<<\矩阵为:\\n\ cin>>b; cout<<\矩阵为:\\n\ c=a+b; cout<<\ system(\ return 0; }

3、具体步骤

开始编译,界面出现提示“请输入行数、列数、非零元个数”。

输入

3 3 3

表示这个矩阵行数为3,列数为3,非零元个数为3,接着提示“请输入一个非零元三元组”。

输入

1 1 1

表示第一个三元组的行为1,列为1,值为1

然后程序继续提示“输入一个非零元三元组”。

按要求再依次输入 2 2 2 3 1 5

则矩阵A输入完成,程序按矩阵格式输出矩阵A

程序继续提示“请输入行数、列数、非零元个数” 在按上述操作继续输入 3 3 4 1 1 2 2 1 1 3 1 1 3 3 3

为矩阵B输入,完成后程序输入矩阵B 程序同时输出A+B

3、结果分析

程序提供输入和输出,根据输入的矩阵A和矩阵B,A+B计算结果准确无误。

六、用户手册

使用本程序简单明了,只需根据提示为稀疏矩阵输入,就可以计算出稀疏矩阵的和。

附录:源代码

(只含 pro1.cpp):

//pro1.cpp

#include using namespace std;

templateclass MatrixNode; templateclass LinkMatrix;

templateistream &operator>>(istream &,LinkMatrix&); templateostream &operator<<(ostream &,LinkMatrix&);

templateLinkMatrix operator +(const LinkMatrix &a,const LinkMatrix &b);

templateclass MatrixNode {

friend class LinkMatrix; friend istream&operator>>(istream&,LinkMatrix&); friend ostream&operator<<(ostream&out, LinkMatrix&);

friend LinkMatrix operator +(const LinkMatrix &a,const LinkMatrix &b); private: int row,col; MatrixNode*right,*down; union{ Type data; MatrixNode*next; }; };

templateclass LinkMatrix { private: MatrixNode *head; void InsertInCol(MatrixNode*p); void DeleteInCol(MatrixNode*p); public: friend istream&operator>>(istream&in,LinkMatrix&); friend ostream&operator<<(ostream&out,LinkMatrix&); MatrixNode* Head(int i); LinkMatrix&operator +=(const LinkMatrix &a);


数据结构课程设计 报告(十字链表实现稀疏矩阵的加法)(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:《机械制图习题集》(第四版)答案(1)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: