Current location - Training Enrollment Network - Books and materials - Find the system code of small library (data structure C)
Find the system code of small library (data structure C)
This program is for reference only.

# include & ltstdio.h & gt

# include & ltmath.h & gt

# include & ltstring.h & gt

# include & ltstdlib.h & gt

Structural Books _ List

{

Char author [20]; /* Author's name */

char bookname[20]; /* Title */

char publisher[20]; /* Publisher */

char Pb time[ 15]; /* Date of publication */

char loginnum[ 10]; /* Login number */

Floating price; /* Price */

char classfy[ 10]; /* Classification number */

Struct books _ list * next/* pointer field of linked list */

};

struct Books _ list * Create _ Books _ Doc(); /* Create a new linked list */

void insert doc(struct books _ list * head); /* Insert */

void delete doc(struct books _ list * head,int num); /* Delete */

void Print _ Book _ Doc(struct books _ list * head); /* Browse */

void search _ book(struct books _ list * head); /* Query */

void info _ change(struct books _ list * head); /* Modify */

void save(struct books _ list * head); /* Save data to a file */

/* Create a new linked list header node */

struct Books _ list * Create _ Books _ Doc()

{

Structure books _ list * head

head =(struct books _ list *)malloc(size of(struct books _ list)); /* Allocate head node space */

head->; Next = NULL/* The pointer field of the head node is initialized and set to null */

Return head;

}

/* Save data to a file */

void save(struct books_list * head)

