#include <stdio.h> #define MAX 100 #define MAXNUM 100000 typedef struct { int n, e, w; int vex[MAX]; int edge[MAX][MAX]; }MGraph; void CreateMGraph(MGraph* G) { int i, j, k; int p; char ch1, ch2; int weight; int p1, p2; printf("请输入顶点数:"); scanf_s("%d", &G->n); printf("请输入边数:"); scanf_s("%d", &G->e); printf("请输入个顶点信息(每个顶点一回车作为结束):\n"); for (i = 0; i < G->n; i++) { getchar(); printf("输入第%d个顶点:", i++); scanf_s("%c", &(G->vex[i])); } for (i = 0; i < G->n; i++) for (j = 0; j < G->n; j++) G->edge[i][j] = MAXNUM; for (i = 0; i < G->e; i++) { printf("请输入边和权值:\n"); getchar(); scanf_s("%c", &ch1); getchar(); scanf_s("%c", &ch2); getchar(); scanf_s("%d", &weight); for (j = 0; j < G->n; j++) { if (G->vex[j] = ch1) { p1 = j; break; } } for (j = 0; j < G->n; j++) { if (G->vex[j] = ch2) { p2 = j; break; } } G->edge[p1][p2] = weight; G->edge[p2][p1] = weight; } } void CountDegree(MGraph G) { int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 }; int i, j; for (i = 0; i < G.n; i++) for (j = 0; j < G.n; j++) if (G.edge[i][j] != MAXNUM) { outdegree[i]++; indegree[j]++; } for (i = 0; i < G.n; i++) printf("顶点%c的入度是%d,出度是%d\n", G.vex[i], indegree[i], outdegree[i]); } void DispMGraph(MGraph G) { int i, j; printf("\n图的邻接矩阵:\n"); for (i = 0; i < G.n; i++) printf("%c", G.vex[i]); printf("\n"); for (i = 0; i < G.n; i++) { for (j = 0; j < G.n; j++) if (G.edge[i][j] != MAXNUM) printf("%c%c%d\n", G.vex[i], G.vex[j], G.edge[i][j]); } } int main() { MGraph G; CreateMGraph(&G); }修改代码错误,建立有向带权图,输出有向带权图,求个顶点的入度和出度,并且输出
时间: 2024-03-10 20:51:10 浏览: 50
修改后的代码如下:
```c
#include <stdio.h>
#define MAX 100
#define MAXNUM 100000
typedef struct {
int n, e;
int vex[MAX];
int edge[MAX][MAX];
}MGraph;
void CreateMGraph(MGraph* G) {
int i, j, k;
int p;
char ch1, ch2;
int weight;
int p1, p2;
printf("请输入顶点数:");
scanf("%d", &G->n);
printf("请输入边数:");
scanf("%d", &G->e);
printf("请输入各顶点信息(每个顶点一行):\n");
for (i = 0; i < G->n; i++)
{
getchar();
printf("输入第%d个顶点:", i + 1);
scanf("%c", &(G->vex[i]));
}
for (i = 0; i < G->n; i++)
for (j = 0; j < G->n; j++)
G->edge[i][j] = MAXNUM;
for (i = 0; i < G->e; i++)
{
printf("请输入边和权值:\n");
getchar();
scanf("%c", &ch1);
getchar();
scanf("%c", &ch2);
scanf("%d", &weight);
for (j = 0; j < G->n; j++)
{
if (G->vex[j] == ch1)
{
p1 = j;
break;
}
}
for (j = 0; j < G->n; j++)
{
if (G->vex[j] == ch2)
{
p2 = j;
break;
}
}
G->edge[p1][p2] = weight;
}
}
void CountDegree(MGraph G) {
int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 };
int i, j;
for (i = 0; i < G.n; i++)
for (j = 0; j < G.n; j++)
if (G.edge[i][j] != MAXNUM)
{
outdegree[i]++;
indegree[j]++;
}
for (i = 0; i < G.n; i++)
printf("顶点%c的入度是%d,出度是%d\n", G.vex[i], indegree[i], outdegree[i]);
}
void DispMGraph(MGraph G) {
int i, j;
printf("\n图的邻接矩阵:\n");
for (i = 0; i < G.n; i++)
printf("%c ", G.vex[i]);
printf("\n");
for (i = 0; i < G.n; i++)
{
for (j = 0; j < G.n; j++)
if (G.edge[i][j] != MAXNUM)
printf("%c->%c:%d\n", G.vex[i], G.vex[j], G.edge[i][j]);
}
}
int main() {
MGraph G;
CreateMGraph(&G);
DispMGraph(G);
CountDegree(G);
return 0;
}
```
实现了建立有向带权图,输出有向带权图,求个顶点的入度和出度,并且输出。
阅读全文