-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
211 lines (165 loc) · 5.21 KB
/
test.c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//#ifndef UTILITY_H_ /* Include guard */
//#define UTILITY_H_
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
//#include <omp.h>
/**
* GROUP PROJECT CISC360-010
* Group Member: Lan Chen, Jessica Chen, Xiaomeng Chen and Zhanglong Peng
*
* __________________________________________________________________________
* PART FIVE: Graph Structure, Helper Function and GraphGenerator Converter
* This class has all the utility for other algorithms,
* Also includs the converter of Random Graph Generator.
*
*/
// Structure
//__________________________________________________________________________
// a structure to represent a weighted edge in graph
struct Edge
{
int src, dest, weight;
}; typedef struct Edge Edge;
// a structure to represent a connected, undirected
// and weighted graph as a collection of edges.
struct Graph
{
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges.
// Since the graph is undirected, the edge
// from src to dest is also edge from dest
// to src. Both are counted as 1 edge here.
Edge* edge;
}; typedef struct Graph Graph;
// A structure to represent a subset for union-find
struct subset
{
int parent;
int rank;
}; typedef struct subset subset;
// Functions and Method For GraphGenerator Converter
//__________________________________________________________________________
// Read from txt file and generate a graph
void converter(const char* filename, Graph* agraph)
{
// dealing with Vertex and Edge in Graph
//Graph* agraph = (Graph*)malloc(sizeof(Graph));
int i = 0;
char cha;
char* temp = (char*)malloc(sizeof(char) * 20);
int numVertex;
int numEdge;
int iindex = 0;
int shiba;
shiba = strlen(filename);
printf("%d\n", shiba);
//printf("%lu\n", sizeof(filename));
//printf("%lu\n", (sizeof(filename) / sizeof(char)));
//sizeof(filename) / sizeof(filename[0]))
for(i = 0; i < shiba && iindex < 20; i++){
cha = filename[i];
if(cha == 'v'){
}else if(cha == 'e'){
numVertex = atoi(temp);
temp[0] = 0;
iindex = 0;
}else if(cha == '.'){
numEdge = atoi(temp);
}else if(isdigit(cha)){
temp[iindex] = cha;
iindex++;
}
}
//printf("%d ", numVertex);
//printf("%d\n", numEdge);
agraph->V = numVertex;
agraph->E = numEdge;
agraph->edge = (Edge*)malloc(sizeof(Edge) * numEdge);
FILE *myf;
myf = fopen(filename,"r");
//ifstream myf;
//myf.open(filename);
//printf("pass\n");
int x;
char* line = (char*)malloc(sizeof(char) * 10);
int parent = -1;
int child, wei;
int index = 0;
int jindex = 0;
//while (!myf.eof()) { // change the origin file to one element each line
int h, k;
if(myf == NULL){
printf("Cannot open the File.\n");
}else {
do {
x = getc (myf);
if(isdigit(x)){
line[jindex] = x;
jindex++;
//printf("pass %c\n", line[index]);
}else if(x == ':'){
//line[0] = '\0'; ///////for loop
for(h = 0; h <10; h++){
line[h] = '\0';
}
jindex = 0;
parent++;
}else if(x == ']'){
child = atoi(line);
//line[0] = '\0';
for(h = 0; h <10; h++){
line[h] = '\0';
}
jindex = 0;
}else if(x == ')'){
wei = atoi(line);
if(child > parent){
//printf(":%d, %d, %d\n", parent, child, wei); ////cun edge
//Edge e = (Edge)malloc(sizeof(Edge));
//e.src = parent;
//e.dest = child;
//e.weight = wei;
agraph->edge[index].src = parent;
agraph->edge[index].dest = child;
agraph->edge[index].weight = wei;
printf(" %d:%d, %d, %d\n", index, agraph->edge[index].src, agraph->edge[index].dest,
agraph->edge[index].weight);
index++;
}
child = 0;
wei = 0;
//line[0] = '\0';
for(h = 0; h <10; h++){
line[h] = '\0';
}
jindex = 0;
}
}while(x != EOF);
}
fclose(myf);
free(temp);
free(line);
//return agraph;
}
// Main
//__________________________________________________________________________
// Driver program to test above functions
int main()
{
Graph* graph = (Graph*)malloc(sizeof(Graph));
converter("v1000e499500.txt", graph);
printf("pass");
printf("you Vert %d\n", graph->V);
printf("yoo Edge %d\n", graph->E);
printf("\n");
int num = graph->E;
int i = 0;
for(i = 0; i < num; i++){
//printf(": %d, %d, %d\n", graph->edge[i].src, graph->edge[i].dest, graph->edge[i].weight);
}
return 0;
}
//#endif /* UTILITY_H_ */