Question: C Language. Loops, Switches. Thats the typical c program. It has the menu with 5 options + exit. The idea is that when I type

C Language. Loops, Switches.

Thats the typical c program. It has the menu with 5 options + exit. The idea is that when I type for example 3 , which corresponds to another menu called "Manage books" , this menu opens and displays the options. The info is as follows:

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);

and

menu.c

include #include "menu.h" #include "sections.h" #include "books.h" tError readBook(tBook *book); int getOption(int numOptions) { int option=0; while(option<1 || option>numOptions) { /* Read the input */ printf(">> "); scanf("%u", &option); getchar(); /* Check the user input */ if(option<1 || option>numOptions) { printf("Incorrect option. "); } } return option; } tError readBook(tBook *book) { tError retVal = OK; /* Request information from user */ /******************** PR1 - EX 2B ********************/ printf("ISBN (13 digits) : >> "); printf("Publication year: >> "); getchar(); printf("Is it available?: (1 yes, 0 no) >> "); getchar(); printf("Section (1 digit): >> "); getchar(); printf("Subsection (1 digit): >> "); getchar(); printf("Author Code (3 char): >> "); printf("Title: (no whitespaces) >> "); return retVal; } tError readSection(tSection *section) { tError retVal = OK; printf("Section ID: >> "); scanf("%c", ion->id); getchar(); printf("Name: >> "); scanf("%s", section->name); getchar(); return retVal; } void printSectionsTable(

tSectionTable table) { int i; char bookStr[MAX_LINE]; if(table.size==0) { printf("No sections. "); } else { for(i=0;i getSectionStr(table.table[i], MAX_LINE, bookStr); printf("[%d] - %s ", i+1, bookStr); } } } void printBookTable(tBookTable table) { int i; char bookStr[MAX_LINE]; if(table.size==0) { printf("No books. "); } else { for(i=0;i getBookStr(table.table[i], MAX_LINE, bookStr); printf("[%d] - %s ", i+1, bookStr); } } } /***************** ** MAIN MENU ** *****************/ void printMainMenuOptions() { /* Show menu options */ printf("======================= "); printf(" Main Menu "); printf("======================= "); printf("1) Load data from file "); printf("2) Save data to file "); printf("3) Manage books "); printf("4) Manage sections "); printf("5) View statistics "); printf("6) Exit "); } tMainMenuOptions getMainMenuOption() { /* Convert to the correct type */ return (tMainMenuOptions)(getOption(6)-1); } void mainMenu(tAppData *appData) { tMainMenuOptions option; /* Assign the data path */ appData_setPath(appData,"../"); /* Start the menu */ /* Show list of options and ask the user for an option */ printf("Ch "); printMainMenuOptions(); option=getMainMenuOption(); /* Do the action for the given option */ switch(option) { case MAIN_MENU_LOAD: if(appData_load(appData)==OK) { printf("Data loaded "); } else { printf("No previous data loaded "); } break; case MAIN_MENU_SAVE: if(appData_save(*appData)==OK) { printf("Data saved "); } else { printf("Cannot save the data "); } break; case MAIN_MENU_BOOKS: if(appData_load(appData)==OK) { printf("Data loaded "); } else { printf("No previous data loaded "); } } } /********************************* ** BOOKS MANAGEMENT MENU ** *********************************/ void printBookMenuOptions() { /* Show menu options */ printf("======================= "); printf(" Manage Books "); printf("======================= "); printf("1) List books "); printf("2) Add book "); printf("3) Delete book "); printf("4) Sort books "); printf("5) Exit "); } tBookMenuOptions getBooksMenuOption() { /* Convert to the correct type */ return (tBookMenuOptions)(getOption(5)-1); } void bookMenu(tAppData *appData) { tBookMenuOptions option; tBook newBook; tBookTable bookTable; char bookStr[MAX_LINE]; int pos=0; /* Start the menu */ do { /* Show list of options and ask the user for an option */ printBookMenuOptions(); option=getBooksMenuOption(); /* Do the action for the given option */ switch(option) { case BOOK_MENU_LIST: if(getBooks(*appData, &bookTable)==OK) { /* Print the books */ printBookTable(bookTable); } break; case BOOK_MENU_ADD: printf("Enter the information for the new book: "); if(readBook(&newBook)==OK) { addBook(appData, newBook); } getBookStr(newBook, MAX_LINE, bookStr); printf("Book added: %s ", bookStr); break; case BOOK_MENU_DEL: if(getBooks(*appData, &bookTable)==OK) { /* Print the books */ printBookTable(bookTable); /* Ask the number of the register to be removed */ pos=getOption(bookTable.size); /* Remove the selected book */ removeBook(appData, bookTable.table[pos-1]); } break; case BOOK_MENU_SORT: break; case BOOK_MENU_EXIT: break; } } while(option!=BOOK_MENU_EXIT); } /******************************** ** SECTION MANAGEMENT MENU ** ********************************/ void printSectionMenuOptions() { /* Show menu options */ printf("======================= "); printf(" Manage sections "); printf("======================= "); printf("1) List sections "); printf("2) Add section "); printf("3) Section info "); printf("4) Exit "); } tSectionMenuOptions getSectionMenuOption() { /* Convert to the correct type */ return (tSectionMenuOptions)(getOption(4)-1); } void secMenu(tAppData *appData) { tSectionMenuOptions option; tSectionTable sections; tSection newSection; tError retVal = OK; /* Start the menu */ do { /* Show list of options and ask the user for an option */ printSectionMenuOptions(); option=getSectionMenuOption(); /* Do the action for the given option */ switch(option) { case SEC_MENU_LIST: getSections(*appData, ions); printSectionsTable(sections); break; case SEC_MENU_ADD: if(readSection(&newSection)==OK) { retVal=addSection(appData, newSection); if(retVal==ERR_DUPLICATED_ENTRY) { printf("ERROR: A section with the same ID already exists "); } } break; case SEC_MENU_INFO: break; case SEC_MENU_EXIT: break; } } while(option!=SEC_MENU_EXIT); } /****************** ** STATS MENU ** ******************/ void printStatsMenuOptions() { /* Show menu options */ printf("======================= "); printf(" Stats "); printf("======================= "); printf("1) Get number of books on loan "); printf("2) Get number of books from an author "); printf("3) Get number of books in a section "); printf("4) Exit "); } tStatsMenuOptions getStatusMenuOption() { /* Convert to the correct type */ return (tStatsMenuOptions)(getOption(4)-1); } void statsMenu(tAppData appData) { tStatsMenuOptions option; tBookTable bookTable; tBookTable bookTableFilt; char author[MAX_BOOK_AUTHOR_CODE]; char sectionId; /* Start the menu */ do { /* Show list of options and ask the user for an option */ printStatsMenuOptions(); option=getStatusMenuOption(); getBooks(appData, &bookTable); /* Do the action for the given option */ switch(option) { case STAT_MENU_ON_LOAN: printf("Number of books on loan: %d ", bookTable_getOnLoanNumber(bookTable)); break; case STAT_MENU_AUTHOR: printf("Introduce author key: >> "); scanf("%s",author); printf("Number of books from %s: %d ", author, bookTable_getAuthorNumber(bookTable, author)); break; case STAT_MENU_SECTION: printf("Introduce section ID: >> "); scanf("%c",ionId); bookTable_filterBySection(bookTable,sectionId,&bookTableFilt); printf("Number of books in %c: %d ", sectionId, bookTableFilt.size); break; case STAT_MENU_EXIT: break; } } while(option!=STAT_MENU_EXIT); }

Extra Info:

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); data.h /* 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_AUTHOR_CODE 4 /* 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]; unsigned int size; } tSectionTable; / /* Definition of the book */ typedef struct { } tBook; /* Table of books */ typedef struct { tBook table[MAX_BOOKS]; unsigned 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; #endif /*__DATA_H*/ section.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); book.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);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!