平台: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;
}