首页 > 解决方案 > 如果我在代码块中运行这个程序,我没有得到正确的 ans,但如果我调试它,我就会得到正确的 ans。它正在发生一圈又一圈

问题描述

这个问题是关于添加两个稀疏矩阵。起初我创建了两个稀疏矩阵 s1,s2 (手动获取元素值),然后将它们相加。如果我在代码块中运行它,我得到的答案不正确。但是如果我调试并运行它,那么我得到的答案是完美的。这种事情一次又一次地发生。我不知道为什么。请给我一个富有成效的解决方案。谢谢

#include<bits/stdc++.h>
using namespace std;
struct Element
{
    int i;
    int j;
    int x;
};
struct Sparse_Matrix
{
    int m;
    int n;
    int num;
    struct Element *e;
};
void creation(struct Sparse_Matrix *s)
{
    cout<<"Enter dimension : ";
    cin>>s->m>>s->n;
    cout<<"Enter the number of nonzero elements : ";
    cin>>s->num;
    s->e=new Element[s->num+1];
    cout<<"Enter all non zero elements with it's location "<<endl;
    for(int p=1; p<=s->num; p++)
        cin>>s->e[p].i>>s->e[p].j>>s->e[p].x;
}
struct Sparse_Matrix *Addition(struct Sparse_Matrix *s1,struct Sparse_Matrix *s2)
{
    if((s1->m != s2->m) && (s1->n != s2->n))
        return 0;
    struct Sparse_Matrix *sum;
    sum=new Sparse_Matrix();
    sum->e=new Element[s1->num+s2->num+1];
    int i=0,j=0,k=0;
    while(i<=s1->num && j<=s1->num)
    {
        if(s1->e[i].i<s2->e[j].i)
            sum->e[k++]=s1->e[i++];
        else if(s1->e[i].i>s2->e[j].i)
            sum->e[k++]=s2->e[j++];
        else
        {
            if(s1->e[i].j<s2->e[j].j)
                sum->e[k++]=s1->e[i++];
            else if(s1->e[i].j>s2->e[j].j)
                sum->e[k++]=s2->e[j++];
            else
            {
                sum->e[k]=s1->e[i++];
                sum->e[k++].x+=s2->e[j++].x;
            }
        }
    }
    for(; i<=s1->num; i++)
        sum->e[k++]=s1->e[i++];
    for(; j<=s2->num; j++)
        sum->e[k++]=s2->e[j++];
    sum->n=k;
    sum->m=s1->m;
    sum->n=s1->n;
    return sum;
};
void display(struct Sparse_Matrix s)
{
    int k=1;
    for(int p=1; p<=s.m; p++)
    {
        for(int q=1; q<=s.n; q++)
        {
            if(s.e[k].i==p && s.e[k].j==q)
                cout<<s.e[k++].x<<" ";
            else
                cout<<"0 ";
        }
        cout<<endl;
    }
}
int main()
{
    struct Sparse_Matrix s1,s2,*sum;
    creation(&s1);
    creation(&s2);
    cout<<"Displaying the s1 Matrix "<<endl;
    display(s1);
    cout<<"Display the s2 Matrix "<<endl;
    display(s2);
    sum=Addition(&s1,&s2);
    cout<<"Displaying the sum "<<endl;
    display(*sum);
}

标签: c++data-structures

解决方案


好的,让我们尝试在该代码中添加一点 C++(同时修复许多错误):

#include <vector>
#include <iostream>
#include <exception>
#include <sstream>

struct SparseMatrix {
    struct Element {
        int i;
        int j;
        int x;

        friend std::istream& operator>>(std::istream& is, SparseMatrix::Element& e) {
            return is >> e.i >> e.j >> e.x;
        }

        bool operator<(const Element& other) const {
            if (i < other.i) {
                return true;
            }
            else if (other.i < i) {
                return false;
            }
            else if (j < other.j) {
                return true;
            }
            else if (other.j < j) {
                return false;
            }
            else {
                return false;
            } 
        }
    };

    int m_; // rows
    int n_; // cols
    std::vector<Element> e_;

    SparseMatrix(int m = 0, int n = 0) : m_(m), n_(n) {}
    Element& operator[](size_t pos) { return e_[pos]; }
    const Element& operator[](size_t pos) const { return e_[pos]; }
    void push_back(const Element& val) { e_.push_back(val); }
    int rows() const { return m_; }
    int cols() const { return n_; }
    // Non Zero Elements
    size_t nze() const { return e_.size(); }

    friend SparseMatrix Add(const SparseMatrix& s1, const SparseMatrix& s2);

    friend std::istream& operator>>(std::istream& is, SparseMatrix& sm) {
        size_t num;
        is >> sm.m_ >> sm.n_ >> num;
        SparseMatrix::Element e;
        for (size_t i = 0; i < num; ++i) {
            is >> e;
            sm.push_back(e);
        }
        return is;
    }

    friend std::ostream& operator<<(std::ostream& os, const SparseMatrix& sm) {
        size_t k = 0;
        for (int r = 0; r < sm.rows(); r++) {
            for (int c = 0; c < sm.cols(); c++) {
                if (sm[k].i == r && sm[k].j == c)
                    os << sm[k++].x << " ";
                else
                    os << "0 ";
            }
            os << '\n';
        }
        return os;
    }
};

SparseMatrix Add(const SparseMatrix& s1, const SparseMatrix& s2)
{
    if (s1.rows() != s2.rows() || s1.cols() != s2.cols()) {
        throw std::exception("Dimension mismatch");
    }

    SparseMatrix sum(s1.rows(), s1.cols());
    sum.e_.resize(s1.nze() + s2.nze());
    size_t i = 0, j = 0, k = 0;
    while (i < s1.nze() && j < s1.nze()) {
        auto e1 = s1[i], e2 = s2[j];
        if (e1 < e2) {
            sum[k++] = e1;
            i++;
        }
        else if (e2 < e1) {
            sum[k++] = e2;
            j++;
        }
        else {
            sum[k] = e1;
            sum[k++].x += e2.x;
            i++;
            j++;
        }
    }
    while (i < s1.nze())
        sum[k++] = s1[i++];
    while (j < s2.nze())
        sum[k++] = s2[j++];
    return sum;
};

int main()
{
    SparseMatrix s1, s2;
    std::stringstream in(R"(
5 7
4
1 4 7
3 4 1
4 2 3
4 6 8

5 7
3
2 3 5
3 2 8
4 6 1
    )");
    in >> s1 >> s2;
    std::cout << "Displaying the s1 Matrix\n" << s1 << '\n';
    std::cout << "Displaying the s2 Matrix\n" << s2 << '\n';
    SparseMatrix sum = Add(s1, s2);
    std::cout << "Displaying the sum\n" << sum << '\n';

    return 0;
}

错误在这里:

    if((s1->m != s2->m) && (s1->n != s2->n))
        return 0;

如果行或列不同,您要停止的位置。使用或 ( ||)。另一个错误在这里:

    for(; i<=s1->num; i++)
        sum->e[k++]=s1->e[i++];
    for(; j<=s2->num; j++)
        sum->e[k++]=s2->e[j++];

事实上ij在循环中增加两次。只需切换到while。


推荐阅读