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

news/2024/7/7 2:43:29

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

 


http://www.niftyadmin.cn/n/2108696.html

相关文章

基础篇——Handler异步回调机制

写代码的四点&#xff1a; 1.明确需求。要做什么&#xff1f; 2.分析思路。要怎么做&#xff1f;&#xff08;1,2,3……&#xff09; 3.确定步骤。每一个思路要用到哪些语句、方法和对象。 4.代码实现。用具体的语言代码将思路实现出来。 学习新技术的四点&#xff1a; 1.该技…

AMP:Google 新技术能让网页瞬间加载完毕

作为一家活在 Web 世界的公司&#xff0c;Google 对提升网页性能一直是不遗余力。今天&#xff0c;为了让用户能够更快地浏览网页&#xff0c;Google 联合 8 家科技公司以及近 30 家新闻机构一起发布了一个名为移动页面加速&#xff08;Accelerated Mobile Pages&#xff09;的…

HTML5中类jQuery选择器querySelector的高级使用 document.querySelectorAll.bind(document);

基本用法 querySelector 该方法返回满足条件的单个元素。按照深度优先和先序遍历的原则使用参数提供的CSS选择器在DOM进行查找&#xff0c;返回第一个满足条件的元素。 ----> querySelector得到一个DOM var element document.querySelector(#container);//返回id为conta…

不能正常关机的处理办法

问&#xff1a;Win98不能正常关机是什么原因&#xff0c;如何处理&#xff1f;安装WinXP后不能正常关机&#xff0c;按下电源开关也不能关机而又重新启动系统&#xff0c;是什么原因&#xff1f;答&#xff1a;首先谈谈Windows系统自动关机的条件。在Win98及更高版本的系统中都…

AndroidStudio的debug功能详解

转载仅供本人存档及后续研究使用&#xff0c;请尊重原创。 转载自&#xff1a;https://blog.csdn.net/u013952370/article/details/52131007 运行debug模式 1. 进入debug - 点击图中红色圆圈圈起的左边绿色按钮&#xff0c;运行app的debug模式&#xff0c;快捷键ShiftF9 - 点…

putty使用方法,中文教程 链接地址

http://www.lupaworld.com/53411/viewspace_42657.html转载于:https://blog.51cto.com/axlrose/1293059

基础篇——泛型(Generics)

写代码的四点&#xff1a; 1.明确需求。要做什么&#xff1f; 2.分析思路。要怎么做&#xff1f;&#xff08;1,2,3……&#xff09; 3.确定步骤。每一个思路要用到哪些语句、方法和对象。 4.代码实现。用具体的语言代码将思路实现出来。 学习新技术的四点&#xff1a; 1.该技…

SOA的真相是什么?[转]

作 者&#xff1a;IT经理世界 周源 CIO心中的SOA和厂商宣传的SOA&#xff0c;好像并不是一码事儿。 为什么SOA(ServiceOrientedArchitecture&#xff0c;面向服务架构)正在大行其道&#xff1f;答案也许简单到极点。今年9月&#xff0c;BEA公司CEO庄思浩在旧金山举行的BEAWorl…