`
bulote
  • 浏览: 1305423 次
文章分类
社区版块
存档分类
最新评论

实训C++语言设计——稀疏矩阵SparseMatrix

 
阅读更多

平台:VC++ 2005 测试通过!
.vcproj
这是使用应用程序向导生成的 VC++ 项目的主项目文件。
它包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。
StdAfx.h, StdAfx.cpp
这些文件用于生成名为 twod.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。
这些都是使用应用程序向导生成的 VC++ 文件故不列出
我只列出程序主要部分!

//ADT_SparseMatrix.h

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

//---------------抽象数据类型稀疏矩阵的定义------------------//
#define ElemMaxSize 12500;
typedef int ElemType;
typedef struct {
int i, j;
ElemType e;
} Triple; //稀疏矩阵的元素-三元组

typedef struct {
vector<Triple> elems;
int row, col, tu;
} TSMatrix; //稀疏矩阵结构体类型


//---------------------稀疏矩阵的接口函数声明-----------------------//
//构造一个稀疏矩阵
bool enterTSMatrix(TSMatrix &atsm);
//释放稀疏矩阵所占内存空间
void DestroyTSMatrix(TSMatrix &atsm);
//打印稀疏矩阵
void Print(const TSMatrix &atsm);
//对稀疏矩阵进行转置
bool TranposeTSMatrix(const TSMatrix &stsm, TSMatrix &ttsm);

//------私有的成员函数-----//
bool rowmajorCriterion(const Triple& t1, const Triple& t2);
ElemType getTriple(const TSMatrix &stsm, int ix, int jx);

//---------------------稀疏矩阵的接口函数的实现-----------------------//
bool enterTSMatrix(TSMatrix &atsm){
cout << "请输入矩阵的行数: "; cin >> atsm.row;
cout << "请输入矩阵的列数: "; cin >> atsm.col;
cout << "请输入矩阵的非零元数: "; cin >> atsm.tu;
int ix, jx; ElemType elem;
for(int i = 0; i < atsm.tu; i++){//接受atsm.tu个非零元
Triple aTriple;
cout << "请输入第" << i <<"个非零元: ";
cin >> ix >>jx >>elem;
aTriple.i = ix; aTriple.j = jx; aTriple.e = elem;
atsm.elems.push_back(aTriple);
}
vector<Triple>::iterator first = atsm.elems.begin();
vector<Triple>::iterator last = atsm.elems.end();
sort(first, last, rowmajorCriterion);
/*for(int i = 0; i < atsm.tu; i++){
cout <<"<"<< atsm.elems[i].i <<", "
<< atsm.elems[i].j <<", "
<< atsm.elems[i].e <<">";
cout <<endl;
}*/
return true;
}

void DestroyTSMatrix(TSMatrix &atsm){

}

void Print(const TSMatrix &atsm){
//能否漂亮打印??
if (!atsm.elems.empty()){
for(int r = 1; r <= atsm.row; r++){
for(int c = 1; c <= atsm.col; c++)
cout << getTriple(atsm, r, c) <<" ";
cout <<endl;
}
}
}

bool TranposeTSMatrix(const TSMatrix &stsm, TSMatrix &ttsm){
ttsm.row = stsm.col; ttsm.col = stsm.row; ttsm.tu = stsm.tu;
if(!ttsm.elems.empty()) //清空目标矩阵的三元组表
ttsm.elems.clear();
if (ttsm.tu){//如果有非零元
for (int c = 1; c <= stsm.col; c++){
for (int tx = 0; tx < stsm.tu; tx++)
if (stsm.elems[tx].j == c ){
Triple aTriple;
aTriple.i = stsm.elems[tx].j;
aTriple.j = stsm.elems[tx].i;
aTriple.e = stsm.elems[tx].e;
ttsm.elems.push_back(aTriple);
}
}//end of for
}
return true;
}

bool rowmajorCriterion(const Triple& t1, const Triple& t2){
if( (t1.i < t2.i) ) return true;
if( (t1.i == t2.i) && (t1.j < t2.j) ) return true;
return false;
}

ElemType getTriple(const TSMatrix &atsm, int ix, int jx){
for (int i = 0; i < atsm.tu; i++){
if ((atsm.elems[i].i == ix)&&(atsm.elems[i].j == jx)){
exit;
return atsm.elems[i].e;
}
}
return 0;//呵呵,有点问题,自己解决.
}

// ADT_SparseMatrix.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "SparseMatrix.h"

int _tmain(int argc, _TCHAR* argv[])
{
TSMatrix m, n;
enterTSMatrix(m);
Print(m);
TranposeTSMatrix(m,n);
cout << "m的转置矩阵是:"<<endl;
Print(n);
return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics