【发布时间】:2020-07-01 20:28:29
【问题描述】:
我必须使用 3 个动态数组(索引从 0 开始)在 C++ 中实现 CSR 矩阵数据结构,但我遇到了困难。所以我必须实现2个功能:
1) modify(int i, int j, TElem e) - 将 (i,j) 的值修改为 e 或添加 if(如果它不存在)或如果 e 为 null 则删除它。
2) element(int i, int j) const - 返回在 (i,j) 上找到的值
我想用下一种方式测试我的代码:
矩阵 m(4,4); m.print();它将打印:
行数:0 0 0 0 0
列:
价值观:
(这很好)
现在如果我想修改:m.modify(1,1,5); //元素(1,1)将被设置为5
m.print() 的输出;将是:
行数:0 1 1 1 1
列数:1
值:5(这也很好)
现在如果我想打印 m.element(1, 1) 它将返回 0 而 m.element(0, 1) 将返回 5。
这是我对 element(int i, int j) 的实现:
int currCol;
for (int pos = this->lines[i]; pos < this->lines[i+1]; pos++) {
currCol = this->columns[pos];
if (currCol == j)
return this->values[pos];
else if (currCol > j)
break;
}
return NULL_TELEM;
构造函数如下所示:
Matrix::Matrix(int nrLines, int nrCols) {
if (nrLines <= 0 || nrCols <= 0)
throw exception();
this->nr_lines = nrLines;
this->nr_columns = nrCols;
this->values = new TElem[100];
this->values_capacity = 1;
this->values_size = 0;
this->lines = new int[nrLines + 1];
this->columns = new TElem[100];
this->columns_capacity = 1;
this->columns_size = 0;
for (int i = 0; i <= nrLines; i++)
this->lines[i] = NULL_TELEM;
}
这是“修改”方法:
TElem Matrix::modify(int i, int j, TElem e) {
if (i < 0 || j < 0 || i >= this->nr_lines || j >= nr_columns)
throw exception();
int pos = this->lines[i];
int currCol = 0;
for (; pos < this->lines[i + 1]; i++) {
currCol = this->columns[pos];
if (currCol >= j)
break;
}
if (currCol != j) {
if (!(e == 0))
add(pos, i, j, e);
}
else if (e == 0)
remove(pos, i);
else
this->values[pos] = e;
return NULL_TELEM;
}
这是插入方法:
void Matrix::add(int index, int line, int column, TElem value)
{
this->columns_size++;
this->values_size++;
for (int i = this->columns_size; i >= index + 1; i--) {
this->columns[i] = this->columns[i - 1];
this->values[i] = this->values[i - 1];
}
this->columns[index] = column;
this->values[index] = value;
for (int i = line; i <= this->nr_lines; i++) //changed to i = line + 1;
this->lines[i]++;
}
有人可以帮帮我吗?我不知道为什么会发生这种情况,我真的需要这些天完成这个实现。
只是无法通过下一个测试。如果我想打印我有 (4,0)=0 (4,1)=0 ... (4,8)=0 和 (4,9)=3 的元素。现在看起来很奇怪,为什么会这样。
void testModify() {
cout << "Test modify" << endl;
Matrix m(10, 10);
for (int j = 0; j < m.nrColumns(); j++)
m.modify(4, j, 3);
for (int i = 0; i < m.nrLines(); i++)
for (int j = 0; j < m.nrColumns(); j++)
if (i == 4)
assert(m.element(i, j) == 3);
//cout << i << " " << j << ":" << m.element(i, j)<<'\n';
else
assert(m.element(i, j) == NULL_TELEM);
}
【问题讨论】:
-
我们需要查看更多细节。
Matrix类的定义是什么,尤其是modify函数。 Edit 为minimal reproducible example 添加足够详细信息的问题(最低限度意味着您可以省略与问题无关的课程详细信息)。 -
我添加了构造函数和插入元素的函数。这些似乎工作正常。但我不明白为什么“元素”方法没有返回好的值。
标签: c++ algorithm matrix data-structures sparse-matrix