[C++] 纯文本查看 复制代码 #include<iostream>
using namespace std;
#define MaxVnum 100
bool visited[MaxVnum];
typedef char VexType;
typedef int EdgeType;
typedef struct{
Vextype Vex[MaxVnum];
EdgeType Edge[MaxVnum][MaxVnum];
int vexnum,edgenum;
}AMGragh;
int locatevex(AMGragh G,VexType x) {
for (int i=0;i<G.vexnum;i++){
if (x==G.Vex[i])
return i;
return -1;
}
}
void CreateAMGragh(AMGragh& G){
int i,j;
VexType u,v;
cout<<"请输入定点数"<<endl;
cin>>G.vexnum;
cout<<"请输入边数"<<endl;
cin>>G.edgenum;
cout<<"请输入定点信息"<<endl;
for (int i=0;i<G.vexnum;i++){
cin>>G.Vex[i];
}
for (int i=0;i<G.vexnum;i++){
for (int j=0;j<G.vexnum;j++){
G.Edge[i][j]=0;
}
}
cout<<"请输入每条边依附的两个顶点"<<endl;
while(G.edgenum--){
cin>>u>>v;
i=locatevex(G,u);
j=locatevex(G,v);
if (i!=-1&&j!=1){
G.Edge[i][j]==G.Edge[j][i]=1;
}else{
cout<<"输入顶点信息错!请重新输入"<<endl;
G,edgenum++;
}
}
}
void print(AMGragh &G){
cout<<"图的邻接矩阵为"<<endl;
for (int i=0;j<G.vexnum;i++){
for (int j=0;j<G.vexnum;j++){
cout<<G.Edge[i][j]<<"\t";
cout<<endl;
}
}
}
int main (){
int v;
VexType c;
AMGragh G;
CreateAMGragh(G);
print(G)
} |