首页 新闻 会员 周边 捐助

关于C++用邻接矩阵创建无向网的问题,为什么以下程序运行不了?????

0
[待解决问题]

#include <iostream>
using namespace std;
#define Max 3200
#define MaxNum 10
struct Graph
{
char *Vex = new char[vexnum];//顶点数组
int Arc[MaxNum][MaxNum];//边
int vexnum;//顶点数
int arcnum;//边数
};
void CreateUDN(Graph *&G)//邻接矩阵创建无向网G
{
cout << "请输入总顶点数,总边数:";
cin >> G->vexnum;
cin >> G->arcnum;
cout << "依次输入顶点的信息(字母):";
for (int i = 0; i < G->vexnum; i++)
cin >> G->Vex[i];
for (int i = 0; i < MaxNum; i++)//初始化邻接矩阵,边的权值均为Max
for (int j = 0; j < MaxNum; j++)
G->Arc[i][j] = Max;
for (int k = 0; k < G->arcnum; k++) //构造邻接矩阵
{
int v1, v2;
int w;
cout << "请依次输入每条边依附的两个顶点及权值:";
cin >> v1 >> v2 >> w;
G->Arc[v1][v2] = w; //边v1到v2的权值为w
G->Arc[v2][v1] = G->Arc[v1][v2];//v1到v2的对称边v2到v1的权值为w
}
}
void main()
{
Graph *G;
CreateUDN(G);
cout << endl;
}

创新思的主页 创新思 | 菜鸟二级 | 园豆:208
提问于:2017-05-06 16:47
< >
分享
所有回答(1)
0

#include <iostream>
using namespace std;
#define Max 3200
#define MaxNum 10

struct Graph
{
char *Vex ; // = new char[vexnum];//顶点数组
int Arc[MaxNum][MaxNum];//边
int vexnum;//顶点数
int arcnum;//边数

bool AllocVex(int nVexnum)
{
Vex = new char[nVexnum] ;
if (Vex == NULL) return false ;
return true ;
}

Graph()
{
Vex = NULL ;
}

~Graph()
{
if (Vex != NULL) delete [] Vex ;
}
};
void CreateUDN(Graph *&G)//邻接矩阵创建无向网G
{
cout << "请输入总顶点数,总边数:";
cin >> G->vexnum;
cin >> G->arcnum;

G->AllocVex(G->vexnum) ;

cout << "依次输入顶点的信息(字母):";
for (int i = 0; i < G->vexnum; i++)
cin >> G->Vex[i];
for (int i = 0; i < MaxNum; i++)//初始化邻接矩阵,边的权值均为Max
for (int j = 0; j < MaxNum; j++)
G->Arc[i][j] = Max;
for (int k = 0; k < G->arcnum; k++) //构造邻接矩阵
{
int v1, v2;
int w;
cout << "请依次输入每条边依附的两个顶点及权值:";
cin >> v1 >> v2 >> w;
G->Arc[v1][v2] = w; //边v1到v2的权值为w
G->Arc[v2][v1] = G->Arc[v1][v2];//v1到v2的对称边v2到v1的权值为w
}
}
void main()
{
Graph *G = new Graph() ;
CreateUDN(G);
cout << endl;

delete G ;
}

mxrmber | 园豆:404 (菜鸟二级) | 2017-05-08 18:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册