Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LINESIZE 1024
- #define BUFSIZE 512
- #define NAMESIZE 20
- #define BLOCK 2
- typedef struct
- {
- char last[NAMESIZE]; /* last name; 1 (non-empty) word */
- char first[NAMESIZE]; /* first name; 1 (non-empty) word */
- } name;
- typedef struct
- {
- int id; /* student ID; between 1000000 & 9999999 inclusive */
- name name; /* name of student */
- int score; /* between 0 & 100 inclusive */
- } record;
- typedef struct{
- record **data; /*points to dynamic array of pointers */
- size_t nalloc; /*number of pointers allocated */
- size_t nused; /*number of pointers in use */
- } record_list;
- /*Function to get input*/
- int getInput(char a[LINESIZE])
- {
- char input[LINESIZE];
- if (!fgets(input, LINESIZE, stdin))
- {
- clearerr(stdin);
- return 0;
- }
- strcpy(a, input);
- return 1;
- }
- /*Comparison function to compare IDs in ascending order*/
- int cmpID(const void *p, const void *q)
- {
- const record *pp = p;
- const record *qq = q;
- return (pp->id - qq->id);
- }
- /*Comparison function to compare name in ascending order*/
- int cmpName(const void *p, const void *q)
- {
- int n = 0;
- record *pp = *(record**)p;
- record *qq = *(record**)q;
- /*if last names are equal, compare the first names*/
- if ((n = (strcmp(pp->name.last, qq->name.last))) == 0)
- {
- /*if first names are not equal return the value of strcmp*/
- if ((n = (strcmp(pp->name.first, qq->name.first))) != 0)
- {
- return n;
- }
- }
- /*return value of strcmp if names are not equal*/
- return strcmp(pp->name.last, qq->name.last);
- }
- /*Validate id*/
- int validateID(int *id)
- {
- if (*id < 1000000 || *id > 9999999)
- {
- return 0;
- }
- if (*id == 0)
- {
- return 2;
- }
- return 1;
- }
- /*Validate score*/
- int validateScore(int *grade)
- {
- if (*grade < 0 || *grade >= 100)
- {
- return 0;
- }
- return 1;
- }
- /*Validate name*/
- int validateName(char a[])
- {
- if (strlen(a) == 0)
- {
- return 0;
- }
- return 1;
- }
- /*Get ID*/
- int getID(int n)
- {
- char line[BUFSIZE];
- while (1)
- {
- fprintf(stderr, "Enter a student id:\n");
- if (!getInput(line))
- {
- break;
- }
- if (sscanf(line, "%d\n", &n) == 1)
- {
- if (n == 0)
- {
- break;
- }
- if (validateID(&n) == 1)
- {
- return 1;
- }
- }
- }
- return 0;
- }
- /*Get fname*/
- int getfName(char *fname)
- {
- char line[BUFSIZE];
- while(1)
- {
- fprintf(stderr,"Enter in student's first name:\n");
- if (!getInput(line))
- {
- break;
- }
- if (sscanf(line, "%s\n", fname) == 1)
- {
- if (validateName(fname) == 1)
- {
- return 1;
- }
- }
- }
- return 0;
- }
- /*Get lname*/
- int getlName(char *lname)
- {
- char line[BUFSIZE];
- while (1)
- {
- fprintf(stderr, "Enter in student's last name:\n");
- if (!getInput(line))
- {
- break;
- }
- if (sscanf(line, "%s\n", lname) == 1)
- {
- if (validateName(lname) == 1)
- {
- return 1;
- }
- }
- }
- return 0;
- }
- /*Get score*/
- int getScore(int score)
- {
- char line[BUFSIZE];
- while (1)
- {
- fprintf(stderr, "Enter in student's score:\n");
- if (!getInput(line))
- {
- break;
- }
- if (sscanf(line, "%d\n", &score) == 1)
- {
- if (validateScore(&score) == 1)
- {
- return 1;
- }
- }
- }
- return 0;
- }
- /*initialize the list */
- void list_init(record_list *list)
- {
- list->data = 0;
- list->nalloc = 0;
- list->nused = 0;
- }
- /*initialize the record */
- void record_init(record *p)
- {
- p->id = 0;
- strcpy(p->name.first, "");
- strcpy(p->name.last, "");
- p->score = 0;
- }
- /*Function to store the grade */
- int store_record(record *p, int *id, char *first,
- char *last, int *score)
- {
- p->id = *id;
- strcpy(p->name.first, first);
- strcpy(p->name.last, last);
- p->score = *score;
- return 1;
- }
- /*Function to print out records*/
- void print_grade(const record *p)
- {
- printf("%d %s, %s: %d\n", p->id, p->name.first, p->name.last, p->score);
- }
- /*Function to go through and print records */
- void show_byID(record_list *list)
- {
- size_t n = 0;
- qsort(list->data,list->nused,sizeof(record*),cmpID);
- for (n = 0; n < list->nused; n++)
- {
- print_grade(list->data[n]);
- }
- }
- void show_byName(record_list *list)
- {
- size_t n = 0;
- qsort(list->data,list->nused, sizeof(record*),cmpName);
- for (n = 0; n < list->nused; n++)
- {
- print_grade(list->data[n]);
- }
- }
- /*function to insert records */
- int insert_record(record_list *list, const record *rec)
- {
- void *tmp = 0;
- if (list->nalloc == list->nused)
- {
- tmp = realloc(list->data, (list->nalloc + BLOCK * sizeof(record)));
- if (tmp == 0)
- {
- fprintf(stderr, "Memory reallocation failed");
- return 0;
- }
- list->data = tmp;
- list->nalloc += BLOCK;
- }
- list->data[list->nused] = malloc(sizeof(record));
- if (list->data[list->nused] == 0)
- {
- fprintf(stderr, "Memory allocation failed");
- return 0;
- }
- *list->data[list->nused] = *rec;
- list->nused++;
- return 1;
- }
- /*go through and delete every single record*/
- int delete_records(record_list *list)
- {
- size_t n = 0;
- for (n = 0; n < list->nalloc; n++)
- {
- free(list->data[n]);
- }
- list_init(list);
- return 1;
- }
- /*get the ID, name, score to store into a record
- to insert into the list */
- int getRecord(int *id, int *score, char fname[BUFSIZE],
- char lname[BUFSIZE], record *p, record_list *list)
- {
- int result = 0;
- while(1)
- {
- result = getID(*id);
- if ((result == 0) || (result == 2))
- {
- return 0;
- }
- if (getfName(fname) == 0)
- {
- return 0;
- }
- if (getlName(lname) == 0)
- {
- return 0;
- }
- if (getScore(*score) == 0)
- {
- return 0;
- }
- if (store_record(p,id,fname,lname,score) == 1)
- {
- if (insert_record(list, p) == 1)
- {
- fprintf(stderr,"Record successfully inserted\n");
- }
- }
- }
- return 0;
- }
- /* Display the menu and prompt for actions*/
- void menu_display()
- {
- fprintf(stderr, "1. Enter records\n");
- fprintf(stderr, "2. Sort by ID\n");
- fprintf(stderr, "3. Sort by name\n");
- fprintf(stderr, "4. Quit\n");
- }
- /*Verify menu prompt*/
- int menu()
- {
- char line[LINESIZE] = "";
- int n = 0;
- while (1)
- {
- menu_display();
- getInput(line);
- if (sscanf(line, "%d\n", &n) == 1)
- {
- if ((n < 5) && (n > 0))
- {
- return n;
- }
- }
- }
- }
- int main(void)
- {
- /*initializing variables */
- int option = 0;
- record_list list;
- record p;
- int id = 0;
- int score = 0;
- char fname[BUFSIZE];
- char lname[BUFSIZE];
- list_init(&list);
- record_init(&p);
- /*program starts*/
- while (1)
- {
- option = menu();
- if (option == 1)
- {
- getRecord(&id, &score, fname, lname, &p, &list);
- }
- if (option == 2)
- {
- show_byID(&list);
- continue;
- }
- if (option == 3)
- {
- show_byName(&list);
- continue;
- }
- if (option == 4)
- {
- delete_records(&list);
- return 0;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment