-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjGraph.c
More file actions
80 lines (72 loc) · 1.54 KB
/
Copy pathAdjGraph.c
File metadata and controls
80 lines (72 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Created by ys on 2019/4/15.
//
#include "../tools.h"
#include "AdjGraph.h"
#define DATASTRUCT_ADJGRAPH_C
#ifndef DATASTRUCT_ADJGRAPH_C
#define DATASTRUCT_ADJGRAPH_C
//构造图
void createGraph(AdjGraph *G)
{
FILE *fp;
char *fname = "../data/graph.txt";
fp = fopen(fname, "r");
if(fp==NULL)
{
printf("文件操作打开不成功\n");
exit(-1);
}
char ch;
int i = 0;
fscanf(fp, "%d %d\n",&(G->n), &(G->e));
while ((ch = fgetc(fp))!='#')
{
if(ch == '\n')
{
continue;
}
G->vexs[i] = ch;
i ++;
}
fgetc(fp);
int weight = -1;
char c1 = 'X', c2 = 'X';
// while ((ch = fgetc(fp))!=EOF)
// {
// putchar(ch);
// }
for (int j = 0; j < G->n; ++j) {
for (int k = 0; k < G->n; ++k) {
G->edges[j][k] = -1;
}
}
while (fscanf(fp, "(%c, %c, %d)\n", &c1, &c2, &weight)!=EOF)
{
printf("c1:%c c2:%c weight:%d\n",c1,c2,weight);
G->edges[c1-'a'][c2-'a'] = weight;
}
for (int j = 0; j < G->n; ++j) {
for (int k = 0; k < G->n; ++k) {
printf("%5d",G->edges[j][k]);
}
printf("\n");
}
fclose(fp);
}
void test()
{
AdjGraph *G = (AdjGraph*)malloc(sizeof(AdjGraph));
createGraph(G);
// FILE *fp;
// fp = fopen("test.txt","r");
// char c1,c2;
// int a;
// while (fscanf(fp,"(%c, %c, %d)\n",&c1,&c2,&a)!=EOF)
// {
// printf("%c %c %d\n",c1,c2,a);
// }
//
// fclose(fp);
}
#endif //DATASTRUCT_ADJGRAPH_H