Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
376 lines (271 loc) · 8.5 KB
/
Copy pathmain.c
File metadata and controls
376 lines (271 loc) · 8.5 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void add_input(int add,char *g ); // to add a book
void del(int re); // to remove the book by id
void search_re (int ser,int itre); // to search for the book by id
void search_str(char *str); // to search for the book by name
void print_sorted(); // to print all books in sorted order
void print_arbitrary(); // to print all books in arbitrary order
void sort (); // to sort the sec[].name
char list[20][50],temp[50];
int index_in_read = 0;
int count_line=4;
struct all
{
int id;
char name[100];
int quantity;
};
struct all sec[10];
int main()
{
FILE * fptr; // file pointer
if((fptr=fopen("pro.txt","r+"))==NULL)
{
printf("erorr");
}
else
{
char g[300];
fscanf(fptr,"%d",&sec[index_in_read].id);
fgets(g, sizeof(g), fptr );
while(!feof(fptr) )
{
char *z = strtok(g,",");
char *c = strtok(NULL,",");
strcpy(sec[index_in_read].name, z);
sec[index_in_read].quantity = atoi(c); // to cast pointer from str to int
index_in_read+=1;
fscanf(fptr,"%d",&sec[index_in_read].id);
fgets(g, sizeof(g), fptr );
}
char esmael;
do
{
int o;
system("cls"); // to clean console
puts("enter 1 to add a book") ;
puts("enter 2 to remove a book") ;
puts("enter 3 to search for the book by id") ;
puts("enter 4 to search for the book by name") ;
puts("enter 5 to print books in sorted order") ;
puts("enter 6 to print all books") ;
scanf("%d",&o); // to choose option
switch(o)
{
// start add input
case 1 :
printf("enter like this : id name,quantity\n");
int add = 0;
scanf("%d",&add);
fgets(g, sizeof(g), stdin );
add_input( add, g);
print_arbitrary();
break;
// end add input
//start remove the book
case 2 :
puts("enter id to remove this book");
int re;
scanf("%d",&re);
del(re);
print_arbitrary();
break;
//end remove the book
//start search for the book by id
case 3 :
puts("enter id to search for the book");
int ser,itre=0;
scanf("%d",&ser);
search_re(ser,itre);
break;
//end search for the book by id
//start search for the book by name
case 4 :
puts("enter your book name for search like:\" name\" without quotes");
getchar();
char str[200];
gets(str);
search_str(str);
break;
//end search for the book by name
//start print all books in sorted order
case 5 :
print_sorted();
break;
//end print all books in sorted order
//start print all books in arbitrary order
case 6 :
print_arbitrary();
//end print all books in arbitrary order
}
//fputs("esmael samir",fptr);
puts("\nenter y|N (continue,exit)");
scanf(" %c",&esmael);
if (esmael!= 'y' || esmael!= 'Y' )
{
// printf("\ncount line = %d",count_line);
rewind(fptr);
for(int i = 0 ; i<count_line ; i++)
{
//printf("8888888\n");
fprintf(fptr,"%d %d",i,sec[i].id);
fprintf(fptr,"%s,",sec[i].name);
fprintf(fptr,"%d\n",sec[i].quantity);
// printf("\n%d\t",sec[i].id);
// printf("%s ",sec[i].name);
// printf("%d",sec[i].quantity);
}
// fputs("well.....",fptr);
}
}
while( esmael== 'y' || esmael== 'Y');
fclose(fptr);
}
return 0 ;
}
// start to add book
void add_input(int add,char *g )
{
sec[index_in_read].id = add; // to append id in struct
char *z = strtok(g,","); // to split name of the book from g
char *c = strtok(NULL,","); // to split quantity of the book from g
strcpy(sec[index_in_read].name, z); // to append name of the book in struct
sec[index_in_read].quantity = atoi(c); // to cast quantity
index_in_read += 1;
count_line+=1;
}
//end to add book
//start delete the book with id
void del(int re)
{
int i;
for ( i =0 ; i< count_line ; i++)
{
if (sec[i].id==re) // to get index the book will delete
{
break;
}
}
for (int j =i+1 ; j< count_line ; j++)
{
sec[j-1].id = sec[j].id ;
sec[j-1].quantity = sec[j].quantity ; // delete the book
strcpy(sec[j-1].name,sec[j].name);
}
count_line -= 1 ;
}
//end delete the book with id
// start search for the book by id
void search_re (int ser,int itre)
{
if(itre>= count_line)
{
puts("NOT FOUND"); // base case
return ;
}
if (sec[itre].id==ser)
{
printf("\n\nid \t name \t\t quantity \n\n");
printf("%d\t %s\t %d\n",sec[itre].id,sec[itre].name,sec[itre].quantity);
return ;
}
search_re ( ser, itre+1 ) ; // call itself again
}
// end search for the book by id
// start search for the book by name
void search_str(char *str)
{
sort ();
int start =0,end=count_line-1,mid;
while ( end >= start) // start binary search
{
mid = (end+start)/2;
int ans_compare = strcmp(str, list[mid]);
if (ans_compare == 0)
{
for(int i = 0 ; i< count_line ; i++)
{
if ( strcmp( list[mid], sec[i].name )==0 )
{
printf("\nid \t name \t\t quantity");
printf("\n\n%d\t",sec[i].id);
printf("%s\t",sec[i].name);
printf("%d\n",sec[i].quantity);
return ;
}
}
}
else if(ans_compare ==-1 )
{
end = mid-1;
}
else
start = mid+1;
}
printf("NOT FOUND\n");
return ;
}
// end search for the book by name
// start print in sorted order
void print_sorted()
{
sort(); // call function sort
printf("\nid \t name \t\t quantity\n\n");
for (int j = 0 ; j < count_line ; j++ )
{
for(int i = 0 ; i< count_line ; i++) // to print all books in sorted order
{
if ( strcmp( list[j], sec[i].name )==0 )
{
printf("%d\t",sec[i].id);
printf("%s\t",sec[i].name);
printf("%d\n",sec[i].quantity);
break;
}
}
}
}
// end print in sorted order
//start print all books in arbitrary order
void print_arbitrary()
{
printf("\nid \t name \t\t\t quantity\n");
for(int i = 0 ; i<count_line ; i++)
{
printf("\n%d\t",sec[i].id);
printf("%s ",sec[i].name);
printf("%d",sec[i].quantity);
}
}
//end print all books in arbitrary order
// start sort the sec[].name
void sort ()
{
for(int i = 0 ; i < count_line ; i++)
{
strcpy(list[i],sec[i].name); // to assign all names in array to sort them
}
for(int i = 0 ; i < count_line ; i++)
{
strcpy( temp, list[i]);
int show=0 ;
for(int k = i+1 ; k < count_line ; k++)
{
if(strcmp(temp, list[k] ) != -1 )
{
strcpy(temp,list[k] ); // to get smallest value in array
show = k;
}
}
if (show != 0)
{
strcpy(temp,list[i] );
strcpy(list[i],list[show] ); // to append smallest value in array
strcpy(list[show],temp );
}
}
}
// end sort the sec[].name