-
Notifications
You must be signed in to change notification settings - Fork 0
/
organism.h
312 lines (258 loc) · 7.66 KB
/
organism.h
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <stdbool.h>
// #define LOG
#define MAGIC_FOOD_WEB "Food Web Predators & Prey:"
#define MAGIC_EAT "eats"
#define MAXNAME 20
typedef struct Org_struct {
char name[MAXNAME];
int* prey; //dynamic array of indices
int numPrey;
} Org;
int fsizefromflename(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
return -1;
}
int _fsizefromstd(FILE *st_in) {
struct stat st;
if (fstat(fileno(st_in), &st) == 0)
return st.st_size;
return -1;
}
bool check_margic(char* strmagic)
{
return (strnicmp(strmagic, MAGIC_FOOD_WEB, strlen(MAGIC_FOOD_WEB)) == 0);
}
int getfindindex(Org* list_org, int size, char* name)
{
int i = 0;
for(i = 0; i < size; i++)
{
if(stricmp(list_org[i].name, name) == 0) return i;
}
return -1;
}
Org* buildWeb(int* websize)
{
Org* list_org;
FILE* fp = stdin;
char ** relationships;
int linenum = 0;
if(fp)
{
long size = _fsizefromstd(fp);
char* line = (char*)malloc(size);
memset(line, 0, size);
int readsize = 0;
relationships = (char**)malloc(size);
while(fgets(line, size, stdin) && readsize < size)
{
*relationships = (char*)malloc(strlen(line)+1);
memset(*relationships, 0, strlen(line)+1);
strncpy(*relationships, line, strlen(line));
#ifdef LOG
printf("line = %s\n", *relationships);
#endif
memset(line, 0, size);
relationships++;
linenum++;
readsize += strlen(line)+1;
}
fclose(fp);
#ifdef LOG
printf("filesize = %d\n", size);
#endif
free(line);
}
else{
return NULL;
}
relationships = relationships-linenum;
#ifdef LOG
printf("getmargic of relationships[0] = %s", *relationships);
#endif
if(!check_margic(*relationships))
{
for(int i = 0; i < linenum; i++)
{
free(*relationships--);
}
return NULL;
}
relationships++;
#ifdef LOG
printf("check margic ok!!!\n");
#endif
list_org = (Org*)malloc(sizeof(Org)*(linenum-1));
//make organisms
for(int i = 0; i < linenum-1; i++)
{
char* lnode;
char* relative = *relationships;
int parselen = 0;
#ifdef LOG
printf("relationships = %s\n", relative);
#endif
lnode = (char*)malloc(strlen(*relationships));
memset(lnode, 0, strlen(*relationships));
int lnodelen = strlen(*relationships);
while(sscanf(relative, "%s", lnode) && parselen < strlen(*relationships)-1)
{
relative += strlen(lnode) + 1;
parselen += strlen(lnode) + 1;
if(lnode[strlen(lnode)-1] == ',')lnode[strlen(lnode)-1] = 0x00;
#ifdef LOG
printf("lnode = %s\n", lnode);
#endif
memset(list_org[i].name, 0, MAXNAME);
strncpy(list_org[i].name, lnode, strlen(lnode) > MAXNAME ? MAXNAME : strlen(lnode));
memset(lnode, 0, lnodelen);
break;
}
relationships++;
free(lnode);
}
#ifdef LOG
printf("make organisms list \n");
#endif
#ifdef LOG
for(int i = 0; i < linenum-1; i++)
{
printf("list_org[%d].name = %s \n", i, list_org[i].name);
}
#endif
#ifdef LOG
printf("parse predator-prey relationships start!!!\n");
#endif
//parse predator-prey relationships
relationships -= linenum-1;
for(int i = 0; i < linenum-1; i++)
{
char* lnode;
char* relative = *relationships;
int parselen = 0;
#ifdef LOG
printf("relationships = %s\n", relative);
#endif
lnode = (char*)malloc(strlen(*relationships));
memset(lnode, 0, strlen(*relationships));
int lnodelen = strlen(*relationships);
int eats = 0;
int *prey;
prey = (int*)malloc(strlen(*relationships)*sizeof(int));
int numPrey = 0;
memset(prey, 0, sizeof(int)*strlen(*relationships));
while(sscanf(relative, "%s", lnode) && parselen < strlen(*relationships)-1)
{
relative += strlen(lnode) + 1;
parselen += strlen(lnode) + 1;
if(lnode[strlen(lnode)-1] == ',')lnode[strlen(lnode)-1] = 0x00;
#ifdef LOG
printf("lnode = %s\n", lnode);
#endif
if(strnicmp(lnode, MAGIC_EAT, strlen(MAGIC_EAT)) == 0 && eats == 0)
{
eats = 1;
}
else{
if(eats == 1)
{
#ifdef LOG
printf("lnode is prey = %s \n", lnode);
#endif
int index = getfindindex(list_org, linenum-1, lnode);
prey[numPrey++] = index;
}
}
memset(lnode, 0, lnodelen);
}
list_org[i].prey = (int*)malloc(numPrey * sizeof(int));
memset(list_org[i].prey, 0, numPrey*sizeof(int));
for(int j = 0; j < numPrey; j++)
{
list_org[i].prey[j] = prey[j];
}
list_org[i].numPrey = numPrey;
free(lnode);
free(prey);
relationships++;
}
for(int i = 0; i < linenum; i++)
{
free(*relationships--);
}
*websize = linenum -1;
#ifdef LOG
printf("buidWeb End websize = %d\n", *websize);
#endif
return list_org;
}
Org* extinction(Org** oldlist, int* size, int del_index)
{
int websize = *size;
Org* list = *oldlist;
Org* newlist = (Org*)malloc(sizeof(Org) * (websize-1));
int i,j,cn;
cn = 0;
for(i = 0; i < websize; i++)
{
if(i == del_index) continue;
memset(newlist[cn].name, 0, MAXNAME);
strncpy(newlist[cn].name, list[i].name, strlen(list[i].name));
int numPrey = 0;
int* prey = (int*)malloc(list[i].numPrey * sizeof(int));
for(j = 0; j < list[i].numPrey; j++)
{
if(list[i].prey[j] == del_index) continue;
if(list[i].prey[j] > del_index)
{
prey[numPrey] = list[i].prey[j] - 1;
numPrey++;
}
else{
prey[numPrey] = list[i].prey[j];
numPrey++;
}
}
newlist[cn].numPrey = numPrey;
newlist[cn].prey = (int*)malloc(sizeof(int)*numPrey);
for(j = 0; j < numPrey; j++)
{
newlist[cn].prey[j] = prey[j];
}
cn++;
free(prey);
}
for(i = 0; i < websize; i++)
{
if(list[i].numPrey > 0) free(list[i].prey);
}
free(list);
*size = websize - 1;
*oldlist = newlist;
return newlist;
}
void printWeb(Org* list, int size)
{
printf("\n\n\n");
printf("printWeb start size = %d \n", size);
int i;
for(i = 0; i < size; i++)
{
printf("\n\n");
printf("Org Index = %d\n", i);
printf("name = %s\n", list[i].name);
printf("numPrey = %d \n", list[i].numPrey);
for(int j = 0; j < list[i].numPrey; j++)
{
printf("prey[%d] = %d\n", j, list[i].prey[j]);
}
printf("Org end\n");
}
printf("printWeb End!!!\n");
}