#pragma once #include"SequenceList.h" typedef struct { SequenceList Vertices; int edge[MaxVertices][MaxVertices]; int numOfEdges; }MatrixGraph; void Initiate(MatrixGraph* G, int n) { int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { if (i = j) G->edge[i][j] = 0; else G->edge[i][j] = MaxWeight; } G->numOfEdges = 0; ListInitialize(&G->Vertices); } void InsertVertex(MatrixGraph* G, ElemType vertex) { ListInsert(&G->Vertices, G->Vertices.size, vertex); } void InsertEdge(MatrixGraph* G, int v1, int v2, int weight) { if (v1 < 0 || v1 >= G->Vertices.size || v2 < 0 || v2 >= G->Vertices.size) { printf("参数v1或v2越界出错\n"); exit(1); } G->edge[v1][v2] = weight; G->numOfEdges++; } void DeleteEdge(MatrixGraph* G, int v1, int v2) { if (v1 < 0 || v1 >= G->Vertices.size || v2 < 0 || v2 >= G->Vertices.size || v1 == v2) { printf("参数v1或v2越界出错\n"); exit(1); } G->edge[v1][v2] = MaxWeight; G->numOfEdges--; } void DeleteVertex(MatrixGraph* G, int v) { int n = ListLength(G->Vertices), i, j; ElemType x; for (i = 0; i < n; i++) for (j = 0; j < n; j++) if ((i == v || j == v) && G->edge[i][j] > 0 && G->edge[i][j] < MaxWeight) G->numOfEdges--; for (i = v; i < n; i++) for (j = 0; j < n; j++) G->edge[i][j] = G->edge[i + 1][j]; for (i = 0; i < n; i++) for (j = v; j < n; j++) G->edge[i][j] = G->edge[i][j + 1]; ListDelete(&G->Vertices, v, &x); } typedef struct { int row; int col; int weight; }RowColWeight; void CreatGraph(MatrixGraph* G, ElemType V[], int n, RowColWeight E[], int e) { int i, k; Initiate(G, n); for (i = 0; i < n; i++) InsertVertex(G, V[i]); for (k = 0; k < e; k++) InsertEdge(G, E[k].row, E[k].col, E[k].weight); } #include<stdio.h> #include<string.h> #define MaxWeight 10000 #define MaxVertices 6 #define MaxSize 100 typedef int ElemType; #include"MGraph.h" void main(void) { MatrixGraph g1; ElemType a[] = { '1','2','3','4','5','6' }; RowColWeight rcw[] = { {0,2,5},{1,0,3},{1,4,8},{2,1,15},{2,5,7},{4,3,4},{5,3,10},{5,4,18} }; int n = 6, e = 6; int i, j; CreatGraph(&g1, a, n, rcw, e); printf("顶点集合为"); for (i = 0; i < g1.Vertices.size; i++) { printf("%c", g1.Vertices.list[i]); } printf("\n"); printf("权值集合为:\n"); for (i = 0; i < g1.Vertices.size; i++) { for (j = 0; j < g1.Vertices.size; j++) printf("%5d", g1.edge[i][j]); printf("\n"); } }为何会乱码,无法正常显示,以及给出解决后的代码
时间: 2023-06-26 17:08:54 浏览: 91
C语言头文件避免重复包含、#pragma once和#ifndef区别
这段代码中出现了中文字符,可能是因为编码格式不同导致的乱码。建议将代码保存为UTF-8格式再进行编译。
以下是保存为UTF-8格式后的代码:
```c
#pragma once
#include"SequenceList.h"
typedef struct {
SequenceList Vertices;
int edge[MaxVertices][MaxVertices];
int numOfEdges;
}MatrixGraph;
void Initiate(MatrixGraph* G, int n) {
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
if (i == j)
G->edge[i][j] = 0;
else
G->edge[i][j] = MaxWeight;
}
G->numOfEdges = 0;
ListInitialize(&G->Vertices);
}
void InsertVertex(MatrixGraph* G, ElemType vertex) {
ListInsert(&G->Vertices, G->Vertices.size, vertex);
}
void InsertEdge(MatrixGraph* G, int v1, int v2, int weight) {
if (v1 < 0 || v1 >= G->Vertices.size || v2 < 0 || v2 >= G->Vertices.size) {
printf("参数v1或v2越界出错\n");
exit(1);
}
G->edge[v1][v2] = weight;
G->numOfEdges++;
}
void DeleteEdge(MatrixGraph* G, int v1, int v2) {
if (v1 < 0 || v1 >= G->Vertices.size || v2 < 0 || v2 >= G->Vertices.size || v1 == v2) {
printf("参数v1或v2越界出错\n");
exit(1);
}
G->edge[v1][v2] = MaxWeight;
G->numOfEdges--;
}
void DeleteVertex(MatrixGraph* G, int v) {
int n = ListLength(G->Vertices), i, j;
ElemType x;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if ((i == v || j == v) && G->edge[i][j] > 0 && G->edge[i][j] < MaxWeight)
G->numOfEdges--;
for (i = v; i < n; i++)
for (j = 0; j < n; j++)
G->edge[i][j] = G->edge[i + 1][j];
for (i = 0; i < n; i++)
for (j = v; j < n; j++)
G->edge[i][j] = G->edge[i][j + 1];
ListDelete(&G->Vertices, v, &x);
}
typedef struct {
int row;
int col;
int weight;
}RowColWeight;
void CreatGraph(MatrixGraph* G, ElemType V[], int n, RowColWeight E[], int e) {
int i, k;
Initiate(G, n);
for (i = 0; i < n; i++)
InsertVertex(G, V[i]);
for (k = 0; k < e; k++)
InsertEdge(G, E[k].row, E[k].col, E[k].weight);
}
#include<stdio.h>
#include<string.h>
#define MaxWeight 10000
#define MaxVertices 6
#define MaxSize 100
typedef int ElemType;
#include"MGraph.h"
void main(void) {
MatrixGraph g1;
ElemType a[] = { '1','2','3','4','5','6' };
RowColWeight rcw[] = { {0,2,5},{1,0,3},{1,4,8},{2,1,15},{2,5,7},{4,3,4},{5,3,10},{5,4,18} };
int n = 6, e = 6;
int i, j;
CreatGraph(&g1, a, n, rcw, e);
printf("顶点集合为");
for (i = 0; i < g1.Vertices.size; i++) {
printf("%c", g1.Vertices.list[i]);
}
printf("\n");
printf("权值集合为:\n");
for (i = 0; i < g1.Vertices.size; i++) {
for (j = 0; j < g1.Vertices.size; j++)
printf("%5d", g1.edge[i][j]);
printf("\n");
}
}
```
阅读全文