Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP with Stacks (Cprog.) This a program stores information of the books there a

ID: 3842556 • Letter: H

Question

HELP with Stacks (Cprog.)

This a program stores information of the books there are in a library (code, author, availability..), and not only that but also lets the user to insert new books in its database which will get store in certain manner or to delete existing ones.

In this exercise, we have to define a stack to save units of data of the type Book, and the methods to have access to this stack. We do that by completing the files stack.h and stack.c

The tasks are:

1)To define in stack.h the type of data tBookStack that represents a stack of units of data of the type tBook

2)Implement in stack.c the action:

Void bookStack_create(tBookStack*stack) so given an input/output parameter of the type tBookStack, it initializes this parameter in an empty stack.

I inclue the .h necessary files as ell as the excecutable books.c

Stack.h

#include "data.h"


typedef struct {
} tBookStack;

void bookStack_create(tBookStack *stack);

tBoolean bookStack_empty(tBookStack stack);

tError bookStack_push(tBookStack *stack, tBook newElement);

tError bookStack_pop (tBookStack *stack, tBook *element);

void bookStack_transfer(tBookStack *stack_dest, tBookStack *stack);

tError bookStack_search(tBookStack *stack, char *ISBN, tBook *book);

Stack.c

#include "books.h"
#include "stack.h"
#include <string.h>


void bookStack_create(tBookStack *stack) {

}

Books.h

#include "data.h"

/* Get a textual representation of a book */
void getBookStr(tBook book, int maxSize, char *str);
   
/* Get a book object from its textual representation */
tError getBookObject(const char *str, tBook *book);

/* Compare two books by author name*/
int book_cmp(tBook t1, tBook t2);

/* Copy the book data in src to dst*/
void book_cpy(tBook *dst, tBook src);

/* Initialize the table of books */
void bookTable_init(tBookTable *bookTable);

/* Add a new book to the table of books */
tError bookTable_add(tBookTable *table, tBook book);

/* Find a book in the table */
int bookTable_find(tBookTable table, char *ISBN);

/* Remove the first occurence of a book in the table */
void bookTable_del(tBookTable *table, tBook book);

/* Load the table of books from a file */
tError bookTable_load(tBookTable *table, const char* filename);

/* Save a table of books to a file */
tError bookTable_save(tBookTable table, const char* filename);

void bookTable_filterBySection(

tBookTable tabBook, char sectionID, tBookTable *result);

unsigned int bookTable_getOnLoanNumber(tBookTable tabBook);

unsigned int bookTable_getAuthorNumber(tBookTable tabBook, char *author);

tError bookTable_sortedAdd(tBookTable *tabBook, tBook book);

void bookTable_sort(tBookTable tabBook, tBookTable *result);

/* Release a books table */
void bookTable_release(tBookTable *table);

Data.h

#define SIMPLE_VERSION
//#define COMPLETE_VERSION

/* This code ensures that this file is included only once */
#ifndef __DATA_H
#define __DATA_H
/* If the constant DATA_H is not defined (ifndef), the code is added, otherwise, this code is excluded. When the code is added, the constant is defined, therefore next time this file will be included it will be defined and no inclusion will be done. */

#define MAX_PATHNAME 256
#define MAX_LINE 512
#define MAX_SECTIONS 10
#define MAX_SECTION_NAME 100
#define MAX_BOOKS 300
#define MAX_SUB 10
#define MAX_BOOK_ISBN 14
#define MAX_BOOK_AUTHOR_CODE 4
#define MAX_BOOK_TITLE 101

/* Definition of a boolean type */
typedef enum {FALSE, TRUE} tBoolean;

/* Definition of the error type. */
typedef enum {OK=1, ERROR=0, ERR_CANNOT_READ=-1, ERR_CANNOT_WRITE=-2, ERR_MEMORY=-3, ERR_DUPLICATED_ENTRY=-4, ERR_INVALID_DATA=-5, ERR_ENTRY_NOT_FOUND=-6} tError;