{

Structure books _ list * p;;

FILE * fp

P = head;

fp=fopen("data.txt "," w+"); /* Create and open the data.txt file by writing */

fprintf(fp,"┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n”); /* Output the table to a file */

Fprintf(fp,"┃ registration number ┃ title ┃ author ┃ publishing unit ┃ publishing time ┃ classification number ┃ price \ n);

fprintf(fp,"┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n”);

/* The pointer moves from the head node to the tail node, and the book information is output in turn */

while(p->; Au Suivant! = empty)

{

p = p-& gt; Next;

fprintf(fp,"┃%-6.6s┃%- 10. 10s┃%- 10. 10s┃%- 10. 10s┃%- 12. 12s┃%-6.6s┃%.2f ┃\n",p->; loginnum,p-& gt; Title, p-> Author, P-> Publisher, P-> pbtime,p-& gt; classfy,p-& gt; Price);

}

fprintf(fp,"┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n”);

fclose(FP);

Printf ("Book data has been saved to data.txt file \ n");

}

/* Insert */

void insert doc(struct books _ list * head)

{

/* Define the first address p of the structure pointer variable S pointing to the opened new node as the intermediate variable */

struct books_list *s,* p;

Char flag =' Y/* defines the flag, which is convenient for users to select repeated input */

P = head;

/* Traverse to the tail node, p points to the tail node */

while(p->; Au Suivant! = empty)

{

p = p-& gt; Next;

}

/* Open a new space, store data and add it to the linked list */

while(flag=='Y'||flag=='y ')

{

s =(struct books _ list *)malloc(size of(struct books _ list));

Printf ("\ nPlease enter the book login number:");

fflush(stdin);

scanf("%s ",s-& gt; loginnum);

Printf ("\ nPlease enter a title:");

fflush(stdin);

scanf("%s ",s-& gt; Title);

Printf ("\ nPlease enter the name of the book author:");

fflush(stdin);

scanf("%s ",s-& gt; Author);

Printf ("\ nPlease enter a book publisher:");

fflush(stdin);

scanf("%s ",s-& gt; Publishers);

Printf ("\ nPlease enter the book publishing time:");

fflush(stdin);

scanf("%s ",s-& gt; Pb time);

Printf ("\ nPlease enter the book classification number:");

fflush(stdin);

scanf("%s ",s-& gt; classfy);

Printf ("\ nPlease enter the book price:");

fflush(stdin);

scanf("%f ",& amps-& gt; Price);

printf(" \ n ");

p->; next = s; /* Add the newly added node to the linked list */

p = s; /*p Point to the tail node and move back */

s-& gt; next = NULL

Printf ("━ ━ ━ added successfully! ━━━━");

Printf ("\ nContinue to add? (Y/N):);

fflush(stdin);

Scanf("%c ",& flag);

printf(" \ n ");

if(flag=='N'||flag=='n ')

{ break}

else if(flag=='Y'||flag=='y ')

{Continue; }

}

Save (header); /* Save data to a file */

Return;

}

/* Query operation */

void search _ book(struct books _ list * head)

{

Structure books _ list * p;;

Charging temperature [20];

P = head;

if(head = = NULL | | head-& gt; Next==NULL) /* Determine whether the database is empty */

{

Printf ("━ ━ ━ Library is empty! ━━━\ n ");

}

other

{

Printf ("Please enter the title of the book you are looking for:");

fflush(stdin);

scanf("%s ",temp);

/* The pointer moves from the head node to the tail node to find bibliographic information */

while(p->; Au Suivant! = empty)

{

p = p-& gt; Next;

if(strcmp(p-& gt; bookname,temp)==0)

{

Printf ("\ nFound a book! \ n ");

printf(" \ n ");

Printf ("login number: %s\t\n", p-> loginnum);

Printf ("Title: %s\t\n ",p-& gt; Title);

Printf ("Author's name: %s\t\n", p-> Author);

Printf ("Publisher: %s\t\n ",p-& gt; Publishers);

Printf ("Release Date: %s\t\n", p-> Pb time);

Printf ("classification number: %s\t\n", p-> classfy);

Printf ("price: %.2f\t\n ",p-& gt; Price);

}

If (p->; next==NULL)

{

Printf ("\ nQuery complete! \ n ");

}

}

}

Return;

}

/* Browse operation */

void Print _ Book _ Doc(struct books _ list * head)

{

Structure books _ list * p;;

if(head = = NULL | | head-& gt; Next==NULL) /* Determine whether the database is empty */

{

Printf ("\ n ━ ━ No book records! ━━━\ n \ n ");

Return;

}

P = head;

printf("┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n”);

Printf("┃ login number ┃ title ┃ author ┃ publishing unit ┃ publishing time ┃ classification number ┃ price ┃ n ");

printf("┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n”);

/* The pointer moves from the head node to the tail node, and the book information is output in turn */

while(p->; Au Suivant! = empty)

{

p = p-& gt; Next;

printf("┃%-6.6s┃%- 10. 10s┃%- 10. 10s┃%- 10. 10s┃%- 12. 12s┃%-6.6s┃%.2f ┃\n",p->; loginnum,p-& gt; Title, p-> Author, P-> Publisher, P-> pbtime,p-& gt; classfy,p-& gt; Price); /* Circular output table */

}

printf("┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n”);

printf(" \ n ");

}

/* Modify operation */

void info _ change(struct books _ list * head)

{

Structure books _ list * p;;

int pan Duan = 0; /* This variable is used to judge whether the document directory is found */

Charging temperature [20];

P = head;

Printf ("Please enter the title of the book to be modified:");

scanf("%s ",temp);

while(p->; Au Suivant! = empty)

{

p = p-& gt; Next;

if(strcmp(p-& gt; bookname,temp)==0)

{

Printf ("\ nPlease enter the book login card number:");

fflush(stdin);

scanf("%s ",p-& gt; loginnum);

Printf ("\ nPlease enter a title:");

fflush(stdin);

scanf("%s ",p-& gt; Title);

Printf ("\ nPlease enter the name of the book author:");

fflush(stdin);

scanf("%s ",p-& gt; Author);

Printf ("\ nPlease enter a book publisher:");

fflush(stdin);

scanf("%s ",p-& gt; Publishers);

Printf ("\ nPlease enter the book publishing time:");

fflush(stdin);

scanf("%s ",p-& gt; Pb time);

Printf ("\ nPlease enter the book classification number:");

fflush(stdin);

scanf("%s ",p-& gt; classfy);

Printf ("\ nPlease enter the book price:");

fflush(stdin);

scanf("%f ",& ampp->; Price);

printf(" \ n ");

panduan = 1;

}

}

if(panduan==0)

{

Printf ("\ n ━ ━ No book records! ━━━\ n \ n ");

}

Return;

}

/* Delete operation */

void delete doc(struct books _ list * head)

{

struct books_list *s,* p; /*s is an intermediate variable and p is a pointer */

Charging temperature [20];

Int panduan/* This variable is used to judge whether the document catalogue has been found */

panduan = 0;

P = s = head;

Printf("[ Please enter the title of the book you want to delete]:);

scanf("%s ",temp);

/* Traverse to the tail node */

And (p! = empty)

{

if(strcmp(p-& gt; bookname,temp)==0)

{

Pan Duan++;

Break;

}

p = p-& gt; Next;

}

if(panduan== 1)

{

for(; s-& gt; Au Suivant! = p; )/* Find the previous node and delete the card number node */

{

s = s-& gt; Next;

}

s-& gt; next = p-& gt; Next; /* Assign the address of the latter node to the pointer field of the previous node */

Free (p);

Printf ("\ n ━ ━ deleted successfully! ━━━\ n ");

}

Else /* No corresponding bibliography found */

{

Printf ("The bibliography you entered does not exist, please confirm and enter! \ n ");

}

Return;

}

int main(void)

{

Structure books _ list * head

Char selection;

head = NULL

for(; ; )/* Realize repeated input and selection */

{

printf(“┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n”);

Printf(“┃┃\n socat book management system ").

printf(“┃┗━━━━━━━━━━━━━━━━━━━┛┃\n”);

Printf(" ┃ ●[ 1] book information entry ┃\ n ");

printf(“┃┃\n”);

Printf(" ┃ ●[2] book information browsing ┃\ n ");

printf(“┃┃\n”);

Printf(" ┃ ●[3] book information query ┃\ n ");

printf(“┃┃\n”);

Printf(" ┃ ●[4] book information modification ┃\ n ");

printf(“┃┃\n”);

Printf(" ┃ ●[5] Delete book information ┃\ n ");

printf(“┃┃\n”);

Printf(" ┃ ●[6] Exit the system ┃\ n ");

printf(“┗━━━━━━━━━━━━━━━━━━━━━━━┛\n”);

Printf ("Please select:");

fflush(stdin);

Scanf("%c ",& select);

if(choice==' 1 ')

{

if(head==NULL)

{

head = Create _ Books _ Doc();

}

insert doc(head);

}

else if(choice=='2 ')

{

Print_Book_Doc (header);

}

else if(choice=='3 ')

{

Search_book (header);

}

else if(choice=='4 ')

{

Info_change (header);

}

else if(choice=='5 ')

{

delete doc(head);

}

else if(choice=='6 ')

{

printf(" \ n ");

Printf ("│ │ │ │ │ Thanks for using the library management system │ │ n");

Break;

}

other

{

Printf ("━ ━ ━ input error, please re-enter! ━━━━");

Break;

}

}

Returns 0;

}