Advertisement
Mahfuz123

Project

Aug 10th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.19 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<stdbool.h>
  5.  
  6. char USER[50];
  7. char pass[50];
  8. char mail[50];
  9. char phone[50];
  10.  
  11. struct book
  12. {
  13. char name[50];
  14. char author[50];
  15. char publish[50];
  16. struct book *next;
  17.  
  18. }*head = NULL;
  19.  
  20. struct List
  21. {
  22. char b_name[50];
  23. char writer[50];
  24. char pbl[50];
  25. struct List *next;
  26.  
  27. }*Start = NULL;
  28.  
  29. void data()
  30. {
  31. struct book *new_book, *current;
  32.  
  33. new_book = (struct book*)malloc(sizeof(struct book));
  34. new_book->next = NULL;
  35.  
  36. strcpy(new_book->name, "Introduction to Algorithms");
  37. strcpy(new_book->author, "Thomas H. Cormen"); // 1st book
  38. strcpy(new_book->publish, "MIT Press");
  39.  
  40. head = new_book;
  41. current = new_book;
  42.  
  43.  
  44. struct book *new_book2 = (struct book*)malloc(sizeof(struct book));
  45. new_book2->next = NULL;
  46.  
  47. strcpy(new_book2->name, "Data Structures and Algorithms Made Easy");
  48. strcpy(new_book2->author, "Narasimha Karumanchi"); // 2nd book
  49. strcpy(new_book2->publish, "CareerMonk");
  50.  
  51. current->next = new_book2;
  52. current = new_book2;
  53.  
  54.  
  55.  
  56. struct book *new_book3 = (struct book*)malloc(sizeof(struct book));
  57. new_book3->next = NULL;
  58.  
  59. strcpy(new_book3->name, "Atomic Habits");
  60. strcpy(new_book3->author, "James Clear"); // 3rd book
  61. strcpy(new_book3->publish, "Penguin Publishing Group");
  62.  
  63.  
  64. current->next = new_book3;
  65. current = new_book3;
  66.  
  67.  
  68.  
  69. struct book *new_book4 = (struct book*)malloc(sizeof(struct book));
  70. new_book4->next = NULL;
  71.  
  72. strcpy(new_book4->name, "Python Crash Course");
  73. strcpy(new_book4->author, "Eric Matthes"); // 4th book
  74. strcpy(new_book4->publish, "No starch press");
  75.  
  76. current->next = new_book4;
  77. current = new_book4;
  78.  
  79.  
  80.  
  81. struct book *new_book5 = (struct book*)malloc(sizeof(struct book));
  82. new_book5->next = NULL;
  83.  
  84. strcpy(new_book5->name, "Operating System Concepts");
  85. strcpy(new_book5->author, "Avi Silberschatz"); // 5th book
  86. strcpy(new_book5->publish, "CareerMonk");
  87.  
  88. current->next = new_book5;
  89. current = new_book5;
  90.  
  91. }
  92.  
  93. void search()
  94. {
  95.  
  96. system("cls");
  97.  
  98. int n;
  99. struct book *current;
  100. char b[50], a[50], p[50];
  101.  
  102. printf("\n\nSearch by\n\n");
  103. printf("1. Book\n2. Author\n3. Publisher\n");
  104.  
  105. scanf("%d", &n);
  106.  
  107. if(n == 1)
  108. {
  109.  
  110. printf("\nEnter book name: ");
  111. scanf(" %[^\n]", b);
  112.  
  113. current = head;
  114.  
  115. while(current != NULL)
  116. {
  117.  
  118. if(strcmp(current->name, b) == 0)
  119. {
  120.  
  121. printf("\n");
  122. printf("Book : %s\n", current->name);
  123. printf("Author : %s\n", current->author);
  124. printf("Publisher: %s\n", current->publish);
  125.  
  126. printf("\n");
  127.  
  128. printf("\nWanna add this to reading list(y/n): ");
  129. char c;
  130. scanf(" %c", &c);
  131.  
  132. if(c == 'y' || c == 'Y')
  133. {
  134.  
  135. readingList(current);
  136. }
  137.  
  138. return;
  139. }
  140.  
  141. current = current->next;
  142. }
  143.  
  144. printf("\nSorry ! We have not found this book\nWould you like to request to add this book?(y/n) ");
  145. char ch;
  146. scanf(" %c", &ch);
  147.  
  148. if(ch == 'y' || ch == 'Y')
  149. {
  150.  
  151. requestBook();
  152. }
  153. else
  154. {
  155.  
  156. return;
  157. }
  158. }
  159. else if(n == 2)
  160. {
  161.  
  162. printf("\nEnter author name: ");
  163. scanf(" %[^\n]", a);
  164.  
  165. current = head;
  166.  
  167. while(current != NULL)
  168. {
  169.  
  170. if(strcmp(current->author, a) == 0)
  171. {
  172.  
  173. printf("\n");
  174. printf("Book : %s\n", current->name);
  175. printf("Author : %s\n", current->author);
  176. printf("Publisher: %s\n", current->publish);
  177.  
  178. printf("\n");
  179.  
  180. printf("\nWanna add this to reading list(y/n): ");
  181. char c;
  182. scanf(" %c", &c);
  183.  
  184. if(c == 'y' || c == 'Y')
  185. {
  186.  
  187. readingList(current);
  188. }
  189. return;
  190. }
  191.  
  192. current = current->next;
  193. }
  194.  
  195. printf("\nSorry ! We have not found this book\nWould you like to request to add this book?(y/n) ");
  196. char ch;
  197. scanf(" %c", &ch);
  198.  
  199. if(ch == 'y' || ch == 'Y')
  200. {
  201.  
  202. requestBook();
  203. }
  204. else
  205. {
  206.  
  207. return;
  208. }
  209. }
  210. else if(n == 3)
  211. {
  212.  
  213. printf("\nEnter publisher name: ");
  214. scanf(" %[^\n]", p);
  215.  
  216. current = head;
  217.  
  218. while(current != NULL)
  219. {
  220.  
  221. if(strcmp(current->publish, p) == 0)
  222. {
  223.  
  224. printf("\n");
  225. printf("Book : %s\n", current->name);
  226. printf("Author : %s\n", current->author);
  227. printf("Publisher: %s\n", current->publish);
  228.  
  229. printf("\n");
  230.  
  231. printf("\nWanna add this to reading list(y/n): ");
  232. char c;
  233. scanf(" %c", &c);
  234.  
  235. if(c == 'y' || c == 'Y')
  236. {
  237.  
  238. readingList(current);
  239. }
  240. return;
  241. }
  242.  
  243. current = current->next;
  244. }
  245.  
  246. printf("\nSorry ! We have not found this book\nWould you like to request to add this book?(y/n) ");
  247. char ch;
  248. scanf(" %c", ch);
  249.  
  250. if(ch == 'y' || ch == 'Y')
  251. {
  252.  
  253. requestBook();
  254. }
  255. else
  256. {
  257.  
  258. return;
  259. }
  260. }
  261. }
  262. struct List *sorting(struct List *a, struct List *b)
  263. {
  264. if (a == NULL)
  265. {
  266. return b;
  267. }
  268.  
  269. else if (b == NULL)
  270. {
  271. return a;
  272. }
  273.  
  274. struct List *result = NULL;
  275.  
  276. char ch[50], c[50], l, r;
  277.  
  278. strcpy(ch, a->b_name);
  279. strcpy(c, b->b_name);
  280.  
  281. l = ch[0];
  282. r = c[0];
  283.  
  284.  
  285. if (l <= r)
  286. {
  287. result = a;
  288. result->next = sorting(a->next, b);
  289. }
  290. else
  291. {
  292. result = b;
  293. result->next = sorting(a, b->next);
  294. }
  295.  
  296. return result;
  297. }
  298.  
  299. void partition(struct List *current, struct List **front, struct List **back)
  300. {
  301. if (current == NULL || current->next == NULL)
  302. {
  303. *front = current;
  304. *back = NULL;
  305. return;
  306. }
  307.  
  308. struct List *last = current;
  309. struct List *first = current->next;
  310.  
  311.  
  312. while (first != NULL)
  313. {
  314. first = first->next;
  315. if (first != NULL)
  316. {
  317. last = last->next;
  318. first = first->next;
  319. }
  320. }
  321.  
  322. *front = current;
  323. *back = last->next;
  324. last->next = NULL;
  325. }
  326.  
  327. void MergeSort(struct List **current)
  328. {
  329. if (*current == NULL || (*current)->next == NULL)
  330. {
  331. return;
  332. }
  333.  
  334. struct List *a;
  335. struct List *b;
  336.  
  337. partition(*current, &a, &b);
  338.  
  339. MergeSort(&a);
  340. MergeSort(&b);
  341.  
  342. *current = sorting(a, b);
  343. }
  344.  
  345. void readingList(struct book *temp)
  346. {
  347. struct List *new_list;
  348.  
  349. new_list = (struct List*)malloc(sizeof(struct List));
  350. new_list->next = NULL;
  351.  
  352. strcpy(new_list->b_name, temp->name);
  353. strcpy(new_list->writer, temp->author);
  354. strcpy(new_list->pbl, temp->publish);
  355.  
  356.  
  357. new_list->next = Start;
  358. Start = new_list;
  359. }
  360.  
  361. void home()
  362. {
  363. struct book *current, *temp;
  364. int i = 0, c = 0, n;
  365. char ch;
  366.  
  367. current = head;
  368. temp = head;
  369. system("cls");
  370.  
  371. printf("\n\t******* Home *******\n\n");
  372.  
  373.  
  374. while(current != NULL)
  375. {
  376. c++;
  377. printf("%d. \n", c);
  378. printf("Book : %s\n\n", current->name);
  379.  
  380. current = current->next;
  381.  
  382. }
  383.  
  384. printf("\nEnter a number for details(0 for main menu): ");
  385. scanf("%d", &n);
  386.  
  387. system("cls");
  388.  
  389. if(n == 0)
  390. {
  391.  
  392. return;
  393. }
  394.  
  395. c = 0;
  396. current = head;
  397.  
  398. printf("\n\n***Details***\n\n");
  399. while(current != NULL)
  400. {
  401.  
  402. c++;
  403.  
  404. if(c == n)
  405. {
  406.  
  407. printf("Book : %s\n", current->name);
  408. printf("Author : %s\n", current->author);
  409. printf("Publisher: %s\n\n", current->publish);
  410.  
  411. break;
  412. }
  413.  
  414. current = current->next;
  415. }
  416.  
  417.  
  418. printf("\nDo you want to add this in reading-list ?(y/n)\n\n");
  419. scanf(" %c", &ch);
  420.  
  421. if(ch == 'y' || ch == 'Y')
  422. {
  423.  
  424. readingList(current);
  425.  
  426. MergeSort(&Start);
  427.  
  428. printf("\n\nAdded to your reading list\n\n");;
  429. }
  430.  
  431. system("pause");
  432. }
  433.  
  434.  
  435. void profile()
  436. {
  437. system("cls");
  438. printf("\n\t<<<<-------- Profile -------->>>>\n\n");
  439. printf("Name : %s\n", USER);
  440. printf("Email: %s\n", mail);
  441. printf("Phone: %s\n\n", phone);
  442. }
  443.  
  444. void showList()
  445. {
  446. struct List *temp;
  447. int c = 0;
  448.  
  449. temp = Start;
  450.  
  451. printf("\n\tReading List:\n\n");
  452.  
  453. while(temp != NULL)
  454. {
  455.  
  456. printf("Book : %s\n", temp->b_name);
  457. printf("Author : %s\n", temp->writer);
  458. printf("Publisher: %s\n\n", temp->pbl);
  459.  
  460. temp = temp->next;
  461. }
  462.  
  463. printf("\n");
  464.  
  465. system("pause");
  466. }
  467.  
  468. void requestBook()
  469. {
  470. struct book *current, *temp, *new_book;
  471.  
  472.  
  473. new_book = (struct book*)malloc(sizeof(struct book));
  474. new_book->next = NULL;
  475.  
  476. printf("\nEnter book name: ");
  477. scanf(" %[^\n]", new_book->name);
  478. printf("Enter author name: ");
  479. scanf(" %[^\n]", new_book->author);
  480. printf("Enter publisher's name: ");
  481. scanf(" %[^\n]", new_book->publish);
  482.  
  483. current = head;
  484.  
  485. while(current->next != NULL)
  486. {
  487. current = current->next;
  488. }
  489.  
  490. current->next = new_book;
  491.  
  492.  
  493. system("cls");
  494. printf("\n\n\t\tThanks for staying with us :-) \n\t\tWe will add this book ASAP\n\n\n");
  495. system("pause");
  496. system("cls");
  497. }
  498. void maintenance()
  499. {
  500.  
  501. int n, i,a;
  502.  
  503. while(1)
  504. {
  505.  
  506.  
  507. printf("\t **Main Menu**\n\tFor\t\t Press\n\n");
  508. printf("\tHome\t\t 0\n");
  509. printf("\tProfile\t\t 1\n");
  510. printf("\tSearch new book\t 2\n");
  511. printf("\tReading List\t 3\n");
  512. printf("\tRequest a book 4\n");
  513. printf("\tExit\t\t 5\n");
  514.  
  515. scanf("%d", &n);
  516.  
  517. if(n == 0)
  518. {
  519.  
  520. home();
  521. system("cls");
  522. }
  523. else if(n == 1)
  524. {
  525.  
  526. profile();
  527. printf("\n");
  528.  
  529. system("pause");
  530. system("cls");
  531. }
  532. else if(n == 2)
  533. {
  534. search();
  535. system("pause");
  536. system("cls");
  537.  
  538. }
  539. else if(n == 3)
  540. {
  541.  
  542. showList();
  543. system("cls");
  544. }
  545. else if(n == 4)
  546. {
  547. requestBook();
  548. }
  549. else if(n==5)
  550. {
  551.  
  552. break;
  553. }
  554. }
  555. }
  556.  
  557. void isValid() // Check a mail is valid or not
  558. {
  559. int a, i, j = 0, b,c, d;
  560. char temp[11], temp1[12];
  561.  
  562. a = strlen(mail) - 10;
  563.  
  564. for(i=a, j = 0; mail[i]!= '\0'; j++, i++)
  565. {
  566.  
  567. temp[j] = mail[i];
  568. }
  569.  
  570. temp[j] = '\0';
  571.  
  572.  
  573. for(i=a-1, j = 0; mail[i]!= '\0'; j++, i++)
  574. {
  575.  
  576. temp1[j] = mail[i];
  577. }
  578.  
  579. temp1[j] = '\0';
  580.  
  581. b = strcmp(temp, "@gmail.com");
  582. c = strcmp(temp1, "@diu.edu.bd");
  583. d = strcmp(temp, "@yahoo.com");
  584.  
  585. if(b == 0 || c == 0 || d == 0)
  586. {
  587.  
  588. return;
  589. }
  590.  
  591. printf("\nPlease enter a valid mail : ");
  592. scanf(" %[^\n]", mail);
  593.  
  594. isValid();
  595. }
  596.  
  597. bool isNum()
  598. {
  599. char no[4];
  600. int i, j, a, b, c, d, e;
  601.  
  602.  
  603. for(i=0; phone[i] != '\0'; i++)
  604. {
  605.  
  606. if(phone[i] < 48 || phone[i] > 57)
  607. {
  608.  
  609. return false;
  610. }
  611. }
  612.  
  613. int len = strlen(phone) - 8;
  614.  
  615. for(i=0, j = 0; i<len; i++, j++)
  616. {
  617.  
  618. no[j] = phone[i];
  619. }
  620.  
  621. no[j] = '\0';
  622.  
  623. a = strcmp(no, "017");
  624. b = strcmp(no, "019");
  625. c = strcmp(no, "013");
  626. d = strcmp(no, "018");
  627. e = strcmp(no, "015");
  628.  
  629. if(a == 0 || b == 0 || c == 0 || d == 0 || e == 0)
  630. {
  631.  
  632. return true;
  633. }
  634.  
  635. return false;
  636. }
  637.  
  638. void validPhone()
  639. {
  640. int length;
  641.  
  642. length = strlen(phone);
  643.  
  644. if(length == 11 && isNum())
  645. {
  646.  
  647. return;
  648. }
  649.  
  650. printf("\nPlease enter a valid no: ");
  651. scanf(" %[^\n]", phone);
  652.  
  653. validPhone();
  654. }
  655. void signup()
  656. {
  657.  
  658. int length = 0, i;
  659.  
  660. printf("\n\n----- SignUp --------\n\n");
  661.  
  662. printf("\nUser Name: ");
  663. scanf(" %[^\n]", USER);
  664.  
  665. printf("\nEmail: ");
  666. scanf(" %[^\n]", mail);
  667.  
  668. isValid();
  669.  
  670. printf("\nPhone number: ");
  671. scanf(" %[^\n]", phone);
  672. validPhone();
  673.  
  674. while(length < 5)
  675. {
  676. printf("\nPassword(must contain 5 letters): ");
  677. scanf(" %[^\n]", pass);
  678.  
  679. length = strlen(pass);
  680. }
  681.  
  682. system("cls");
  683.  
  684. printf("\n\n\t***************** SignUp Successful *****************\n\n");
  685.  
  686. system("pause");
  687. }
  688. void login()
  689. {
  690. int a, b, i, c, d;
  691. char name[50], p[50];
  692.  
  693. system("cls");
  694.  
  695. printf("\n--- Log-In -----\n\n");
  696. printf("\nUser Name: ");
  697. scanf("%s", name);
  698. printf("\nPassword: ");
  699. scanf("%s", p);
  700.  
  701. a = strcmp(name,USER);
  702. b = strcmp(p, pass);
  703.  
  704. if(a== 0 && b == 0)
  705. {
  706. system("cls");
  707. printf("\n\n*************** Welcome ***************\n\n\n");
  708. data();
  709. maintenance();
  710. }
  711. else
  712. {
  713. system("cls");
  714. printf("\n\nUsername/password error\n\n");
  715. system("pause");
  716. login();
  717. }
  718.  
  719. }
  720. int main()
  721. {
  722. printf("\n\n\t\t\t\t----------- Welcome To our E-Library System ----------- \n\n\t\t\t\t\t\t\tTeam: SemiColon\n\n");
  723.  
  724. int a = 0, b, n;
  725. char ch;
  726.  
  727. system("pause");
  728.  
  729. system("cls");
  730.  
  731. printf("\n--- Log-In -----\n\n");
  732.  
  733.  
  734. printf("New user ? (y or n)\n\n");
  735. ch = getchar();
  736. system("cls");
  737.  
  738. if(ch == 'y' || ch == 'Y')
  739. {
  740.  
  741. signup();
  742. login();
  743. a = 1;
  744. }
  745. else if(ch == 'n' || ch == 'N')
  746. {
  747.  
  748. login();
  749. }
  750.  
  751. printf("\n\n\t\t***** Thanks for staying with us *****\n\n");
  752.  
  753. return 0;
  754. }
  755.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement