-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassoc_tab.c
96 lines (74 loc) · 2.03 KB
/
assoc_tab.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
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include "assoc_tab.h"
void assoc_push(assoc_tab* where, char *word){
if(where->possible==NULL){where->possible=malloc(sizeof(node));}
where->size++;
list_push(word,where->possible);
}
char* get_word_by_id(node* first,int number){
if(first == NULL){return "(null)";}
if (number == 0){return first->word;}
number--;
while(number--){
if(first==NULL){return "(null)";}
first=first->next;
}
return first->word;
}
char *get_word_random(assoc_tab *tab[],int hash){
if(tab[hash]->size == 0){return "";}
unsigned int wyrandowany = 0;
srand(hash*time(NULL));
wyrandowany = rand() % tab[hash]->size;
node *pom = malloc (sizeof (node));
pom = tab[hash]->possible;
pom = tab[hash]->possible;
while(wyrandowany--){
pom = pom->next;
}
return pom->word;
}
void make_trans_from_file(char* out_file, assoc_tab *where[]){
int size,assoc_size,i,j;
char *tmp;
int k;
tmp=malloc(sizeof(char)*100);
int dud;
FILE *in=fopen(out_file,"r");
fscanf(in,"%d",&assoc_size);
for(i=0;i<assoc_size;i++){
fscanf(in,"%d",&size);
while(size--){
fscanf(in,"%s ",tmp);
assoc_push(where[i],tmp);
}
}
fclose(in);
}
void write_trans_to_file(char* out_file, assoc_tab *from[], int assoc_tab_size){
printf("write!\n");
int i,j;
FILE *out=fopen(out_file,"w");
if(out==NULL){
FILE *err=fopen("errors.log","a"); /*bedzie dopisywac do pliku*/
fprintf(err,"Couldn`t write trans to file.\n");
fclose(err);
}
fprintf(out,"%d\n", assoc_tab_size);
for(i=0;i<assoc_tab_size;i++){
fprintf(out,"%d",from[i]->size);
for(j=0;j<from[i]->size;j++){
fprintf(out," %s",get_word_by_id(from[i]->possible,j));
}
fprintf(out,"\n");
}
fclose(out);
}
void assoc_initialize(assoc_tab *what[],int t_width){
int i;
for(i=0;i<t_width;i++){
what[i]=malloc(sizeof(node));
}
}