Advertisement
ppupil2

Assignment-B.cpp

Mar 13th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #define MAX_book 100    // max numbers of book
  6. #define SPACE_menu 30   // adjust space of menu Book in stock
  7.  
  8. struct bookstore {
  9.     char code[11];
  10.     char title[31];
  11.     int quantity;
  12.     double price;
  13.     double value;
  14. } bkinstck[MAX_book];
  15.  
  16. void print_space(int x); /* print x times ' ' */
  17. bool dupcheck_code(char a[11], int k); /* check duplicate a in array bkinstck[].code which have k elements */
  18. bool dupcheck_title(char a[31], int k); /* check duplicate a in array bkinstck[].title which have k elements */
  19. void input_code(int i); /* input bkinstck[i].code */
  20. void input_title(int i); /* input bkinstck[i].title */
  21. void input_quantity(int i); /* input bkinstck[i].quantity */
  22. void input_price(int i); /* input bkinstck[i].price */
  23. void value(int i); /* calculate bkinstck[i].value */
  24. double value_total(int k); /* calculate total value */
  25. void enterlist(double &total, int &k); /* enter books in array bkinstck[] which have k elements */
  26. void display_header(void); /* display the header of table */
  27. void display_1row(int i); /* display row bkinstck[i] */
  28. void display_full(double total, int k); /* display full table of books */
  29. void swap(int a, int b); /* swap book at position bkinstck[a] and bkinstck[b] */
  30. void sortASC(int k); /* sort book in bkinstck[] in ascending order */
  31. int loadf(int &line, int &k); /* return error code at line number *line when load data from file */
  32. int loadfile(double &total, int k); /* load data from file bookstore.txt */
  33. void fexport(double total, int k); /* Export data to file */
  34. void fstmax(int k); /* find first max in bkinstck[].price which have k elements and display it */
  35. void display_fheader(void); /* display the header of find table */
  36. bool find(int k, int pos[], int &j); /* find and display the list which are matched with code */
  37. void edit_code(int i, int k); /* edit bkinstck[i].code */
  38. void edit_title(int i, int k); /* edit bkinstck[i].title */
  39. void edit_quantity(int i); /* edit bkinstck[i].quantity */
  40. void edit_price(int i); /* edit bkinstck[i].price */
  41. void findnedit(double &total, int k, int pos[], int j); /* find and edit book */
  42. void delele_book(int i, int &k); /* delete book bkinstck[i] in array bkinstck[] which have k elements */
  43. void findndelete(double &total, int &k, int pos[], int j); /* find and delete book */
  44. void info(void); /* display infomation of group */
  45. void menu(void); /* display menu Books in stock */
  46.  
  47. int main() {
  48.     /* we use an array to store information of the list which are founed by code
  49.         pos[i]
  50.         i: number from 0 to the max of choice
  51.         value of pos[i] = k mean: bkinstck[k].xxx                  */
  52.     int pos[MAX_book];
  53.     int choice;
  54.     char ch;
  55.     int k = 0; // number of books in bkinstck[MAX_book]
  56.     int j = 0; // number of books were found in number[MAX_book] = j+1
  57.     double total; // total value of books  
  58.    
  59.     while (1) {
  60.         while (1) {
  61.             system("cls");
  62.             menu();
  63.             printf("\n");
  64.             print_space(SPACE_menu);
  65.             printf("Enter your selection (0 -> 9): ");
  66.             fflush(stdin);
  67.             ch = '\0';
  68.             scanf("%d%c", &choice, &ch);
  69.             if (choice >= 0 && choice <= 9 && ch == '\n') {
  70.                 break;
  71.             }
  72.             print_space(SPACE_menu);
  73.             printf("Your selection is incorrect\n");
  74.             printf("\n\n");
  75.             printf("Press any key to continue...");
  76.             getch();
  77.         }
  78.         printf("\n");
  79.        
  80.         if (choice == 0) {
  81.             break;
  82.         }
  83.  
  84.         switch (choice) {
  85.             case 1: {
  86.                 enterlist(total, k);
  87.                 break;
  88.             }
  89.             case 2: {
  90.                 if (k == 0) {
  91.                     print_space(SPACE_menu);
  92.                     printf("Please input data first.\n");
  93.                 }
  94.                 else display_full(total, k);
  95.                 break;
  96.             }
  97.             case 3: {
  98.                 if (k == 0) {
  99.                     print_space(SPACE_menu);
  100.                     printf("Please input data first.\n");
  101.                 }
  102.                 else sortASC(k);
  103.                 break;
  104.             }
  105.             case 4: {
  106.                 k = loadfile(total, k);
  107.                 break;
  108.             }
  109.             case 5: {
  110.                 if (k == 0) {
  111.                     print_space(SPACE_menu);
  112.                     printf("Please input data first.\n");
  113.                 }
  114.                 else fexport(total, k);
  115.                 break;
  116.             }
  117.             case 6: {
  118.                 if (k == 0) {
  119.                     print_space(SPACE_menu);
  120.                     printf("Please input data first.\n");
  121.                 }
  122.                 else fstmax(k);
  123.                 break;
  124.             }
  125.             case 7: {
  126.                 if (k == 0) {
  127.                     print_space(SPACE_menu);
  128.                     printf("Please input data first.\n");
  129.                 }
  130.                 else findnedit(total, k, pos, j);
  131.                 break;
  132.             }
  133.             case 8: {
  134.                 if (k == 0) {
  135.                     print_space(SPACE_menu);
  136.                     printf("Please input data first.\n");
  137.                 }
  138.                 else findndelete(total, k, pos, j);
  139.                 break;
  140.             }
  141.             case 9: {
  142.                 info();
  143.                 break;
  144.             }
  145.             default: {
  146.                 printf("Your selection is incorrect\n");
  147.                 break;
  148.             }
  149.         }
  150.         printf("\n");
  151.         printf("Press any key to continue...");
  152.         getch();
  153.     }
  154.  
  155.     return (0);
  156. }
  157.  
  158. void print_space(int x) { /* print x times ' ' */
  159.     for (int i = 1; i <= x; i++) {
  160.         printf(" ");
  161.     }
  162. }
  163.  
  164. bool dupcheck_code(char a[11], int k) { /* check duplicate a in array bkinstck[].code which have k elements */
  165.     for (int i = 0; i<k; i++) {
  166.         if (strcmp(bkinstck[i].code, a) == 0) {
  167.             return(true);
  168.         }
  169.     }
  170.     return(false);
  171. }
  172.  
  173. bool dupcheck_title(char a[31], int k) { /* check duplicate a in array bkinstck[].title which have k elements */
  174.     for (int i = 0; i<k; i++) {
  175.         if (strcmp(bkinstck[i].title, a) == 0) {
  176.             return(true);
  177.         }
  178.     }
  179.     return(false);
  180. }
  181.  
  182. void input_code(int i) { /* input bkinstck[i].code */
  183.     char a[11];
  184.    
  185.     while (1) {
  186.         print_space(SPACE_menu/2); // adjust space on the screen.
  187.         printf(" Enter code (enter STOP to stop): ");
  188.         fflush(stdin);
  189.         gets(a);
  190.         if ((strlen(a) > 0) && (strlen(a) <= 10)) {
  191.             if (dupcheck_code(a, i) == true) {
  192.                 print_space(SPACE_menu/2); // adjust space on the screen.
  193.                 printf(" The code %s already exists, please re-enter!\n", a);
  194.             }
  195.             else {
  196.                 strcpy(bkinstck[i].code, a);
  197.                 break;
  198.             }
  199.         }
  200.         else {
  201.             print_space(SPACE_menu/2); // adjust space on the screen.
  202.             printf(" Invalid code, please re-enter!\n");
  203.         }
  204.     }
  205. }
  206.  
  207. void input_title(int i) { /* input bkinstck[i].title */
  208.     char a[31];
  209.    
  210.     while (1) {
  211.         print_space(SPACE_menu/2); // adjust space on the screen.
  212.         printf(" Enter title: ");
  213.         fflush(stdin);
  214.         gets(a);
  215.         if ((strlen(a) > 0) && (strlen(a) <= 31)) {
  216.             if (dupcheck_title(a, i) == true) {
  217.                 print_space(SPACE_menu/2); // adjust space on the screen.
  218.                 printf(" The title %s already exists, please re-enter!\n", a);
  219.             }
  220.             else {
  221.                 strcpy(bkinstck[i].title, a);
  222.                 break;
  223.             }
  224.         }
  225.         else {
  226.             print_space(SPACE_menu/2); // adjust space on the screen.
  227.             printf(" Invalid title, please re-enter!\n");
  228.         }
  229.     }
  230. }
  231.  
  232. void input_quantity(int i) { /* input bkinstck[i].quantity */
  233.     char ch;
  234.    
  235.     while (1) {
  236.         print_space(SPACE_menu/2); // adjust space on the screen.
  237.         printf(" Enter quantity: ");
  238.         fflush(stdin);
  239.         ch = '\0';
  240.         scanf("%d%c", &bkinstck[i].quantity, &ch);
  241.         if ((bkinstck[i].quantity > 0) && (ch == '\n')) {
  242.             break;
  243.         }
  244.         else {
  245.             print_space(SPACE_menu/2); // adjust space on the screen.
  246.             printf(" Invalid quantity, please re-enter!\n");
  247.         }
  248.     }
  249. }
  250.  
  251. void input_price(int i) { /* input bkinstck[i].price */
  252.     char ch;
  253.    
  254.     while (1) {
  255.         print_space(SPACE_menu/2); // adjust space on the screen.
  256.         printf(" Enter price: ");
  257.         fflush(stdin);
  258.         ch = '\0';
  259.         scanf("%lf%c", &bkinstck[i].price, &ch);
  260.         if ((bkinstck[i].price > 0) && (ch == '\n')) {
  261.             break;
  262.         }
  263.         else {
  264.             print_space(SPACE_menu/2); // adjust space on the screen.
  265.             printf(" Invalid price, please re-enter!\n");
  266.         }
  267.     }
  268. }
  269.  
  270. void value(int i) { /* calculate bkinstck[i].value */
  271.     bkinstck[i].value = bkinstck[i].quantity*bkinstck[i].price;
  272. }
  273.  
  274. double value_total(int k) { /* calculate total value */
  275.     double total = 0;
  276.    
  277.     for (int i = 0; i<k; i++) {
  278.         total += bkinstck[i].value;
  279.     }
  280.     return(total);
  281. }
  282.  
  283. void enterlist(double &total, int &k) { /* enter books in array bkinstck[] which have k elements */
  284.     int i = 1;
  285.     char stop[5] = "STOP";
  286.    
  287.     while (1) {
  288.         print_space(SPACE_menu/2); // adjust space on the screen.
  289.         printf("Book %d:\n", i);
  290.         input_code(k);
  291.         if  (strcmp(bkinstck[k].code, stop) == 0) {
  292.             break;
  293.         }
  294.         input_title(k);
  295.         input_quantity(k);
  296.         input_price (k);
  297.         value(k);
  298.         i++;
  299.         k++;
  300.     }
  301.     total = value_total(k);
  302. }
  303.  
  304. void display_header(void) { /* display the header of table */
  305.     print_space(SPACE_menu/2); // adjust space on the screen.
  306.     printf("+------------+--------------------------------+----------+------------+------------+\n");
  307.     print_space(SPACE_menu/2); // adjust space on the screen.
  308.     printf("| %-10s | %-30s | %8s | %10s | %10s |\n", "Code", "Title", "Quantity", "Price", "Value");
  309.     print_space(SPACE_menu/2); // adjust space on the screen.
  310.     printf("+------------+--------------------------------+----------+------------+------------+\n");
  311. }
  312.  
  313. void display_1row(int i) { /* display row bkinstck[i] */
  314.     printf("| %-10s | %-30s | %8d | %10.1lf | %10.1lf |\n", bkinstck[i].code, bkinstck[i].title, bkinstck[i].quantity, bkinstck[i].price, bkinstck[i].value);
  315. }
  316.  
  317. void display_full(double total, int k) { /* display full table of books */
  318.  
  319.     display_header();
  320.     for (int i = 0; i<k; i++) {
  321.         print_space(SPACE_menu/2); // adjust space on the screen.
  322.         display_1row(i);
  323.     }
  324.     print_space(SPACE_menu/2); // adjust space on the screen.
  325.     printf("+------------+--------------------------------+----------+------------+------------+\n");
  326.    
  327.     print_space(SPACE_menu/2); // adjust space on the screen.
  328.     printf("|                                                          Total value: %-10.1lf |\n", total);
  329.     print_space(SPACE_menu/2); // adjust space on the screen.
  330.     printf("+----------------------------------------------------------------------------------+\n");
  331. }
  332.  
  333. void swap(int a, int b) { /* swap book at position bkinstck[a] and bkinstck[b] */
  334.     char temp_code[11];
  335.     char temp_title[31];
  336.     int temp_quantity;
  337.     double temp_price;
  338.     double temp_value;
  339.    
  340.     /* swap code */
  341.     strcpy(temp_code, bkinstck[a].code);
  342.     strcpy(bkinstck[a].code, bkinstck[b].code);
  343.     strcpy(bkinstck[b].code, temp_code);
  344.    
  345.     /* swap title */
  346.     strcpy(temp_title, bkinstck[a].title);
  347.     strcpy(bkinstck[a].title, bkinstck[b].title);
  348.     strcpy(bkinstck[b].title, temp_title);
  349.    
  350.     /* swap quantity */
  351.     temp_quantity = bkinstck[a].quantity;
  352.     bkinstck[a].quantity = bkinstck[b].quantity;
  353.     bkinstck[b].quantity = temp_quantity;
  354.    
  355.     /* swap price */
  356.     temp_price = bkinstck[a].price;
  357.     bkinstck[a].price = bkinstck[b].price;
  358.     bkinstck[b].price = temp_price;
  359.    
  360.     /* swap value */
  361.     temp_value = bkinstck[a].value;
  362.     bkinstck[a].value = bkinstck[b].value;
  363.     bkinstck[b].value = temp_value;
  364. }
  365.  
  366. void sortASC(int k) { /* sort book in bkinstck[] in ascending order */
  367.     for (int i = 1; i < k-1; i++) {
  368.         for (int j = 0; j < k-i; j++) {
  369.             if (strcmp(bkinstck[j].code, bkinstck[j+1].code) > 0) {
  370.                 swap(j, j+1);
  371.             }
  372.         }
  373.     }
  374.     print_space(SPACE_menu/2); // adjust space on the screen.
  375.     printf("Successfull.\n");
  376. }
  377.  
  378. int loadf(int &line, int &k) { /* return error code at line number *line when load data from file */
  379.     FILE *fp;
  380.     char header[3][72], last[72], tcode[11], ttitle[31], temp[100];
  381.     int val, tquantity;
  382.     double tprice;
  383.     /*
  384.         funtion return as a Error code:
  385.         1 [Error] file does not exists!
  386.         2 [Error] file empty!
  387.         3 [Error] Invalid format (line %d).
  388.         4 [Error] Code duplicated (line %d).
  389.         5 [Error] Title duplicated (line %d).
  390.         6 [Error] Invalid quantity (line %d).
  391.         7 [Error] Invalid price (line %d).
  392.     */
  393.    
  394.     line = 1; val = 0; // val store return value of fscanf
  395.     fp = fopen("bookstore.txt", "r");
  396.     if (fp == NULL) {
  397.         fclose(fp);
  398.         return 1;
  399.     }
  400.     else {
  401.         if (fscanf(fp, "%s", temp) == EOF) {
  402.             fclose(fp);
  403.             return 2;
  404.         }
  405.         else rewind(fp); //turn back to begining of file
  406.         while (!feof(fp)) {
  407.             val = 0;
  408.             val = fscanf(fp, "%71[^\n]\n", header[0]); // line 1
  409.             if (val == EOF || (strcmp(header[0], "+------------+--------------------------------+----------+------------+") != 0)) {
  410.                 fclose(fp);
  411.                 return 3;
  412.             }
  413.             else {
  414.                 line++; val = 0;
  415.                 val = fscanf(fp, "%71[^\n]\n", header[1]); // line 2
  416.                 if (val == EOF || strcmp(header[1], "| Code       | Title                          | Quantity |      Price |") != 0) {
  417.                     fclose(fp);
  418.                     return 3;
  419.                 }
  420.                 else {
  421.                     line++; val = 0;
  422.                     val = fscanf(fp, "%71[^\n]\n", header[2]); // line 3
  423.                     if (val == EOF || strcmp(header[2], "+------------+--------------------------------+----------+------------+") != 0) {
  424.                         fclose(fp);
  425.                         return 3;
  426.                     }
  427.                     else {
  428.                         line++;
  429.                         while (1) {
  430.                             val = 0;
  431.                             val = fscanf(fp, "| %10[^ |] | %30[^ |] | %8d | %10lf |\n", tcode, ttitle, &tquantity, &tprice);
  432.  
  433.                             if (val == 4) {
  434.                                 if (strlen(tcode) > 0 && strlen(tcode) <= 10 && dupcheck_code(tcode, k) == true) {
  435.                                     fclose(fp);
  436.                                     return 4;
  437.                                 }
  438.                                 else if (strlen(ttitle) > 0 && strlen(ttitle) <= 30 && dupcheck_title(ttitle, k) == true) {
  439.                                     fclose(fp);
  440.                                     return 5;  
  441.                                 }
  442.                                 else if (tquantity < 0) {
  443.                                     fclose(fp);
  444.                                     return 6;
  445.                                 }
  446.                                 else if (tprice < 0) {
  447.                                     fclose(fp);
  448.                                     return 7;
  449.                                 }
  450.                                 else {
  451.                                     strcpy(bkinstck[k].code, tcode);
  452.                                     strcpy(bkinstck[k].title, ttitle);
  453.                                     bkinstck[k].quantity = tquantity;
  454.                                     bkinstck[k].price = tprice;            
  455.                                     k++;
  456.                                 }
  457.                             }
  458.                             else {
  459.                                 val = 0;
  460.                                 val = fscanf(fp, "%s", last);
  461.                                 if (val == EOF || strcmp(last, "+------------+--------------------------------+----------+------------+") != 0) {
  462.                                     fclose(fp);
  463.                                     return 3;
  464.                                 }
  465.                                 else {
  466.                                     fclose(fp);
  467.                                     return 0;
  468.                                 }
  469.                             }
  470.                             line++;
  471.                         }
  472.                     }
  473.                 }
  474.             }
  475.         }
  476.     }
  477. }
  478.  
  479. int loadfile(double &total, int k) { /* load data from file bookstore.txt */
  480.     int f, line = 0; // error code at [line] when load file. if f = 0 means no error
  481.     int frstk = k;
  482.  
  483.     print_space(SPACE_menu/2); // adjust space on the screen.
  484.     printf("Load data from file bookstore.txt:\n");
  485.     f = loadf(line, k);
  486.     for (int i = 0; i<k; i++) {
  487.         value(i);
  488.     }
  489.     total = value_total(k);
  490.    
  491.     if (f == 0) {
  492.         print_space(SPACE_menu/2); // adjust space on the screen.
  493.         printf("Load successfully %d books from file bookstore.txt!\n", k - frstk);
  494.     }
  495.  
  496.     switch (f) {
  497.         case 1: {
  498.             print_space(SPACE_menu/2); // adjust space on the screen.
  499.             printf("[Error] file does not exists!\n");
  500.             break;
  501.         }
  502.         case 2: {
  503.             print_space(SPACE_menu/2); // adjust space on the screen.
  504.             printf("[Error] file empty!\n");
  505.             break;
  506.         }
  507.         case 3: {
  508.             if (line > 3) {
  509.                 print_space(SPACE_menu/2); // adjust space on the screen.
  510.                 printf("Load successfully %d books with: ", k - frstk);
  511.             }
  512.             printf("[Error] Invalid format (line %d).\n", line);
  513.             break;
  514.         }
  515.         case 4: {
  516.             if (line > 3) {
  517.                 print_space(SPACE_menu/2); // adjust space on the screen.
  518.                 printf("Load successfully %d books with: ", k - frstk);
  519.             }
  520.             printf("[Error] Code duplicated (line %d).\n", line);
  521.             break;
  522.         }
  523.         case 5: {
  524.             if (line > 3) {
  525.                 print_space(SPACE_menu/2); // adjust space on the screen.
  526.                 printf("Load successfully %d books with: ", k - frstk);
  527.             }
  528.             printf("[Error] Title duplicated (line %d).\n", line);
  529.             break;
  530.         }
  531.         case 6: {
  532.             if (line > 3) {
  533.                 print_space(SPACE_menu/2); // adjust space on the screen.
  534.                 printf("Load successfully %d books with: ", k - frstk);
  535.             }
  536.             printf("[Error] Invalid quantity  (line %d).\n", line);
  537.             break;
  538.         }
  539.         case 7: {
  540.             if (line > 3) {
  541.                 print_space(SPACE_menu/2); // adjust space on the screen.
  542.                 printf("Load successfully %d books with: ", k - frstk);
  543.             }
  544.             printf("[Error] Invalid price  (line %d).\n", line);
  545.             break;
  546.         }
  547.     }
  548.     return k; // return new k after load file
  549. }
  550.  
  551. void fexport(double total, int k) { /* Export data to file */
  552.     int choice;
  553.     char ch;
  554.     FILE *fp, *fp1;
  555.    
  556.     print_space(SPACE_menu/2); // adjust space on the screen.
  557.     printf("1. Save file to load next time - bookstore.txt\n");
  558.     print_space(SPACE_menu/2); // adjust space on the screen.
  559.     printf("2. Export full information - bookstore_full.txt\n\n");
  560.    
  561.     while (1) {
  562.         print_space(SPACE_menu/2); // adjust space on the screen.
  563.         printf("Enter number (1-2): ");
  564.         fflush(stdin); ch = '\0';
  565.         scanf("%d%c", &choice, &ch);
  566.         if (choice >=1 && choice <= 2 && ch == '\n') break;
  567.         print_space(SPACE_menu/2); // adjust space on the screen.
  568.         printf("Invalid code, please re-enter!\n");
  569.     }
  570.    
  571.     if (choice == 1) {
  572.         fp = fopen("bookstore.txt", "w");
  573.         fprintf(fp, "+------------+--------------------------------+----------+------------+\n");
  574.         fprintf(fp, "| %-10s | %-30s | %8s | %10s |\n", "Code", "Title", "Quantity", "Price");
  575.         fprintf(fp, "+------------+--------------------------------+----------+------------+\n");
  576.         for (int i = 0; i<k; i++) {
  577.             fprintf(fp, "| %-10s | %-30s | %8d | %10.1lf |\n", bkinstck[i].code, bkinstck[i].title, bkinstck[i].quantity, bkinstck[i].price);
  578.         }
  579.         fprintf(fp, "+------------+--------------------------------+----------+------------+\n");
  580.         fclose(fp);
  581.     }
  582.     else { // choice == 2
  583.         fp1 = fopen("bookstore_full.txt", "w");
  584.         fprintf(fp1, "+------------+--------------------------------+----------+------------+------------+\n");
  585.         fprintf(fp1, "| %-10s | %-30s | %8s | %10s | %10s |\n", "Code", "Title", "Quantity", "Price", "Value");
  586.         fprintf(fp1, "+------------+--------------------------------+----------+------------+------------+\n");
  587.         for (int i = 0; i<k; i++) {
  588.             fprintf(fp1, "| %-10s | %-30s | %8d | %10.1lf | %10.1lf |\n", bkinstck[i].code, bkinstck[i].title, bkinstck[i].quantity, bkinstck[i].price, bkinstck[i].value);
  589.         }
  590.         fprintf(fp1, "+------------+--------------------------------+----------+------------+------------+\n");
  591.         fprintf(fp1, "|                                                          Total value: %-10.1lf |\n", total);
  592.         fprintf(fp1, "+----------------------------------------------------------------------------------+\n");
  593.         fclose(fp1);
  594.     }
  595.     printf("\n");
  596.     print_space(SPACE_menu/2); // adjust space on the screen.
  597.     printf("File exported successfully.\n");
  598. }
  599.  
  600. void fstmax(int k) { /* find first max in bkinstck[].price which have k elements and display it */
  601.     double max;
  602.     int a;
  603.    
  604.     max = bkinstck[0].price; a = 0;
  605.     for (int i = 1; i<k; i++) {
  606.         if (max < bkinstck[i].price) {
  607.             max = bkinstck[i].price;
  608.             a = i;
  609.         }  
  610.     }
  611.     print_space(SPACE_menu/2); // adjust space on the screen.
  612.     printf("Here is the (first) max price:\n");
  613.     display_header();
  614.     print_space(SPACE_menu/2); // adjust space on the screen.
  615.     display_1row(a);
  616.     print_space(SPACE_menu/2); // adjust space on the screen.
  617.     printf("+------------+--------------------------------+----------+------------+------------+\n");
  618. }
  619.  
  620. void display_fheader(void) { /* display the header of find table */
  621.     print_space(SPACE_menu/2); // adjust space on the screen.
  622.     printf("+--------+------------+--------------------------------+----------+------------+------------+\n");
  623.     print_space(SPACE_menu/2); // adjust space on the screen.
  624.     printf("| %-6s | %-10s | %-30s | %8s | %10s | %10s |\n", "Number", "Code", "Title", "Quantity", "Price", "Value");
  625.     print_space(SPACE_menu/2); // adjust space on the screen.
  626.     printf("+--------+------------+--------------------------------+----------+------------+------------+\n");
  627. }
  628.  
  629. bool find(int k, int pos[], int &j) { /* find and display the list which are matched with code */
  630.     char a[11], *b, temp[11];
  631.     bool check;
  632.    
  633.     /* input code & check validation */
  634.     while (1) {
  635.         print_space(SPACE_menu/2); // adjust space on the screen.
  636.         printf("Enter code you want to find: ");
  637.         fflush(stdin);
  638.         gets(a);
  639.         if ((strlen(a) > 0) && (strlen(a) <= 10)) {
  640.             break;
  641.         }
  642.         else {
  643.             print_space(SPACE_menu/2); // adjust space on the screen.
  644.             printf("Invalid code, please re-enter!\n");
  645.         }
  646.     }
  647.    
  648.     j = 0;
  649.     for (int i = 0; i<k; i++) {
  650.         strcpy(temp, bkinstck[i].code); // stored value of bkinstck[i].code to temp,
  651.                                         // because if we use direct bkinstck[i].code, after reset b = *strstr()
  652.                                         // we have also reset the small-string in bkinstck[i].code ~> lost information
  653.         b = strstr(temp, a);
  654.         if (b != NULL) {
  655.             pos[j] = i;
  656.             j++;
  657.             strcpy(b, "\0"); // reset the value of b. It's also reset the small-string INSIDE temp
  658.                             // but if not, b will be a string which store all the pointers of small-string.
  659.         }
  660.     }
  661.    
  662.     /* print list matched with code want to find */
  663.     if (j > 0) {
  664.         print_space(SPACE_menu/2); // adjust space on the screen.
  665.         printf("Here is the list which matched with '%s':\n", a);
  666.         display_fheader();
  667.         for (int i = 0; i<j; i++) {
  668.             print_space(SPACE_menu/2); // adjust space on the screen.
  669.             printf("| %-6d ", i);
  670.             display_1row(pos[i]);
  671.         }
  672.         print_space(SPACE_menu/2); // adjust space on the screen.
  673.         printf("+--------+------------+--------------------------------+----------+------------+------------+\n");
  674.         printf("\n");
  675.         check = true;      
  676.     }
  677.     else {
  678.         print_space(SPACE_menu/2); // adjust space on the screen.
  679.         printf("None of these book are match with code '%s'.\n", a);
  680.         check = false;     
  681.     }
  682.    
  683.     return(check);
  684. }
  685.  
  686. void edit_code(int i, int k) { /* edit bkinstck[i].code */
  687.     char code[11];
  688.    
  689.     while (1) {
  690.         print_space(SPACE_menu/2); // adjust space on the screen.
  691.         printf(" Enter code: ");
  692.         fflush(stdin);
  693.         gets(code);
  694.         if ((strlen(code) > 0) && (strlen(code) <= 10)) {
  695.             if (strcmp(code, "-1") == 0) {
  696.                 break;
  697.             }      
  698.             else if (dupcheck_code(code, k) == true) {
  699.                 print_space(SPACE_menu/2); // adjust space on the screen.
  700.                 printf(" The code %s already exists, please re-enter!\n", code);
  701.             }
  702.             else {
  703.                 strcpy(bkinstck[i].code, code);
  704.                 break;
  705.             }
  706.         }
  707.         else {
  708.             print_space(SPACE_menu/2); // adjust space on the screen.
  709.             printf(" Invalid code, please re-enter!\n");
  710.         }
  711.     }
  712. }
  713.  
  714. void edit_title(int i, int k) { /* edit bkinstck[i].title */
  715.     char title[31];
  716.    
  717.     while (1) {
  718.         print_space(SPACE_menu/2); // adjust space on the screen.
  719.         printf(" Enter title: ");
  720.         fflush(stdin);
  721.         gets(title);
  722.         if ((strlen(title) > 0) && (strlen(title) <= 30)) {
  723.             if (strcmp(title, "-1") == 0) {
  724.                 break;
  725.             }      
  726.             else if (dupcheck_title(title, k) == true) {
  727.                 print_space(SPACE_menu/2); // adjust space on the screen.
  728.                 printf(" The title %s already exists, please re-enter!\n", title);
  729.             }
  730.             else {
  731.                 strcpy(bkinstck[i].title, title);
  732.                 break;
  733.             }
  734.         }
  735.         else {
  736.             print_space(SPACE_menu/2); // adjust space on the screen.
  737.             printf(" Invalid title, please re-enter!\n");
  738.         }
  739.     }
  740. }
  741.  
  742. void edit_quantity(int i) { /* edit bkinstck[i].quantity */
  743.     char ch;
  744.     int quantity;
  745.    
  746.     while (1) {
  747.         print_space(SPACE_menu/2); // adjust space on the screen.
  748.         printf(" Enter quantity: ");
  749.         fflush(stdin);
  750.         ch = '\0'; // reset value ~> make sure program uses the value from last run & BREAK
  751.         scanf("%d%c", &quantity, &ch);
  752.         if ((quantity == -1) && (ch == '\n')) {
  753.             break;
  754.         }
  755.         else if ((quantity > 0) && (ch == '\n')) {
  756.             bkinstck[i].quantity = quantity;
  757.             break;
  758.         }
  759.         else {
  760.             print_space(SPACE_menu/2); // adjust space on the screen.
  761.             printf(" Invalid quantity, please re-enter!\n");
  762.         }
  763.     }
  764. }
  765.  
  766. void edit_price(int i) { /* edit bkinstck[i].price */
  767.     char ch;
  768.     double price;
  769.    
  770.     while (1) {
  771.         print_space(SPACE_menu/2); // adjust space on the screen.
  772.         printf(" Enter price: ");
  773.         fflush(stdin);
  774.         ch = '\0'; // reset value ~> make sure program uses the value from last run & BREAK
  775.         scanf("%lf%c", &price, &ch);
  776.         if ((price == -1) && (ch == '\n')) {
  777.             break;
  778.         }
  779.         else if ((price > 0) && (ch == '\n')) {
  780.             bkinstck[i].price = price;
  781.             break;
  782.         }
  783.         else {
  784.             print_space(SPACE_menu/2); // adjust space on the screen.
  785.             printf(" Invalid price, please re-enter!\n");
  786.         }
  787.     }
  788. }
  789.  
  790. void findnedit(double &total, int k, int pos[], int j) { /* find and edit book */
  791.     int choice;
  792.     char ch, confirm;
  793.     bool check;
  794.    
  795.     check = find(k, pos, j);
  796.     if (check == true) {
  797.         /* loop if choice == 'n' */
  798.         do {
  799.             /* input selection & check validation */
  800.             while (1) {
  801.                 print_space(SPACE_menu/2); // adjust space on the screen.
  802.                 printf("Enter number (0-%d): ", j-1);
  803.                 fflush(stdin); ch = '\0';
  804.                 scanf("%d%c", &choice, &ch);
  805.                 if ((choice >= 0) && (choice < j) && (ch == '\n')) {
  806.                     break;
  807.                 }
  808.                 else {
  809.                     print_space(SPACE_menu/2); // adjust space on the screen.
  810.                     printf("Your selection is incorrect!\n");
  811.                 }
  812.             }
  813.            
  814.             /* loop confirm until confirm == 'n' or 'y' */
  815.             do {
  816.                 print_space(SPACE_menu/2); // adjust space on the screen.
  817.                 printf("Do you want to edit line number %d <y/n>? ", choice);
  818.                 fflush(stdin);
  819.                 scanf("%c", &confirm);
  820.                 if ((confirm != 'n') && (confirm != 'y')) {
  821.                     print_space(SPACE_menu/2); // adjust space on the screen.
  822.                     printf("Your selection is incorrect!\n");
  823.                 }
  824.             } while ((confirm != 'n') && (confirm != 'y'));
  825.            
  826.             /* get out do-while(confirm == 'n') if choice == 'n'  ~> go to edit */
  827.             if (confirm == 'y') {
  828.                 break;
  829.             }
  830.             printf("\n");
  831.         } while(confirm == 'n');
  832.        
  833.         print_space(SPACE_menu/2); // adjust space on the screen.
  834.         printf("Enter your new information, if you want to keep it, enter -1 \n");
  835.            
  836.         edit_code(pos[choice], k);
  837.         edit_title(pos[choice], k);
  838.         edit_quantity(pos[choice]);
  839.         edit_price(pos[choice]);
  840.         value(pos[choice]);
  841.         total = value_total(k);
  842.        
  843.         print_space(SPACE_menu/2); // adjust space on the screen.
  844.         printf("Your book after edit:\n");
  845.         display_header();
  846.         print_space(SPACE_menu/2); // adjust space on the screen.
  847.         display_1row(pos[choice]);
  848.         print_space(SPACE_menu/2); // adjust space on the screen.
  849.         printf("+------------+--------------------------------+----------+------------+------------+\n");
  850.         printf("\n");
  851.     }  
  852. }
  853.  
  854. void delele_book(int i, int &k) { /* delete book bkinstck[i] in array bkinstck[] which have k elements */
  855.     for (int j = i; j <= k-2; j++) {
  856.         strcpy(bkinstck[j].code, bkinstck[j+1].code);
  857.         strcpy(bkinstck[j].title, bkinstck[j+1].title);
  858.         bkinstck[j].quantity = bkinstck[j+1].quantity;
  859.         bkinstck[j].price = bkinstck[j+1].price;
  860.         bkinstck[j].value = bkinstck[j+1].value;
  861.     }
  862.     k--;
  863. }
  864.  
  865. void findndelete(double &total, int &k, int pos[], int j) { /* find and delete book */
  866.     int choice;
  867.     char ch, confirm;
  868.     bool check;
  869.    
  870.     check = find(k, pos, j);
  871.     if (check == true) {
  872.         /* loop if choice == 'n' */
  873.         do {
  874.             /* input selection & check validation */
  875.             while (1) {
  876.                 print_space(SPACE_menu/2); // adjust space on the screen.
  877.                 printf("Enter number (0-%d): ", j-1);
  878.                 fflush(stdin); ch = '\0';
  879.                 scanf("%d%c", &choice, &ch);
  880.                 if ((choice >= 0) && (choice < j) && (ch == '\n')) {
  881.                     break;
  882.                 }
  883.                 else {
  884.                     print_space(SPACE_menu/2); // adjust space on the screen.
  885.                     printf("Your selection is incorrect!\n");
  886.                 }
  887.             }
  888.            
  889.             /* loop confirm until confirm == 'n' or 'y' */
  890.             do {
  891.                 print_space(SPACE_menu/2); // adjust space on the screen.
  892.                 printf("Do you want to delete line number %d <y/n>? ", choice);
  893.                 fflush(stdin);
  894.                 scanf("%c", &confirm);
  895.                 if ((confirm != 'n') && (confirm != 'y')) {
  896.                     print_space(SPACE_menu/2); // adjust space on the screen.
  897.                     printf("Your selection is incorrect!\n");
  898.                 }
  899.             } while ((confirm != 'n') && (confirm != 'y'));
  900.            
  901.             /* get out do-while(confirm == 'n') if choice == 'n'  ~> go to edit */
  902.             if (confirm == 'y') {
  903.                 break;
  904.             }
  905.             printf("\n");
  906.         } while(confirm == 'n');
  907.        
  908.         delele_book(pos[choice], k);
  909.         total = value_total(k);
  910.         print_space(SPACE_menu/2); // adjust space on the screen.
  911.         printf("Successfull.\n");
  912.     }  
  913. }
  914.  
  915. void info(void) { /* display infomation of group */
  916.  
  917.     print_space(SPACE_menu);                             
  918.     printf(" ______________________________ \n");
  919.     print_space(SPACE_menu);
  920.     printf("|Class: SE1440                 |\n");
  921.     print_space(SPACE_menu);
  922.     printf("|Group: 1                      |\n");
  923.     print_space(SPACE_menu);
  924.     printf("|-----------------+------------|\n");
  925.     print_space(SPACE_menu);
  926.     printf("|      Name       | Student ID |\n");
  927.     print_space(SPACE_menu);
  928.     printf("|-----------------+------------|\n");
  929.     print_space(SPACE_menu);
  930.     printf("| Dao Hoai Nam    | HE130139   |\n");
  931.     print_space(SPACE_menu);
  932.     printf("| Nguyen Mau Vinh | HE140137   |\n");
  933.     print_space(SPACE_menu);
  934.     printf("| Le Thanh Vuong  | HE140580   |\n");
  935.     print_space(SPACE_menu);
  936.     printf("| Ngo Van Phuong  | HE140784   |\n");
  937.     print_space(SPACE_menu);
  938.     printf("+------------------------------+\n");
  939. }
  940.  
  941. void menu(void) { /* display menu Books in stock */
  942.     print_space(SPACE_menu);
  943.     printf(".---------------------------------------.\n");
  944.     print_space(SPACE_menu);
  945.     printf("|             BOOK IN STOCK             |\n");
  946.     print_space(SPACE_menu);
  947.     printf(":---------------------------------------:\n");
  948.     print_space(SPACE_menu);
  949.     printf("|  1. Enter the list of books           |\n");
  950.     print_space(SPACE_menu);
  951.     printf("|  2. Display the list of books         |\n");
  952.     print_space(SPACE_menu);
  953.     printf("|  3. Sort the list of books by code    |\n");
  954.     print_space(SPACE_menu);
  955.     printf("|  4. Load data from file               |\n");
  956.     print_space(SPACE_menu);
  957.     printf("|  5. Export data to file               |\n");
  958.     print_space(SPACE_menu);
  959.     printf("|  6. Find the (first) max price        |\n");
  960.     print_space(SPACE_menu);
  961.     printf("|  7. Find by code and edit the book    |\n");
  962.     print_space(SPACE_menu);
  963.     printf("|  8. Find by code and delete the book  |\n");
  964.     print_space(SPACE_menu);
  965.     printf("|  9. About us                          |\n"); 
  966.     print_space(SPACE_menu);
  967.     printf("|  0. Exit                              |\n");
  968.     print_space(SPACE_menu);
  969.     printf(":_______________________________________:\n");
  970. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement