今天是2015年第五天,老师马上让交C++论文了,一哥们在网上找了一个图书管理系统,准备以此来作为他的论文程序,
然而,这个程序有错误之处,不知是不是原著内心有鬼故意为之来坑我们这些菜鸟,还是原著也没有改正这个不足之处。
他后来让我帮他改一下这个程序的错误。
我先说一下原来的错误:
1、这个程序,原来时不能新增图书。
为什么呢?原先这个程序只能先在文件当前目录下建立一个book.txt文件才能在运行程序时新增图书。我觉得这会非常不方便,为什么不能程序自动建立一个book.txt文件呢
哥们原来给我的程序:
#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); //错误之处在这里。第一次运行程序时,程序不能自动建立一个book.txt文件,导致无法新增图书。
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 << "\t** (0) 返 回 **" << endl;
cout << "\t**--------------------------------------------**" << endl;
cout << "\t***********请选择数字键选择相应的服务***********" << endl;
switch (getch ())
{
case '1':
SearchByName ();
break;
case '2':
SearchByAuthor ();
break;
case '3':
SearchByPublisher ();
break;
case '4':
SearchByDate ();
break;
case '5':
SearchByISBN ();
break;
case '0':
return;
default:
break;
}
}
}
/**********************************主界面********************************/
void Menu ()
{
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** (0) 退 出 **" << endl;
cout << "\t**----------------------------------------------------**" << endl;
cout << "\t****************请选择数字键选择相应的服务**************" << endl;
switch (getch ())
{
case '1':
AddBook ();
break;
case '2':
Search ();
break;
case '3':
DeleteBook ();
break;
case '0':
cout << "\n\n\n\t\t\t谢 谢 使 用 !" << endl << endl;
exit (0);
default:
break;
}
}
}
int main ()
{
Menu ();
return 0;
}
经过我矫正后的程序:
#include "stdafx.h"
// 图书管理系统.cpp : 定义控制台应用程序的入口点。
//
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;
static int i=0; //判断是不是第一次运行程序。
/************************图书资料***************************/
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);
if(i!=0)
ReadData (L); //注意:第n(n>1)次时,将book.txt文件中的每一个图书记录插入到单链表L中
else
i=0; //注意:第n(n=1)次运行程序i=0,此时程序会自动建立一个book.txt文件,用于存储图书记录。
s = new Book;
i=1;
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->next << endl << endl;//注意一定是p->next,不然它将删除上一组数据。
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 << "\t** (0) 返 回 **" << endl;
cout << "\t**--------------------------------------------**" << endl;
cout << "\t***********请选择数字键选择相应的服务***********" << endl;
switch (getch ())
{
case '1':
SearchByName ();
break;
case '2':
SearchByAuthor ();
break;
case '3':
SearchByPublisher ();
break;
case '4':
SearchByDate ();
break;
case '5':
SearchByISBN ();
break;
case '0':
return;
default:
break;
}
}
}
/**********************************主界面********************************/
void Menu ()
{
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** (0) 退 出 **" << endl;
cout << "\t**----------------------------------------------------**" << endl;
cout << "\t****************请选择数字键选择相应的服务**************" << endl;
switch (getch ())
{
case '1':
AddBook ();
break;
case '2':
Search ();
break;
case '3':
DeleteBook ();
break;
case '0':
cout << "\n\n\n\t\t\t谢 谢 使 用 !" << endl << endl;
exit (0);
default:
break;
}
}
}
int main ()
{
Menu ();
return 0;
}
//Over