【c语言】练手:一个简单的图书管理系统
最近在学习结构体和结构体指针等内容,为了巩固所学习的内容,于是写了一个简陋的图书管理系统,在敲代码过程中也遇到一些容易忽略的问题,于是记录下来,大家可以给给建议!
核心功能主要有3个:录入书籍信息,打印书籍信息,删除书籍信息。
思路很简单,定义一个结构体Book来实例化结构体变量从而用于存放每一本书的信息。书的信息包括 书名,作者, 价格, 出版时间和出版日期。
struct Book
{
char title[128];
char author[40];
double price;
struct Date date;
char publisher[40];
};
定义一个结构体用于存放日期的年,月,日
struct Date
{
int year;
int month;
int day;
};
struct Book * library[1000] = {NULL};
int book_num = 0;
int count = 0;
main函数里提示操作的部分:
int main(void)
{
printf("==========欢迎使用图书馆1.0==========\n");
while(1)
{
printf("请输入操作指令前对应的序号:\n\
1.录入书籍信息\n\
2.打印书籍信息\n\
3.删除书籍信息\n\
4.退出图书馆\n");
int order = 0;
scanf("%d" , &order);
......(后面省略)
书籍信息录入部分:
if (order == 1)
{
libraryInput();
}
libraryInput 函数的内容
void libraryInput()
{
int num = 0;
printf("请输入要录入书籍的数量:");
scanf("%d" , &num);
while(num--)
{
count++;
struct Book *b;
b = (struct Book *)malloc(sizeof(struct Book));
if (b == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
printf("请录入第%d本书籍的信息:\n" , count);
b = getInput(b);
book_num++;
for(int i = 0 ; i < 1000 ; i++)
{
if (library[i] == NULL)
{
library[i] = b;
break;
}
}
}
free(b);
printf("书籍信息录入完成!\n\n");
count = 0;
}
struct Book * getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s" , book->title);
printf("请输入作者:");
scanf("%s" , book->author);
printf("请输入价格:");
scanf("%lf" , &book->price);
printf("请输入出版日期:");
scanf("%d %d %d" , &book->date.year , &book->date.month , &book->date.day);
getchar();
printf("请输入出版社:");
scanf("%s" , book->publisher);
putchar('\n');
return book;
}
打印模块
if (order == 2)
{
libraryPrintf();
}
void libraryPrintf()
{
for (int i = 0 ; i < 1000 ; i++)
{
if (library[i] != NULL)
{
count++;
printf("\n打印第%d本书籍的信息:\n" , count);
printfBook(library[i]);
if (count == book_num)
{
break;
}
}
}
printf("\n打印完成!总共打印了%d本书籍!\n\n" , count);
count = 0;
}
void printfBook(struct Book *book)
{
printf("书名:%s\n" , book->title);
printf("作者:%s\n" , book->author);
printf("价格:%.2lf\n" , book->price);
printf("出版时间:%d-%d-%d\n" , book->date.year , book->date.month , book->date.day);
printf("出版社:%s\n" , book->publisher);
}
删除书籍信息模块
if (order == 3)
{
libraryDel();
}
void libraryDel()
{
char book_title[128];
printf("请输入要删除的书名:" );
scanf("%s" , &book_title);
getchar();
for (int i = 0 ; i < 1000 ; i++)
{
if ( !strcmp( library[i]->title , book_title) )
{
printf("确定要删除该书籍吗?(Y/N) :\n");
if (getchar() == 'Y')
{
library[i] = NULL;
book_num--;
printf("书籍%s删除成功!\n\n" , book_title);
break;
}
else
{
break;
}
}
if (i == 999)
{
printf("找不到书籍信息,请检查格式是否错误!\n");
break;
}
}
}
完整代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book * getInput(struct Book *book);
void printfBook(struct Book *book);
void libraryInput();
void libraryPrintf();
void libraryDel();
struct Book * library[1000] = {NULL};
int book_num = 0;
int count = 0;
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[40];
double price;
struct Date date;
char publisher[40];
};
struct Book * getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s" , book->title);
printf("请输入作者:");
scanf("%s" , book->author);
printf("请输入价格:");
scanf("%lf" , &book->price);
printf("请输入出版日期:");
scanf("%d %d %d" , &book->date.year , &book->date.month , &book->date.day);
getchar();
printf("请输入出版社:");
scanf("%s" , book->publisher);
putchar('\n');
return book;
}
void printfBook(struct Book *book)
{
printf("书名:%s\n" , book->title);
printf("作者:%s\n" , book->author);
printf("价格:%.2lf\n" , book->price);
printf("出版时间:%d-%d-%d\n" , book->date.year , book->date.month , book->date.day);
printf("出版社:%s\n" , book->publisher);
}
void libraryInput()
{
int num = 0;
printf("请输入要录入书籍的数量:");
scanf("%d" , &num);
while(num--)
{
count++;
struct Book *b;
b = (struct Book *)malloc(sizeof(struct Book));
if (b == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
printf("请录入第%d本书籍的信息:\n" , count);
b = getInput(b);
book_num++;
for(int i = 0 ; i < 1000 ; i++)
{
if (library[i] == NULL)
{
library[i] = b;
break;
}
}
}
printf("书籍信息录入完成!\n\n");
count = 0;
}
void libraryPrintf()
{
for (int i = 0 ; i < 1000 ; i++)
{
if (library[i] != NULL)
{
count++;
printf("\n打印第%d本书籍的信息:\n" , count);
printfBook(library[i]);
if (count == book_num)
{
break;
}
}
}
printf("\n打印完成!总共打印了%d本书籍!\n\n" , count);
count = 0;
}
void libraryDel()
{
char book_title[128];
printf("请输入要删除的书名:" );
scanf("%s" , &book_title);
getchar();
for (int i = 0 ; i < 1000 ; i++)
{
if ( !strcmp( library[i]->title , book_title) )
{
printf("确定要删除该书籍吗?(Y/N) :\n");
if (getchar() == 'Y')
{
library[i] = NULL;
book_num--;
printf("书籍%s删除成功!\n\n" , book_title);
break;
}
else
{
break;
}
}
if (i == 999)
{
printf("找不到书籍信息,请检查格式是否错误!\n");
break;
}
}
}
int main(void)
{
printf("==========欢迎使用图书馆1.0==========\n");
while(1)
{
printf("请输入操作指令前对应的序号:\n\
1.录入书籍信息\n\
2.打印书籍信息\n\
3.删除书籍信息\n\
4.退出图书馆\n");
int order = 0;
scanf("%d" , &order);
if (order == 1)
{
libraryInput();
}
if (order == 2)
{
libraryPrintf();
}
if (order == 3)
{
libraryDel();
}
if (order == 4)
{
printf("\n========恭送皇上~=======\n");
break;
}
}
return 0;
}
|