#pragma once
#define MAXSIZE 1000
#include<iostream>
using namespace std;
template<class DataType>
class triple {
public:
int row, col;
DataType e;
};
template<class DataType>
class Mtriple {
public:
int mrow, mcol, mnum;//mnum是非0个数
triple<DataType>data[MAXSIZE + 1];
void transpose(Mtriple a);
void printm(Mtriple a);
Mtriple(int mrow, int mcol, DataType *m);
Mtriple();
~Mtriple();
};
template<class DataType>
inline Mtriple<DataType>::Mtriple(int mrow,int mcol, DataType *m)
{
cout << 'i' << " " << 'j' << " " << 'v' << endl;
int cnt = 0;
this->mrow = mrow;
this->mcol = mcol;
for (int i = 0; i < mrow; i++) {
for (int j = 0; j < mcol; j++) {
if (*(m+i*mcol+j) != 0) {
//this->data[++cnt] = m[i][j];
this->data[++cnt].row = i+1;
this->data[cnt].col = j+1;
this->data[cnt].e = *(m + i * mcol + j);
cout << this->data[cnt].row << " " << this->data[cnt].col << " " << this->data[cnt].e << endl;
}
}
}
this->mnum = cnt;
}
template<class DataType>
inline Mtriple<DataType>::Mtriple()
{
}
template<class DataType>
inline void Mtriple<DataType>::transpose(Mtriple a)
{
cout <<endl<< 'i' << " " << 'j' << " " << 'v' << endl;
int cnt = 1;
a.mrow = this->mcol;
a.mcol = this->mrow;
a.mnum = this->mnum;
for (int j = 1; j <= this->mcol; j++) {
for (int i = 1; i <= this->mnum; i++) {
if (this->data[i].col == j) {
a.data[cnt].row = j;
a.data[cnt].col = this->data[i].row;
a.data[cnt].e = this->data[i].e;
cout << a.data[cnt].row << " " << a.data[cnt].col << " " << a.data[cnt].e << endl;
++cnt;
}
}
}
printm(a);
}
template<class DataType>
inline void Mtriple<DataType>::printm(Mtriple a)
{
cout << endl << "转置矩阵:" << endl;
int flag = 1;
for (int i = 1; i <= a.mrow; i++) {
for (int j = 1; j <= a.mcol; j++) {
for (int k = 1; k <= a.mnum; k++) {
if ((j == a.data[k].col)&&(i == a.data[k].row)) {
cout << a.data[k].e << " ";
flag = 1;
break;
}
else
flag = 0;
}
if (flag == 0) {
cout << 0 << " ";
}
}
cout << '\n';
}
}
template<class DataType>
inline Mtriple<DataType>::~Mtriple()
{
}
#include<iostream>
#include<stdlib.h>
#include"triple.h"
using namespace std;
//void fun(int m[3][4]) {
//
// for (int i = 0; i < 3; i++)
// for (int j = 0; j < 4; j++)
// cout << m[i][j];
//}
int main() {
int m[3][4] = {
{0,0,0,1},
{1,0,0,1},
{0,0,1,0} };
Mtriple<int> t(3, 4, (int*)m);
Mtriple<int> p;
t.transpose(p);
//fun(m);
system("pause");
return 0;
}