图书管理系统-C语言作业
一个简短的图书管理系统,C语言完成。
头文件部分function.h
#ifndef FUNCTION_H_
#define FUNCTION_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct
{
//书号
int ID;
//书名
char name[20];
//作者
char author[20];
//分类
int type;
//出版单位
char publishingUnit[20];
//出版时间
char publishingTime[20];
//价格
int price;
} Book;
//书籍总表
Book book[20];
//录入图书信息
bool InputBook();
//查找空位
int SeekVoidTable();
//浏览图书信息
void BrowseBook();
//查询图书信息
//int参数为查询类型,0为按书名查询,1为按作者查询
int SeekBook(int seektype,char* index);
//删除书籍
//int参数为查询类型,0为按书名删除,1为按作者删除
void DeleteBook(int seektype, char *index);
//修改书籍
//int参数为查询类型,0为按书名修改,1为按作者修改
void ModifyBook(int seektype, char *index);
//输出主菜单
char PrintMainMenu();
//根据主菜单选择选择相应操作
void MainChooseOperation(char tempchoose);
//查询书籍菜单
char PrintSeekBookMenu();
//根据查询菜单选择选择相应操作
void SeekBookChooseOperation(char tempchoose);
#endif // !FUNCTION_H_
实现部分function.C
#include "function.h"
//查找空位
int SeekVoidTable()
{
for (int i = 0; i < sizeof(book) / sizeof(Book); i++)
{
if (book[i].ID == 0)
{
return i;
}
}
return -1;
}
//录入图书信息
bool InputBook()
{
int tempbook = SeekVoidTable();
if (tempbook != -1)
{
printf("开始录入书籍信息:\n\n");
printf("请输入书号:");
scanf("%d", &book[tempbook].ID);
printf("请输入书名:");
scanf("%s", book[tempbook].name);
printf("请输入作者:");
scanf("%s", book[tempbook].author);
printf("请输入类型:");
scanf("%d", &book[tempbook].type);
printf("请输入出版单位:");
scanf("%s", &book[tempbook].publishingUnit);
printf("请输入出版时间:");
scanf("%s", &book[tempbook].publishingTime);
printf("请输入价格:");
scanf("%d", &book[tempbook].price);
printf("*********输入完成*********\n");
printf("*********按任意键返回主菜单*********\n");
system("pause");
system("cls");
return true;
}
else
{
printf("已经满了\n");
printf("*********按任意键继续*********\n");
system("pause");
system("cls");
return false;
}
}
//查询图书信息
//int参数为查询类型,0为按书名查询,1为按作者查询
int SeekBook(int seektype, char *index)
{
if (seektype == 0)
{
for (int i = 0; i < sizeof(book) / sizeof(Book); i++)
{
if (strcmp(index, book[i].name) == 0)
{
return i;
}
}
}
else if (seektype == 1)
{
for (int i = 0; i < sizeof(book) / sizeof(Book); i++)
{
if (strcmp(index, book[i].author) == 0)
{
return i;
}
}
}
return -1;
}
//浏览图书信息
void BrowseBook()
{
for (int i = 0; i < sizeof(book) / sizeof(Book); i++)
{
if (book[i].ID != 0)
{
printf("书号:%d\t", book[i].ID);
printf("书名:%s\t", book[i].name);
printf("作者:%s\t", book[i].author);
printf("类型:%d\t", book[i].type);
printf("出版单位:%s\t", book[i].publishingUnit);
printf("出版时间:%s\t", book[i].publishingTime);
printf("价格:%d\n", book[i].price);
}
}
printf("*********按任意键继续*********\n");
system("pause");
system("cls");
}
//删除书籍
//int参数为查询类型,0为按书名查询,1为按作者查询
void DeleteBook(int seektype, char *index)
{
int tempbook = SeekBook(seektype, index);
if (tempbook != -1)
{
Book temp = {0};
book[tempbook] = temp;
printf("删除完成\n");
printf("*********按任意键继续*********\n");
system("pause");
system("cls");
}
else
{
printf("书籍不存在\n");
printf("*********按任意键继续*********\n");
system("pause");
system("cls");
}
}
//修改书籍
//int参数为修改类型,0为按书名查询,1为按作者查询
void ModifyBook(int seektype, char *index)
{
int tempbook = SeekBook(seektype, index);
if (tempbook != -1)
{
printf("请输入书号:");
scanf("%d", &book[tempbook].ID);
printf("请输入书名:");
scanf("%s", book[tempbook].name);
printf("请输入作者:");
scanf("%s", book[tempbook].author);
printf("请输入类型:");
scanf("%d", &book[tempbook].type);
printf("请输入出版单位:");
scanf("%s", &book[tempbook].publishingUnit);
printf("请输入出版时间:");
scanf("%s", &book[tempbook].publishingTime);
printf("请输入价格:");
scanf("%d", &book[tempbook].price);
printf("修改完成\n");
printf("*********按任意键继续*********\n");
system("pause");
system("cls");
}
else
{
printf("书籍不存在\n");
printf("*********按任意键继续*********\n");
system("pause");
system("cls");
}
}
//输出主菜单,捕获输入的选项
char PrintMainMenu()
{
system("cls");
char menustr[10][100] =
{"讲解人: XXXXX\n",
" 图书信息管理系统 ",
"\n------------------------------\n",
"\n|| >>1 录入书籍<< ||\n",
"\n|| >>2 浏览目录<< ||\n",
"\n|| >>3 查询书籍<< ||\n",
"\n|| >>4 删除书籍<< ||\n",
"\n|| >>5 修改书籍<< ||\n",
"\n|| >>6 退出程序<< ||\n",
"\n------------------------------\n"};
printf("\n");
for (int i = 0; i < sizeof(menustr) / sizeof(menustr[0]); i++)
{
printf("%s\n", menustr[i]);
}
//使用字符串,方便后续判断是否是违法输出
char choose[10];
//选择是否合规
bool judgechoose = false;
//选项目录表
char choosetable[6][100] = {"1", "2", "3", "4", "5", "6"};
do
{
printf("\n请输入功能选项:");
scanf("%s", choose);
for (int i = 0; i < 6; i++)
{
if (strcmp(choose, choosetable[i]) == 0)
{
judgechoose = true;
break;
}
}
} while (!judgechoose);
return choose[0];
}
//查询书籍菜单
char PrintSeekBookMenu()
{
system("cls");
printf("1 书名查询\n");
printf("2 作者查询\n");
char choose[10];
//选择是否合规
bool judgechoose = false;
//选项目录表
char choosetable[2][100] = {"1", "2"};
do
{
printf("\n请输入功能选项:");
scanf("%s", choose);
for (int i = 0; i < 2; i++)
{
if (strcmp(choose, choosetable[i]) == 0)
{
judgechoose = true;
break;
}
}
} while (!judgechoose);
return choose[0];
}
//根据主菜单选择选择相应操作
void MainChooseOperation(char tempchoose)
{
system("cls");
char tempname[20];
switch (tempchoose)
{
case '1':
//录入书籍
InputBook();
break;
case '2':
//浏览书籍
BrowseBook();
break;
case '3':
//查询书籍
SeekBookChooseOperation(PrintSeekBookMenu());
break;
case '4':
//删除书籍
printf("请输入书名进行删除:");
scanf("%s", tempname);
DeleteBook(0, tempname);
break;
case '5':
//修改书籍
printf("请输入书名进行修改:");
scanf("%s", tempname);
ModifyBook(0, tempname);
break;
case '6':
exit(0);
break;
}
}
//根据查询菜单选择选择相应操作
void SeekBookChooseOperation(char tempchoose)
{
system("cls");
char tempbookname[20];
int tempbook;
switch (tempchoose)
{
case '1':
//书名查询
printf("请输入书名:");
scanf("%s", tempbookname);
tempbook = SeekBook(0, tempbookname);
if (tempbook != -1)
printf("书号:%d 书名:%s\t 作者:%s\t 分类:%d\t 出版单位:%s\t 出版时间:%s\t 价格:%d\n",
book[tempbook].ID, book[tempbook].name, book[tempbook].author, book[tempbook].type,
book[tempbook].publishingUnit, book[tempbook].publishingTime, book[tempbook].price);
else
{
printf("书籍不存在\n");
}
break;
case '2':
printf("请输入作者名:");
scanf("%s", tempbookname);
tempbook = SeekBook(0, tempbookname);
if (tempbook != -1)
{
printf("书号:%d\t 书名:%s\t 作者:%s\t 分类:%d\t 出版单位:%s\t 出版时间:%s\t 价格:%d\n",
book[tempbook].ID, book[tempbook].name, book[tempbook].author, book[tempbook].type,
book[tempbook].publishingUnit, book[tempbook].publishingTime, book[tempbook].price);
}
else
{
printf("书籍不存在\n");
}
break;
}
printf("*********按任意键继续*********\n");
system("pause");
system("cls");
}
int main(int argc, char const *argv[])
{
//清除之前的指令
system("cls");
while (1)
{
MainChooseOperation(PrintMainMenu());
}
return 0;
}