📄 三元组表快转置.cpp
字号:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
//++++++++++++++++++++*****************
ifstream fin("in.txt");
const int maxterms = 8; //*
class SparseMatrix;
class Trituple {
friend class SparseMatrix;
private:
int row,col;
int valve;
};
//--------------------------------------
class SparseMatrix {
int rows, cols, terms;
Trituple smarray[maxterms];
public:
SparseMatrix(int maxrow,int maxcol)
{
rows = maxrow;
cols = maxcol;
}
SparseMatrix fasttranspose();
void set();
void print();
};
//----------------------------------------
void SparseMatrix::print() {
for(int i = 0; i< terms; i++)
{
cout<<setw(5)<<smarray[i].row
<<setw(5)<<smarray[i].col
<<setw(5)<<smarray[i].valve
<<endl;
}
cout<<endl;
}
void SparseMatrix::set() {
fin>>terms;
for(int i = 0; i< terms; i++)
{
fin>>smarray[i].row>>smarray[i].col>>smarray[i].valve ;
}
}
//----------------------------------------------------------------------
SparseMatrix SparseMatrix::fasttranspose () {
int *rowsize = new int[cols];
int *rowstart = new int[cols];
SparseMatrix b(cols,rows);
b.terms = terms;
if(terms > 0)
{
for(int i = 0; i < cols; i++) rowsize[i] = 0;
for(i = 0; i < terms; i++) rowsize[smarray[i].col]++;
rowstart[0] = 0;
for(i = 1; i < cols; i++)
rowstart[i] = rowstart[i-1] + rowsize[i-1];
for(i = 0; i < terms; i++)
{
int j = rowstart[smarray[i].col];
b.smarray[j].row = smarray[i].col ;
b.smarray[j].col = smarray[i].row ;
b.smarray[j].valve = smarray[i].valve ;
rowstart [smarray[i].col]++;
}
}
delete[] rowsize; delete[]rowstart;
return b;
}
//===================
void main()
{
int r,c;
fin>>r>>c;
SparseMatrix sm(r,c);
sm.set();
cout<<"原矩阵为:"<<endl;
sm.print();
cout<<"转置结果为:"<<endl;
sm.fasttranspose().print();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -