URGENT HELP In this exercise, we have to define a type of data, which is capable
ID: 3842708 • Letter: U
Question
URGENT HELP
In this exercise, we have to define a type of data, which is capable to store the information of one section of the library. Each section is formed by different subsections and each subsection has a list of different books.
So I guess it is like we have to create a structure which is capable to save and classify the books according ti the Section>Subsection they are
And to do so we have to complete the implementation of the types of data tSubInfo and tSectionInfo , in data.h
tSubInfo
id: subsection identifier (one character)
subBooks: Vector with the positions we find the bools of this subsection within the table of books
totSubBooks: number of books that the section contains
tSectionInfo
section: Structure of the type tSection
secSubs: Vector with all the subsections (type tSubInfo) of the section (max 10 sections) that have at least one book.
totSecSubs: number of subsections within the section
totSecBooks: number of books within the section
*I include data.h where the changes need to be made as well as the executable books.c as well as other.h files that may be necessary in order to execute the program
DATA.H
/* Uncomment the practice version you want to run */
#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
#include
#include
#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
#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 else {
if (b1.clas.subId>b2.clas.subId) retVal = 1;
else if (b1.clas.subId 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
#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;isize-1; i++) {
book_cpy(&(tabBook->table[i]), tabBook->table[i+1]);
}
tabBook->size=tabBook->size-1;
#ifdef COMPLETE_VERSION
#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 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 /* 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;
}
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
#endif
return retVal;
}
void bookTable_sort(tBookTable tabBook, tBookTable *result){
}
/* Release a books table */
void bookTable_release(tBookTable *tabBook) {
#ifdef COMPLETE_VERSION
#endif
}
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);
API.H
#include "data.h"
/*
* Methods for application data management
*/
/* Initialize the application data */
void appData_init(tAppData *object);
/* Load the application data from file */
tError appData_load(tAppData *object);
/* Save the application data to a file */
tError appData_save(tAppData object);
/* Allow to assign a path to the application data */
void appData_setPath(tAppData *object, const char *path);
/*
* API
*/
/* Return a table with the books */
tError getBooks(tAppData object, tBookTable *result);
/* Get the section information */
tError getBook(tAppData object, char *ISBN, tBook *book);
/* Add a new book */
tError addBook(tAppData *object, tBook book);
/* Remove a certain book */
tError removeBook(tAppData *object, tBook book);
/* Return the table of sections */
tError getSections(tAppData object, tSectionTable *result);
/* Get the section information */
tError getSection(tAppData object, char C1, tSection *section);
/* Add a new section */
tError addSection(tAppData *object, tSection section);
INFO.H
#include "data.h"
tError si_getSectionInfo(tBookTable tabB, tSectionTable tabS, char sectionId, tSectionInfo *si );
tBook si_getBook(tBookTable tabB, tSectionInfo si, unsigned int nSub, unsigned int nBook);
void si_listSectionInfo(tBookTable tabB, tSectionInfo si );
MENU.H
include "data.h"
#include "api.h"
/* Request an option to the user and check its validity */
int getOption(int numOptions);
/* Define the main menu options type */
typedef enum {MAIN_MENU_LOAD, MAIN_MENU_SAVE, MAIN_MENU_BOOKS, MAIN_MENU_SECTIONS, MAIN_MENU_STATS, MAIN_MENU_EXIT} tMainMenuOptions;
/* Define the book management menu options type */
typedef enum {BOOK_MENU_LIST, BOOK_MENU_ADD, BOOK_MENU_DEL, BOOK_MENU_SORT, BOOK_MENU_EXIT} tBookMenuOptions;
/* Define the section management menu options type */
typedef enum {SEC_MENU_LIST, SEC_MENU_ADD, SEC_MENU_INFO, SEC_MENU_EXIT} tSectionMenuOptions;
/* Define the status menu options type */
typedef enum {STAT_MENU_ON_LOAN, STAT_MENU_AUTHOR, STAT_MENU_SECTION, STAT_MENU_EXIT} tStatsMenuOptions;
/* Print the main menu options */
void printMainMenuOptions();
/* Get the option for the main menu */
tMainMenuOptions getMenuOption();
/* Perform the actions for the main menu */
void mainMenu(tAppData *appData);
/* Print the book management menu options */
void printBookMenuOptions();
/* Get the option for the book management menu */
tBookMenuOptions getBooksMenuOption();
/* Perform the actions for the book management menu */
void bookMenu(tAppData *appData);
/* Print the section management menu options */
void printSectionMenuOptions();
/* Get the option for the section management menu */
tSectionMenuOptions getSectionMenuOption();
/* Perform the actions for the section management menu */
void secMenu(tAppData *appData);
/* Print the stats menu options */
void printStatsMenuOptions();
/* Get the option for the status menu */
tStatsMenuOptions getStatsMenuOption();
/* Perform the actions for the status menu */
void statsMenu(tAppData appData);
SECTIONS.H
#include "data.h"
/* Get a textual representation of a section */
void getSectionStr(tSection section, int maxSize, char *str);
/* Get a section object from its textual representation */
tError getSectionObject(const char *str, tSection *section);
/* Compare two sections */
int section_cmp(tSection s1, tSection s2);
/* Copy the section data in src to dst*/
void section_cpy(tSection *dst, tSection src);
/* Initialize the table of sections */
void secTable_init(tSectionTable *tabSec);
/* Add a new section to the table of sections */
tError secTable_add(tSectionTable *tabSec, tSection section);
/* Find a section in the table */
int secTable_find(tSectionTable tabSec, char sectionId);
/* Remove the first occurence of a section in the table */
void secTable_del(tSectionTable *tabSec, tSection section);
/* Load the table of sections from a file */
tError secTable_load(tSectionTable *tabSec, const char* filename);
/* Save a table of sections to a file */
tError secTable_save(tSectionTable tabSec, const char* filename);
Explanation / Answer
Following is the code from DATA.H with updated tSubInfo and tSectionInfo.
/* Uncomment the practice version you want to run */
#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;
unsigned int totSubBooks;
int posi[MAX_BOOKS];
/* Table of books of the subsection */
#ifdef SIMPLE_VERSION
#endif
#ifdef COMPLETE_VERSION
#endif
} tSubInfo;
/* Classes of a section */
typedef struct {
tSection section;
tSubinfo subinfo[10];
unsigned int totSecSubs;
unsigned int totSecBooks;
} tSectionInfo;
#endif /*__DATA_H*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.