简单作业代码。。求改

叶子君 2014-04-10 11:07:09
/*
Project:假定按列主次序把一个稀疏矩阵映射到一个一维数组中
1)给出4-10a中稀疏矩阵的描述
---------------------------------

a[] | 0 1 2 3 4 5 6 7 8
-----+----------------------------
row | 2 4 4 1 3 2 3 1 2
col | 2 2 3 4 4 5 6 7 8
value| 6 4 5 2 9 7 8 1 3
----------------------------------
2)对按照这种方式存储的稀疏矩阵,编写相应的Store和Retrieve函数,store函数可以往稀疏矩阵中某位置存入一个数,retrieve函数可以在稀疏矩阵中搜索某位置并返回该位置的值

我的思路:定义一个稀疏矩阵的类模板,然后稀疏矩阵中每一个元素定义为一个结构体(包括该位置的行列号和值)
我的问题:编译报错,实现不了
3)时间复杂性各是多少?
*/
#include <iostream>
using namespace std;

template <class T>
struct ele
{
int row,col;//非零元素所在的行和列
T value;//非零元素的值
};

template <class T>
class SparseMatrix
{
friend ostream& operator<< (ostream&, const SparseMatrix<T>&);
friend istream& operator>> (istream&, SparseMatrix<T>&);
public:
SparseMatrix(int maxTerms = 10);
~SparseMatrix() {delete [] a;}
SparseMatrix<T> & Store(const T &x, int i,int j);//新添加的Store函数!!!!
T Retrieve (int i,int j)const;//新添加的Retrieve函数!!!!!!


private:
// void Append(const Term<T>& t);
int rows, cols; // 矩阵维数

int terms; // 非零元素个数
ele *a; // 存储非零元素的数组
int MaxTerms; //数组a的大小
};

template<class T>//***构造函数
SparseMatrix<T>::SparseMatrix(int maxTerms)
{// Sparse matrix constructor.
if (maxTerms < 1) exit(1);
MaxTerms = maxTerms;
a = new ele<T> [MaxTerms];
terms = rows = cols = 0;
}

template <class T>
ostream& operator<<(ostream& out, const SparseMatrix<T>& x)
{// Put *this in output stream.
out << " columns = "<< x.cols << "rows = " << x.rows << endl;
out << "nonzero terms = " << x.terms << endl;

for (int i = 0; i < x.terms; i++)
out << "a(" << x.a[i].col << ',' << x.a[i].row
<< ") = " << x.a[i].value << endl;
return out;
}

template<class T>
istream& operator>>(istream& in, SparseMatrix<T>& x)
{// Input a sparse matrix.
cout << "Enter number of columns, rows, and terms"
<< endl;
in >> x.cols >> x.rows >> x.terms;
if (x.terms > x.MaxTerms) exit(1);

for (int i = 0; i < x.terms; i++) {
cout << "Enter column,row, and value of term "
<< (i + 1) << endl;
in >> x.a[i].col >> x.a[i].row>> x.a[i].value;
}
return in;
}

template<class T>//*********************store
SparseMatrix<T>&SparseMatrix<T>::Store(const T &x, int i,int j)
{
for ( int b=0;b<terms;b++)//欲存储位置本来有数值
{
if (a[b].col==j)
{
if (a[b].row==i)
{
a[b].value=x;
return *this;
}
}
}
terms++;
for (int p=0;p<terms;p++)
{
if (j<=a[p].col||i<=a[p].row)
{
for (int m=terms-1;m>=p;m--)
{
a[m+1]=a[m];
}
a[p]=x;
a[p].col=j;
a[p].row=i;
return *this;
}
}
}

template<class T>//********************retrieve
T SparseMatrix<T>::Retrieve(int i,int j)const
{
if (i<=0||j<=0) exit(1);
for (int q=0;q<terms;q++)
{
if (a[q].col==j && a[q].row==i)
return a[q].value;
}
exit(1);
}

int main()
{
SparseMatrix<int> s(32);
cin>>s;
cout<<s;

s.Store(7,2,1);
cout<<s;

s.Retrieve(3,4);

return 0;
}

希望大神帮忙改一下以输出正确结果,谢谢~
...全文
144 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
一阳初 2014-04-12
  • 打赏
  • 举报
回复
private: // void Append(const Term<T>& t); int rows, cols; // 矩阵维数 int terms; // 非零元素个数 ele *a; // 存储非零元素的数组 改为ele<T> *a; int MaxTerms; //数组a的大小 store中 a[p]=x; 改为a[p]=x.value; a[p].col=j; a[p].row=i; return *this 这样就没错了,不知道结果对不对,结果好像和要求不同

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