#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct book_type {
char title[100],authorFirstName[100],authorLastName[100];
int year;
float rCost;
}node;
// This function loads a catalogue of books from a user specified file name.
// Note you need to use malloc inside this function to allocate the memory
// needed for the catalogue of books.
// After catalogue is allocated and filled up with books loaded from the file,
// it is returned back to the main program via the book_type * return type.
// The pointer parameter numBooks is used to pass the number of books back
// out to the numBooks variable in the main function. This way the other
// functions will be able to use this variable to find how many books
// are stored in the catalogue.
struct book_type * loadCatalogue(int * numBooks);
// This function saves catalogue into a user specified text file
void saveCatalogue(struct book_type * bCatalogue, int numBooks);
// This function displays the catalogue onto the screen.
void displayCatalogue(struct book_type * bCatalogue, int numBooks);
// This function finds a user specified book or set of books and displays them
void findBook(struct book_type * bCatalogue, int numBooks);
//This function finds a user specified book or set of books and deletes them
struct book_type * deleteBook(struct book_type * bCatalogue, int numBooks);
int main() {
struct book_type * bookCatalogue=NULL;
int numBooks,choice=0;
//This loop goes on untill the user gives 6 as a choice
while(choice!=6){
printf("\nChoose one of the following options:\n1. Load catalogue from file\n2. Save catalogue to file\n3. Display catalogue\n4. Find book from catalogue\n5. Delete book from catalogue\n6. Quit\n");
scanf("%d",&choice);
fflush(stdin);
switch(choice){
case 1:
bookCatalogue = loadCatalogue(&numBooks);//calls the load catalogue function
break;
case 2:
saveCatalogue(bookCatalogue, numBooks);//calls the function to save the catalogue
break;
case 3:
displayCatalogue(bookCatalogue, numBooks); // calls the function to display the catalogue on screen
break;
case 4:
findBook(bookCatalogue, numBooks); //calls the function to search for a book in a catalogue
break;
case 5:
bookCatalogue = deleteBook(bookCatalogue, numBooks); //calls the function to delete a book from the catalogue
break;
case 6:
printf("Program will exit now\n");
break;
default:
printf("Wrong option selected, please select again\n"); // If user gives an invalid input, ask him to re-enter
break;
}
}
return 0;
}
struct book_type * loadCatalogue(int * numBooks) {
struct book_type *bCatalogue;
char fileName[100],line[100];
int i=0;
FILE *fp;
bCatalogue=(node *)malloc(sizeof(node)); //allocate memory
printf("Enter the name of the file to open:\n");
gets(fileName);// Take filename as input
//fflush(stdin);
fp=fope ...
1. #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct book_type {
char
title[100],authorFirstName[100],authorLastName[100];
int year;
float rCost;
}node;
// This function loads a catalogue of books from a user specified
file name.
// Note you need to use malloc inside this function to allocate
the memory
// needed for the catalogue of books.
// After catalogue is allocated and filled up with books loaded
from the file,
// it is returned back to the main program via the book_type *
return type.
// The pointer parameter numBooks is used to pass the number
2. of books back
// out to the numBooks variable in the main function. This way
the other
// functions will be able to use this variable to find how many
books
// are stored in the catalogue.
struct book_type * loadCatalogue(int * numBooks);
// This function saves catalogue into a user specified text file
void saveCatalogue(struct book_type * bCatalogue, int
numBooks);
// This function displays the catalogue onto the screen.
void displayCatalogue(struct book_type * bCatalogue, int
numBooks);
// This function finds a user specified book or set of books and
displays them
void findBook(struct book_type * bCatalogue, int numBooks);
3. //This function finds a user specified book or set of books and
deletes them
struct book_type * deleteBook(struct book_type * bCatalogue,
int numBooks);
int main() {
struct book_type * bookCatalogue=NULL;
int numBooks,choice=0;
//This loop goes on untill the user gives 6 as a choice
while(choice!=6){
printf("nChoose one of the following options:n1.
Load catalogue from filen2. Save catalogue to filen3. Display
cataloguen4. Find book from cataloguen5. Delete book from
cataloguen6. Quitn");
scanf("%d",&choice);
fflush(stdin);
switch(choice){
case 1:
bookCatalogue =
loadCatalogue(&numBooks);//calls the load catalogue function
4. break;
case 2:
saveCatalogue(bookCatalogue,
numBooks);//calls the function to save the catalogue
break;
case 3:
displayCatalogue(bookCatalogue,
numBooks); // calls the function to display the catalogue on
screen
break;
case 4:
findBook(bookCatalogue, numBooks);
//calls the function to search for a book in a catalogue
break;
case 5:
bookCatalogue =
deleteBook(bookCatalogue, numBooks); //calls the function to
delete a book from the catalogue
break;
case 6:
printf("Program will exit nown");
5. break;
default:
printf("Wrong option selected, please select
againn"); // If user gives an invalid input, ask him to re-enter
break;
}
}
return 0;
}
struct book_type * loadCatalogue(int * numBooks) {
struct book_type *bCatalogue;
char fileName[100],line[100];
int i=0;
FILE *fp;
bCatalogue=(node *)malloc(sizeof(node)); //allocate
memory
printf("Enter the name of the file to open:n");
gets(fileName);// Take filename as input
6. //fflush(stdin);
fp=fopen(fileName,"r");//opens the file in read only mode
if(fp==NULL){//If file cannot be opened due to some
reason than print the error
perror(fileName);
return NULL;
}
while ( fgets ( line, sizeof(line), fp ) != NULL ) /* read a
line */
{
//First line represents the number of books
if(!i)
{
sscanf(line,"%d",&numBooks);
continue;
}
//Take different attributes of book and place them in
proper variables.
if((i-1)%4==2)
7. {
sscanf(line,"%s",bCatalogue[i/4].title);
}
//Since first name and last name are in the same line,
we need to split it from ','
else if((i-1)%4==3)
{
char fName[100],lName[100];
int j;
for(j=0;line[j]!=',';j++)
{
fName[j]=line[j];
}
fName[j++]='0';
for(;line[j]!='0';j++)
{
lName[j]=line[j];
}
lName[j]='0';
9. void saveCatalogue(struct book_type * bCatalogue, int
numBooks) {
if(bCatalogue==NULL)
printf("Please load the the book catalogue firstn");
else
{
char fileName[100], name[200],output[20];
FILE *fp=NULL;
int i=0,j=0,k=0;
printf("Enter the name of the file to save:n");
gets(fileName);
fp=fopen(fileName,"w");//open a new file in write
only mode
if(fp==NULL){
perror(fileName);
}
else
{
sprintf(output,"%d",numBooks);
11. fputs(output,fp);
fwrite("n", sizeof(char), 1, fp);
sprintf(output,"%f",bCatalogue[i/4].rCost);
fputs(output,fp);
fwrite("n", sizeof(char), 1, fp);
i++;
}
}
fclose(fp);//close the file
}
}
void displayCatalogue(struct book_type * bCatalogue, int
numBooks) {
if(bCatalogue==NULL)
printf("Please load the the book catalogue firstn");
else
{
12. int i=0;
printf("Number of books in catalogue = %dn----------
-----------------------------------------------------n",numBooks);
while(i<numBooks)
{
printf("Title: %snAuthor: %s %snYear of
publication: %dnReplacement cost: $%fn-------------------------
--------------------------------------
n",bCatalogue[i].title,bCatalogue[i].authorFirstName,bCatalogu
e[i].authorLastName,bCatalogue[i].year,bCatalogue[i].rCost);
i++;
}
}
}
void findBook(struct book_type * bCatalogue, int numBooks) {
if(bCatalogue==NULL)
printf("Please load the the book catalogue firstn");
else
{
int flag=0,i=0,choice=0;
13. char find[100];
printf("nChoose one of the following options:n1.
Specify book titlen2. Specify author first namen3. Specify
author last namen");
scanf("%d",&choice);
if(choice==1)
{
printf("Enter book title you want to search
forn");
gets(find);
while(i<numBooks)
{
if(!(strcmpi(find,bCatalogue[i].title)))
{
displayCatalogue(&bCatalogue[i],1);
flag=1;
}
i++;
}
14. }
else if(choice==2){
printf("Enter author first name that you want to
search forn");
gets(find);
while(i<numBooks)
{
if(!(strcmpi(find,bCatalogue[i].authorFirstName)))
{
displayCatalogue(&bCatalogue[i],1);
flag=1;
}
i++;
}
}
else if(choice==3){
printf("Enter author last name that you want to
search forn");
16. struct book_type * deleteBook(struct book_type * bCatalogue,
int numBooks) {
if(bCatalogue==NULL)
printf("Please load the the book catalogue firstn");
else
{
int flag=0,i=0,j=0,choice;
char find[100];
struct book_type *bCatalogue1;
bCatalogue1=(node *)malloc(sizeof(node));
printf("nChoose one of the following options:n1.
Specify book titlen2. Specify author first namen3. Specify
author last namen");
scanf("%d",&choice);
if(choice==1)
{
printf("Enter book title you want to search
forn");
gets(find);
while(i<numBooks)
20. printf("Wrong choicen");
if(flag==0)
printf("No search result foundn");
return bCatalogue1;
}
return NULL;
}
1
<Name>
<Course>
<Date>
<Instructor>
Typing Template for GCU Papers for Lower Division Courses
Formatting: This is an electronic template for papers written in
GCU style. The purpose of the template is to help you follow
the basic writing expectations for beginning your coursework at
GCU. Margins are set at 1 inch for top, bottom, left, and right.
Each paragraph is indented five spaces. It is best to use the tab
key to indent. The line spacing is double throughout the paper,
even on the reference page. The font style used in this template
is Times New Roman. The font size is 12. When you are ready
to write, and after having read these instructions completely,
you can delete these directions and start typing. The formatting
21. should stay the same. If you have any questions, please consult
with your instructor.
Citations: Citations are used to reference material from another
source. When paraphrasing material from another source (such
as a book, journal, Web site, etc.), include the author’s last
name and the publication year in parentheses.When directly
quoting material word-for-word from another source, use
quotation marks and include the page number after the author’s
last name and year.
Using citations to give credit to others whose ideas or words
you have used is an essential requirement to avoid issues of
plagiarism. Just as you would never steal someone else’s car,
you should not steal their words either. To avoid potential
problems, always be sure to cite your sources by referring to the
author’s last name and the year of publication in parentheses at
the end of the sentence, such as (Daresh, 2004) and page
numbers if you are using word-for-word materials, such as
“There are no simple strategies for accomplishing successful
transitions, but we do know a great deal about how to get off to
a good start” (King & Blumer, 2000, p. 356).
The reference list should appear at the end of a paper (see the
next page). It provides the information necessary for a reader to
locate and retrieve any source you cite in the body of the paper.
Each source you cite in the paper must appear in your reference
list; likewise, each entry in the reference list must be cited in
your text. A sample reference page is included below; this page
includes examples of how to format different reference types
(e.g., books, journal articles, a Web site).
References
Arnold, J. B., & Dodge, H. W. (1994). Room for all. The
American School Board Journal, 181(10), 22-26.
Black, J. A., & English, F. W. (1986). What they don’t tell you
in schools of education about school administration. Lancaster,
PA: Technomic.
22. Daresh, J. C. (2004). Beginning the assistant principalship: A
practical guide for newschool administrators. Thousand Oaks,
CA: Corwin.
King, M., & Blumer, I. (2000). A good start. Phi Delta Kappan,
81(5), 356-360.
USA swimming. (n.d.). Retrieved August 24, 2004, from
http://www.usaswimming.org/usasweb/DesktopDefault.aspx