惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 大牛

第二次答疑 11.25晚C语言答疑 C语言:编程练习参考程序 C语言:循环作业参考程序 数据结构: 二叉树的建立与遍历源代码( 用c++ STL 实现) 题目: 从键盘输入若干个正整数, 按从小到大的顺序输出. 输入负数表示输入结束. 用链表实现. 微软的面试题及答案-超变态但是很经典[转] 数据结构-稀疏矩阵和树的程序 一道数据结构&算法题 部分面试题 算法题 辩论题 计算机语言兴趣小组水平测试参考答案 C语言活动小组的练习题 有关C语言的随机函数的解答 有关 alter tablespace begin backup 学生问的一道C语言题目 一篇有关教育的文章 答学生问,并不专业地
数据结构:矩阵程序C++实现
大牛 · 2009-06-12 · via 博客园 - 大牛

#include"SMatrix.h"
#include
"convert.h"
#include
<iostream>
#include
<fstream>
#include
<string>
#include
<vector>
#include
<typeinfo>
using namespace std;
template 
<class Type>
void Convert(const string &s, Type a[])
{
    
string separate = ",| ";
    vector
<Type> vec;
    
int pos = 0;
    
int pos2 = 0;
    
while(string::npos != pos)
    {
        pos 
= s.find_first_of(separate, pos2);
        
if(pos == s.find_last_of(separate))
        {
            
if(strcmp(typeid(Type).name(), "int"== 0)
            {
                vec.push_back(atoi(s.substr(pos2, pos 
- pos2).c_str()));
                vec.push_back(atoi(s.substr(pos 
+ 1).c_str()));
            }
            
break;
        }
if(0 < pos - pos2)
        {
            
if(strcmp(typeid(Type).name(), "int"== 0)
                vec.push_back(atoi(s.substr(pos2, pos 
- pos2).c_str()));
        }

        pos2 

= pos + 1;
    }
for(int i = 0; i < vec.size(); i++)
        a[i] 
= vec[i];
    
}
void CreateSMatrix(TSMatrix &m)
{
    ifstream f(
"smatrix.txt");
    
char *buf = new char[100];
    
string s;
    f.getline(buf,
100);
    s 
= buf;
    
int a[3];
    Convert(s, a);
    m.mu 
= a[0];
    m.nu 
= a[1];
    m.tu 
= a[2];
    
int i = 1;
    
while(f.getline(buf,100))
    {
        s 
= buf;
        Convert(s, a);
        m.data[i].i 
= a[0];
        m.data[i].j 
= a[1];
        m.data[i].e 
= a[2];
        i
++;
    }

    f.close();
    delete[] buf;
}

void PrintSMatrix(TSMatrix &m)
{
    
int i, j;
    
int pos = 1;
    
int currentPos;
    
for(i = 1; i <= m.tu; i++)
    {
        currentPos 
= (m.data[i].i - 1* m.nu + m.data[i].j;
        
for(j = pos; j < currentPos; j++)
        {
            cout
<<setw(5)<<0;
            
if(j % m.nu== 0)
                cout
<<endl;
        }
        cout
<<setw(5)<<m.data[i].e;
        
if(currentPos % m.nu == 0)
            cout
<<endl;
        pos 
= currentPos + 1;
    }
for(j = pos; j <= m.mu * m.nu; j++)
    {
        cout
<<setw(5)<<0;
        
if(j % m.nu== 0)
            cout
<<endl;
    }
}
int FastTransposeSMatrix(TSMatrix M, TSMatrix &T)
{
    T.mu 
= M.nu;
    T.nu 
= M.mu;
    T.tu 
= M.tu;if(T.tu == 0)
        
return 0;int col;
    
int t;
    
int *num = new int[M.nu + 1];
    
int *cpot = new int[M.nu + 1];for(col = 1; col <= M.nu; col++)
        num[col] 
= 0;for(t = 1; t <= M.tu; t++)
        num[M.data[t].j]
++;

    cpot[

1= 1;
    
for(col = 2; col <= M.nu; col++)
        cpot[col] 
= cpot[col -1 ] + num[col -1];int p, q;
    
for(p = 1; p <= M.tu; p++)
    {
        col 
= M.data[p].j;
        q 
= cpot[col];
        T.data[q].i 
= M.data[p].j;
        T.data[q].j 
= M.data[p].i;
        T.data[q].e 
= M.data[p].e;
        cpot[col]
++;
    }

    delete[] num;
    delete[] cpot;

return 1;
}
int TransposeSMatrix(TSMatrix M, TSMatrix &T)
{
    T.mu 
= M.nu;
    T.nu 
= M.mu;
    T.tu 
= M.tu;if(T.tu == 0)
        
return 0;int q = 1;
    
int col;
    
int p;
    
for(col = 1; col <= M.nu; col++)
    {
        
for(p = 1; p <= M.tu; p++)
        {
            
if(M.data[p].j == col)
            {
                T.data[q].i 
= M.data[p].j;
                T.data[q].j 
= M.data[p].i;
                T.data[q].e 
= M.data[p].e;
                q
++;
            }
        }
    }
    
return 1;
}
void main()
{
    TSMatrix m, t;
    CreateSMatrix(m);
    PrintSMatrix(m);
    printf(
"\n");
    FastTransposeSMatrix(m, t);
    PrintSMatrix(t);

}

程序从文本文件中读取三元组数据.例如文本文件smatrix.txt中的内容是:

6,7,8
1,2,12
1,3,9
3,1,-3
3,6,14
4,3,24
5,2,18
6,1,15
6,4,-7