Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Program : Assignment 7
- Student Name : YeEum Lee (Kate)
- Student Number : A00966565
- Date : March 6, 2018
- Version : 1.7
- comment: read_record - size_t n removed for now
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #define IDSIZE 10
- #define NAMESIZE 20
- #define BLOCK 2
- #define LINESIZE 1024
- typedef struct{
- char last[NAMESIZE];
- char first[NAMESIZE];
- } name;
- typedef struct{
- char id[IDSIZE];
- name name;
- int score;
- } record;
- int read_record(record *prec);
- int scoreDescIdAsc(const void *a, const void *b);
- int scoreDescNameAsc(const void *a, const void *b);
- int scoreDescNameAscIdAsc(const void *a, const void *b);
- void sort_score_desc_id_asc(record a[], size_t n);
- void sort_score_desc_name_asc(record a[], size_t n);
- void sort_score_desc_name_asc_id_asc(record a[], size_t n);
- void print_record(const record *prec);
- int sortBy = 0; //global variable
- int main(void){
- int i;
- record r;
- for (i = 0; i < 3; i++) {
- if (read_record(&r)!= 1) {
- printf("function error\n");
- return 1;
- }
- }
- sort_score_desc_name_asc_id_asc(&r, 3);
- for (i = 0; i < 3; i++) {
- print_record(&r);
- }
- return 0;
- }
- int read_record(record *prec) {
- char line[LINESIZE];
- char studentnumber_temp[LINESIZE];
- char fname_temp[LINESIZE];
- char lname_temp[LINESIZE];
- int score_temp[LINESIZE];
- int index;
- printf("Enter the id, first name, last name and score with space\n");
- if(!fgets(line, LINESIZE, stdin)){
- clearerr(stdin);
- return 0;
- }
- if((sscanf(line, "%s %s %s %d", studentnumber_temp, fname_temp, lname_temp, score_temp )) >=4){
- /* id - consists of an 'a' followed by 8 digits */
- if(strlen(studentnumber_temp) == 9 && studentnumber_temp[0] == 'a'){
- /* score - must be an integer between 0 and 100 */
- if(*score_temp >= 0 && *score_temp <= 100){
- /* name - both first name and last name should have length less than NAMESIZE */
- if(strlen(fname_temp) < NAMESIZE && strlen(lname_temp) < NAMESIZE){
- /* store the first & last names in all lowercase in the record */
- for(index = 0; fname_temp[index] != '\0'; index++){
- fname_temp[index] = tolower(fname_temp[index]);
- }
- for(index = 0; lname_temp[index] != '\0'; index++){
- lname_temp[index] = tolower(lname_temp[index]);
- }
- /* copy to the record */
- strcpy(prec->id, studentnumber_temp);
- strcpy(prec->name.first, fname_temp);
- strcpy(prec->name.last, lname_temp);
- prec->score = *score_temp;
- return 1;
- }
- }
- }
- }
- return 0;
- }
- int scoreDescIdAsc(const void *a, const void *b){
- const record *p = a;
- const record *q = b;
- int n = q->score - p->score;
- if(n != 0){
- return n;
- }
- return p->id - q->id;
- }
- int scoreDescNameAsc(const void *a, const void *b){
- const record *p = a;
- const record *q = b;
- int m;
- int n = q->score - p->score;
- if(n != 0){
- return n;
- }
- m = strcmp(p->name.last,q->name.last);
- if( m != 0){
- return m;
- }
- return strcmp(p->name.first, q->name.first);
- }
- int scoreDescNameAscIdAsc(const void *a, const void *b){
- const record *p = a;
- const record *q = b;
- int n = q->score - p->score;
- int m,s;
- if(n != 0){
- return n;
- }
- m = strcmp(p->name.last, q->name.last);
- if(m != 0){
- return m;
- }
- s = strcmp(p->name.first, q->name.first);
- if(s != 0){
- return s;
- }
- return strcmp(p->id, q->id);
- }
- /*
- sorts the array in descending order of scores. If two or more records have the same score,
- they are then sorted in ascending order of their IDs.
- */
- void sort_score_desc_id_asc(record a[], size_t n){
- sortBy = 1;
- qsort(a, n, sizeof(a[0]), scoreDescIdAsc);
- }
- /*
- sorts the array in descending order of scores. If two or more records have the same score,
- they are then sorted in ascending order of their names.
- */
- void sort_score_desc_name_asc(record a[], size_t n){
- sortBy = 2;
- qsort(a, n, sizeof(a[0]), scoreDescNameAsc);
- }
- /*
- sorts the array in descending order of scroes. If two or more records have the same score,
- they are then sorted in ascending order of their names.
- If furthermore, several records have the same score & the same name (first , last), they are then
- sorted in ascending order of their IDs.
- */
- void sort_score_desc_name_asc_id_asc(record a[], size_t n){
- sortBy = 0;
- qsort(a, n, sizeof(a[0]), scoreDescNameAscIdAsc);
- }
- /*
- Prints the record pointed to by prec.
- */
- void print_record(const record *prec){
- //Need to organize each case
- switch(sortBy){
- case 0:
- printf("%d : %s, %s : %s\n", prec->score, prec->name.last, prec->name.first, prec->id);
- break;
- case 1:
- printf("%d : %s, %s : %s\n", prec->id, prec->name.last, prec->name.first, prec->score );
- break;
- case 2:
- printf("%d : %s, %s : %s\n", prec->score, prec->name.last, prec->name.first, prec->id);
- break;
- default:
- break;
- }
- }
Add Comment
Please, Sign In to add comment