/* Definition of a location */
typedef struct {
    char row;
    char column;
    char shelf;
} tLocation;

/* Definition of a section */
typedef struct {
    char id;
    char name[MAX_SECTION_NAME];
    tLocation init;
} tSection;

/* Table of sections */
typedef struct {
    tSection table[MAX_SECTIONS];
    int size;
} tSectionTable;

/* Definition of a classification */
typedef struct {
    char secId;
    char subId;
} tClass;

/* Definition of the book */
typedef struct {
    char ISBN[MAX_BOOK_ISBN];
    unsigned short year;
    tBoolean avail;
    tClass clas;
    char author[MAX_BOOK_AUTHOR_CODE];
    char title[MAX_BOOK_TITLE];
} tBook;

/* Table of books */
typedef struct {
#ifdef SIMPLE_VERSION
    tBook table[MAX_BOOKS];
#endif
#ifdef COMPLETE_VERSION

#endif   
    int size;
} tBookTable;


/* Definition of the application data structure */
typedef struct {
    /* Path where data will be stored */
    char path[MAX_PATHNAME];
   
    /* sections table */
    tSectionTable sections;
   
    /* Books table */
    tBookTable books;
   
} tAppData;


/* Books of a class */
typedef struct {
    char id;
    /* Table of books of the subsection */
#ifdef SIMPLE_VERSION   
#endif   
#ifdef COMPLETE_VERSION   

#endif
} tSubInfo;

/* Classes of a section */
typedef struct {
    unsigned int totSecSubs;
} tSectionInfo;

#endif /*__DATA_H*/

Books.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "books.h"


void getBookStr(tBook book, int maxSize, char *str) {
    int length;
    unsigned short int tmpAvail;
   
    if (book.avail==TRUE)
        tmpAvail=1;
    else
        tmpAvail=0;
       
    length = snprintf(str,maxSize-1,"%s %hu %hu %c %c %s %s", book.ISBN, book.year, tmpAvail, book.clas.secId, book.clas.subId, book.author, book.title);
    if (length>0)
        str[length]='';
}

tError getBookObject(const char *str, tBook *book) {

    tError retVal = OK;
    unsigned short int tmpAvail;
   
    sscanf(str, "%s %hu %hu %c %c %s %s", book->ISBN, &book->year, &tmpAvail, &book->clas.secId, &book->clas.subId, book->author, book->title);
   
    book->avail = (tmpAvail==1);

    return retVal;
}

void bookTable_init(tBookTable *bookTable) {
    bookTable->size=0;
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6C ********************/
#endif
}

int book_cmp(tBook b1, tBook b2) {
   
    int retVal=0;
   
    if (b1.clas.secId>b2.clas.secId) retVal = 1;
    else if (b1.clas.secId<b2.clas.secId) retVal = -1;
    else {
        if (b1.clas.subId>b2.clas.subId) retVal = 1;
        else if (b1.clas.subId<b2.clas.subId) retVal = -1;
        else {
            retVal = strcmp(b1.author, b2.author);
            if(retVal==0) {
                /* If the author is equal, order by title */
                retVal=strcmp(b1.title, b2.title);
                if (retVal==0)
                    retVal=strcmp(b1.ISBN,b2.ISBN);
            }
        }
    }
   
    return retVal;
}

void book_cpy(tBook *dst, tBook src) {
    strcpy(dst->ISBN,src.ISBN);
    dst->year = src.year;
    dst->avail = src.avail;
    dst->clas.secId = src.clas.secId;
    dst->clas.subId = src.clas.subId;
    strcpy(dst->author, src.author);
    strcpy(dst->title, src.title);
}

tError bookTable_add(tBookTable *tabBook, tBook book) {

    tError retVal = OK;

#ifdef SIMPLE_VERSION
    /* Check if there enough space for the new book */
    if(tabBook->size>=MAX_BOOKS) {
        retVal = ERR_MEMORY;
    }
#endif
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6D ********************/
#endif
    if (retVal==OK){
    /* Add the new book to the end of the table */
        book_cpy(&(tabBook->table[tabBook->size]), book);   
        tabBook->size++;
    }

    return retVal;
}

