#include <iostream> using namespace std; #include <iomanip> #define OK 1 #define MaxInt 32767 #define MVNum 100 typedef int Status; typedef char VerTexType; typedef int ArcType; typedef struct { VerTexType vexs[MVNum]; ArcType arcs[MVNum][MVNum]; int vexnum, arcnum; } MGraph; struct Edge { VerTexType Head; VerTexType Tail; ArcType lowcost; } Edge[(MVNum * (MVNum - 1)) / 2]; int Vexset[MVNum]; Status LocateVex(MGraph G, VerTexType e) { int i; for (i = 0; i < G.vexnum; i++) if (G.vexs[i] == e) return i; } Status CreateUDN(MGraph &G) { int i, j, k, w; char v1, v2; cout << "输入顶点个数和边的条数:"; cin >> G.vexnum >> G.arcnum; cout << "输入各个顶点:"; 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] = MaxInt; for (k = 0; k < G.arcnum; k++) { cout << "输入一条边依附的顶点和权值:"; 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]; Edge[k].lowcost = w; Edge[k].Head = v1; Edge[k].Tail = v2; } return OK; } void DispGraph(MGraph G) { int i, j; cout << "顶点:\n"; for (i = 0; i < G.vexnum; i++) cout << G.vexs[i] << " "; cout << "\n邻接矩阵:\n"; for (i = 0; i < G.vexnum; i++) { for (j = 0; j < G.vexnum; j++) cout << setw(7) << G.arcs[i][j]; cout << endl; } } Status Min(MGraph G) { int i, index = -1, min = MaxInt; for (i = 0; i < G.vexnum; i++) { if (Edge[i].lowcost < min && Edge[i].lowcost != 0) { min = Edge[i].lowcost; index = i; } } return index; } void Sort(MGraph G) { //冒泡排序 int m = G.arcnum - 2; struct Edge t; int flag = 1; while ((m > 0) && flag == 1) { flag = 0; for (int j = 0; j <= m; j++) { if (Edge[j].lowcost > Edge[j + 1].lowcost) { flag = 1; t = Edge[j]; Edge[j] = Edge[j + 1]; Edge[j + 1] = t; }//if }//for --m; }//while }//Sort void MiniSpanTree_Kruskal(MGraph G) { int i, j; int v1, v2, vs1 Sort(G); for (i = 0; i < G.vexnum; i++) Vexset[i] = i; for (i = 0; i < G.arcnum; i++) { v1 = LocateVex(G, Edge[i].Head); v2 = LocateVex(G, Edge[i].Tail); vs1 = Vexset[v1]; vs2 = Vexset[v2]; if (vs1 != vs2) { cout << Edge[i].Head << Edge[i].Tail << endl; for (j = 0; j < G.vexnum; j++) if (Vexset[j] == vs2) Vexset[j] = vs1; } } } int main() { MGraph G; CreateUDN(G); DispGraph(G); cout << "最小生成树的边如下:" << endl; MiniSpanTree_Kruskal(G); return 0; } 函数前写注释
理解问题#include <iostream> using namespace std; #include <iomanip> #define OK 1 #define MaxInt 32767 #define MVNum 100 typedef int Status; typedef char VerTexType; typedef int ArcType; typedef struct { VerTexType vexs[MVNum]; ArcType arcs[MVNum][MVNum]; int vexnum, arcnum; } MGraph; struct Edge { VerTexType Head; VerTexType Tail; ArcType lowcost; } Edge[(MVNum * (MVNum - 1)) / 2]; int Vexset[MVNum]; Status LocateVex(MGraph G, VerTexType e) { int i; for (i = 0; i < G.vexnum; i++) if (G.vexs[i] == e) return i; } Status CreateUDN(MGraph &G) { int i, j, k, w; char v1, v2; cout << "输入顶点个数和边的条数:"; cin >> G.vexnum >> G.arcnum; cout << "输入各个顶点:"; 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] = MaxInt; for (k = 0; k < G.arcnum; k++) { cout << "输入一条边依附的顶点和权值:"; 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]; Edge[k].lowcost = w; Edge[k].Head = v1; Edge[k].Tail = v2; } return OK; } void DispGraph(MGraph G) { int i, j; cout << "顶点:\n"; for (i = 0; i < G.vexnum; i++) cout << G.vexs[i] << " "; cout << "\n邻接矩阵:\n"; for (i = 0; i < G.vexnum; i++) { for (j = 0; j < G.vexnum; j++) cout << setw(7) << G.arcs[i][j]; cout << endl; } } Status Min(MGraph G) { int i, index = -1, min = MaxInt; for (i = 0; i < G.vexnum; i++) { if (Edge[i].lowcost < min && Edge[i].lowcost != 0) { min = Edge[i].lowcost; index = i; } } return index; } void Sort(MGraph G) { //冒泡排序 int m = G.arcnum - 2; struct Edge t; int flag = 1; while ((m > 0) && flag == 1) { flag = 0; for (int j = 0; j <= m; j++) { if (Edge[j].lowcost > Edge[j + 1].lowcost) { flag = 1; t = Edge[j]; Edge[j] = Edge[j + 1]; Edge[j + 1] = t; }//if }//for --m; }//while }//Sort void MiniSpanTree_Kruskal(MGraph G) { int i, j; int v1, v2, vs1 Sort(G); for (i = 0; i < G.vexnum; i++) Vexset[i] = i; for (i = 0; i < G.arcnum; i++) { v1 = LocateVex(G, Edge[i].Head); v2 = LocateVex(G, Edge[i].Tail); vs1 = Vexset[v1]; vs2 = Vexset[v2]; if (vs1 != vs2) { cout << Edge[i].Head << Edge[i].Tail << endl; for (j = 0; j < G.vexnum; j++) if (Vexset[j] == vs2) Vexset[j] = vs1; } } } int main() { MGraph G; CreateUDN(G); DispGraph(G); cout << "最小生成树的边如下:" << endl; MiniSpanTree_Kruskal(G); return 0; } 函数前写注释
已完成理解「#include <iostream> using namespace std; #include <iomanip> #define OK 1 #define MaxInt 32767 #define MVNum 100 typedef int Status; typedef char VerTexType; typedef int ArcType; typedef struct { VerTexType vexs[MVNum]; ArcType arcs[MVNum][MVNum]; int vexnum, arcnum; } MGraph; struct Edge { VerTexType Head; VerTexType Tail; ArcType lowcost; } Edge[(MVNum * (MVNum - 1)) / 2]; int Vexset[MVNum]; Status LocateVex(MGraph G, VerTexType e) { int i; for (i = 0; i < G.vexnum; i++) if (G.vexs[i] == e) return i; } Status CreateUDN(MGraph &G) { int i, j, k, w; char v1, v2; cout << "输入顶点个数和边的条数:"; cin >> G.vexnum >> G.arcnum; cout << "输入各个顶点:"; 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] = MaxInt; for (k = 0; k < G.arcnum; k++) { cout << "输入一条边依附的顶点和权值:"; 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]; Edge[k].lowcost = w; Edge[k].Head = v1; Edge[k].Tail = v2; } return OK; } void DispGraph(MGraph G) { int i, j; cout << "顶点:\n"; for (i = 0; i < G.vexnum; i++) cout << G.vexs[i] << " "; cout << "\n邻接矩阵:\n"; for (i = 0; i < G.vexnum; i++) { for (j = 0; j < G.vexnum; j++) cout << setw(7) << G.arcs[i][j]; cout << endl; } } Status Min(MGraph G) { int i, index = -1, min = MaxInt; for (i = 0; i < G.vexnum; i++) { if (Edge[i].lowcost < min && Edge[i].lowcost != 0) { min = Edge[i].lowcost; index = i; } } return index; } void Sort(MGraph G) { //冒泡排序 int m = G.arcnum - 2; struct Edge t; int flag = 1; while ((m > 0) && flag == 1) { flag = 0; for (int j = 0; j <= m; j++) { if (Edge[j].lowcost > Edge[j + 1].lowcost) { flag = 1; t = Edge[j]; Edge[j] = Edge[j + 1]; Edge[j + 1] = t; }//if }//for --m; }//while }//Sort void MiniSpanTree_Kruskal(MGraph G) { int i, j; int v1, v2, vs1 Sort(G); for (i = 0; i < G.vexnum; i++) Vexset[i] = i; for (i = 0; i < G.arcnum; i++) { v1 = LocateVex(G, Edge[i].Head); v2 = LocateVex(G, Edge[i].Tail); vs1 = Vexset[v1]; vs2 = Vexset[v2]; if (vs1 != vs2) { cout << Edge[i].Head << Edge[i].Tail << endl; for (j = 0; j < G.vexnum; j++) if (Vexset[j] == vs2) Vexset[j] = vs1; } } } int main() { MGraph G; CreateUDN(G); DispGraph(G); cout << "最小生成树的边如下:" << endl; MiniSpanTree_Kruskal(G); return 0; } 函数前写注释」
展开阅读网页
在C++中,注释是用来解释代码的目的、功能和实现方式的文本。注释不会被编译器执行,它们只是为阅读代码的人提供帮助。下面是你提供的代码中每个函数的注释示例:
// 声明一个结构体,用于表示图的顶点和边
typedef struct {
// ...
} MGraph;
// 定位图中的顶点
// 参数 G 是图的结构体
// 参数 e 是要定位的顶点值
// 返回值是顶点在图中的索引,如果找不到则返回 -1
Status LocateVex(MGraph G, VerTexType e) {
// ...
}
// 创建一个无向图
// 参数 G 是图的结构体引用
// 函数首先读取顶点和边的数量,然后读取顶点名称和边的详细信息
// 并初始化邻接矩阵和边的数组
Status CreateUDN(MGraph &G) {
// ...
}
// 显示图的信息
// 参数 G 是图的结构体
// 函数显示图的顶点和邻接矩阵
void DispGraph(MGraph G) {
// ...
}
// 寻找最小边的索引
// 参数 G 是图的结构体
// 返回值是最小边的索引,如果没有找到则返回 -1
Status Min(MGraph G) {
// ...
}
// 对边进行排序
// 参数 G 是图的结构体
// 函数使用冒泡排序算法对边数组进行排序,以便找到最小生成树的边
void Sort(MGraph G) {
// ...
}
// 克鲁斯卡尔算法实现最小生成树
// 参数 G 是图的结构体
// 函数首先对边进行排序,然后使用并查集数据结构来找到最小生成树的边
void MiniSpanTree_Kruskal(MGraph G) {
// ...
}
// 主函数
// 程序的入口点,创建图,显示图的信息,并计算最小生成树
int main() {
// ...
}
请注意,注释应该简洁明了,避免冗余,同时要确保它们准确地描述了代码的功能和目的。在实际开发中,良好的注释习惯可以帮助其他开发者更快地理解你的代码。