Untitled

 avatar
unknown
plain_text
2 years ago
3.7 kB
5
Indexable
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_BOOKS 10
#define TITLE_MAX 50
#define AUTHOR_MAX 40
#define PUBLISHER_MAX 20

struct book {
    char title[TITLE_MAX], author[AUTHOR_MAX], publisher[PUBLISHER_MAX];
    int publishing_year;
    int university_text_book;
};
typedef struct book book;

book make_book(char *title, char *author, char *publisher, int year, int text_book);
void prnt_book(book b);
void sort_books_name(book shelf[], int last);
int compare(const void * a, const void * b);
void sort_books_year(book shelf[], int last);
int compare2(const void * a, const void * b);

int main(void) {
    book shelf[MAX_BOOKS];
    int i;

    shelf[0] =
            make_book("Problem Solving and Program Design in C", "Hanly and Koffman",
                      "Pearson", 2010, 1);

    shelf[1] =
            make_book("C by Disssection", "Kelley and Pohl",
                      "Addison Wesley", 2001, 1);

    shelf[2] =
            make_book("The C Programming Language", "Kernighan og Ritchie",
                      "Prentice Hall", 1988, 1);

    shelf[3] =
            make_book("Pelle Erobreren", "Martin Andersen Nexoe",
                      "Gyldendal", 1910, 0);

    shelf[4] = shelf[3];
    strcpy(shelf[4].title, "Ditte Menneskebarn");
    shelf[4].publishing_year = 1917;

    for(i = 0; i <=4; i++){
        prnt_book(shelf[i]);
    }

    int sort_choice = 0;
    while (sort_choice != 1 && sort_choice != 2){
        printf("How would you like to sort the books? 1: By Title, 2: By publishing year - ");
        scanf("%d", &sort_choice);
    }

    if (sort_choice == 1){
        sort_books_name(shelf, i);
        for(i = 0; i <=4; i++){
            prnt_book(shelf[i]);
        }
    } else {
        sort_books_year(shelf, i);
        for(i = 0; i <=4; i++){
            prnt_book(shelf[i]);
        }
    }

    return 0;
}


book make_book(char *title, char *author, char *publisher,
               int year, int text_book){
    book result;
    strcpy(result.title, title); strcpy(result.author, author); strcpy(result.publisher, publisher);
    result.publishing_year = year;
    result.university_text_book = text_book;

    return result;
}

void prnt_book(book b){
    char *yes_or_no;

    yes_or_no = (b.university_text_book ? "yes" : "no");
    printf("Title: %s\n"
           "Author: %s\n"
           "Publisher: %s\n"
           "Year: %4i\n"
           "University text book: %s\n\n",
           b.title, b.author, b.publisher,
           b.publishing_year, yes_or_no);
}

void sort_books_name(book shelf[], int last){

    qsort(shelf, last, sizeof(book), compare);
}

int compare(const void * a, const void * b){

    book * book1 = (book*)a;
    book * book2 = (book*)b;

    if (strcmp(book1->title, book2->title) > 0){
        return 1;
    } else if (strcmp(book1->title, book2->title) < 0){
        return -1;
    } else {
        return 0;
    }

}

void sort_books_year(book shelf[], int last){

    qsort(shelf, last, sizeof(book), compare2);
}

int compare2(const void * a, const void * b){

    book * book1 = (book*)a;
    book * book2 = (book*)b;

    if(book1->university_text_book == book2->university_text_book){
        if(book1->publishing_year > book2->publishing_year){
            return 1;
        } else if (book1->publishing_year < book2->publishing_year){
            return -1;
        } else {
            return 0;
        }
    } else {
        if (book1->university_text_book > book2->university_text_book){
            return 1;
        } else {
            return -1;
        }
    }

}
Editor is loading...