65,208
社区成员
发帖
与我相关
我的任务
分享#ifndef TRIPLELIST_H
#define TRIPLELIST_H
template <class Type>
class TripleList;
template <class Type>
class Triple
{
friend class TripleList<Type>;
private:
int Row, Col;
Type Value;
public:
Triple() :Row(0), Col(0), Value(0){}
Triple(int R, int C, Type V) :Row(R), Col(C), Value(V){}
};
template <class Type>
class TripleList
{
public:
TripleList(int MaxSize = 100);
~TripleList(){ delete[]Array; }
void Insert();
int CompareTriple(const Triple<Type> &a, const Triple<Type> &b);
friend TripleList<Type> operator+(const TripleList<Type> &a, const TripleList<Type> &b);
TripleList<Type> *Transpose();
TripleList<Type> *FastTranspose();
void Display();
private:
int Rows, Cols, Terms;
Triple<Type> *Array;
int ArraySize;
};
template <class Type>
TripleList<Type>::TripleList(int Maxsize)
{
if (Maxsize < 1)
{
cout << "初始化错";
}
ArraySize = Maxsize;
Array = new Triple<Type>[Maxsize];
Terms = Rows = Cols = 0;
}
template <class Type>
void TripleList<Type>::Insert()
{
int R, C;
Type V;
cin >> R >> C >> V;
Triple<Type> p = Triple<Type>(R, C, V);
if (R>Rows)
Rows = R;
if (C>Cols)
Cols = C;
if (Terms == 0)
{
Array[Terms++] = p;
}
else
{
int pos = Terms;
for (int i = 0; i < Terms; ++i)
{
if (p.Row < Array[i].Row)
{
if (i<pos)
pos = i;
}
else if (p.Row == Array[i].Row)
{
if (p.Col < Array[i].Col)
if (i<pos)
pos = i;
}
}
if (pos < Terms)
{
for (int i = Terms - 1; i >= pos; --i)
Array[i + 1] = Array[i];
Array[pos] = p;
++Terms;
}
else
Array[Terms++] = p;
}
}
template <class Type>
int TripleList<Type>::CompareTriple(const Triple<Type> &a, const Triple<Type> &b)
{
if (a.Row == b.Row && a.Col == b.Col)
return 0;
else if (a.Row < b.Row || (a.Row == b.Row && a.Col < b.Col))
return -1;
else
return 1;
}
template <class Type>
TripleList<Type> operator+(const TripleList<Type> &a, const TripleList<Type> &b)
{
if (a.Rows != b.Rows || a.Cols != b.Cols)
throw domain_error("非同型矩阵不得相加");
TripleList<Type> c(a.Terms + b.Terms);
c.Rows = a.Rows;
c.Cols = a.Cols;
int i = 0, j = 0, k = 0;
while (i < a.Terms&&j < b.Terms)
{
switch (CompareTriple(a.Array[i], b.Array[j]))
{
case -1:
c.Array[k++] = a.Array[i++];
break;
case 1:
c.Array[k++] = b.Array[j++];
break;
default:
c.Array[k] = a.Array[i];
c.Array[k].Value += b.Array[j].Value;
if (c.Array[k].Value != 0)
++k;
++i;
++j;
break;
}
}
while (i<a.Terms)
c.Array[k++] = a.Array[i++];
while (j<b.Terms)
c.Array[k++] = b.Array[j++];
c.Terms = k;
return c;
}
template <class Type>
TripleList<Type> *TripleList<Type>::Transpose()
{
TripleList<Type> *b = new TripleList<Type>(ArraySize);
b->Rows = Cols;
b->Cols = Rows;
b->Terms = Terms;
if (Terms > 0)
{
int current = 0;
for (int col = 1; col <= Cols; ++col)
for (int p = 0; p < Terms; ++p)
if (Array[p].Col == col)
{
b->Array[current].Row = Array[p].Col;
b->Array[current].Col = Array[p].Row;
b->Array[current].Value = Array[p].Value;
++current;
}
}
return b;
}
template <class Type>
TripleList<Type> *TripleList<Type>::FastTranspose()
{
TripleList<Type> *b = new TripleList<Type>(ArraySize);
int *num = new int[Cols];
int *cpot = new int[Cols];
b->Rows = Cols;
b->Cols = Rows;
b->Terms = Terms;
int i;
if (Terms > 0)
{
for (i = 0; i < Cols; ++i)
num[i] = 0;
for (i = 0; i < Terms; ++i)
++num[Array[i].Col - 1];
cpot[0] = 0;
for (i = 1; i < Cols; ++i)
cpot[i] = cpot[i - 1] + num[i - 1];
for (int i = 0; i < Terms; ++i)
{
int current = cpot[Array[i].Col - 1];
b->Array[current].Row = Array[i].Col;
b->Array[current].Col = Array[i].Row;
b->Array[current].Value = Array[i].Value;
++cpot[Array[i].Col - 1];
}
}
delete[]num;
delete[]cpot;
return b;
}
template <class Type>
void TripleList<Type>::Display()
{
int p = 0;
for (int i = 1; i <= Rows; ++i)
{
for (int j = 1; j <= Cols; ++j)
{
if (i == Array[p].Row && j == Array[p].Col)
cout << Array[p++].Value << "\t";
else
cout << 0 << "\t";
}
cout << endl;
}
}
#endif#include <iostream>
#include "TripleList.h"
using namespace std;
int main()
{
int n;
cout << "建立矩阵A,请输入您要插入的非零元个数:";
cin >> n;
TripleList<int> A(n);
cout << "请按行数,列数,值的格式插入数据:"<<endl;
for (int i = 0; i < n; ++i)
{
A.Insert();
}
cout << "您输入的矩阵为:" << endl;
A.Display();
int m;
cout << "建立矩阵B,请输入您要插入的非零元个数:";
cin >> m;
TripleList<int> B(m);
cout << "请按行数,列数,值的格式插入数据:" << endl;
for (int i = 0; i < m; ++i)
{
B.Insert();
}
cout << "您输入的矩阵为:" << endl;
B.Display();
TripleList<int> C = A + B;
cout << "转置后的矩阵为:" << endl;
C.FastTranspose()->Display();
system("pause");
return 0;
}