三元组矩阵
问题:比较简单^^
代码:
#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; }
运行结果: