Guest User

Untitled

a guest
Jul 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define LINESIZE 1024
  6. #define BUFSIZE 512
  7. #define NAMESIZE 20
  8. #define BLOCK 2
  9.  
  10. typedef struct
  11. {
  12.     char last[NAMESIZE]; /* last name; 1 (non-empty) word */
  13.     char first[NAMESIZE]; /* first name; 1 (non-empty) word */
  14. } name;
  15.  
  16. typedef struct
  17. {
  18.     int id; /* student ID; between 1000000 & 9999999 inclusive */
  19.     name name; /* name of student */
  20.     int score; /* between 0 & 100 inclusive */
  21. } record;
  22.  
  23. typedef struct{
  24.     record **data; /*points to dynamic array of pointers */
  25.     size_t nalloc; /*number of pointers allocated */
  26.     size_t nused; /*number of pointers in use */        
  27. } record_list;
  28.  
  29. /*Function to get input*/
  30. int getInput(char a[LINESIZE])
  31. {
  32.     char input[LINESIZE];
  33.     if (!fgets(input, LINESIZE, stdin))
  34.     {
  35.         clearerr(stdin);
  36.         return 0;
  37.     }
  38.     strcpy(a, input);
  39.     return 1;
  40. }
  41.  
  42. /*Comparison function to compare IDs in ascending order*/
  43. int cmpID(const void *p, const void *q)
  44. {
  45.     const record *pp = p;
  46.     const record *qq = q;
  47.     return (pp->id - qq->id);
  48. }
  49.  
  50. /*Comparison function to compare name in ascending order*/
  51. int cmpName(const void *p, const void *q)
  52. {
  53.     int n = 0;
  54.     record *pp = *(record**)p;
  55.     record *qq = *(record**)q;
  56.     /*if last names are equal, compare the first names*/
  57.     if ((n = (strcmp(pp->name.last, qq->name.last))) == 0)
  58.     {
  59.         /*if first names are not equal return the value of strcmp*/
  60.         if ((n = (strcmp(pp->name.first, qq->name.first))) != 0)
  61.         {
  62.             return n;
  63.         }
  64.     }
  65.     /*return value of strcmp if names are not equal*/
  66.     return strcmp(pp->name.last, qq->name.last);
  67. }
  68.  
  69. /*Validate id*/
  70. int validateID(int *id)
  71. {
  72.     if (*id < 1000000 || *id > 9999999)
  73.     {
  74.         return 0;
  75.     }
  76.     if (*id == 0)
  77.     {
  78.         return 2;
  79.     }
  80.     return 1;
  81. }
  82.  
  83. /*Validate score*/
  84. int validateScore(int *grade)
  85. {
  86.     if (*grade < 0 || *grade >= 100)
  87.     {
  88.         return 0;
  89.     }
  90.     return 1;
  91. }
  92.  
  93. /*Validate name*/
  94. int validateName(char a[])
  95. {
  96.     if (strlen(a) == 0)
  97.     {
  98.         return 0;
  99.     }
  100.     return 1;
  101. }
  102.  
  103. /*Get ID*/
  104. int getID(int n)
  105. {
  106.     char line[BUFSIZE];
  107.     while (1)
  108.     {
  109.         fprintf(stderr, "Enter a student id:\n");
  110.         if (!getInput(line))
  111.         {
  112.             break;
  113.         }
  114.         if (sscanf(line, "%d\n", &n) == 1)
  115.         {
  116.             if (n == 0)
  117.             {
  118.                 break;
  119.             }
  120.             if (validateID(&n) == 1)
  121.             {
  122.                 return 1;
  123.             }
  124.         }      
  125.        
  126.     }
  127.     return 0;
  128. }
  129.  
  130. /*Get fname*/
  131. int getfName(char *fname)
  132. {
  133.     char line[BUFSIZE];
  134.     while(1)
  135.     {
  136.         fprintf(stderr,"Enter in student's first name:\n");
  137.         if (!getInput(line))
  138.         {
  139.             break;
  140.         }
  141.         if (sscanf(line, "%s\n", fname) == 1)
  142.         {
  143.             if (validateName(fname) == 1)
  144.             {
  145.                 return 1;
  146.             }          
  147.         }
  148.     }
  149.     return 0;
  150. }
  151.  
  152. /*Get lname*/
  153. int getlName(char *lname)
  154. {
  155.     char line[BUFSIZE];
  156.     while (1)
  157.     {
  158.         fprintf(stderr, "Enter in student's last name:\n");
  159.         if (!getInput(line))
  160.         {
  161.             break;
  162.         }
  163.         if (sscanf(line, "%s\n", lname) == 1)
  164.         {
  165.             if (validateName(lname) == 1)
  166.             {
  167.                 return 1;
  168.             }
  169.         }
  170.     }
  171.     return 0;
  172. }
  173.  
  174. /*Get score*/
  175. int getScore(int score)
  176. {
  177.     char line[BUFSIZE];
  178.     while (1)
  179.     {
  180.         fprintf(stderr, "Enter in student's score:\n");
  181.         if (!getInput(line))
  182.         {
  183.             break;
  184.         }
  185.         if (sscanf(line, "%d\n", &score) == 1)
  186.         {
  187.             if (validateScore(&score) == 1)
  188.             {
  189.                 return 1;
  190.             }
  191.         }
  192.     }  
  193.     return 0;
  194. }
  195.  
  196. /*initialize the list */
  197. void list_init(record_list *list)
  198. {
  199.      list->data = 0;
  200.      list->nalloc = 0;
  201.      list->nused = 0;
  202. }
  203.  
  204. /*initialize the record */
  205. void record_init(record *p)
  206. {
  207.     p->id = 0;
  208.     strcpy(p->name.first, "");
  209.     strcpy(p->name.last, "");
  210.     p->score = 0;
  211. }
  212.  
  213. /*Function to store the grade */
  214. int store_record(record *p, int *id, char *first,
  215.         char *last, int *score)
  216. {
  217.     p->id = *id;
  218.     strcpy(p->name.first, first);
  219.     strcpy(p->name.last, last);
  220.     p->score = *score;
  221.     return 1;
  222. }
  223.  
  224. /*Function to print out records*/
  225. void print_grade(const record *p)
  226. {
  227.     printf("%d %s, %s: %d\n", p->id, p->name.first, p->name.last, p->score);
  228. }
  229.  
  230. /*Function to go through and print records */
  231. void show_byID(record_list *list)
  232. {
  233.     size_t n = 0;
  234.     qsort(list->data,list->nused,sizeof(record*),cmpID);
  235.     for (n = 0; n < list->nused; n++)
  236.     {
  237.         print_grade(list->data[n]);
  238.     }
  239. }
  240.  
  241. void show_byName(record_list *list)
  242. {
  243.     size_t n = 0;
  244.     qsort(list->data,list->nused, sizeof(record*),cmpName);
  245.     for  (n = 0; n < list->nused; n++)
  246.     {
  247.         print_grade(list->data[n]);
  248.     }  
  249. }
  250.  
  251. /*function to insert records */
  252. int insert_record(record_list *list, const record *rec)
  253. {
  254.     void *tmp = 0;
  255.     if (list->nalloc == list->nused)
  256.     {
  257.         tmp = realloc(list->data, (list->nalloc + BLOCK * sizeof(record)));
  258.         if (tmp == 0)
  259.         {
  260.             fprintf(stderr, "Memory reallocation failed");
  261.             return 0;
  262.         }
  263.         list->data = tmp;
  264.         list->nalloc += BLOCK;
  265.     }
  266.     list->data[list->nused] = malloc(sizeof(record));
  267.     if (list->data[list->nused] == 0)
  268.     {
  269.         fprintf(stderr, "Memory allocation failed");
  270.         return 0;
  271.     }
  272.     *list->data[list->nused] = *rec;
  273.     list->nused++;
  274.     return 1;
  275. }
  276.  
  277. /*go through and delete every single record*/
  278. int delete_records(record_list *list)
  279. {
  280.     size_t n = 0;
  281.     for (n = 0; n < list->nalloc; n++)
  282.     {
  283.         free(list->data[n]);
  284.     }
  285.     list_init(list);
  286.     return 1;
  287. }
  288.  
  289. /*get the ID, name, score to store into a record
  290. to insert into the list */
  291. int getRecord(int *id, int *score, char fname[BUFSIZE],
  292.          char lname[BUFSIZE], record *p, record_list *list)
  293. {
  294.     int result = 0;
  295.     while(1)
  296.     {
  297.         result = getID(*id);
  298.         if ((result == 0) || (result == 2))
  299.         {
  300.             return 0;
  301.         }
  302.         if (getfName(fname) == 0)
  303.         {
  304.             return 0;
  305.         }
  306.         if (getlName(lname) == 0)
  307.         {
  308.             return 0;
  309.         }
  310.         if (getScore(*score) == 0)
  311.         {
  312.             return 0;
  313.         }
  314.         if (store_record(p,id,fname,lname,score) == 1)
  315.         {
  316.             if (insert_record(list, p) == 1)
  317.             {
  318.                 fprintf(stderr,"Record successfully inserted\n");
  319.             }
  320.         }
  321.     }
  322.     return 0;
  323. }
  324.  
  325.  
  326. /* Display the menu and prompt for actions*/
  327. void menu_display()
  328. {
  329.     fprintf(stderr, "1. Enter records\n");
  330.     fprintf(stderr, "2. Sort by ID\n");
  331.     fprintf(stderr, "3. Sort by name\n");
  332.     fprintf(stderr, "4. Quit\n");
  333. }
  334.  
  335. /*Verify menu prompt*/
  336. int menu()
  337. {
  338.     char line[LINESIZE] = "";
  339.     int n = 0;
  340.     while (1)
  341.     {
  342.         menu_display();
  343.         getInput(line);
  344.         if (sscanf(line, "%d\n", &n) == 1)
  345.         {
  346.             if ((n < 5) && (n > 0))
  347.             {
  348.                 return n;
  349.             }
  350.         }
  351.     }
  352. }
  353.  
  354. int main(void)
  355. {
  356.     /*initializing variables */
  357.     int option = 0;
  358.     record_list list;
  359.     record p;
  360.     int id = 0;
  361.     int score = 0;
  362.     char fname[BUFSIZE];
  363.     char lname[BUFSIZE];
  364.    
  365.     list_init(&list);
  366.     record_init(&p);
  367.    
  368.     /*program starts*/    
  369.     while (1)
  370.     {
  371.         option = menu();
  372.         if (option == 1)
  373.         {
  374.             getRecord(&id, &score, fname, lname, &p, &list);
  375.         }
  376.         if (option == 2)
  377.         {
  378.             show_byID(&list);
  379.             continue;
  380.         }
  381.         if (option == 3)
  382.         {
  383.             show_byName(&list);
  384.             continue;
  385.         }
  386.         if (option == 4)
  387.         {
  388.             delete_records(&list);
  389.             return 0;
  390.         }
  391.     }
  392.     return 0;
  393. }
Add Comment
Please, Sign In to add comment