最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

稀疏矩阵C++语言实现

来源:动视网 责编:小OO 时间:2025-09-30 08:33:08
文档

稀疏矩阵C++语言实现

#include#includeusingnamespacestd;template//三元组structTrituple{introw;intcol;Tval;};//稀疏矩阵声明templateclassSparseMatrix{public:SparseMatrix(intmaxt=100);~SparseMatrix();boolTransposeTo(SparseMatrix&);boolAddTo(constSparseMatrix&);boolTransposeTo_Faster
推荐度:
导读#include#includeusingnamespacestd;template//三元组structTrituple{introw;intcol;Tval;};//稀疏矩阵声明templateclassSparseMatrix{public:SparseMatrix(intmaxt=100);~SparseMatrix();boolTransposeTo(SparseMatrix&);boolAddTo(constSparseMatrix&);boolTransposeTo_Faster
#include 

#include 

using namespace std;

template

//三元组

struct Trituple

{

    int row;

    int col;

    T val;

};

//稀疏矩阵声明

template

class SparseMatrix

{

public:

    SparseMatrix(int maxt=100);

    ~SparseMatrix();

    bool TransposeTo(SparseMatrix &);

    bool AddTo(const SparseMatrix&);

    bool TransposeTo_Faster(SparseMatrix&);

    void Input();

    void Output();

private:

    Trituple* data;

    int rows,cols,terms;

    int maxterms;

};

template

SparseMatrix::SparseMatrix(int maxt)

{

    maxterms=maxt;

    data=new Trituple[maxterms];

    terms=rows=cols=0;

}

template

SparseMatrix::~SparseMatrix()

{

    if (data!=NULL)

    {

        delete[] data;

    }

}

//普通转置

template

bool SparseMatrix::TransposeTo(SparseMatrix &B)

{

    if (terms>B.maxterms)

    {

        return false;

    }

    B.rows=cols;

    B.cols=rows;

    B.terms=terms;

    if (terms>0)

    {

        int p=0;

        for (int j=1;j<=cols;j++)

        {

            for (int k=0;k            {

                if (data[k].col==j)

                {

                    B.data[p].row=j;

                    B.data[p].col=data[k].row;

                    B.data[p].val=data[k].val;

                    p++;

                }

            }

        }

    }

    return true;

}

//快速转置

template

bool SparseMatrix::TransposeTo_Faster(SparseMatrix& B)

{

    if (terms>B.maxterms)

    {

        return false;

    }

    B.rows=cols;

    B.cols=rows;

    B.terms=terms;

    if (terms>0)

    {

        int *num,*cpot;

        num=new int[cols];

        cpot=new int[cols];

        for (int j=0;j        {

            num[j]=0;

        }

        for (int k=0;k        {

            num[data[k].col-1]++;

        }

        //求出B中每一行的起始下标cpot[]

        cpot[0]=0;

        for (int j=1;j        {

            cpot[j]=cpot[j-1]+num[j-1];

        }

        //执行转置操作

        for (int p,k=0;k        {

            p=cpot[data[k].col-1]++;

            B.data[p].row=data[k].col;

            B.data[p].col=data[k].row;

            B.data[p].val=data[k].val;

        }

        delete[] num;

        delete[] cpot;

    }

    return true;

}

template

void SparseMatrix::Input()

{

    cout<<"intput the matrix' row:";

    int row1;

    cin>>row1;

    cout<<"intput the matrix' col:";

    int col1;

    cin>>col1;

    cout<<"input "<    int number;

    rows=row1;

    cols=col1;

    for (int i=0;i    {

        for (int j=0;j        {

            cin>>number;

            if (number!=0)

            {

                data[terms].row=i+1;

                data[terms].col=j+1;

                data[terms].val=number;

                terms++;

            }

        }

    }

}

template  //输出好看,但是违背了最初的原则

void SparseMatrix::Output()

{

    T **tempArray=new T*[rows];

    for (int i1=0;i1    {

        tempArray[i1]=new int[cols];

    }

    for (int j=0;j    {

        for (int k=0;k        {

            tempArray[j][k]=0;

        }

    }

    for (int i=0;i    {

        tempArray[data[i].row-1][data[i].col-1]=data[i].val;

    }

    for (int j=0;j    {

        for (int k=0;k        {

            cout<        }

        cout<    }

    for (int l=0;l    {

        delete[] tempArray[l];

    }

    delete tempArray;

    cout<}

template

bool SparseMatrix::AddTo(const SparseMatrix& B)

{

    if (rows!=B.rows||cols!=B.cols)

    {

        return false;

    }

    bool flag=false;

    int tempTerms=terms;

    for (int i=0;i    {

        flag=false;

        for (int j=0;j        {

            if (data[j].col==B.data[i].col&&data[j].row==B.data[i].row)

            {

                data[j].val+=B.data[i].val;

                flag=true;

            }

        }

        if (flag==false)

        {

            data[++terms-1].col=B.data[i].col;  //数组下标操作注意事项

            data[terms-1].row=B.data[i].row;

            data[terms-1].val=B.data[i].val;

        }

    }

    return true;

}

int main()

{

    cout<<"此程序演示稀疏矩阵的普通转置和快速转置操作"<    SparseMatrix sm(50);

    SparseMatrix sm1(50);

    SparseMatrix sm2(50);

    sm.Input();

    cout<<"sm is"<    sm.Output();

    sm.TransposeTo(sm1);

    cout<<"Transposition of sm is "<    sm1.Output();

    sm.TransposeTo_Faster(sm2);

    cout<<"Transposition of sm is "<    sm2.Output();

    SparseMatrix sm3;

    cout<<"input a new matrix"<    sm3.Input();

    cout<<"sm3 is"<    sm3.Output();

    if(sm.AddTo(sm3))

    {

        cout<<"after adding sm3 ,sm is"<        sm.Output();

    }

    else

        cout<<"the two matrix can't add"<    cout<<"Good job!"<    system("pause");

    return 0;

}

文档

稀疏矩阵C++语言实现

#include#includeusingnamespacestd;template//三元组structTrituple{introw;intcol;Tval;};//稀疏矩阵声明templateclassSparseMatrix{public:SparseMatrix(intmaxt=100);~SparseMatrix();boolTransposeTo(SparseMatrix&);boolAddTo(constSparseMatrix&);boolTransposeTo_Faster
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top