Skip to content

Instantly share code, notes, and snippets.

@kalenwallin
Created December 17, 2019 21:24
Show Gist options
  • Save kalenwallin/91cabcab4fc03169f194387aec90605f to your computer and use it in GitHub Desktop.
Save kalenwallin/91cabcab4fc03169f194387aec90605f to your computer and use it in GitHub Desktop.
auto-posted gist
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "book.h"
Book * createBook(const char * title,
const char * authorFirstName,
const char * authorLastName,
int releaseYear,
double rating) {
//TODO
}
void initBook(Book * book,
const char *title,
const char * authorFirstName,
const char *authorLastName,
int releaseYear,
double rating) {
//TODO
}
char * bookToString(const Book *b) {
//TODO
}
int cmpByAuthorYear(const void *a, const void *b) {
//TODO
}
int cmpByTitle(const void *a, const void *b) {
//TODO
}
int cmpByRating(const void *a, const void *b) {
//TODO
}
void generateReports(Book *books, int n) {
printf("\n\nList of books - Sorted by Title\n");
printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
//TODO: sort books by title
printBookArray(books, n);
printf("\n\nList of books - Sorted by Author/Year\n");
printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
//TODO: sort books by author (last name/first name),
// then by year (oldest first) for
// books by the same author
printBookArray(books, n);
printf("\n\nList of books - Sorted by Rating\n");
printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
//TODO: sort books by rating, highest to lowest
printBookArray(books, n);
return;
}
/* utility functions -- these have been done for you */
char * deepCopy(const char *s) {
char *copy = (char *) malloc( (strlen(s) + 1) * sizeof(char) );
strcpy(copy, s);
return copy;
}
void printBookArray(const Book *b, int n) {
int i;
for(i=0; i<n; i++) {
char *s = bookToString(&b[i]);
printf("%s\n", s);
free(s);
}
}
/**
* This structure models a book.
*/
typedef struct {
//TODO
} Book;
/**
* A function that creates a new Book instance with
* the given values. Deep copies of the strings are
* made.
*/
Book * createBook(const char *title,
const char *authorFirstName,
const char *authorLastName,
int releaseYear,
double rating);
/**
* Initializes the given book with the given
* values, making deep copies of the strings.
*/
void initBook(Book * book,
const char *title,
const char * authorFirstName,
const char *authorLastName,
int releaseYear,
double rating);
/**
* A function that produces a human-readable
* string of the given book.
*/
char * bookToString(const Book *b);
/**
* A comparator function that orders books first
* by author (last name/first name) and then by
* year oldest to newest
*/
int cmpByAuthorYear(const void *a, const void *b);
/**
* A comparator function that orders books by title
*/
int cmpByTitle(const void *a, const void *b);
/**
* A comparator function that orders books by
* rating, highest to lowest
*/
int cmpByRating(const void *a, const void *b);
/**
* Generates several different orderings of the given
* array of Book structures
*/
void generateReports(Book *books, int n);
/**
* Utility function that performs a deep copy of a string.
*
* This function has been implemented for you.
*/
char * deepCopy(const char *s);
/**
* This function prints an entire array of Books
* to the standard output.
*
* This function has been implemented for you.
*/
void printBookArray(const Book *b, int n);
/**
* This is just a demo file for local testing
* of your book functions. You may modify it if
* you wish, but it is not to be turned in.
*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "book.h"
int main(int argc, char **argv) {
int n = 5;
Book *books = (Book *) malloc(sizeof(Book) * n);
initBook(&books[0], "Harry Potter and the Deathly Hallows",
"J.K.","Rowling",2007,4.62);
initBook(&books[1], "Pachinko",
"Min Jin","Lee",2017,4.26);
initBook(&books[2], "The Name of the Wind",
"Patrick","Rothfuss",2007,4.55);
initBook(&books[3], "The Wise Man's Fear",
"Patrick","Rothfuss",2011,4.58);
initBook(&books[4], "The Three-Body Problem",
"Cixin","Liu",2014,4.06);
generateReports(books, n);
return 0;
}
/**
* TODO: header documentation
*/
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
int main(int argc, char **argv) {
if(argc != 2) {
fprintf(stderr, "Usage: %s fileName\n", argv[0]);
exit(1);
}
return 0;
}
/**
* TODO: header documentation
*/
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv) {
//TODO: implement
return 0;
}
/**
* TODO: header documentation
*/
#include<stdlib.h>
#include<stdio.h>
int main(int argc, char **argv) {
int n;
int *arr = NULL;
//this code populates an integer array of size n
// with the command line arguments for you
if(argc > 1) {
n = argc-1;
arr = (int *) malloc(n * sizeof(int));
for(int i=1; i<argc; i++) {
arr[i-1] = atoi(argv[i]);
}
} else {
fprintf(stderr, "Usage: %s [list of integers]\n", argv[0]);
exit(1);
}
//TODO: the rest of the program
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment