#include <iostream>
#include <stdio.h>
using namespace std;
#define MAX_VERTEX_NUM 100
typedef int AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef char v_type;
typedef struct {
v_type vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
}AdjGraph;
bool DirectedorNot() {
char d;
cout << "Is your graph directed or not?(Y/N):" ;
A:
cin >> d;
if (d=='Y') return true;
else if (d=='N') return false;
else {
cout << "Please input correctly!" << endl ;
goto A;
}
}
int LocateVex(AdjGraph G, v_type u) {
int i;
for (i=0; i<G.vexnum; i++) {
if (u==G.vexs[i]) return i;
}
if (i==G.vexnum) {
cout << "Error!" << endl;
exit(1);
}
return 0;
}
void CreatMGraph(AdjGraph &G) {
int i,j,k,w;
v_type v1,v2;
bool flag;
flag = DirectedorNot();
cout << "Input vexnum & arcnum!" ;
cin >> G.vexnum >> G.arcnum ;
cout << "Input Vertices!" ;
for (i=0; i<G.vexnum; i++) {
cin >> G.vexs[i] ;
}
for (i=0; i<G.vexnum; i++) {
for (j=0; j<G.vexnum; j++) {
G.arcs[i][j] = 0;
}
}
for (k=0; k<G.arcnum; k++) {
cout << "Input Arcs(v1, v2 & w):" ;
cin >> v1 >> v2 >> w ;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
G.arcs[i][j] = w ;
if (!flag)
G.arcs[j][i] = w ;
}
}
void PrintGraph(AdjGraph G) {
int i,j;
printf("Output Vertices:");
printf("%s",G.vexs); printf("/n");
printf("Output AdjMatrix:/n");
for (i=0;i<G.vexnum;i++)
{ for (j=0;j<G.vexnum;j++)
printf("%4d",G.arcs[i][j]);
printf("/n");
}
}
int main() {
AdjGraph G;
CreatMGraph(G);
PrintGraph(G);
system ("pause");
return 0;
}