實訓C++語言設計——疏鬆陣列SparseMatrix

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.