-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
357 lines (281 loc) · 9.11 KB
/
main.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
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
349
350
351
352
353
354
355
356
357
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
#include<string.h>
#define MAX_N 10000
void print_select_mode(void)
{
printf("Please select mode : \n") ;
printf("0 init the file\n") ;
printf("1 write in information\n") ;
printf("2 take a look \n") ;
printf("3 find someone\n") ;
printf("4 delete someone\n") ;
printf("5 show my favorite\n") ;
printf("6 add in my favorite\n") ;
printf("7 remove my favorite\n") ;
printf("-1 Closed \n") ;
}
void init_file(void)
{
FILE* fp = fopen( "contact_people_information" , "w" ) ;
assert( fp != NULL ) ;
fclose(fp) ;
return ;
}
bool change_my_favorite(int n)
{
printf("Name : ") ;
char name[100] = {0} ;
scanf("%s" , name) ;
putchar('\n') ;
FILE* fp = fopen( "contact_people_information" , "r+" ) ;
char temp_name[100] = {0} ;
long long int temp_number ;
int temp_priority ;
bool success = false ;
while ( fscanf(fp , "%s%lld%d" , temp_name , &temp_number , &temp_priority ) != EOF ) {
if ( strcmp(temp_name , name) == 0 ) {
printf("Is this one?(Y/N) : %s %010lld\n" , temp_name , temp_number) ;
char s[3] ;
scanf("%s" , s) ;
putchar('\n') ;
if ( s[0] == 'Y' ) {
success = true ;
fseek(fp , -1 , SEEK_CUR) ;
fprintf(fp , "%d" , n) ;
break ;
}
}
memset( temp_name , 0 , 100 ) ;
}
fclose(fp) ;
return success ;
}
bool fuzzy_match( char* name1 , char* name2 )
{
unsigned long int len1 = strlen(name1) ;
unsigned long int len2 = strlen(name2) ;
unsigned long int min = (len1 > len2)? len2 : len1 ;
unsigned long int count = 0 ;
for(unsigned long int i = 0 ; i < min ; ++i) {
if ( name1[i] != name2[i] )
++count ;
}
return ( count <= 2 ) ;
}
bool find_someone(void)
{
printf("Choose what do you want to find : \n") ;
printf("1 Find by name\n") ;
printf("2 Find by number\n") ;
int mode ;
scanf("%d" , &mode) ;
putchar('\n') ;
if ( mode == 1 ) {
/* for fuzzy search */
bool fuzzy ;
printf("Do you want fuzzy search ?(Y/N) : ") ;
char ans[3] ;
scanf("%s" , ans) ;
fuzzy = ( ans[0] == 'Y' )? true : false ;
printf("Find name : ") ;
char name[100] = {0} ;
scanf("%s" , name) ;
putchar('\n') ;
FILE* fp = fopen("contact_people_information" , "r") ;
assert( fp != NULL ) ;
char temp_name[100] = {0} ;
long long int temp_number ;
int priority ;
int find = 0 ;
while ( fscanf(fp , "%s%lld%d" , temp_name , &temp_number , &priority ) != EOF ) {
if ( strcmp(temp_name , name) == 0 || ( fuzzy && fuzzy_match(temp_name , name) ) ) {
++find ;
printf("%d : %s %010lld\n" , find , temp_name , temp_number) ;
}
memset( temp_name , 0 , 100 ) ;
}
fclose(fp) ;
return (find > 0) ;
}
else {
assert( mode == 2 ) ;
long long int number ;
printf("Find number : ") ;
scanf("%lld" , &number) ;
putchar('\n') ;
FILE* fp = fopen("contact_people_information" , "r") ;
assert( fp != NULL ) ;
char temp_name[100] = {0} ;
long long int temp_number ;
int priority ;
int find = 0 ;
while ( fscanf(fp , "%s%lld%d" , temp_name , &temp_number , &priority ) != EOF ) {
if ( temp_number == number ) {
++find ;
printf("%d : %s %010lld\n" , find , temp_name , temp_number) ;
}
memset( temp_name , 0 , 100 ) ;
}
fclose(fp) ;
return (find > 0) ;
}
}
bool write_in_information(void)
{
printf("Name : ") ;
char name[100] = {0} ;
scanf("%s" , name ) ;
putchar('\n') ;
printf("Number : ") ;
long long int number ;
scanf("%lld" , &number) ;
putchar('\n') ;
FILE* fp = fopen( "contact_people_information" , "a+" ) ;
assert( fp != NULL ) ;
int result = fprintf(fp, "%s %lld 0\n" , name , number) ;
fclose(fp) ;
return ( result >= 0 ) ;
}
bool traverse_file(int standard)
{
FILE* fp = fopen( "contact_people_information" , "r" ) ;
assert( fp != NULL ) ;
char name[100] = {0} ;
long long int number ;
int cursor = 1 ;
int priority ;
while ( fscanf(fp, "%s%lld%d" , name , &number , &priority ) != EOF ) {
if ( priority >= standard ) {
printf("%d : %s %010lld\n" , cursor , name , number ) ;
++cursor ;
}
memset(name , 0 , 100) ;
}
fclose(fp) ;
return (cursor >= 2) ;
}
bool delete_someone(void)
{
char delete_name[100] = {0} ;
printf("Delete name : ") ;
scanf("%s" , delete_name) ;
putchar('\n') ;
FILE* fp = fopen( "contact_people_information" , "r+" ) ;
assert( fp != NULL ) ;
char string[MAX_N][100] = {{0}} ;
long long int phone_number[MAX_N] ;
int priority[MAX_N] ;
int cursor = 0 ;
char temp[100] = {0} ;
long long int temp_number ;
int temp_priority ;
int candidate_cursor = 0 ;
char candidate_name[MAX_N][100] = {{0}} ;
long long int candidate_number[MAX_N] ;
int candidate_priority[MAX_N] ;
while ( fscanf(fp, "%s%lld%d" , temp , &temp_number , &temp_priority ) != EOF ) {
if ( strcmp(temp , delete_name) != 0 ) {
strcpy( string[cursor] , temp ) ;
phone_number[cursor] = temp_number ;
priority[cursor] = temp_priority ;
++cursor ;
}
else {
strcpy( candidate_name[candidate_cursor] , temp ) ;
candidate_number[candidate_cursor] = temp_number ;
candidate_priority[candidate_cursor] = temp_priority ;
++candidate_cursor ;
}
memset(temp, 0, 100) ;
}
fclose(fp) ;
FILE* fp2 = fopen( "contact_people_information" , "w" ) ;
assert( fp2 != NULL ) ;
long long int record = 0 ;
for( int i = 0 ; i < cursor ; i++ ) {
record += fprintf(fp , "%s %lld %d\n" , string[i] , phone_number[i] , priority[i]) ;
}
printf("Choose which one should delete : \n") ;
for(int i = 0 ; i < candidate_cursor ; i++) {
printf("%d : %s %010lld\n" , i + 1 , candidate_name[i] , candidate_number[i] ) ;
}
int choose ;
scanf("%d" , &choose) ;
--choose ;
bool deleted = false ;
for(int i = 0 ; i < candidate_cursor ; i++) {
if ( i == choose ) {
deleted = true ;
continue ;
}
record += fprintf(fp2 , "%s %lld %d\n" , candidate_name[i] ,
candidate_number[i] , candidate_priority[i] ) ;
}
fclose(fp2) ;
return (deleted) ;
}
int main(void)
{
printf("Welcome to 通訊錄\n") ;
print_select_mode() ;
int mode ;
while ( scanf("%d" , &mode) != EOF ) {
putchar('\n') ;
switch ( mode ) {
case 0 :
init_file() ;
break ;
case 1 :
if (write_in_information() )
printf("Success\n") ;
else
printf("Failed\n") ;
break ;
case 2 :
if (traverse_file(0))
printf("Success\n") ;
else
printf("Failed : the file is empty\n") ;
break ;
case 3 :
if ( find_someone() )
printf("Success\n") ;
else
printf("No one match the name \n") ;
break ;
case 4 :
if ( delete_someone() )
printf("Success\n") ;
else
printf("Failed : the file is empty\n") ;
break ;
case 5 :
if (traverse_file(1))
printf("Success\n") ;
else
printf("Failed : your favorite is empty\n") ;
break ;
case 6 :
if ( change_my_favorite(1) )
printf("Success\n") ;
else
printf("Failed : no such person\n") ;
break ;
case 7 :
if ( change_my_favorite(0) )
printf("Success\n") ;
else
printf("Failed : no such person\n") ;
break ;
case -1 :
printf("Closed\n") ;
return 0 ;
default :
printf("please select correct mode \n") ;
}
print_select_mode() ;
}
return 0 ;
}