三元组矩阵

问题:比较简单^^

代码:

#include <iostream>
using namespace std;
 
#define MAXSIZE 1000
typedef struct tMatrix
{
    int row;
    int col;
    int data;
}TMatrix;
 
typedef struct matrix
{
    TMatrix arr[MAXSIZE];
    int r,c,num;
}Matrix;
 
void CreateTMatrix(Matrix &c)
{
    int rs,cs,ns;
    int p,q,e;
    cout<<"please input the rows,cols and numbers:";
    cin>>rs>>cs>>ns;
    c.r=rs;
    c.c=cs;
    c.num=ns;
     
    for(int i=0;i<ns;i++)
    {
       cout<<"please input the row,col,data:";
       cin>>p>>q>>e;
       c.arr[i].row=p;
       c.arr[i].col=q;
       c.arr[i].data=e;
    }
     
}
 
void playMatrix(Matrix c)
{
    cout<<"output the r,c,n:";
    cout<<c.r<<" "<<c.c<<" "<<c.num<<endl;
    for(int i=0;i<c.num;i++)
    {
        cout<<c.arr[i].row<<" ";
        cout<<c.arr[i].col<<" ";
        cout<<c.arr[i].data;
        cout<<endl;
    }
}
 
void transitionMatrix(Matrix c,Matrix &t)
{
    t.r=c.c;
    t.c=c.r;
    t.num=c.num;
    for(int i=0;i<c.num;i++)
    {
        t.arr[i].row=c.arr[i].col;
        t.arr[i].col=c.arr[i].row;
        t.arr[i].data=c.arr[i].data;
    }
}
 
void displayMatrix(Matrix c)
{
    int k;
    int flag=0;
    for(int i=0;i<c.r;i++)
    {
        for(int j=0;j<c.c;j++)
        {
            for(k=0;k<c.num;k++)
            {
                if((c.arr[k].row==i)&&(c.arr[k].col==j))
                {
                    cout<<c.arr[k].data<<" ";
                    flag=1;
                    break;
                }
            }
            if(flag==0)
                cout<<"0 ";
            flag=0;
        }
        cout<<endl;
    }
}
int main()
{
    Matrix c,t;
    cout<<"创建三元组:"<<endl;
    CreateTMatrix(c);
    cout<<"输出三元组:"<<endl;
    playMatrix(c);
    transitionMatrix(c,t);
    cout<<"转置后的三元组:"<<endl;
    playMatrix(t);
    cout<<"原矩阵:"<<endl;
    displayMatrix(c);
    cout<<"转置后的矩阵:"<<endl;
    displayMatrix(t);
    cout<<endl;
    return 0;
}

运行结果:

posted @   xshang  阅读(372)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示