int bookTable_find(tBookTable tabBook, char *ISBN) {
    int i;
    int idx = -1;
   
    i=0;
    while(i< tabBook.size && idx==-1) {
        /* Check if the id is the same */
        if(strcmp(tabBook.table[i].ISBN,ISBN)==0) {
            /* Get the position of the match */
            idx = i;
        }
        i++;
    }
   
    return idx;
}

void bookTable_del(tBookTable *tabBook, tBook book) {
    int i;
    int pos;

    pos = bookTable_find(*tabBook,book.ISBN);
    if (pos!=-1){
    /* If the book is found, all the rest of the elements are displaced one position */
        for(i=pos;i<tabBook->size-1; i++) {       
            book_cpy(&(tabBook->table[i]), tabBook->table[i+1]);           
        }
        tabBook->size=tabBook->size-1;   
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6E ********************/
#endif
    }
}

tError bookTable_save(tBookTable tabBook, const char* filename) {
    FILE *fout=0;
    int i;
    char str[MAX_LINE];
    tError retVal = OK;
   
    /* Open the output file */
    if((fout=fopen(filename, "w"))!=0) {

        /* Save all books to the file */
        for(i=0;i<tabBook.size;i++) {
            getBookStr(tabBook.table[i], MAX_LINE, str);
            fprintf(fout, "%s ", str);
        }
       
        /* Close the file */
        fclose(fout);
        retVal = OK;
    } else {
        retVal = ERR_CANNOT_WRITE;
    }
   
    return retVal;
}

tError bookTable_load(tBookTable *tabBook, const char* filename) {

    tError retVal = OK;
    FILE *fin=0;   
    char line[MAX_LINE];
    tBook newBook;
   
    /* Initialize the output table */
    bookTable_init(tabBook);
   
    /* Open the input file */
    if((fin=fopen(filename, "r"))==0) {
        retVal = ERR_CANNOT_READ;
    }
   
    /* Read all the books */
    while(retVal==OK && !feof(fin) && tabBook->size<MAX_BOOKS) {
        /* Remove any content from the line */
        line[0] = '';
        /* Read one line and store it in "line" variable */
        fgets(line, MAX_LINE-1, fin);
        /* Ensure that the string is ended by 0*/
        line[MAX_LINE - 1]='';
        if(strlen(line)>0) {
            /* Obtain the object */
            getBookObject(line, &newBook);
            /* Add the new book to the output table */
            bookTable_add(tabBook, newBook);       
        }
    }   
       
    /* Close the file */
    fclose(fin);
   
    return retVal;
}


void bookTable_filterBySection(tBookTable tabBook, char sectionId, tBookTable *result) {
    int i;
   
    bookTable_init(result);
    i=0;
    while(i< tabBook.size) {
        /* Check if the section is the same */
        if(tabBook.table[i].clas.secId==sectionId) {
            /* Add to the result table */
            bookTable_add(result,tabBook.table[i]);
        }
        i++;
    }
}

unsigned int bookTable_getOnLoanNumber(tBookTable tabBook){
    unsigned int i;
    unsigned int numBooks=0;
   
    i=0;
    while(i< tabBook.size) {
        /* Check if the book is not available */
        if(tabBook.table[i].avail!=TRUE) {
            /* Add a unit to the total */
            numBooks++;
        }
        i++;
    }
   
    return numBooks;
}

unsigned int bookTable_getAuthorNumber(tBookTable tabBook, char *author){
    unsigned int i;
    unsigned int numBooks=0;
   
    i=0;
    while(i< tabBook.size) {
        /* Check if the author matches */
        if(strcmp(tabBook.table[i].author,author)==0) {
            /* Add a unit to the total */
            numBooks++;
        }
        i++;
    }
   
    return numBooks;
}

/******************** PR2 - EX1A ********************/
tError bookTable_sortedAdd(tBookTable *tabBook, tBook book){
    tError retVal = OK;

    /* Check if there enough space for the new book */
#ifdef SIMPLE_VERSION

#endif
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6D ********************/
#endif

   
    return retVal;   
}

/******************** PR2 - EX1B ********************/
void bookTable_sort(tBookTable tabBook, tBookTable *result){

}

/* Release a books table */
void bookTable_release(tBookTable *tabBook) {
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6F ********************/
#endif   
}

Explanation / Answer

Stack.h

#include "data.h"


typedef struct {
} tBookStack;

void bookStack_create(tBookStack *stack);

tBoolean bookStack_empty(tBookStack stack);

tError bookStack_push(tBookStack *stack, tBook newElement);

tError bookStack_pop (tBookStack *stack, tBook *element);

void bookStack_transfer(tBookStack *stack_dest, tBookStack *stack);

tError bookStack_search(tBookStack *stack, char *ISBN, tBook *book);

Stack.c

#include "books.h"
#include "stack.h"
#include <string.h>


void bookStack_create(tBookStack *stack) {

}

Books.h

#include "data.h"

/* Get a textual representation of a book */
void getBookStr(tBook book, int maxSize, char *str);
   
/* Get a book object from its textual representation */
tError getBookObject(const char *str, tBook *book);

/* Compare two books by author name*/
int book_cmp(tBook t1, tBook t2);

/* Copy the book data in src to dst*/
void book_cpy(tBook *dst, tBook src);

/* Initialize the table of books */
void bookTable_init(tBookTable *bookTable);

/* Add a new book to the table of books */
tError bookTable_add(tBookTable *table, tBook book);

/* Find a book in the table */
int bookTable_find(tBookTable table, char *ISBN);

/* Remove the first occurence of a book in the table */
void bookTable_del(tBookTable *table, tBook book);

/* Load the table of books from a file */
tError bookTable_load(tBookTable *table, const char* filename);

/* Save a table of books to a file */
tError bookTable_save(tBookTable table, const char* filename);

void bookTable_filterBySection(

tBookTable tabBook, char sectionID, tBookTable *result);

unsigned int bookTable_getOnLoanNumber(tBookTable tabBook);

unsigned int bookTable_getAuthorNumber(tBookTable tabBook, char *author);

tError bookTable_sortedAdd(tBookTable *tabBook, tBook book);

void bookTable_sort(tBookTable tabBook, tBookTable *result);

/* Release a books table */
void bookTable_release(tBookTable *table);

Data.h

#define SIMPLE_VERSION
//#define COMPLETE_VERSION

/* This code ensures that this file is included only once */
#ifndef __DATA_H
#define __DATA_H
/* If the constant DATA_H is not defined (ifndef), the code is added, otherwise, this code is excluded. When the code is added, the constant is defined, therefore next time this file will be included it will be defined and no inclusion will be done. */

#define MAX_PATHNAME 256
#define MAX_LINE 512
#define MAX_SECTIONS 10
#define MAX_SECTION_NAME 100
#define MAX_BOOKS 300
#define MAX_SUB 10
#define MAX_BOOK_ISBN 14
#define MAX_BOOK_AUTHOR_CODE 4
#define MAX_BOOK_TITLE 101

/* Definition of a boolean type */
typedef enum {FALSE, TRUE} tBoolean;

/* Definition of the error type. */
typedef enum {OK=1, ERROR=0, ERR_CANNOT_READ=-1, ERR_CANNOT_WRITE=-2, ERR_MEMORY=-3, ERR_DUPLICATED_ENTRY=-4, ERR_INVALID_DATA=-5, ERR_ENTRY_NOT_FOUND=-6} tError;

/* Definition of a location */
typedef struct {
    char row;
    char column;
    char shelf;
} tLocation;

/* Definition of a section */
typedef struct {
    char id;
    char name[MAX_SECTION_NAME];
    tLocation init;
} tSection;

/* Table of sections */
typedef struct {
    tSection table[MAX_SECTIONS];
    int size;
} tSectionTable;

/* Definition of a classification */
typedef struct {
    char secId;
    char subId;
} tClass;

/* Definition of the book */
typedef struct {
    char ISBN[MAX_BOOK_ISBN];
    unsigned short year;
    tBoolean avail;
    tClass clas;
    char author[MAX_BOOK_AUTHOR_CODE];
    char title[MAX_BOOK_TITLE];
} tBook;

/* Table of books */
typedef struct {
#ifdef SIMPLE_VERSION
    tBook table[MAX_BOOKS];
#endif
#ifdef COMPLETE_VERSION

#endif   
    int size;
} tBookTable;


/* Definition of the application data structure */
typedef struct {
    /* Path where data will be stored */
    char path[MAX_PATHNAME];
   
    /* sections table */
    tSectionTable sections;
   
    /* Books table */
    tBookTable books;
   
} tAppData;


/* Books of a class */
typedef struct {
    char id;
    /* Table of books of the subsection */
#ifdef SIMPLE_VERSION   
#endif   
#ifdef COMPLETE_VERSION   

#endif
} tSubInfo;

/* Classes of a section */
typedef struct {
    unsigned int totSecSubs;
} tSectionInfo;

#endif /*__DATA_H*/

Books.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "books.h"


void getBookStr(tBook book, int maxSize, char *str) {
    int length;
    unsigned short int tmpAvail;
   
    if (book.avail==TRUE)
        tmpAvail=1;
    else
        tmpAvail=0;
       
    length = snprintf(str,maxSize-1,"%s %hu %hu %c %c %s %s", book.ISBN, book.year, tmpAvail, book.clas.secId, book.clas.subId, book.author, book.title);
    if (length>0)
        str[length]='';
}

tError getBookObject(const char *str, tBook *book) {

    tError retVal = OK;
    unsigned short int tmpAvail;
   
    sscanf(str, "%s %hu %hu %c %c %s %s", book->ISBN, &book->year, &tmpAvail, &book->clas.secId, &book->clas.subId, book->author, book->title);
   
    book->avail = (tmpAvail==1);

    return retVal;
}

void bookTable_init(tBookTable *bookTable) {
    bookTable->size=0;
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6C ********************/
#endif
}

int book_cmp(tBook b1, tBook b2) {
   
    int retVal=0;
   
    if (b1.clas.secId>b2.clas.secId) retVal = 1;
    else if (b1.clas.secId<b2.clas.secId) retVal = -1;
    else {
        if (b1.clas.subId>b2.clas.subId) retVal = 1;
        else if (b1.clas.subId<b2.clas.subId) retVal = -1;
        else {
            retVal = strcmp(b1.author, b2.author);
            if(retVal==0) {
                /* If the author is equal, order by title */
                retVal=strcmp(b1.title, b2.title);
                if (retVal==0)
                    retVal=strcmp(b1.ISBN,b2.ISBN);
            }
        }
    }
   
    return retVal;
}

void book_cpy(tBook *dst, tBook src) {
    strcpy(dst->ISBN,src.ISBN);
    dst->year = src.year;
    dst->avail = src.avail;
    dst->clas.secId = src.clas.secId;
    dst->clas.subId = src.clas.subId;
    strcpy(dst->author, src.author);
    strcpy(dst->title, src.title);
}

tError bookTable_add(tBookTable *tabBook, tBook book) {

    tError retVal = OK;

#ifdef SIMPLE_VERSION
    /* Check if there enough space for the new book */
    if(tabBook->size>=MAX_BOOKS) {
        retVal = ERR_MEMORY;
    }
#endif
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6D ********************/
#endif
    if (retVal==OK){
    /* Add the new book to the end of the table */
        book_cpy(&(tabBook->table[tabBook->size]), book);   
        tabBook->size++;
    }

    return retVal;
}

int bookTable_find(tBookTable tabBook, char *ISBN) {
    int i;
    int idx = -1;
   
    i=0;
    while(i< tabBook.size && idx==-1) {
        /* Check if the id is the same */
        if(strcmp(tabBook.table[i].ISBN,ISBN)==0) {
            /* Get the position of the match */
            idx = i;
        }
        i++;
    }
   
    return idx;
}

