微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法调试此程序使用链表的稀疏矩阵

如何解决无法调试此程序使用链表的稀疏矩阵

我的功能operator+无法正常工作,其他功能正常,我已经在这个程序上工作了一整天,仍然无法正常工作。

它只适用于一些特殊情况,例如对角矩阵。希望有人给我一些提示

矩阵的概念:

enter image description here

执行过程:

input(row,col,value)。
3 3 3 //输入稀疏矩阵的维数和个数
0 0 1 //稀疏矩阵1
1 1 1
2 2 1 //稀疏矩阵 3.
0,1 //ofstream.
1,1,1
2,2,1
输入(行,列,值)
3 3 3
0 0 1
1 1 1
2 2 1
3 3. //输入加法维度
0,2 //result,这个结果是正确的。
1,2
2,2

#include <iostream>
using namespace::std;

struct Triple{
    int row;
    int col;
    int val;
};

class Matrix;

class Matrixnode{
    friend class Matrix;
    friend istream &operator>>(istream &is,Matrix &matrix);
    friend ostream &operator<<(ostream &os,Matrix &matrix);
    
private:
    Matrixnode *right;
    Matrixnode *down;
    bool head;
    
    union{
        Matrixnode* next;
        Triple triple;
    };
public:
    Matrixnode(){
        
    };
    Matrixnode(bool b,Triple*t){
        head=b;
        if(head){right=down=next=this;}
        else triple=*t;
    };
};

class List{
    
};

class Matrix{
    friend istream& operator>>(istream& is,Matrix& matrix);
    friend ostream& operator<<(ostream& os,Matrix& matrix);
public:
    Matrix(){
    
    }
    int max(int a,int b){
        if(a>b)
            return a;
        else
            return b;
    }
    Matrix Transpose();
    Matrix operator+(Matrix &matrix);
    
private:
    Matrixnode* headnode;
};

Matrix Matrix::Transpose(){
    Matrixnode *headnode=this->headnode->right;

    while(headnode!=this->headnode){
    
        for(Matrixnode *cur=headnode->right;cur!=headnode;cur=cur->right)
        swap(cur->triple.row,cur->triple.col);
            
        headnode=headnode->next;
    }
    return *this;
}

Matrix Matrix::operator+(Matrix &matrix){
    
    Matrixnode *cur1=headnode->right;
    Matrixnode *cur2=matrix.headnode->right;
    Matrixnode *headnode1=cur1->right;
    Matrixnode *headnode2=cur2->right;


    Matrix add;
    
    
    Triple s;

    cin >> s.row >> s.col;
    s.val=10;
    int p=max(s.row,s.col);
    int x=matrix.headnode->triple.val;
    int y=headnode->triple.val;
    

    add.headnode =new Matrixnode(false,&s);

    if(p==0)
    {
           add.headnode->right=add.headnode;
           return add;
    }
    Matrixnode **head = new Matrixnode*[p];

    for (int i=0;i<p;i++)
           head[i]=new Matrixnode(true,0);

    int currentRow=0;
    Matrixnode *last=head[0];
    

    for(int i=0;i<x+y;i++)
    {
        while(cur1!=this->headnode && cur2!=matrix.headnode){
        if (headnode1->triple.row>currentRow && headnode2->triple.row>currentRow)
           {
                   last->right=head[currentRow];
               if(headnode1->triple.row>headnode2->triple.row){
                   currentRow =headnode1->triple.row;
                   last=head[currentRow];
               }
               else{
                   currentRow =headnode2->triple.row;
                   last=head[currentRow];
               }
           }
        
        if(headnode1->triple.col==headnode2->triple.col && headnode1->triple.row==headnode2->triple.row){
            Triple t;
            t.val=headnode1->triple.val+headnode2->triple.val;
            t.col=headnode1->triple.col;
            t.row=headnode1->triple.row;
            last=last->right=new Matrixnode(false,&t);
            head[t.col]->next=head[t.col]->next->down=last;
            
            headnode1=headnode1->right;
            headnode2=headnode2->right;
            if(headnode1==cur1 && cur1!=headnode){
                cur1=cur1->next;
                headnode1=cur1->right;
            }
            if(headnode2==cur2 && cur2!=matrix.headnode){
                cur2=cur2->next;
                headnode2=cur2->right;
            }

        }
        else if(headnode1->triple.col>headnode2->triple.col){
            last=last->right=new Matrixnode(false,&headnode2->triple);
            head[headnode2->triple.col]->next=head[headnode2->triple.col]->next->down=last;
            

            headnode2=headnode2->right;
            if(headnode2==cur2){
                cur2=cur2->next;
                headnode2=cur2->right;
            }

        }
        else if(headnode1->triple.col<headnode2->triple.col){
            last=last->right=new Matrixnode(false,&headnode1->triple);
            head[headnode1->triple.col]->next=head[headnode1->triple.col]->next->down=last;
            
            headnode1=headnode1->right;
            if(headnode1==cur1 ){
                cur1=cur1->next;
                headnode1=cur1->right;
            }
        }
        }
    }

    last->right=head[currentRow];
    for (int i=0;i<s.col;i++)
           head[i]->next->down=head[i];

    for (int i=0;i<p-1;i++)
           head[i]->next=head[i+1];

    head[p-1]->next=add.headnode;
    add.headnode->right=head[0];
    delete[]head;
    return add;
}

istream &operator>>(istream &is,Matrix &matrix)
{
        Triple s;
        cout<<"input(row,value)"<<endl;
        is >> s.row >> s.col >> s.val;
 
        int p=matrix.max(s.row,s.col);

        matrix.headnode =new Matrixnode(false,&s);
    
        if(p==0)
        {
               matrix.headnode->right=matrix.headnode;
               return is;
        }
        Matrixnode **head = new Matrixnode*[p];
 
        for (int i=0;i<p;i++)
               head[i]=new Matrixnode(true,0);

        int currentRow=0;
        Matrixnode *last=head[0];
 
        for(int i=0;i<s.val;i++)
        {
               Triple t;
               is >>t.row>>t.col>>t.val;
               if (t.row>currentRow)
               {
                       last->right=head[currentRow];
                       currentRow =t.row;
                       last=head[currentRow];
               }
               last=last->right=new Matrixnode(false,&t);
               head[t.col]->next=head[t.col]->next->down=last;
        }
 
        last->right=head[currentRow];
        for (int i=0;i<s.col;i++)
               head[i]->next->down=head[i];

        for (int i=0;i<p-1;i++)
               head[i]->next=head[i+1];
    
        head[p-1]->next=matrix.headnode;
        matrix.headnode->right=head[0];
        delete[]head;
        return is;
}

ostream &operator<<(ostream &os,Matrix &matrix){
    Matrixnode *headnode=matrix.headnode->right;
    
    while(headnode!=matrix.headnode)
    {
        for(Matrixnode *cur=headnode->right;cur!=headnode;cur=cur->right){
        os<<cur->triple.row<<","<<cur->triple.col<<","<<cur->triple.val<<endl;
        }
        headnode=headnode->next;

    }
    return os;
}





int main(int argc,const char * argv[]) {
    
    Matrix m,n,o;
    cin >> m;
    cout << m;
    cin >> n;

    m=m.operator+(n);
    cout << m;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。