Advertisement
ppupil2

Assignment-B.c

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