跪求C++图书管理系统。。。
对大量的图书信息,包括书名、作者、出版社、出版日期、ISBN(书号)等进行增加、查询、保存等操作。实现如下功能:(1) 新增图书信息处理,从图书文件中读出各图书的相关信息,在此基础上,增加新图书的相关信息。
(2) 查询图书信息处理,分别按照书名、作者、出版社、出版日期、ISBN(书号)对单链表中的信息进行查询,其中,前四种为模糊查询,书号为精确查询。
(3) 删除图书信息 根据输入的ISBN查找图书链表,如果该图书不存在则给出提示信息,否则,删除该图书,并显示删除后的所有图书信息。
(4) 保存图书信息处理 将单链表中的图书信息保存到文件。
(5) 退出,保存图书信息,并释放链表。
追问:我邮箱252168919@qq.com,好像有点问题
答案:
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;
/************************图书资料***************************/
struct Book
{
string name;
string author;
string publisher;
string date;
string ISBN;
Book *next;
};
/*****************************初始化链表*****************************/
void InitList (Book *&L)
{
L = new Book;
L->next = NULL;
}
/***************************重载 << 运算符*********************************/
//用于输出Book结构体变量
ostream &operator << (ostream &output, Book *&b)
{
output << b->name << '\t' << b->author << '\t' << b->publisher << '\t' << b->date << '\t' << b->ISBN;
return output;
}
/****************************重载 >> 运算符********************************/
//用于输入Book结构体变量
istream &operator >> (istream &input, Book *&b)
{
input >> b->name >> b->author >> b->publisher >> b->date >> b->ISBN;
return input;
}
/****************************保存图书资料******************************/
//将图书资料保存到book.txt文件中
void SaveData (Book *L)
{
Book *p = L->next;
ofstream outfile ("book.txt", ios::out);
while (p != NULL)
{
outfile << p << endl;
p = p->next;
}
outfile.close ();
}
/*******************************读取图书资料********************************/
//将book.txt文件中的每一个图易做图录插入到单链表中
void ReadData (Book *&L)
{
Book *r = L, *s;
ifstream infile ("book.txt", ios::in);
while (!infile.eof ())
{
s = new Book;
infile >> s;
r->next = s;
r = s;
}
r->next = NULL;
}
/*********************************新增图书资料******************************/
void AddBook ()
{
system ("cls");
Book *L, *s;
InitList (L);
ReadData (L);
s = new Book;
cout << "请输入以下信息:" << endl;
cout << "书名:";
cin >> s->name;
cout << "作者:";
cin >> s->author;
cout << "出版社:";
cin >> s->publisher;
cout << "出版日期:";
cin >> s->date;
cout << "书号:";
cin >> s->ISBN;
cout << "\n\n\t\t\t是否保存该图书资料?(Y/N)" << endl;
char c = getch ();
if ((c == 'Y') || (c == 'y'))
{
s->next = L->next;
L->next = s;
SaveData (L);
cout << "\n\n\n\t\t\t保 存 成 功 !" << endl << endl;
}
else
cout << "\n\n\n\t\t\t没有保存任何信息!" << endl << endl;
system ("pause");
}
/****************************按书名查找***********************************/
void SearchByName ()
{
system ("cls");
Book *L;
InitList (L);
ReadData (L);
cout << "请输入书名:";
string str;
cin >> str;
Book *p = L->next;
bool flag = false;
while (p != NULL)
{
if (p->name == str)
{
cout << p << endl;
flag = true;
}
p = p->next;
}
if (!flag)
cout << "\n\n\n\t\t\t\t查无此书!" << endl << endl;
system ("pause");
}
/********************************按作者查找*********************************/
void SearchByAuthor ()
{
system ("cls");
Book *L;
InitList (L);
ReadData (L);
cout << "请输入作者姓名:";
string str;
cin >> str;
Book *p = L->next;
bool flag = false;
while (p != NULL)
{
if (p->author == str)
{
cout << p << endl;
flag = true;
}
p = p->next;
}
if (!flag)
cout << "\n\n\n\t\t\t\t查无此书!" << endl << endl;
system ("pause");
}
/********************************按出版社查找************************************/
void SearchByPublisher ()
{
system ("cls");
Book *L;
InitList (L);
ReadData (L);
cout << "请输入出版社名称:";
string str;
cin >> str;
Book *p = L->next;
bool flag = false;
while (p != NULL)
{
if (p->publisher == str)
{
cout << p << endl;
flag = true;
}
p = p->next;
}
if (!flag)
cout << "\n\n\n\t\t\t\t查无此书!" << endl << endl;
system ("pause");
}
/************************************按出版日期查找**************************************/
void SearchByDate ()
{
system ("cls");
Book *L;
InitList (L);
ReadData (L);
cout << "请输入出版日期:";
string str;
cin >> str;
Book *p = L->next;
bool flag = false;
while (p != NULL)
{
if (p->date == str)
{
cout << p << endl;
flag = true;
}
p = p->next;
}
if (!flag)
cout << "\n\n\n\t\t\t\t查无此书!" << endl << endl;
system ("pause");
}
/******************************************按书号查找**************************************/
void SearchByISBN ()
{
system ("cls");
Book *L;
InitList (L);
ReadData (L);
cout << "请输入书号:";
string str;
cin >> str;
Book *p = L->next;
while ((p != NULL) && (p->ISBN != str))
p = p->next;
if (p == NULL)
cout << "\n\n\n\t\t\t\t查无此书!" << endl << endl;
else
cout << p << endl;
system ("pause");
}
/**************************************删除图书信息*************************************/
void DeleteBook ()
{
system ("cls");
Book *L;
InitList (L);
ReadData (L);
cout << "请输入书号:";
string str;
cin >> str;
Book *p = L, *q = p->next;
while ((q != NULL) && (q->ISBN != str))
{
p = q;
q = q->next;
}
if (q == NULL)
cout << "\n\n\n\t\t\t查无此书!" << endl << endl;
else
{
cout << "该书信息如下:" << p << endl << endl;
cout << "\t\t\t确认删除吗?(Y/N)" << endl;
char c = getch ();
if ((c == 'Y') || (c == 'y'))
{
p->next = q->next;
delete q;
SaveData (L);
cout << "\n\n\n\t\t\t删除成功!" << endl << endl;
}
else
cout << "\n\n\n\t\t\t没有删除任何信息!" << endl;
}
system ("pause");
}
/*****************************查找界面**************************************/
void Search ()
{
while (1)
{
system ("cls");
cout << endl << endl << endl;
cout << "\t*****************请选择查找方式*****************" << endl;
cout << "\t**--------------------------------------------**" << endl;
cout << "\t** (1) 按书名查找 **" << endl;
cout << "\t** (2) 按作者查找 **" << endl;
cout << "\t** (3) 按出版社查找 **" << endl;
cout << "\t** (4) 按出版日期查找 **" << endl;
cout << "\t** (5) 按书号查找 **" << endl;
cout
上一个:C++转换构造函数的问题
下一个:学习 Visual C++的一些问题!