| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 445 人关注过本帖, 1 人收藏
标题:谁帮我改改这个错误
只看楼主 加入收藏
做个好人
Rank: 1
等 级:新手上路
帖 子:23
专家分:1
注 册:2010-4-9
结帖率:33.33%
收藏(1)
已结贴  问题点数:20 回复次数:2 
谁帮我改改这个错误
#include<iostream>
using namespace std;
class SparseMatrix                         //  稀疏矩阵类。
{
private:
    int Rows,Cols,Count;                       //行数,列数,非零元素的个数。
    Trituple * smArray;                        //存储三元组表的数组。
    int MaxTerm;                               //数组的规模
public:
    SparseMatrix()
    {
        Rows=0;
        Cols=0;
        Count=0;
        MaxTerm=0;
        smArray=NULL;
    }
    SparseMatrix (int Mrows,int Mcols,int Mcount,int Mmaxterm)         //构造函数
    {
        if(Mrows<=0||Mcols<=0)
        {
            cout<<"error"<<endl;
            exit(1);
        }
        if(Mrows==1&&Mcols==1)
        {
            cout<<"error"<<endl;
            exit(1);
        }
        Rows=Mrows;
        Cols=Mcols;
        Count=Mcount;
        MaxTerm=Mmaxterm;
        smArray=new Trituple[Mrows*Mcols];               
    }
    void create(void)
    {
        int a,b,c;
        cout<<"请依次输入row,col,value"<<endl;
        cin>>a>>b>>c;
        while(a!=0&&b!=0&&c!=0)
        {
            for(int k=0;k<Count;k++)
                smArray[k](a,b,c);
            
            cout<<"请依次输入row,col,value"<<endl;
            cin>>a>>b>>c;
        }
    }
    void printout(void)
    {
        for(int i=0;i<Count;i++)
            smArray[i].printT();
    }

    SparseMatrix  Transpose()                //  矩阵a转置。
    {
        SparseMatrix  b;
        b.Rows=Cols;
        b.Cols=Rows;
        b.Cout=Cout;
        if(Cout>0)
        {
            int Bnumber=0;
            for(int k=0;k<Cols;k++)
                for(int i=0;i<Cout;i++)
                    if(a.smArray[i].col==k)
                    {
                        b.smArray[Bnumber].row=k;
                        b.smArray[Bnumber].col=a.smArray[i].row;
                        b.smArray[Bnumber].value=a.smArray[i].value;
                        Bnumber++;
                    }
        }
        return b;
    }
};
class Trituple                                          //三元节点类。
{
friend class SparseMatrix;
private:
    int row;
    int col;
    int value;
public:
    Trituple(int a,int b,int c)                     //构造函数
    {
        row=a;
        col=b;
        value=c;
    }
    void printT()                                   //输出函数。
    {
        cout<<row<<"       "<<col<<"         "<<value<<endl;
    }
};
int main(void)
{
    cout<<"请输入矩阵的行数,列数,非零元素的个数:"<<endl;
    int a,b,c;
    cin>>a>>b>>c;
    SparseMatrix  sp1(a,b,c,10);
    sp1.create();
    sp1.printout();
//    b=sp1.Transpose();
    return 0;
}
   












//

