标签:gen isp text cpp bre break 16px define include
#include<iostream> using namespace std; #define MAX_VERTEX_NUM 20 #define VertexType int typedef enum{DG,DN,UDG,UDN}GraphKind; typedef struct { VertexType vexs[MAX_VERTEX_NUM];//顶点信息 int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//矩阵 int pointnum;//顶点数 int edgenum;//边数 GraphKind kind; }MGraph; int Locate(MGraph G,VertexType v) { for(int i = 0;i<G.pointnum;i++) { if(v == G.vexs[i]) return i; } return -1; } void CreateUDN(MGraph &G) { int i; int j; int k; int w; char flag; VertexType v1,v2; G.kind = UDN; cout<<"请输入无向网的顶点数,边数"<<endl; cin>>G.pointnum>>G.edgenum; cout<<"请输入顶点信息"<<endl; for(i = 0;i<G.pointnum;i++) { cin>>G.vexs[i]; for(j = 0;j<i;j++) { if(G.vexs[j] == G.vexs[i]) break; } if(j < i) { i--; cout<<"已经存在这样的顶点,重新输入"<<endl; continue; } } for(i = 0;i<G.pointnum;i++) { for( j = 0;j<G.pointnum;j++) { G.arcs[i][j] = 0; } } for(k = 0;k<G.edgenum;k++) { cout<<"输入相邻边的顶点及其距离"<<endl; cin>>v1>>v2>>w; i = Locate(G,v1); j = Locate(G,v2); if(G.arcs[i][j] == 0) { G.arcs[i][j] = w; G.arcs[j][i] = G.arcs[i][j]; } else { cout<<"已经存在该边,是否重新输入?Y 确认"<<endl; cin>>flag; if(flag == ‘y‘|| flag == ‘Y‘) { G.arcs[i][j] = w; G.arcs[j][i] = G.arcs[i][j]; } k--; } } } void Disp(MGraph G) { for(int i = 0 ;i<G.pointnum;i++) { for(int j = 0;j<G.pointnum;j++) { cout<<" "<<G.arcs[i][j]; } cout<<endl; } } int main() { MGraph G; CreateUDN(G); Disp(G); return 1; }
运行结果
标签:gen isp text cpp bre break 16px define include
原文地址:https://www.cnblogs.com/ygsworld/p/10023761.html