//采用邻接矩阵表示法创建无向网 #include <iostream> using namespace std; #define MaInt 32767 #define MVNum 100 #define OK 1 typedef char VerTextType; typedef int ArcType; typedef struct { VerTextType Vexs[MVNum]; ArcType arcs[MVNum][MVNum]; int vexnum, arcnum; }AMGrach; int LocateVex(AMGrach G, VerTextType v) { for (int i = 0;i < G.vexnum;i++) { if (G.Vexs[i] == v) return i; } return -1; } int CreateUDN(AMGrach& G) { int i, j, k; cout << "请输入总顶点数,总边数,以空格隔开:"; cin >> G.vexnum >> G.arcnum; cout << endl; cout << "输入点的名称,如a" << endl; for (i = 0;i < G.vexnum;i++) { cout << "input the " << i << " name"; cin >> G.Vexs[i]; } cout << endl; for (i = 0;i < G.vexnum;i++) for (j = 0;j < G.vexnum;++j) G.arcs[i][j] = MaInt; cout << "输入边依附的顶点及权值,如 a b 5" << endl; for (k = 0;k < G.arcnum;++k) { VerTextType v1, v2; ArcType w; cout << "input the" << (k + 1) << " side of weigth"; cin >> v1 >> v2 >> w; i = LocateVex(G, v1); j = LocateVex(G, v2); G.arcs[i][j] = w; G.arcs[j][i] = G.arcs[i][j]; } return 0; } int main() { cout << "采用邻接矩阵表示法创建无向网"; AMGrach G; int i, j; CreateUDN(G); cout << endl; for (i = 0;i < G.vexnum;i++) { for (j = 0;j < G.vexnum;++j) { if (j != G.vexnum - 1) { if (G.arcs[i][j] != MaInt) { cout << G.arcs[i][j] << "\t"; } else { cout << "~" << "\t"; } } else { if (G.arcs[i][j] != MaInt) cout << G.arcs[i][j] << endl; else cout << "~" << endl; } } } cout << endl; return 0; }
来源:https://www.cnblogs.com/ygjzs/p/11877585.html