// Graph.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "stdlib.h"#include "conio.h"#define VERTEX_MAX 26#define MAXVALUE 32767typedef struct { char Vertex[VERTEX_MAX]; //保存顶点信息 int Edges[VERTEX_MAX][VERTEX_MAX]; //保存边的权 int isTrav[VERTEX_MAX]; //遍历标志 int VertexNum; //顶点数量 int EdgeNum; //边数量 int GraphType; //图的类型}MatrixGraph;void CreateMartixGraph(MatrixGraph * G) { int i, j, k, weight; char start, end; printf("输入各顶点的信息\n"); for (i = 0; i < G->VertexNum; i++) { getchar(); //几个顶点 printf("第%d个顶点", i + 1); scanf("%c", &(G->Vertex[i])); //保存到顶点的数组中 } printf("输入构成各边的两个顶点及权值(用逗号分隔)"); for (k = 0; k < G->EdgeNum; k++) { getchar(); //暂停输入 printf("第%d条边",k+1); scanf("%c,%c,%d",&start,&end,&weight); //起始,结束,权重 for (i = 0; start != G->Vertex[i]; i++); for (j = 0; end != G->Vertex[j]; j++); G->Edges[i][j] = weight; //将权重存到二维数组中保存 if (G->GraphType == 0) { G->Edges[j][i] = weight; } }}void OutMartix(MatrixGraph *G) { int i, j; for (j = 0; j < G->VertexNum; j++) printf("\t%c",G->Vertex[j]); printf("\n"); for (i = 0; i < G->VertexNum; i++) { printf("%c", G->Vertex[i]); for (j = 0; j < G->VertexNum; j++) { if (G->Edges[i][j] == MAXVALUE) printf("\t@"); else printf("\t%d",G->Edges[i][j]); } printf("\n"); }}int main(){ MatrixGraph G; int i, j; printf("输入生成的图片的类型(0:无向图,1,有向图)"); scanf("%d",&G.GraphType); printf("请输入图的顶点和边数量"); scanf("%d,%d", &G.VertexNum, &G.EdgeNum); for (i = 0; i < G.VertexNum; i++) for (j = 0; j < G.VertexNum; j++) G.Edges[i][j] = MAXVALUE; CreateMartixGraph(&G); printf("邻接的数据如下\n"); OutMartix(&G); getch(); return 0;}