完善下列代码 #include<iostream> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MVNum 100 //最大顶点数 using namespace std; typedef struct {//图的邻接矩阵存储表示 int vexs[MVNum]; //顶点表 int arcs[MVNum][MVNum]; //邻接矩阵 int vexnum,arcnum; //图的当前点数和边数 }AMGragh; int CreateUDN(AMGragh &G,int vexnum,int arcnum) {//采用邻接矩阵表示法,创建无向网G } int InsertVex(AMGragh &G) {//在以邻接矩阵形式存储的无向图G上插入顶点 } int OutputUDN(AMGragh G) {//输出图G },使其功能与#include <iostream> #define MVNum 100 using namespace std; typedef struct{ int vexs[MVNum]; int arcs[MVNum][MVNum]; int vexnum,arcnum; }AMGraph; void Create_V(AMGraph &G,int name){ int pos=++G.vexnum; G.vexs[pos-1]=name; for(int i=1;i<=pos;i++){ G.arcs[i-1][pos-1]=0; G.arcs[pos-1][i-1]=0; } } void Create_Arc(AMGraph &G,int h,int k){ G.arcs[h-1][k-1]=G.arcs[k-1][h-1]=1; } void Out_Graph(AMGraph G){ cout<<"0 "; for(int i=1;i<G.vexnum;i++) cout<<G.vexs[i-1]<<" "; cout<<G.vexs[G.vexnum-1]<<endl; for(int i=1;i<=G.vexnum;i++){ cout<<G.vexs[i-1]<<" "; for(int j=1;j<G.vexnum;j++){ cout<<G.arcs[i-1][j-1]<<" "; } cout<<G.arcs[i-1][G.vexnum-1]<<endl; } } void Calculate(int m,int n){ AMGraph G; G.vexnum=G.arcnum=0; for(int i=1;i<=m;i++) Create_V(G,i); for(int i=1;i<=n;i++){ int h,k; cin>>h>>k; Create_Arc(G,h,k); } int new_point; cin>>new_point; Create_V(G,new_point); Out_Graph(G); } int main(){ int m,n; while(cin>>m>>n&&m!=0&&n!=0){ Calculate(m,n); } return 0; }相同
时间: 2024-01-05 14:02:36 浏览: 103
关于预处理指令的介绍
完善代码如下:
#include<iostream>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MVNum 100 //最大顶点数
using namespace std;
typedef struct {//图的邻接矩阵存储表示
int vexs[MVNum]; //顶点表
int arcs[MVNum][MVNum]; //邻接矩阵
int vexnum,arcnum; //图的当前点数和边数
}AMGragh;
int CreateUDN(AMGragh &G,int vexnum,int arcnum) {//采用邻接矩阵表示法,创建无向网G
G.vexnum = vexnum;
G.arcnum = arcnum;
for(int i=0;i<G.vexnum;i++){
cout<<"请输入第"<<i+1<<"个顶点的值:";
cin>>G.vexs[i];
}
for(int i=0;i<G.vexnum;i++){
for(int j=0;j<G.vexnum;j++){
G.arcs[i][j]=0;
}
}
for(int k=0;k<G.arcnum;k++){
int i,j,w;
cout<<"请输入第"<<k+1<<"条边依附的顶点及其权值(用空格隔开):";
cin>>i>>j>>w;
G.arcs[i-1][j-1]=w;
G.arcs[j-1][i-1]=w;
}
return OK;
}
int InsertVex(AMGragh &G) {//在以邻接矩阵形式存储的无向图G上插入顶点
if(G.vexnum==MVNum){
return ERROR;
}
cout<<"请输入需要插入的顶点值:";
int v;
cin>>v;
G.vexs[G.vexnum]=v;
G.vexnum++;
return OK;
}
int OutputUDN(AMGragh G) {//输出图G
cout<<"图的顶点集为:";
for(int i=0;i<G.vexnum;i++){
cout<<G.vexs[i]<<" ";
}
cout<<endl<<"图的邻接矩阵为:"<<endl;
for(int i=0;i<G.vexnum;i++){
for(int j=0;j<G.vexnum;j++){
cout<<G.arcs[i][j]<<" ";
}
cout<<endl;
}
return OK;
}
int main(){
AMGragh G;
int vexnum,arcnum;
cout<<"请输入顶点数和边数(用空格隔开):";
cin>>vexnum>>arcnum;
CreateUDN(G,vexnum,arcnum);
OutputUDN(G);
InsertVex(G);
OutputUDN(G);
return 0;
}
阅读全文