数据结构
typedef struct Books
{
char accession_number[20];
char title[50];
char author_name[50];
double price;
char flag;
}BOOK;
typedef struct Library
{
BOOK data;
struct Library* next;
}LIB;
代码:
#pragma warning(disable:4996)
#include<stdio.h>
#include <stdlib.h>
typedef struct Books
{
char accession_number[20];
char title[50];
char author_name[50];
double price;
char flag;
}BOOK;
typedef struct Library
{
BOOK data;
struct Library* next;
}LIB;
int number = 0;
int mainSelect();
LIB* addBookInfo(int n);
void displayBookInfo(LIB* b);
void searchByAuthor(LIB* b);
void searchByTitle(LIB* b);
void bookNumbers(LIB* b);
void mySort(LIB* b);
void storeFile(LIB* b);
int main()
{
LIB* lib;
lib = (LIB*)malloc(sizeof(LIB));
while (1)
{
switch (mainSelect()) {
case 1: // 1. Add book information
printf("1. Add book information\n");
printf("Type in number of book:\n");
scanf("%d", &number);
lib = addBookInfo(number);
printf("Information input success\n");
break;
case 2: // 2. Display book information
printf("2. Display book information\n");
displayBookInfo(lib);
break;
case 3: // 3. List all books of given author
printf("3. List all books of given author\n");
searchByAuthor(lib);
break;
case 4: // 4. List the title of specified book
printf("4. List the title of specified book\n");
searchByTitle(lib);
break;
case 5: // 5. List the count of books in the library
printf("5. List the count of books in the library \n");
bookNumbers(lib);
break;
case 6: // 6. List the books in the order of accession number
printf("6. List the books in the order of accession number\n");
mySort(lib);
break;
case 7:
printf("Storing the file \"library.dat\"............\n");
storeFile(lib);
printf("Store success! exiting............");
exit(0);
break;
}
}
return 0;
}
int mainSelect()
{
int choice;
do {
system("cls");
printf(" WELCOME TO LIBRARY \n");
printf("1. Add book information \n");
printf("2. Display book information \n");
printf("3. List all books of given author \n");
printf("4. List the title of specified book \n");
printf("5. List the count of books in the library \n");
printf("6. List the books in the order of accession number \n");
printf("7. exit \n");
printf("\n\n");
printf("Input your choice: \n");
scanf("%d", &choice);
} while (choice < 1 || choice > 7);
return choice;
}
LIB* addBookInfo(int n)
{
LIB* p, * q, * h;
h = (LIB*)malloc(sizeof(LIB));
p = (LIB*)malloc(sizeof(LIB));
q = (LIB*)malloc(sizeof(LIB));
int i;
for (i = 0; i < n; i++)
{
p = (LIB*)malloc(sizeof(LIB));
printf("Type in Accession number:\n");
scanf("%s", &p->data.accession_number);
printf("Type in Title of the book:\n");
scanf("%s", &p->data.title);
printf("Type in Author name:\n");
scanf("%s", &p->data.author_name);
printf("Type in Price of the book:\n");
scanf("%lf", &p->data.price);
printf("If book is issued or not(Y or N):\n");
scanf("%s", &p->data.flag);
p->next = NULL;
if (i == 0)
h = p;
else
q->next = p;
q = p;
}
return h;
}
void displayBookInfo(LIB* h)
{
LIB* b = h;
while (b != NULL)
{
printf("Accession number : %s \n", b->data.accession_number);
printf("title of the book : %s \n", b->data.title);
printf("author name : %s \n", b->data.author_name);
printf("price of the book : %lf \n", b->data.price);
printf("flag : %c \n", b->data.flag);
b = b->next;
}
printf("press to continue ");
getch();
}
void searchByAuthor(LIB* h)
{
LIB* b;
b = h;
printf("Type in the name of author: \n");
char name[50];
scanf("%s", &name);
while (b != NULL)
{
if (strcmp(b->data.author_name, name) == 0)
{
printf("Accession number : %s \n", b->data.accession_number);
printf("Title of the book : %s \n", b->data.title);
printf("Author name : %s \n", b->data.author_name);
printf("Price of the book : %lf \n", b->data.price);
printf("Flag : %c \n", b->data.flag);
break;
}
else
b = b->next;
}
getch();
}
void searchByTitle(LIB* h)
{
LIB* b;
b = h;
printf("please input the title: \n");
char name[50];
scanf("%s", &name);
while (b != NULL)
{
if (strcmp(b->data.title, name) == 0)
{
printf("Accession number : %s \n", b->data.accession_number);
printf("title of the book : %s \n", b->data.title);
printf("author name : %s \n", b->data.author_name);
printf("price of the book : %lf \n", b->data.price);
printf("flag : %c \n", b->data.flag);
break;
}
else
b = b->next;
}
getch();
}
void bookNumbers(LIB* h)
{
LIB* b;
b = h;
int number = 0;
while (b != NULL)
{
if (b->data.flag == 'N')
{
number++;
}
b = b->next;
}
printf("there are %d books in library.\n", number);
getch();
}
void mySort(LIB* h)
{
int x, i = 1;
BOOK t;
LIB* p, * r, * s;
p = h;
r = p->next;
s = r->next;
while (p != NULL)
{
while (r != NULL)
{
if (strcmp(p->data.accession_number, r->data.accession_number) > 0)
{
t = p->data;
p->data = r->data;
r->data = t;
}
else
r = r->next;
}
p = p->next;
r = s;
}
p = h;
printf("Sort finished!\n");
}
void storeFile(LIB* b)
{
FILE* fp;
if ((fp = fopen("library.dat", "ab+")) == NULL) {
fputs("Can not open book.dat file", stderr);
exit(1);
}
while (b != NULL)
{
fprintf(fp, "%S ", b->data.accession_number);
b = b->next;
}
fclose(fp);
}