#include"SMatrix.h"
#include"convert.h"
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<typeinfo>
using namespace std;
template <class Type>
void Convert(const string &s, Type a[])
{
string separate = ",| ";
vector<Type> vec;
int pos = 0;
int pos2 = 0;
while(string::npos != pos)
{
pos = s.find_first_of(separate, pos2);
if(pos == s.find_last_of(separate))
{
if(strcmp(typeid(Type).name(), "int") == 0)
{
vec.push_back(atoi(s.substr(pos2, pos - pos2).c_str()));
vec.push_back(atoi(s.substr(pos + 1).c_str()));
}
break;
}
if(0 < pos - pos2)
{
if(strcmp(typeid(Type).name(), "int") == 0)
vec.push_back(atoi(s.substr(pos2, pos - pos2).c_str()));
}
pos2 = pos + 1;
}
for(int i = 0; i < vec.size(); i++)
a[i] = vec[i];
}
void CreateSMatrix(TSMatrix &m)
{
ifstream f("smatrix.txt");
char *buf = new char[100];
string s;
f.getline(buf,100);
s = buf;
int a[3];
Convert(s, a);
m.mu = a[0];
m.nu = a[1];
m.tu = a[2];
int i = 1;
while(f.getline(buf,100))
{
s = buf;
Convert(s, a);
m.data[i].i = a[0];
m.data[i].j = a[1];
m.data[i].e = a[2];
i++;
}
f.close();
delete[] buf;
}
void PrintSMatrix(TSMatrix &m)
{
int i, j;
int pos = 1;
int currentPos;
for(i = 1; i <= m.tu; i++)
{
currentPos = (m.data[i].i - 1) * m.nu + m.data[i].j;
for(j = pos; j < currentPos; j++)
{
cout<<setw(5)<<0;
if(j % m.nu== 0)
cout<<endl;
}
cout<<setw(5)<<m.data[i].e;
if(currentPos % m.nu == 0)
cout<<endl;
pos = currentPos + 1;
}
for(j = pos; j <= m.mu * m.nu; j++)
{
cout<<setw(5)<<0;
if(j % m.nu== 0)
cout<<endl;
}
}
int FastTransposeSMatrix(TSMatrix M, TSMatrix &T)
{
T.mu = M.nu;
T.nu = M.mu;
T.tu = M.tu;
if(T.tu == 0)
return 0;
int col;
int t;
int *num = new int[M.nu + 1];
int *cpot = new int[M.nu + 1];
for(col = 1; col <= M.nu; col++)
num[col] = 0;
for(t = 1; t <= M.tu; t++)
num[M.data[t].j]++;
cpot[1] = 1;
for(col = 2; col <= M.nu; col++)
cpot[col] = cpot[col -1 ] + num[col -1];
int p, q;
for(p = 1; p <= M.tu; p++)
{
col = M.data[p].j;
q = cpot[col];
T.data[q].i = M.data[p].j;
T.data[q].j = M.data[p].i;
T.data[q].e = M.data[p].e;
cpot[col]++;
}
delete[] num;
delete[] cpot;
return 1;
}
int TransposeSMatrix(TSMatrix M, TSMatrix &T)
{
T.mu = M.nu;
T.nu = M.mu;
T.tu = M.tu;
if(T.tu == 0)
return 0;
int q = 1;
int col;
int p;
for(col = 1; col <= M.nu; col++)
{
for(p = 1; p <= M.tu; p++)
{
if(M.data[p].j == col)
{
T.data[q].i = M.data[p].j;
T.data[q].j = M.data[p].i;
T.data[q].e = M.data[p].e;
q++;
}
}
}
return 1;
}
void main()
{
TSMatrix m, t;
CreateSMatrix(m);
PrintSMatrix(m);
printf("\n");
FastTransposeSMatrix(m, t);
PrintSMatrix(t);
}
#include"convert.h"
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<typeinfo>
using namespace std;
template <class Type>
void Convert(const string &s, Type a[])
{
string separate = ",| ";
vector<Type> vec;
int pos = 0;
int pos2 = 0;
while(string::npos != pos)
{
pos = s.find_first_of(separate, pos2);
if(pos == s.find_last_of(separate))
{
if(strcmp(typeid(Type).name(), "int") == 0)
{
vec.push_back(atoi(s.substr(pos2, pos - pos2).c_str()));
vec.push_back(atoi(s.substr(pos + 1).c_str()));
}
break;
}
if(0 < pos - pos2)
{
if(strcmp(typeid(Type).name(), "int") == 0)
vec.push_back(atoi(s.substr(pos2, pos - pos2).c_str()));
}
pos2 = pos + 1;
}
for(int i = 0; i < vec.size(); i++)
a[i] = vec[i];
}
void CreateSMatrix(TSMatrix &m)
{
ifstream f("smatrix.txt");
char *buf = new char[100];
string s;
f.getline(buf,100);
s = buf;
int a[3];
Convert(s, a);
m.mu = a[0];
m.nu = a[1];
m.tu = a[2];
int i = 1;
while(f.getline(buf,100))
{
s = buf;
Convert(s, a);
m.data[i].i = a[0];
m.data[i].j = a[1];
m.data[i].e = a[2];
i++;
}
f.close();
delete[] buf;
}
void PrintSMatrix(TSMatrix &m)
{
int i, j;
int pos = 1;
int currentPos;
for(i = 1; i <= m.tu; i++)
{
currentPos = (m.data[i].i - 1) * m.nu + m.data[i].j;
for(j = pos; j < currentPos; j++)
{
cout<<setw(5)<<0;
if(j % m.nu== 0)
cout<<endl;
}
cout<<setw(5)<<m.data[i].e;
if(currentPos % m.nu == 0)
cout<<endl;
pos = currentPos + 1;
}
for(j = pos; j <= m.mu * m.nu; j++)
{
cout<<setw(5)<<0;
if(j % m.nu== 0)
cout<<endl;
}
}
int FastTransposeSMatrix(TSMatrix M, TSMatrix &T)
{
T.mu = M.nu;
T.nu = M.mu;
T.tu = M.tu;
if(T.tu == 0)
return 0;
int col;
int t;
int *num = new int[M.nu + 1];
int *cpot = new int[M.nu + 1];
for(col = 1; col <= M.nu; col++)
num[col] = 0;
for(t = 1; t <= M.tu; t++)
num[M.data[t].j]++;
cpot[1] = 1;
for(col = 2; col <= M.nu; col++)
cpot[col] = cpot[col -1 ] + num[col -1];
int p, q;
for(p = 1; p <= M.tu; p++)
{
col = M.data[p].j;
q = cpot[col];
T.data[q].i = M.data[p].j;
T.data[q].j = M.data[p].i;
T.data[q].e = M.data[p].e;
cpot[col]++;
}
delete[] num;
delete[] cpot;
return 1;
}
int TransposeSMatrix(TSMatrix M, TSMatrix &T)
{
T.mu = M.nu;
T.nu = M.mu;
T.tu = M.tu;
if(T.tu == 0)
return 0;
int q = 1;
int col;
int p;
for(col = 1; col <= M.nu; col++)
{
for(p = 1; p <= M.tu; p++)
{
if(M.data[p].j == col)
{
T.data[q].i = M.data[p].j;
T.data[q].j = M.data[p].i;
T.data[q].e = M.data[p].e;
q++;
}
}
}
return 1;
}
void main()
{
TSMatrix m, t;
CreateSMatrix(m);
PrintSMatrix(m);
printf("\n");
FastTransposeSMatrix(m, t);
PrintSMatrix(t);
}
程序从文本文件中读取三元组数据.例如文本文件smatrix.txt中的内容是:
6,7,8
1,2,12
1,3,9
3,1,-3
3,6,14
4,3,24
5,2,18
6,1,15
6,4,-7