#include stdio.h#include stdlib.h#include string.ht.docx

katherncarlyle 13 views 22 slides Oct 31, 2022
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

#include

#include

#include

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 ...


Slide Content

#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=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)

{

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

sscanf(fName,"%s",bCatalogue[i/4].authorFirstName);


sscanf(fName,"%s",bCatalogue[i/4].authorLastName);

}

else if((i-1)%4==0)

{

sscanf(line,"%d",&bCatalogue[i/4].year);

}

else if((i-1)%4==1)

{

sscanf(line,"%f",&bCatalogue[i/4].rCost);

}

i++;

}

fclose ( fp );

return bCatalogue;

}

void saveCatalogue(struct book_type * bCatalogue, int
numBooks) {

if(bCatalogue==NULL)

printf("Please load the the book catalogue first\n");

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

fputs(output,fp);

fwrite("\n", sizeof(char), 1, fp);

while(i<4*numBooks)

{

fputs(bCatalogue[i/4].title,fp);

fwrite("\n", sizeof(char), 1, fp);


for(;bCatalogue[i/4].authorFirstName[j]!='\0';j++)


name[j]=bCatalogue[i/4].authorFirstName[j];

name[j]=',';

name[++j]=' ';


for(;bCatalogue[i/4].authorLastName[k]!='\0';k++,j++)


name[j]=bCatalogue[i/4].authorLastName[k];

name[j]='\0';

fputs(name,fp);

fwrite("\n", sizeof(char), 1, fp);

sprintf(output,"%d",bCatalogue[i/4].year);

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 first\n");

else

{

int i=0;

printf("Number of books in catalogue = %d\n----------
-----------------------------------------------------\n",numBooks);

while(i<numBooks)

{

printf("Title: %s\nAuthor: %s %s\nYear of
publication: %d\nReplacement cost: $%f\n-------------------------
--------------------------------------
\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 first\n");

else

{

int flag=0,i=0,choice=0;

char find[100];

printf("\nChoose one of the following options:\n1.
Specify book title\n2. Specify author first name\n3. Specify
author last name\n");

scanf("%d",&choice);

if(choice==1)

{

printf("Enter book title you want to search
for\n");

gets(find);

while(i<numBooks)

{

if(!(strcmpi(find,bCatalogue[i].title)))

{

displayCatalogue(&bCatalogue[i],1);

flag=1;

}

i++;

}

}

else if(choice==2){

printf("Enter author first name that you want to
search for\n");

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 for\n");

gets(find);

while(i<numBooks)

{


if(!(strcmpi(find,bCatalogue[i].authorLastName)))

{

displayCatalogue(&bCatalogue[i],1);

flag=1;

}

i++;

}

}

else

printf("Wrong choice\n");

if(flag==0)

printf("No search result found\n");

}

}

struct book_type * deleteBook(struct book_type * bCatalogue,
int numBooks) {

if(bCatalogue==NULL)

printf("Please load the the book catalogue first\n");

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 title\n2. Specify author first name\n3. Specify
author last name\n");

scanf("%d",&choice);

if(choice==1)

{

printf("Enter book title you want to search
for\n");

gets(find);

while(i<numBooks)

{

if(!(strcmpi(find,bCatalogue[i].title)))

{

displayCatalogue(&bCatalogue[i],1);

flag=1;

}

else

{

bCatalogue1[j]=bCatalogue[i];//add
book if the user doesnot specified this book to be deleted

j++;

}

i++;

}

}

else if(choice==2){

printf("Enter author first name that you want to
search for\n");

gets(find);

while(i<numBooks)

{


if(!(strcmpi(find,bCatalogue[i].authorFirstName)))

{

displayCatalogue(&bCatalogue[i],1);

flag=1;

}

else

{

bCatalogue1[j]=bCatalogue[i];//add
book if the user doesnot specified this book to be deleted

j++;

}

i++;

}

}

else if(choice==3){

printf("Enter author last name that you want to
search for\n");

gets(find);

while(i<numBooks)

{


if(!(strcmpi(find,bCatalogue[i].authorLastName)))

{

displayCatalogue(&bCatalogue[i],1);

flag=1;

}

else

{

bCatalogue1[j]=bCatalogue[i];//add
book if the user doesnot specified this book to be deleted

j++;

}

i++;

}

}

else

printf("Wrong choice\n");

if(flag==0)

printf("No search result found\n");

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

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.

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
Tags