This repository has been archived by the owner on Jan 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word.c~
255 lines (213 loc) · 3.92 KB
/
word.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
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
#include "word.h"
list word_list[2];
int flag_rep = 0;
int case_flag;
void sys_init(void)
{
int i;
//for(i = 0; i < 52;i++){
initlist(&word_list[0]);
word_list[0].data = 0;
//}
}
char read_word(FILE *fp, char str[])
{
int i;
char ch;
char temp[MAXLENGTH] = "\0";
i = 0;
while(1){
ch = fgetc(fp);
if(ch == -1)
return '#';
else if (i > MAXLENGTH - 2) {
break;
}
else if((ch >= 'a' && ch <= 'z')
|| (ch == '-')) {
temp[i] = ch;
i++;
continue;
} else if (ch >= 'A' && ch <= 'Z') {
ch += 32;
temp[i] = ch;
i++;
continue;
} else if (ch == 0x0A){
if (temp[i-1] == '-'){
i--;
continue;
} else
break;
} else if (i == 0) {
continue;
} else {
break;
}
}
strcpy(str,temp);
return temp[0];
}
/*************88
void word_detect(char str[])
{
static int flag = 0; //flag of each situation
int i;
if(flag == 1){
for(i = 0; str[i] != '\n';i++);
i--;
if(str[i] == '-'){
flag = 1;
}
********/
/********************only available for char list
int list_binsrch(plist list, char *data)
{
int low, mid, high;
int temp;
str_pword p;
low = 2;
high = list_length(list);
while(low <= high){
mid = (low + high) / 2;
p = get_data(list,mid);
temp = strcmp(data,p->word);
if (temp == 0){
flag_rep = 1;
return mid;
} else if (temp < 0)
high = mid - 1;
else
low = mid + 1;
}
p = get_data(list,mid);
temp = strcmp(data,p->word);
if(temp < 0)
mid--;
return mid;
}
**********************/
int list_search(plist list, char *data)
{
plist p;
int i;
int temp;
str_pword pp;
p = list->next;
i = 1;
// printf("aaaaaaaa\n");
while(p != list){
// printf("ok\n");
pp = p->data;
temp = strcmp(pp->word, data);
// printf("%d\n",temp);
if (temp == 0) {
i++;
flag_rep = 1;
break;
} else if(temp > 0)
break;
else {
p = p->next;
i++;
// printf("%d\n",i);
}
}
return i;
}
void add_to_list(FILE *fp)
{
int i;
int index;
int length;
char prefix;
char str[MAXLENGTH];
str_pword pword,p;
char except_word[3][10] = {"you", "to", "and"};
length = list_length(&word_list[0]);
printf("length is %d \n",length);
while (1) {
prefix = read_word(fp,str);
if (prefix == '#')
break;
if ((pword=(str_pword)malloc(sizeof(str_word)))==NULL){
printf("no enough memory to allocte bufer.\n");
exit(1);
}
strcpy(pword->word,str);
pword->freq = 1;
// length = list_length(&word_list[0]);
if (length > 1){
index = list_search(&word_list[0], pword->word);
// printf("index is %d\t",index);
if (flag_rep == 1) {
p = get_data(&word_list[0], index);
p->freq++;
flag_rep = 0;
free(pword);
} else {
// printf("here\n");
insert_data(&word_list[0], index, pword);
}
} else {
index = 1;
length++;
insert_data(&word_list[0], index, pword);
}
}
// if(case_flag == 1){
// sort_byfreq();
//}
printf_word(100, except_word);
free_list(&word_list[0]);
// length = list_length(&word_list[0]);
//printf("%d",length);
}
void sort_byfreq(void)
{
int i, j, n, change;
str_pword p1, p2, p3;
str_word p4 = {"hello", 5};
change = 1;
n = list_length(&word_list[0]);
p3 = (str_pword)malloc(sizeof(str_word));
for (i = 2; i < n && change; i++){
change = 0;
for (j = 2; j <= n - 1;j++){
p1 = get_data(&word_list[0], j);
p2 = get_data(&word_list[0], j + 1);
if((p1->freq) < (p2->freq)) {
*p3 = *p1;
*p1 = *p2;
*p2 = *p3;
change = 1;
}
}
}
}
//print all the word in the text file
void printf_word(int percent, char **str)
{
int i, j, k, m;
int length;
str_pword p;
k = 0;
length = list_length(&word_list[0]);
for(i = 2; i <= length; i++) {
p = get_data(&word_list[0], i);
m = 1;
/*********
for (j = 0; j < 3; j++, str++) {
if((strcmp(p->word, str)) == 0) {
m = 0;
break;
}
}
**************/
if (m == 1) {
printf("%-20s\t%4d;\n", p->word, p->freq);
k++;
}
// free(p);
}
}