-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS.c
635 lines (605 loc) · 21.1 KB
/
DS.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX 100000
FILE *fp, *fp1, *fptr;
struct Laptop
{
char brand[15];
char model[25];
int price;
int processor;
char series[25];
int ram;
int storage;
float screenSize;
float weight;
} st[MAX];
void verify_admin();
void main_res();
void getPreferences();
void res(int);
void deleteLaptop();
void modifyLaptop();
void addLaptop();
void admin();
void main_res();
void userRes();
void welcome();
void addLaptop();
void modifyLaptop();
void deleteLaptop();
void display();
void welcome();
void admin();
void main_res();
int totalLaptops = 0;
void oldData()
{
int i = 0;
fp = fopen("laptops.txt", "r");
fp1 = fopen("olddata.txt", "a");
while (fscanf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f", st[i].brand, st[i].model, &st[i].price, &st[i].processor, st[i].series, &st[i].ram, &st[i].storage, &st[i].screenSize, &st[i].weight) != EOF)
{
fprintf(fp1, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f\n", st[i].brand, st[i].model, st[i].price, st[i].processor, st[i].series, st[i].ram, st[i].storage, st[i].screenSize, st[i].weight);
i++;
}
fclose(fp1);
fclose(fp);
}
void sortLaptops()
{
// Read laptops from laptops.txt file into memory
struct Laptop laptops[1000];
fp = fopen("laptops.txt", "r");
if (fp == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
int i = 0;
char tempBrand[15], tempModel[25], tempSeries[25];
int price, processor, ram, storage;
float screenSize, weight;
while (fscanf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f", &tempBrand, &tempModel, &price, &processor, &tempSeries, &ram, &storage, &screenSize, &weight) != EOF)
{
strcpy(laptops[i].brand, tempBrand);
strcpy(laptops[i].model, tempModel);
laptops[i].price = price;
laptops[i].processor = processor;
strcpy(laptops[i].series, tempSeries);
laptops[i].ram = ram;
laptops[i].storage = storage;
laptops[i].screenSize = screenSize;
laptops[i].weight = weight;
i++;
}
fclose(fp);
totalLaptops = i;
// Sort laptops array by price using bubble sort
int j;
struct Laptop temp;
for (i = 0; i < totalLaptops - 1; i++)
{
for (j = 0; j < totalLaptops - i - 1; j++)
{
if (laptops[j].price > laptops[j + 1].price)
{
temp = laptops[j];
laptops[j] = laptops[j + 1];
laptops[j + 1] = temp;
}
}
}
// Write sorted laptops back to laptops.txt file
fptr = fopen("laptops.txt", "w");
if (fptr == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
for (i = 0; i < totalLaptops; i++)
{
fprintf(fptr, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f\n", laptops[i].brand, laptops[i].model, laptops[i].price, laptops[i].processor, laptops[i].series, laptops[i].ram, laptops[i].storage, laptops[i].screenSize, laptops[i].weight);
}
fclose(fptr);
printf("Laptops sorted successfully by budget!\n");
}
}
}
void getPreferences()
{
system("cls");
int choice;
printf("\n----------------------------------------------------------------------------\n");
printf(" Welcome to Laptop Recommendation Portal");
printf("\n----------------------------------------------------------------------------\n");
int budget, processor, ram, storage;
int maxPrice, minPrice;
int laptopsFound = 0;
printf("Enter your budget: ");
scanf("%d", &budget);
printf("Enter processor (in GHz): ");
scanf("%s", &processor);
printf("Enter RAM (in GB): ");
scanf("%d", &ram);
printf("Enter storage (in GB): ");
scanf("%d", &storage);
// Read laptops from laptops.txt file and check for matches with user preferences
fp = fopen("laptops.txt", "r");
if (fp == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
char brand[15], model[25], ser[25];
int price, proc, r, stor;
float screen, wt;
while (fscanf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f", &brand, &model, &price, &proc, &ser, &r, &stor, &screen, &wt) != EOF)
{
if (proc >= processor && r >= ram && stor >= storage && price <= budget)
{
printf("\nLaptop found: %s %s", brand, model);
printf("\nPrice: %d", price);
printf("\nProcessor: %d GHz", proc);
printf("\nRAM: %d GB", r);
printf("\nStorage: %d GB", stor);
printf("\nScreen Size: %d inches", screen);
printf("\nWeight: %d lbs\n", wt);
laptopsFound++;
}
}
fclose(fp);
// If no matches found, suggest 5 laptops near to budget
if (laptopsFound == 0)
{
printf("\nSorry, we couldn't find a perfect match for your preferences.\n");
printf("Here are laptops near to your budget:\n");
printf("----------------------------------------------------------------------------");
minPrice = budget - 1000;
maxPrice = budget + 1000;
fp = fopen("laptops.txt", "r");
if (fp == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
int laptopsSuggested = 0;
while (fscanf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f", &brand, &model, &price, &proc, &ser, &r, &stor, &screen, &wt) != EOF)
{
if (price >= minPrice && price <= maxPrice && laptopsSuggested < 5)
{
printf("\n%s %s\nPrice: %d", brand, model, price);
laptopsSuggested++;
}
}
fclose(fp);
if (laptopsSuggested == 0)
{
printf("\nSorry, we couldn't find any laptops near to your budget. Please try again with a different budget.\n");
}
}
}
}
userRes();
}
void userRes()
{
char add;
printf("\n----------------------------------------------------------------------------\n");
printf("\n\tWant to Search Another Laptop \n");
printf("Enter(y/n) :\t");
scanf(" %c", &add);
printf("\n----------------------------------------------------------------------------\n");
if (add == 'y')
getPreferences();
else if (add == 'n')
{
main_res();
}
}
void welcome()
{
system("cls");
int choice;
printf("\n----------------------------------------------------------------------------\n");
printf(" Welcome to Laptop Recommendation Portal");
printf("\n----------------------------------------------------------------------------\n");
printf("\n\tSelect Mode as follows: \n\t\t1.Admin\n\t\t2.Customer\n\t\t3.Exit");
printf("\n----------------------------------------------------------------------------\n");
printf("\t\tEnter (1/2) :\t");
scanf("%d", &choice);
printf("----------------------------------------------------------------------------\n");
printf("\n");
if (choice == 1)
{
verify_admin();
admin();
}
else if (choice == 2)
getPreferences();
else if (choice == 3)
{
printf("\t *********Laptop Recommendation Portal Terminated*********\n\n");
}
else
{
printf("Select valid choice");
welcome();
}
}
void main_res()
{
char Main_res;
printf("\tWant to Go to Main Portal-->\n \t\tEnter(y/n) : ");
scanf(" %c", &Main_res);
printf("----------------------------------------------------------------------------\n\n");
if (Main_res == 'y')
welcome();
else if (Main_res == 'n')
printf("\t *********Laptop Recommendation Portal Terminated*********\n\n\n");
}
void res(int num)
{
char Admin_res, Main_res;
if (num == 0)
{
printf("Want to Go to Admin Mode :\n\tEnter(y/n) :\t");
scanf(" %c", &Admin_res);
if (Admin_res == 'y')
admin();
else if (Admin_res == 'n')
main_res();
}
}
void addLaptop()
{
char Admin_res, Main_res, add;
int add_num;
float screenSize, weight;
// Append new laptop information to laptops.txt file
fp = fopen("laptops.txt", "a");
if (fp == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
printf("\n----------------------------------------------------------------------------\n");
printf("\tNo. of Laptops do you want to add : ");
scanf("%d", &add_num);
printf("----------------------------------------------------------------------------\n");
for (int i = 0; i < add_num; i++)
{
printf("Enter brand: ");
scanf("%s", st[i].brand);
printf("Enter model: ");
scanf("%s", st[i].model);
printf("Enter price: ");
scanf("%d", &st[i].price);
printf("Processor:\n\t1.Intel I3 or Ryzen 3\n\t2.Intel I5 or Ryzen 5\n\t3.IntelI7 or Ryzen 7\n\t\tEnter(1/2/3) :\t");
scanf("%d", &st[i].processor);
printf("Enter Specific Series: ");
scanf("%s", st[i].series);
printf("Enter RAM (in GB): ");
scanf("%d", &st[i].ram);
printf("Enter storage (in GB): ");
scanf("%d", &st[i].storage);
printf("Enter screen size (in inches): ");
scanf("%f", &st[i].screenSize);
printf("Enter weight (in KG): ");
scanf("%f", &st[i].weight);
printf("\n----------------------------------------------------------------------------\n");
fprintf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f\n", st[i].brand, st[i].model, st[i].price, st[i].processor, st[i].series, st[i].ram, st[i].storage, st[i].screenSize, st[i].weight);
}
}
fclose(fp);
printf("\nLaptop added successfully!\n");
oldData();
sortLaptops();
printf("----------------------------------------------------------------------------\n");
printf("\tWant to Add More Entries of Laptop");
printf("\n----------------------------------------------------------------------------\n");
printf("\tEnter(y/n) :\t");
scanf(" %c", &add);
printf("\n----------------------------------------------------------------------------\n");
if (add == 'y')
addLaptop();
else if (add == 'n')
res(0);
}
void modifyLaptop()
{
oldData();
fp = fopen("laptops.txt", "r");
fp1 = fopen("tempModify.txt", "w"); // temprary file
if (fp == NULL && fp1 == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
int i = 0;
int flag = 0;
char brand[15], model[25], series[25], add;
int price, processor, ram, storage;
float screenSize, weight;
printf("Enter brand of laptop to modify: ");
scanf("%s", brand);
printf("Enter model of laptop to modify: ");
scanf("%s", model);
// Read laptops from laptops.txt file into memory
while (fscanf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f", st[i].brand, st[i].model, &st[i].price, &st[i].processor, st[i].series, &st[i].ram, &st[i].storage, &st[i].screenSize, &st[i].weight) != EOF)
{
if (strcmp(st[i].brand, brand) == 0 && strcmp(st[i].model, model) == 0)
{
printf("Enter new price: ");
scanf("%d", &price);
printf("Processor:\n\t1.Intel I3 or Ryzen 3\n\t2.Intel I5 or Ryzen 5\n\t3.IntelI7 or Ryzen 7\n\t\tEnter(1/2/3) :\t");
scanf("%d", &processor);
printf("Enter Specific Series of Processor:");
scanf("%s", &series);
printf("Enter new RAM (in GB): ");
scanf("%d", &ram);
printf("Enter new storage (in GB): ");
scanf("%d", &storage);
printf("Enter new screen size (in inches): ");
scanf("%f", &screenSize);
printf("Enter new weight (in lbs): ");
scanf("%f", &weight);
// writing in temporary file
fprintf(fp1, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f\n", st[i].brand, st[i].model, price, processor, series, ram, storage, screenSize, weight);
printf("Laptop modified successfully!\n");
flag = 1;
}
else
fprintf(fp1, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f\n", st[i].brand, st[i].model, st[i].price, st[i].processor, st[i].series, st[i].ram, st[i].storage, st[i].screenSize, st[i].weight);
i++;
}
if (flag == 0)
{
printf("Details not Matched !\n\tNot Found\n");
}
fclose(fp);
fclose(fp1);
// Write laptops back to laptops.txt file
fp = fopen("laptops.txt", "w");
fp1 = fopen("tempModify.txt", "r");
if (fp == NULL && fp1 == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
i = 0;
while (fscanf(fp1, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f", st[i].brand, st[i].model, &st[i].price, &st[i].processor, st[i].series, &st[i].ram, &st[i].storage, &st[i].screenSize, &st[i].weight) != EOF)
{
fprintf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f\n", st[i].brand, st[i].model, st[i].price, st[i].processor, st[i].series, st[i].ram, st[i].storage, st[i].screenSize, st[i].weight);
i++;
}
}
fclose(fp);
fclose(fp1);
sortLaptops();
printf("\n----------------------------------------------------------------------------\n");
printf("\n\tWant to Modify More Entries of Laptop \n");
printf("Enter(y/n) :\t");
scanf(" %c", &add);
printf("\n----------------------------------------------------------------------------\n");
if (add == 'y')
modifyLaptop();
else if (add == 'n')
res(0);
}
}
void deleteLaptop()
{
char add;
fp = fopen("laptops.txt", "r");
fp1 = fopen("tempDelete.txt", "w"); // temprary file
if (fp == NULL && fp1 == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
int i = 0;
int flag = 0;
char brand[15], model[25], series[25];
int price, processor, ram, storage;
float screenSize, weight;
printf("Enter brand of laptop to modify: ");
scanf("%s", brand);
printf("Enter model of laptop to modify: ");
scanf("%s", model);
// Read laptops from laptops.txt file into memory
while (fscanf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f", st[i].brand, st[i].model, &st[i].price, &st[i].processor, st[i].series, &st[i].ram, &st[i].storage, &st[i].screenSize, &st[i].weight) != EOF)
{
if (strcmp(st[i].brand, brand) == 0 && strcmp(st[i].model, model) == 0)
{
printf("Laptop Found & Deleted Successfully");
flag = 1;
}
else
fprintf(fp1, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f\n", st[i].brand, st[i].model, st[i].price, st[i].processor, st[i].series, st[i].ram, st[i].storage, st[i].screenSize, st[i].weight);
i++;
}
if (flag == 0)
{
printf("Details not Matched !\n Not Found");
}
fclose(fp);
fclose(fp1);
// Write laptops back to laptops.txt file
fp = fopen("laptops.txt", "w");
fp1 = fopen("tempDelete.txt", "r");
if (fp == NULL && fp1 == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
i = 0;
while (fscanf(fp1, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f", st[i].brand, st[i].model, &st[i].price, &st[i].processor, st[i].series, &st[i].ram, &st[i].storage, &st[i].screenSize, &st[i].weight) != EOF)
{
fprintf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f\n", st[i].brand, st[i].model, st[i].price, st[i].processor, st[i].series, st[i].ram, st[i].storage, st[i].screenSize, st[i].weight);
i++;
}
}
fclose(fp);
fclose(fp1);
sortLaptops();
}
printf("\n----------------------------------------------------------------------------\n");
printf("\n\tWant to Delete More Entries of Laptop \n");
printf("Enter(y/n) :\t");
scanf(" %c", &add);
printf("\n----------------------------------------------------------------------------\n");
if (add == 'y')
deleteLaptop();
else if (add == 'n')
res(0);
}
void display()
{
int i;
fp = fopen("laptops.txt", "r");
if (fp == NULL)
{
printf("\n\t\tFile opening error");
exit(0);
}
else
{
i = 0;
printf("\n\tBrand\tModel\tPrice\tProcessor Series\tRam\tStorage\tScreen \tWeight");
printf("\n-----------------------------------------------------------------------------------------------------\n");
while (fscanf(fp, "%s\t%s\t%d\t%d\t%s\t%d\t%d\t%f\t%f", st[i].brand, st[i].model, &st[i].price, &st[i].processor, st[i].series, &st[i].ram, &st[i].storage, &st[i].screenSize, &st[i].weight) != EOF)
{
printf("\t%s\t%s\t%d\t%d %s \t%d\t%d\t%f\t%f\n", st[i].brand, st[i].model, st[i].price, st[i].processor, st[i].series, st[i].ram, st[i].storage, st[i].screenSize, st[i].weight);
i++;
}
fclose(fp);
}
printf("\n");
}
void admin()
{
int choice, num;
// Admin mode
system("cls");
printf("\n----------------------------------------------------------------------------\n");
printf(" Entered in Admin Mode");
printf("\n----------------------------------------------------------------------------\n");
printf("\n\tSelect an option: \n\t1. Add a laptop\n\t2. Modify a laptop\n\t3. Delete a laptop\n\t4. Display a laptop\n\t5. Main Menu\n");
printf("----------------------------------------------------------------------------\n");
printf("\tEnter (1/2/3/4/5) :\t");
scanf("%d", &choice);
printf("----------------------------------------------------------------------------\n");
if (choice == 1)
{
addLaptop();
}
else if (choice == 2)
{
modifyLaptop();
}
else if (choice == 3)
{
deleteLaptop();
}
else if (choice == 4)
{
num = 0;
display();
res(num);
}
else if (choice == 5)
{
main_res();
}
else
{
printf("Invalid choice\n");
admin();
}
}
void verify_admin()
{
char ch;
char Admin_name[20], Admin_pass[20], saved_name[20] = "Nikhil", saved_pass[20] = "NK123@";
name:
printf("Enter Username : \t");
scanf("%s", Admin_name);
fptr = fopen("Admin.txt", "r");
// {
// fprintf(fptr,"%s\t%s",saved_name,saved_pass);
// }
// fclose(fptr);
fscanf(fptr, "%s\t%s", saved_name, saved_pass);
if (strcmp(Admin_name, saved_name) != 0)
{
printf("\n\tIncorrect Username\n \tPlease Try Again\n\n");
goto name;
}
else
{
start:
printf("Enter Password : \t");
int i = 0;
while (1)
{
ch = getch();
if (ch == 13) // Ascii valur of Enter
{
Admin_pass[i] = '\0';
break;
}
else if (ch == 8) // AsCII value of Backspace
{
if (i > 0)
{
i--;
printf("\b \b");
}
}
else if (ch == 9 || ch == 32) // ASCII value of tab and space
{
continue;
}
else
{
Admin_pass[i++] = ch;
printf("*");
}
}
int l = strcmp(Admin_pass, saved_pass);
if (l == 0)
{
printf("\n#### LOGIN SUCCESSFULL ####\n");
}
else
{
printf("\n\n\tINCORRECT PASSWORD\n\n");
goto start;
}
}
}
int main()
{
welcome();
}