简略作业代码。求改
简单作业代码。。求改
/*
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
/*
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