图书管理系统设计,供大家参考,具体内容如下
一、问题描述及功能要求
(1)图书信息录入功能(图书信息用文件保存)
(2)图书信息浏览功能
(3)查询和排序功能:(至少一种查询方式)
.按书名查询
.按作者名查询
(4)图书信息的删除与修改
二、代码实现 带有注释
废话不说,直接代码,欢迎指正。
大家CV可能有不兼容的情况,可以滴滴,尽可能解决问题地回复。
#include<iostream>
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include <cstring>
#include<windows.h>
#include<vector>
#define BOOKMAXREPERTORY 99
using namespace std;
class Common{
public:
int id;
char title[66];
char author[66];
Common(){
}
Common(int _id,char _title[],char _author[]){
id = _id;
strcpy(title , _title);
strcpy(author , _author);
}
};
class Book:public Common{
public:
char publisher[66];
int pageNumber;
Book(){}
Book(int _id,char _title[],char _author[],char _publisher[],char _iSBNNumber[],int _pageNumber){
Common(_id,_title,_author);
strcpy(publisher,_publisher);
pageNumber = _pageNumber;
}
};
class MediaLibraryManageSystem{
public:
MediaLibraryManageSystem(){
nowBookTotal = 0;
}
void _run(){
readDataByFile();
printf("程序加载中");
for (int i = 0; i < 10; i++) {
Sleep(100);
printf(".");
fflush(stdout);//强制刷新缓存,输出显示
}
printf("\n");
system("cls");
int cmd;
while(true){
cout<<home_menu<<endl;
cout<<"请输入你需要执行的命令序号:";cin>>cmd;
if(cmd == 0){
_exit();
}
system("cls");
switch(cmd){
case 1:
addBook();
break;
case 2:
queryGoods();
break;
case 3:
showGoods();
break;
case 4:
updateGoods();
break;
case 5:
deleteGoods();
break;
default:
cout<<"输入的指令有误!请重新输入!"<<endl;
}
system("pause");
system("cls");
}
}
private:
const string home_menu =
"\n********************************** 图书管理系统 **********************************\n"
"* *\n"
"* *\n"
"* 1.添加 *\n"
"* *\n"
"* 2.查询 *\n"
"* *\n"
"* 3.显示图书库 *\n"
"* *\n"
"* 4.修改图书库 *\n"
"* *\n"
"* 5.删除 *\n"
"* *\n"
"* 0.退出 *\n"
"* *\n"
"* *\n"
"************************************************************************************\n";
const string query_goods_menu =
"\n************************************ 图书查询 ************************************\n"
"* *\n"
"* *\n"
"* 1.按查询 *\n"
"* *\n"
"* 2.按编号查询 *\n"
"* *\n"
"* 0.退出 *\n"
"* *\n"
"* *\n"
"************************************************************************************\n";
Book bookList[BOOKMAXREPERTORY];
int nowBookTotal;
///添加图书
void addBook(){
Book book;
cout<<"请输入编号:";cin>>book.id;
cout<<"请输入";cin>>book.title;
cout<<"请输入作者:";cin>>book.author;
cout<<"请输入出版社:";cin>>book.publisher;
cout<<"请输入页数:";cin>>book.pageNumber;
if(getBookById(book.id) != -1){
cout<<"添加失败! 添加编号重复! 请重新添加!"<<endl;
}else if(nowBookTotal == BOOKMAXREPERTORY){
cout<<"添加失败! 图书库已满!"<<endl;
}else{
bookList[nowBookTotal ++] = book;
}
}
void queryGoods(){
int cmd;
while(true){
cout<<query_goods_menu<<endl;
cout<<"请输入你需要执行的命令序号:";cin>>cmd;
if(cmd == 0){
break;
}
system("cls");
switch(cmd){
case 1:
queryByTitle();
break;
case 2:
queryById();
break;
default:
cout<<"输入的指令有误!请重新输入!"<<endl;
}
system("pause");
system("cls");
}
}
///按查询
void queryByTitle(){
int cmd;
char title[66];
cout<<"请输入";cin>>title;
getBookByTitle(title);
}
///book
void getBookByTitle(char title[]){
bool flag = true;
printf("%-6s%-10s%-10s%-10s%-10s%\n","编号","","作者","出版社","页数");
for(int i = 0;i < nowBookTotal;i ++){
if(strcmp(bookList[i].title,title) == 0){
flag = false;
printf("%-6d%-10s%-10s%-10s%-10d\n",bookList[i].id,bookList[i].title,bookList[i].author,bookList[i].publisher,bookList[i].pageNumber);
}
}
if(flag){
printf("\n\n空的!\n\n");
}
}
///按编号查询
void queryById(){
int cmd,i,id;
cout<<"请输入Id:";cin>>id;
i = getBookById(id);
if(i == -1){
printf("查找不到!\n");
}else{
printf("%-6s%-10s%-10s%-10s%-10s\n","编号","","作者","评级","出版社","页数");
printf("%-6d%-10s%-10s%-10s%-10d\n",bookList[i].id,bookList[i].title,bookList[i].author,bookList[i].publisher,bookList[i].pageNumber);
}
}
///Book
int getBookById(int id){
int index = -1;
for(int i = 0;i < nowBookTotal;i ++){
if(bookList[i].id == id){
index = i;
break;
}
}
return index;
}
void showGoods(){
if(nowBookTotal == 0){
printf("空的!\n");
}else{
printf("%-6s%-10s%-10s%-10s%-10s\n","编号","","作者","出版社","页数");
for(int i = 0;i < nowBookTotal;i ++){
printf("%-6d%-10s%-10s%-10s%-10d\n",bookList[i].id,bookList[i].title,bookList[i].author,bookList[i].publisher,bookList[i].pageNumber);
}
}
}
void updateGoods(){
int cmd,id,i;
cout<<"请输入你需要修改的图书编号:";cin>>id;
i = getBookById(id);
if(i == -1){
cout<<"图书不存在!"<<endl;
}else{
cout<<"原图书信息为:"<<endl;
printf("%-6s%-10s%-10s%-10s%-10s\n","编号","","作者","出版社","页数");
printf("%-6d%-10s%-10s%-10s%-10d\n",bookList[i].id,bookList[i].title,bookList[i].author,bookList[i].publisher,bookList[i].pageNumber);
Book book;
book.id = id;
cout<<"请输入修改后的";cin>>book.title;
cout<<"请输入修改后的作者:";cin>>book.author;
cout<<"请输入修改后的出版社:";cin>>book.publisher;
cout<<"请输入修改后的页数:";cin>>book.pageNumber;
bookList[i] = book;
}
}
void deleteGoods(){
int cmd,id,i;
if(nowBookTotal == 0){
cout<<"空的!"<<endl;
return;
}
cout<<"请输入你需要删除的图书编号:";cin>>id;
deleteBook(id);
}
///book
void deleteBook(int id){
int index = getBookById(id);
if(index == -1){
cout<<"没有该图书!"<<endl;
}else{
for(int i = index;i < nowBookTotal - 1;i ++){
bookList[i] = bookList[i + 1];
}
nowBookTotal --;
cout<<"删除成功!"<<endl;
}
}
void saveDataToFile(){
///book
FILE *bookDB = fopen("bookList.txt", "wb");
for (int i = 0; i < nowBookTotal; i++) {
fwrite(&bookList[i], sizeof(Book), 1, bookDB);
}
fclose(bookDB);
}
void readDataByFile(){
///Book
FILE *bookDB = fopen("bookList.txt", "rb");
nowBookTotal = 0;
Book book;
while (fread(&book, sizeof(Book), 1, bookDB) == 1) {
bookList[nowBookTotal++] = book;
}
fclose(bookDB);
}
void _exit(){
saveDataToFile();
system("cls");
printf("正在退出");
for (int i = 0; i < 10; i++) {
Sleep(100);
printf(".");
fflush(stdout);//强制刷新缓存,输出显示
}
system("cls");
printf("已退出!");
exit(1);
}
};
int main(){
MediaLibraryManageSystem mediaLibraryManageSystem;
mediaLibraryManageSystem._run();
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。