//    SparseMatrix  Add (SparseMatrix  b);
//    SparseMatrix  Multiply(SparseMatrix  b);
搜索更多相关主题的帖子: 改改 
2010-05-24 22:16
最近不在
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:204
专家分:842
注 册:2010-2-28
收藏
得分:10 
程序代码:
#include<iostream>
using namespace std;
class Trituple;   //Trituple * smArray;声明smArray用到了类Trituple类型,要声明Trituple类
class SparseMatrix                         //  稀疏矩阵类。
{
private:
    int Rows,Cols,Count;                       //行数,列数,非零元素的个数。
    Trituple * smArray;                        //存储三元组表的数组。
    int MaxTerm;                               //数组的规模
public:
    SparseMatrix()
    {
        Rows=0;
        Cols=0;
        Count=0;
        MaxTerm=0;
        smArray=NULL;
    }
    SparseMatrix (int Mrows,int Mcols,int Mcount,int Mmaxterm)         //构造函数
    {
        if(Mrows<=0||Mcols<=0)
        {
            cout<<"error"<<endl;
            exit(1);
        }
        if(Mrows==1&&Mcols==1)
        {
            cout<<"error"<<endl;
            exit(1);
        }
        Rows=Mrows;
        Cols=Mcols;
        Count=Mcount;
        MaxTerm=Mmaxterm;
        smArray=new Trituple[Mrows*Mcols];               //Trituple类,写了带参构造函数,就不能使用默认缺省构造函数,要自己写。要不这里会出错
    }
    void create(void)
    {
        int a,b,c;
        cout<<"请依次输入row,col,value"<<endl;
        cin>>a>>b>>c;
        while(a!=0&&b!=0&&c!=0)
        {
            for(int k=0;k<Count;k++)
                smArray[k](a,b,c);     //数组下标内的值,只能为常量或常量表达式,const常量,枚举类型,define类型
                                    //smArray[k](a,b,c)这里应该是赋值,初始化是在定义时进行,只有初始化才能这么用。所以这里不能这样进行所谓的“初始化”
                                    //解决方案用new动态分配,注意写析构函数释放内存
                                    //smArray=new Trituple[Mrows*Mcols];
                                    // smArray[k](a,b,c); 这两处的维数不太对吧,lz再思考
            cout<<"请依次输入row,col,value"<<endl;
            cin>>a>>b>>c;
        }
    }
    void printout(void)
    {
        for(int i=0;i<Count;i++)
            smArray[i].printT();     //数组下标内的值,只能为常量或常量表达式,const常量,枚举类型,define类型
    }

    SparseMatrix  Transpose()                //  矩阵a转置。
    {
        SparseMatrix  b;
        b.Rows=Cols;
        b.Cols=Rows;
        b.Cout=Cout;
       //这里为Count。
       //建议你这么写。
       //int nTemp = Cols;
       //Cols = Rows;
       //Rows = nTemp;
       //Count不操作
        if(Cout>0)
        {
            int Bnumber=0;
            for(int k=0;k<Cols;k++)
                for(int i=0;i<Cout;i++)
                    if(a.smArray[i].col==k)   //这里的a,无中生有,应该直接if(this->smArray[i].col==k) this可隐藏
                    {                          //直接调用另一个类的私有成员是不行,解决方法,自己写个成员函数获取私有成员的值,或者声明有员类
                                                   //后面大量重复该错误
                        b.smArray[Bnumber].row=k;
                        b.smArray[Bnumber].col=a.smArray[i].row;
                        b.smArray[Bnumber].value=a.smArray[i].value;
                        Bnumber++;
                    }
        }
        return b;
    }
};
class Trituple                                          //三元节点类。
{
friend class SparseMatrix;
private:
    int row;
    int col;
    int value;
public:
    Trituple(int a,int b,int c)                     //构造函数
    {
        row=a;
        col=b;
        value=c;
    }
    void printT()                                   //输出函数。
    {
        cout<<row<<"       "<<col<<"         "<<value<<endl;
    }
};
int main(void)
{
    cout<<"请输入矩阵的行数,列数,非零元素的个数:"<<endl;
    int a,b,c;
    cin>>a>>b>>c;
    SparseMatrix  sp1(a,b,c,10);
    sp1.create();
    sp1.printout();
//    b=sp1.Transpose();
    return 0;
}

//总结:主要错误,直接使用未声明的类,调用类的私有成员,数组下标内的值为变量,this指针的用法.
2010-05-25 00:45
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:10 
同意楼上!

南国利剑
2010-05-28 00:18
快速回复:谁帮我改改这个错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021785 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved