-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRO_TELIKH.cpp
More file actions
348 lines (310 loc) · 7.31 KB
/
Copy pathPRO_TELIKH.cpp
File metadata and controls
348 lines (310 loc) · 7.31 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node* deiktis;
typedef struct node
{
char name[50];
char items[50];
int ratting;
int time;
int idu;
int idi;
int itemcount;
deiktis next;
}node;
deiktis *arrayU;
deiktis *arrayI;
deiktis fin;
deiktis temp;
int B=3;
int hash_func(char* name);
int initialize (int B);
void create_num_id (FILE* f1, FILE* f2);
int return_id (node **cur,char* name, char eidos);
int main ()
{
FILE *f1,*f2;
f1=fopen("sample_2.txt","r");
f2=fopen("result.txt","w");
if (f1 == NULL && f2==NULL)
{
printf("file can't be opened \n");
exit(1);
}
create_num_id(f1,f2);
fclose(f1);
fclose(f2);
return 0;
}
void create_num_id (FILE* f1, FILE* f2)
{
node *array;
deiktis neos_kombos;
char onoma[50],items[50],u='u',it='i';
int i,j,pos,count,ratting,time,idu=0,idi=0,check,c1,k;
for(count=0;!feof(f1);count++)
{
fscanf(f1,"%s %s %d %d ",onoma,items,&ratting,&time);
}
array=(node*)malloc(count*sizeof(node));
if(array==NULL)
{
printf("Lathos malloc");
exit(1);
}
fseek(f1,0,SEEK_SET);
int ite[count],on[count],rat[count],tim[count],temp_1,temp_2,temp_3,temp_4;
for(i=0;i<count;i++)
{
fscanf(f1,"%s %s %d %d",onoma,items,&ratting,&time);
strcpy(array[i].name,onoma);
strcpy(array[i].items,items);
array[i].ratting=ratting;
array[i].time=time;
}
if(initialize (B)==1)
{
for(i=0;i<count;i++)
{
check=0;
temp = (deiktis)malloc(sizeof(node));
if(array==NULL)
{
printf("Lathos malloc to temp");
exit(1);
}
pos=hash_func(array[i].name);
strcpy(temp->name,array[i].name);
strcpy(temp->items,array[i].items);
temp->ratting=array[i].ratting;
temp->time=array[i].time;
temp->idu=idu;
temp->next=NULL;
if(arrayU[pos]==NULL)
{
arrayU[pos]=temp;
//printf("1ST TIME {%d} IDu:%d %s \n",pos,arrayU[pos]->idu,arrayU[pos]->name);
}
else
{
for( fin=arrayU[pos]; fin!=NULL; fin=fin->next )
{
//printf("mphke sthn epanalipsi sugkrisi %s me %s ",fin->name,array[i].name);
if( strcmp(fin->name,array[i].name) == 0 )
{
check=1;
//printf("brike idio \n");
break;
}
else
{
//printf("den brike \n");
check=2;
}
}
if(check==2)
{
neos_kombos=temp;
neos_kombos->next = arrayU[pos];
arrayU[pos] = neos_kombos;
//printf("CREATE NODE {%d} IDu:%d %s\n",pos,arrayU[pos]->idu,arrayU[pos]->name);
//printf("%s",arrayU[pos]->name);
}
else
continue;
}
idu++;
}
k=1;
for(i=0;i<count;i++)
{
check=0;
temp = (deiktis)malloc(sizeof(node));
pos=hash_func(array[i].items);
strcpy(temp->name,array[i].name);
strcpy(temp->items,array[i].items);
temp->ratting=array[i].ratting;
temp->time=array[i].time;
temp->idi=idi;
temp->itemcount=k;
temp->next=NULL;
if(arrayI[pos]==NULL)
{
arrayI[pos]=temp;
//k[pos]=k[pos]+1;
//printf("1ST TIME {%d} IDi:%d %s \n",pos,arrayI[pos]->idi,arrayI[pos]->items);
}
else
{
//printf("eimaste sthn epanalipsi {%d}->",i);
for( fin=arrayI[pos]; fin!=NULL; fin=fin->next )
{
//printf("\nSugkrisi %s me %s: ",fin->items,array[i].items);
if( strcmp(fin->items,array[i].items) == 0 )
{
//printf("Einai idio");
arrayI[pos]->itemcount=k++;
check=1;
//printf("Hrthe to item:%s kai ekane to k[%d]= %d\n",array[i].items,pos,fin->itemcount);
//printf("brike idio \n");
break;
}
else
{
//printf("den brike \n");
check=2;
}
}
if(check==2)
{
neos_kombos=temp;
neos_kombos->next = arrayI[pos];
arrayI[pos] = neos_kombos;
//printf("CREATE NODE {%d} IDi:%d %s\n",pos,arrayI[pos]->idi,arrayI[pos]->items);
//printf("%s",arrayI[pos]->items);
}
else
continue;
}
idi++;
//printf("%d \n",k);
}
fprintf(f2,"%d %d\n",idu,idi);
//printf("\n%d %d\n\n",idu,idi);
for(i=0;i<count;i++)
{
pos = hash_func(array[i].name);
on[i]= return_id(&arrayU[pos],array[i].name,u);
pos = hash_func(array[i].items);
ite[i] = return_id(&arrayI[pos],array[i].items,it);
rat[i]=array[i].ratting;
tim[i]=array[i].time;
//printf("%d %d\n",on[i],ite[i]);
//fprintf(f2,"%d %d %d %d\n",on[i],ite[i],array[i].ratting,array[i].time);
}
for(i=0; i<count; i++)
{
for(j=count-1; j > i; j--)
{
if(on[j] < on[j-1])
{
temp_1 = on[j];
on[j] = on[j-1];
on[j-1] = temp_1;
temp_2= ite[j];
ite[j]=ite[j-1];
ite[j-1]=temp_2;
temp_3= rat[j];
rat[j]= rat[j-1];
rat[j-1]=temp_3;
temp_4= tim[j];
tim[j]= tim[j-1];
tim[j-1]= temp_4;
}
}
}
for(i=0;i<count;i++)
{
//printf("%d %d\n",on[i],ite[i]);
fprintf(f2,"%d %d %d %d\n",on[i],ite[i],rat[i],tim[i]);
}
//for(i=0;i<count;i++)
//printf("%d %d\n",on[i],ite[i]);
//printf("\n");
/*for(i=0;i<B;i++)
{
printf("POS {%d}:",i);
if(arrayU[i]==NULL)
{
printf("NULL");
}
else{
while(arrayU[i]!=NULL)
{
printf("%s->",arrayU[i]->name);
arrayU[i]=arrayU[i]->next;
}
printf("NULL");
}
printf("\n");
}
printf("\n");*/
/*for(i=0;i<B;i++)
{
printf("POS {%d}:",i);
if(arrayI[i]==NULL)
{
printf("NULL");
}
else
{
while(arrayI[i]!=NULL)
{
printf("%s %d->",arrayI[i]->items,arrayI[i]->itemcount);
arrayI[i]=arrayI[i]->next;
}
printf("NULL");
}
printf("\n");
}*/
}
}
int hash_func(char* name)
{
int pos=0;
for (int i=0; i<strlen(name); i++)
pos=(pos+name[i])%B;
return pos;
}
int initialize (int B)
{
arrayU = (deiktis*)malloc(B*sizeof(node));
arrayI = (deiktis*)malloc(B*sizeof(node));
for(int i=0; i<B; i++)
{
arrayU[i]= NULL;
arrayI[i]= NULL;
//printf("ArrayU:%s ArrayI:%s\n",arrayU[i],arrayI[i]);
}
if (arrayU==NULL)
{
printf("Den egine swsta h desmeush mnhmhs");
return -1;
}
else
{
//printf("Egine swsta h desmeush mnhmhs");
return 1;
}
}
int return_id (node **head, char* string, char eidos)
{
int i;
char u;
if(eidos=='u')
{
//printf("u: ");
//printf("%s %d ",temp->name,temp->idu);
for( fin=*head; fin!=NULL; fin=fin->next )
{
//printf("%s",fin->name);
if( strcmp(fin->name,string) == 0 )
{
return fin->idu;
}
}
}
else
{
//printf("i: ");
for( fin=*head; fin!=NULL; fin=fin->next )
{
if( strcmp(fin->items,string) == 0 )
{
return fin->idi;
}
}
}
return 0;
}