void bookTable_del(tBookTable *tabBook, tBook book) {
    int i;
    int pos;

    pos = bookTable_find(*tabBook,book.ISBN);
    if (pos!=-1){
    /* If the book is found, all the rest of the elements are displaced one position */
        for(i=pos;i<tabBook->size-1; i++) {       
            book_cpy(&(tabBook->table[i]), tabBook->table[i+1]);           
        }
        tabBook->size=tabBook->size-1;   
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6E ********************/
#endif
    }
}

tError bookTable_save(tBookTable tabBook, const char* filename) {
    FILE *fout=0;
    int i;
    char str[MAX_LINE];
    tError retVal = OK;
   
    /* Open the output file */
    if((fout=fopen(filename, "w"))!=0) {

        /* Save all books to the file */
        for(i=0;i<tabBook.size;i++) {
            getBookStr(tabBook.table[i], MAX_LINE, str);
            fprintf(fout, "%s ", str);
        }
       
        /* Close the file */
        fclose(fout);
        retVal = OK;
    } else {
        retVal = ERR_CANNOT_WRITE;
    }
   
    return retVal;
}

tError bookTable_load(tBookTable *tabBook, const char* filename) {

    tError retVal = OK;
    FILE *fin=0;   
    char line[MAX_LINE];
    tBook newBook;
   
    /* Initialize the output table */
    bookTable_init(tabBook);
   
    /* Open the input file */
    if((fin=fopen(filename, "r"))==0) {
        retVal = ERR_CANNOT_READ;
    }
   
    /* Read all the books */
    while(retVal==OK && !feof(fin) && tabBook->size<MAX_BOOKS) {
        /* Remove any content from the line */
        line[0] = '';
        /* Read one line and store it in "line" variable */
        fgets(line, MAX_LINE-1, fin);
        /* Ensure that the string is ended by 0*/
        line[MAX_LINE - 1]='';
        if(strlen(line)>0) {
            /* Obtain the object */
            getBookObject(line, &newBook);
            /* Add the new book to the output table */
            bookTable_add(tabBook, newBook);       
        }
    }   
       
    /* Close the file */
    fclose(fin);
   
    return retVal;
}


void bookTable_filterBySection(tBookTable tabBook, char sectionId, tBookTable *result) {
    int i;
   
    bookTable_init(result);
    i=0;
    while(i< tabBook.size) {
        /* Check if the section is the same */
        if(tabBook.table[i].clas.secId==sectionId) {
            /* Add to the result table */
            bookTable_add(result,tabBook.table[i]);
        }
        i++;
    }
}

unsigned int bookTable_getOnLoanNumber(tBookTable tabBook){
    unsigned int i;
    unsigned int numBooks=0;
   
    i=0;
    while(i< tabBook.size) {
        /* Check if the book is not available */
        if(tabBook.table[i].avail!=TRUE) {
            /* Add a unit to the total */
            numBooks++;
        }
        i++;
    }
   
    return numBooks;
}

unsigned int bookTable_getAuthorNumber(tBookTable tabBook, char *author){
    unsigned int i;
    unsigned int numBooks=0;
   
    i=0;
    while(i< tabBook.size) {
        /* Check if the author matches */
        if(strcmp(tabBook.table[i].author,author)==0) {
            /* Add a unit to the total */
            numBooks++;
        }
        i++;
    }
   
    return numBooks;
}

/******************** PR2 - EX1A ********************/
tError bookTable_sortedAdd(tBookTable *tabBook, tBook book){
    tError retVal = OK;

    /* Check if there enough space for the new book */
#ifdef SIMPLE_VERSION

#endif
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6D ********************/
#endif

   
    return retVal;   
}

/******************** PR2 - EX1B ********************/
void bookTable_sort(tBookTable tabBook, tBookTable *result){

}

/* Release a books table */
void bookTable_release(tBookTable *tabBook) {
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6F ********************/
#endif   
}