边集数组
来源:互联网 发布:武汉java达内传智播客 编辑:程序博客网 时间:2024/05/06 16:41
#include <iostream>
#include <strstream>
#include <string>
using namespace std;
//边集数组
//template<class T>
class CEdgeSet
{
public:
CEdgeSet():m_fromVex(0), m_endVex(0), m_weight(0), m_edgeNum(0), m_edgeSet(NULL) { }
~CEdgeSet() {
if( m_edgeSet != NULL) {
delete[] m_edgeSet;
}
}
void CreateEdgeSet();
void PrintEdgeSet();
CEdgeSet *GetEdgeSet(); //得到边集数组
private:
int m_fromVex; //边的起点
int m_endVex; //边的终点
int m_weight; //权
int m_edgeNum; //图中的总边数
CEdgeSet *m_edgeSet; //边集数组
};
//
void CEdgeSet::CreateEdgeSet() {
cout<<"输入图的总边数n:";
cin>>m_edgeNum;
//分配内存
m_edgeSet = new CEdgeSet[m_edgeNum];
//读取边集
cout<<"输入边集(如:{<1,2>}\\{(1,2)},若为带权图则为{<1,2>3}\\{(1,2)3}:"<<endl;
string str;
cin>>str;
//将 string 转换成 char*
char *cstr = new char[str.size()+1]; //最后加上'\0'
int i=0;
for(i=0; i != str.size(); ++i)
cstr[i] = str.at(i);
cstr[i]='\0';
cout<<cstr<<endl;
istrstream strin(cstr);
char cTmp, cEdge1, cEdge2, cEdge3;//cEdge1存 < ,cEdge2存 , ,cEdge3存 >
int iBgn, iEnd; //边的起点、终点
strin>>cTmp;
//m_edgeSet[indx];
int indx =-1;
while (true) {
char cPeek;
cPeek = strin.peek();
//边集读取完成退出
if( '}' == cTmp || '}' == cPeek) {
break;
}
//自增
++indx;
//读取边集<a,b> 或 (a,b)
strin>>cEdge1>>iBgn>>cEdge2>>iEnd>>cEdge3;
cout<<cEdge1<<iBgn<<cEdge2<<iEnd<<cEdge3<<endl;
//存储起点和终点
m_edgeSet[indx].m_fromVex = iBgn;
m_edgeSet[indx].m_endVex = iEnd;
//peek下一个字符,看是否为带权图
cTmp = strin.peek();
cout<<cTmp<<endl;
//此图是否为 带权图
if ( cTmp>='0' && cTmp<='9') {
strin>>m_edgeSet[indx].m_weight;
} else {
m_edgeSet[indx].m_weight = 0;
}
//读取下一个字符
strin>>cTmp;
}
}
//
void CEdgeSet::PrintEdgeSet() {
cout<<"打印边集数组如下:"<<endl<<endl;
//打印边序号
cout<<"边序号";
for(int i=0; i != m_edgeNum; ++i) {
cout<<'\t'<<i;
}
cout<<endl;
//打印起点
cout<<"边起点";
for(int i=0; i != m_edgeNum; ++i) {
cout<<'\t'<<m_edgeSet[i].m_fromVex;
}
cout<<endl;
//打印终点
cout<<"边终点";
for(int i=0; i != m_edgeNum; ++i) {
cout<<'\t'<<m_edgeSet[i].m_endVex;
}
cout<<endl;
//打印权
if (m_edgeSet[0].m_weight >0) {
cout<<"边 权";
for(int i=0; i != m_edgeNum; ++i) {
cout<<'\t'<<m_edgeSet[i].m_weight;
}
}
cout<<endl;
}
//
CEdgeSet *CEdgeSet::GetEdgeSet() {
return m_edgeSet;
}
int main()
{
CEdgeSet edgeSet;
edgeSet.CreateEdgeSet();
edgeSet.PrintEdgeSet();
return 0;
}
#include <strstream>
#include <string>
using namespace std;
//边集数组
//template<class T>
class CEdgeSet
{
public:
CEdgeSet():m_fromVex(0), m_endVex(0), m_weight(0), m_edgeNum(0), m_edgeSet(NULL) { }
~CEdgeSet() {
if( m_edgeSet != NULL) {
delete[] m_edgeSet;
}
}
void CreateEdgeSet();
void PrintEdgeSet();
CEdgeSet *GetEdgeSet(); //得到边集数组
private:
int m_fromVex; //边的起点
int m_endVex; //边的终点
int m_weight; //权
int m_edgeNum; //图中的总边数
CEdgeSet *m_edgeSet; //边集数组
};
//
void CEdgeSet::CreateEdgeSet() {
cout<<"输入图的总边数n:";
cin>>m_edgeNum;
//分配内存
m_edgeSet = new CEdgeSet[m_edgeNum];
//读取边集
cout<<"输入边集(如:{<1,2>}\\{(1,2)},若为带权图则为{<1,2>3}\\{(1,2)3}:"<<endl;
string str;
cin>>str;
//将 string 转换成 char*
char *cstr = new char[str.size()+1]; //最后加上'\0'
int i=0;
for(i=0; i != str.size(); ++i)
cstr[i] = str.at(i);
cstr[i]='\0';
cout<<cstr<<endl;
istrstream strin(cstr);
char cTmp, cEdge1, cEdge2, cEdge3;//cEdge1存 < ,cEdge2存 , ,cEdge3存 >
int iBgn, iEnd; //边的起点、终点
strin>>cTmp;
//m_edgeSet[indx];
int indx =-1;
while (true) {
char cPeek;
cPeek = strin.peek();
//边集读取完成退出
if( '}' == cTmp || '}' == cPeek) {
break;
}
//自增
++indx;
//读取边集<a,b> 或 (a,b)
strin>>cEdge1>>iBgn>>cEdge2>>iEnd>>cEdge3;
cout<<cEdge1<<iBgn<<cEdge2<<iEnd<<cEdge3<<endl;
//存储起点和终点
m_edgeSet[indx].m_fromVex = iBgn;
m_edgeSet[indx].m_endVex = iEnd;
//peek下一个字符,看是否为带权图
cTmp = strin.peek();
cout<<cTmp<<endl;
//此图是否为 带权图
if ( cTmp>='0' && cTmp<='9') {
strin>>m_edgeSet[indx].m_weight;
} else {
m_edgeSet[indx].m_weight = 0;
}
//读取下一个字符
strin>>cTmp;
}
}
//
void CEdgeSet::PrintEdgeSet() {
cout<<"打印边集数组如下:"<<endl<<endl;
//打印边序号
cout<<"边序号";
for(int i=0; i != m_edgeNum; ++i) {
cout<<'\t'<<i;
}
cout<<endl;
//打印起点
cout<<"边起点";
for(int i=0; i != m_edgeNum; ++i) {
cout<<'\t'<<m_edgeSet[i].m_fromVex;
}
cout<<endl;
//打印终点
cout<<"边终点";
for(int i=0; i != m_edgeNum; ++i) {
cout<<'\t'<<m_edgeSet[i].m_endVex;
}
cout<<endl;
//打印权
if (m_edgeSet[0].m_weight >0) {
cout<<"边 权";
for(int i=0; i != m_edgeNum; ++i) {
cout<<'\t'<<m_edgeSet[i].m_weight;
}
}
cout<<endl;
}
//
CEdgeSet *CEdgeSet::GetEdgeSet() {
return m_edgeSet;
}
int main()
{
CEdgeSet edgeSet;
edgeSet.CreateEdgeSet();
edgeSet.PrintEdgeSet();
return 0;
}
- 边集数组
- 图的储存(边集数组)
- 图存储5-边集数组
- 图的存储结构-邻接多重表、边集数组
- 十字链表 、邻接多重表 、边集数组
- 【图论】【单源最短路】【dijikstra】pascal+邻接矩阵/边集数组
- 关于图的存储方法 (静态邻接表、前向星、边集数组)
- 图的存储结构(十字链表、邻接多重表、边集数组)
- 图的存储结构-邻接表-边集数组-前向星
- 大话数据结构二十二:图的存储结构之边集数组
- 7.2图的存储结构(十字链表、邻接多重表、边集数组)
- 数据结构之图的存储表示(邻接矩阵、邻接表和边集数组)
- 已知两个数组其中一个为另一个数组的子集,求补集数组
- 取相关记录集数据
- 并查集数据结构
- 人脸集数据库
- 人脸集数据库
- 人脸集数据库
- ABAP SY标签一览表
- OK6410之裸机led
- 0,1背包问题
- 记一个链接库导出函数被覆盖的问题
- 把一个整数划分成若干个质数的和
- 边集数组
- 用户接口(UI)设计的 20 条原则
- linux时钟浅析
- zoj 3705 Applications
- C语言、windows程序设计、汇编笔记(很有用哦)
- 24点问题
- 继续动态规划。
- 学习JSP的一点心得
- 整理一些网页常用的tab选项卡效果