单链表建立图书管理系统

版权声明:吴鹏 https://blog.csdn.net/qq_28311415/article/details/81113922

      这个是自己基于别人博客写的关于图书管理系统的一个项目,这个项目实现了关于图书的增,删,改,查以及书号不能重复的功能,自己再写这个的时候也经历了挺多没想到的错误,一个最难受的错误就是getchar()获取字符时每次会遇到我键盘都没输入但是就出现了字符,这个问题我在上篇文章关于缓冲区的问题已经做了解释。
      接下来就是在20行自己定义的初始化链表的问题,要对定义的变量取指针的别名,这个说真的我不太确定自己是不是想的对,我想的是因为自己结构体定义了一个next指针,所以如果取一次指针,则next指针地址不会改变,相应也不能取到任意自己定义的值,我看了大多数定义的链表,他们初始化基本都是用的*&L,所以实在想不明白的可以牢牢记住就可以了。
         还有就是关于27行运算符重载,我感觉这个<<和>>这两个运算符重载不和一般复数重载一样。感觉怪怪的,不过对这两个运算符重载,网上几乎出奇的一致,所以就拿来用了。以后有时间自己会在重载运算符上好好琢磨一下的。
           接下来是存储信息到硬盘上,这个感觉没甚麽好说的,直接定义个文件夹,利用自己定义好的运算符往进输入就好了,每次添加的信息都放到头部,记得写完关闭文件,要不会出现一些莫名的问题。
           还有这里关于读取函数是的peek()函数,这里也不想讲了,在上面文章中写了关于peek()函数和eof()函数的区别,也做了验证,以后就用peek()函数了。
           还有就是经常用到的system函数,执行系统shell指令类似;pause类似于“请按任意键返回”等待用户,按任意键返回。
          再有就是一些基础知识,关于break,exit(),return几个的用法,自己感觉掌握了,但是这次写的时候还是出错了,所以在这里记一下。
         break:在while()直接退出本层while()循环,在while(1)....switch()中,在case下的break是退出之后又返回while里,在这里载过。
        return:是返回主程序;exit():直接退出函数,1是正常退出,0是异常退出;
        下面是源码:
          

#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>

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;
}

/*****************重载运算符<<******************/
ostream &operator<<(ostream &output, Book *&b)
{
	output << b->Name << '\t' << b->Author << '\t' << b->Publisher << '\t' << b->Date << '\t' << b->ISBN << '\t';
	return output;
}

/*****************重载运算符>>******************/

istream &operator >> (istream &input, Book *&b)
{
	input >> b->Name >> b->Author >> b->Publisher >> b->Date >> b->ISBN;
	return input;
}

/*****************存储图书信息******************/

void  SaveData(Book *&L)
{
	Book *p = L->next;
	ofstream outfile;
	outfile.open("Book.txt", ios::out);
	while(p != NULL)
	{
		outfile << p<<endl;
		p = p->next;
	}
	outfile.close();
}

/*****************读取图书信息******************/

void ReadData(Book *&L)
{
	Book *p, *s;
	ifstream infile;
	p = L;
	infile.open("Book.txt", ios::in);
	while (infile.peek() != EOF)
	{
		s = new Book;
		infile >> s;
		p->next = s;
		p = s;
	}
	p->next = NULL;
}

/*****************重复书号信息的判定******************/
int ISBN(string *str)
{
	Book *L, *s;
	InitList(L);
	ReadData(L);
	s = L->next;
	while (s != NULL)
	{
		if (s->ISBN == (*str))
		{
			return 1;
		}
		s = s->next;
	}
	return 0;
}
/*****************增加图书信息******************/

void AddBook()
{
	while (1)
	{
		Book *L, *s;
		InitList(L);
		ReadData(L);
		s = new Book;
		cout << "增加图书信息如下:" << endl;
			cout << "书名" << endl;
			cin >> s->Name;
			cout << "作者" << endl;
			cin >> s->Author;
			cout << "出版商" << endl;
			cin >> s->Publisher;
			cout << "日期" << endl;
			cin >> s->Date;
			cout << "书号" << endl;
			cin >> s->ISBN;
			if (ISBN(&(s->ISBN)))
			{
				cout << "书号重复,请重新输入:" << endl;
				continue;
			}


		cout << "\n是否要存储该图书信息(Y/N)" << endl;
		cin.ignore(INT_MAX, '\n');

		char c = getchar();
		if (c == 'Y')
		{
			s->next = L->next;
			L->next = s;
			SaveData(L);
		}
		else
			cout << "\n不保存任何图书信息" << endl;
		cout << "是否需要再次增加图书信息(Y/N)\n" << endl;
		cin.ignore(INT_MAX, '\n');

		c = getchar();
		if (c == 'N')
			break;
	}
}

/*****************修改图书信息******************/

void UpdateBook()
{
	system("cls");
	string str;
	Book *L, *s;
	InitList(L);
	ReadData(L);
	s = L->next;
	cout << "请输入要更改的图书信息的书号:" << endl;
	string ISBN;
	cin >> ISBN;
	bool flag;
	flag = false;
	while (s != NULL)
	{
		if (s->ISBN == ISBN)
		{
			flag = true;
			cout << s << endl;
			while (1)
			{
				cout << "是否结束修改(Y/N)" << endl;
				cin.ignore(INT_MAX, '\n');
				char c = getchar();
				if (c == 'Y')
					break;
				cout << endl;
				cout << "/****************修改图书界面************/" << endl;
				cout << "/*****************************************/" << endl;
				cout << "/**************(1)书名修改*****************/" << endl;
				cout << "/**************(2)作者修改*****************/" << endl;
				cout << "/**************(3)出版商修改***************/" << endl;
				cout << "/**************(4)日期修改*****************/" << endl;
				cout << "/**************(0)返回*********************/" << endl;
				cout << "/*****************************************/" << endl;
				cout << "/********通过数字键来选取相应的服务*******/" << endl;
				switch (_getch())
				{
				    case '1':
						cout << "请输入要修改的书名:" << endl;
						cin >> str;
						s->Name = str;
						SaveData(L);
						break; 
					case '2':
						cout << "请输入要修改的作者:" << endl;
						cin >> str;
						s->Author = str;
						SaveData(L);
						break;
					case '3':
						cout << "请输入要修改的出版商:" << endl;
						cin >> str;
						s->Publisher = str;
						SaveData(L);
						break;
					case '4':
						cout << "请输入要修改的日期:" << endl;
						cin >> str;
						s->Date = str;
						SaveData(L);
						break;
					case '0':
						return;
					default:
						break;
				}
			}
		}
		s = s->next;
	}
	if (!flag)
		cout << "查无此书" << endl;
	system("pause");
}
/*****************删除图书信息******************/

void DeleteBook()
{
	system("cls");
	Book *L, *p;
	InitList(L);
	ReadData(L);
	Book *r;
	r = L;
	p = L->next;
	string ISBN;              //根据书号来删除图书信息;
	cout << "请输入要删除图书的书号:" << endl;
	cin >> ISBN;
	bool flag = false;
	while (p != NULL)
	{
		if (p->ISBN == ISBN)
		{
			r->next = p->next;

			cout << "删除的图书信息如下:" << endl;
			cout << p << endl;
			SaveData(L);
			flag = true;
		}
		r = p;
		p = p->next;
	}
	if (!flag)
		cout << "\n没有该图书信息" << endl;
	system("pause");
}

/*****************查找图书信息通过书名******************/

void SearchbyName()
{
	system("cls");
	Book *L, *s;
	InitList(L);
	ReadData(L);
	string  str;
	cout << "请输入要查找的书名" << endl;
	cin >> str;
	s = L->next;
	bool flag = false;
	while (s != NULL)
	{
		if (s->Name == str)
		{
			cout << "要查找的图书信息如下:" << endl;
			flag = true;
			cout << s << endl;
		}
		s = s->next;
	}
	if (!flag)
		cout << "查无此书" << endl;
}


/*****************查找图书信息通过书名******************/

void SearchbISBN()
{
	system("cls");
	Book *L, *s;
	InitList(L);
	ReadData(L);
	string  str;
	cout << "请输入要查找的书号" << endl;
	cin >> str;
	s = L->next;
	bool flag = false;
	while (s != NULL)
	{
		if (s->ISBN == str)
		{
			cout << "要查找的图书信息如下:" << endl;
			flag = true;
			cout << s << endl;
		}
		s = s->next;
	}
	if (!flag)
		cout << "查无此书" << endl;
}


/*****************查找图书信息通过作者******************/

void SearchbyAuthor()
{
	system("cls");
	Book *L, *s;
	InitList(L);
	ReadData(L);
	string  str;
	cout << "请输入要查找的作者" << endl;
	cin >> str;
	s = L->next;
	bool flag = false;
	while (s != NULL)
	{
		if (s->Author == str)
		{
			cout << "要查找的图书信息如下:" << endl;
			flag = true;
			cout << s << endl;
		}
		s = s->next;
	}
	if (!flag)
		cout << "查无此书" << endl;
}


/*****************查找图书信息通过出版商******************/

void SearchbyPublisher()
{
	system("cls");
	Book *L, *s;
	InitList(L);
	ReadData(L);
	string  str;
	cout << "请输入要查找的出版商" << endl;
	cin >> str;
	s = L->next;
	bool flag = false;
	while (s != NULL)
	{
		if (s->Publisher == str)
		{
			cout << "要查找的图书信息如下:" << endl;
			flag = true;
			cout << s << endl;
		}
		s = s->next;
	}
	if (!flag)
		cout << "查无此书" << endl;
}


/*****************查找图书信息通过日期******************/

void SearchbyDate()
{
	system("cls");
	Book *L, *s;
	InitList(L);
	ReadData(L);
	string  str;
	cout << "请输入要查找的日期" << endl;
	cin >> str;
	s = L->next;
	bool flag = false;
	while (s != NULL)
	{
		if (s->Date == str)
		{
			cout << "要查找的图书信息如下:" << endl;
			flag = true;
			cout << s << endl;
		}
		s = s->next;
	}
	if (!flag)
		cout << "查无此书" << endl;
}

/*****************查找主界面******************/

void Search()
{
	system("cls");
	while (1)
	{
		cout << endl;
		cout << "/****************查找图书主界面************/"<<endl;
		cout << "/*****************************************/" << endl;
		cout << "/**************(1)书名查找*****************/" << endl;
		cout << "/**************(2)作者查找*****************/" << endl;
		cout << "/**************(3)出版商查找***************/" << endl;
		cout << "/**************(4)日期查找*****************/" << endl;
		cout << "/**************(5)书号查找*****************/" << endl;
		cout << "/**************(0)返回*********************/" << endl;
		cout << "/*****************************************/" << endl;
		cout << "/********通过数字键来选取相应的服务*******/" << endl;
		switch (_getch())
		{
		  case '1':
			  SearchbyName();
			  break;
		  case '2':
			  SearchbyAuthor();
			  break;
		  case'3':
			  SearchbyPublisher();
			  break;
		  case'4':
			  SearchbyDate();
			  break;
		  case'5':
			  SearchbISBN();
			 break;
		  case'0':
			  return;
		  default:
			  break;
		}
	}
}

/*****************菜单主界面******************/

void menu()
{
	system("cls");
	while (1)
	{
		cout << endl;
		cout << "/****************图书管理主界面************/" << endl;
		cout << "/*****************************************/" << endl;
		cout << "/**************(1)新增图书*****************/" << endl;
		cout << "/**************(2)查找图书*****************/" << endl;
		cout << "/**************(3)删除图书****************/" << endl;
		cout << "/**************(4)修改图书****************/" << endl;
		cout << "/**************(0)返回********************/" << endl;
		cout << "/*****************************************/" << endl;
		cout << "/********通过数字键来选取相应的服务*******/" << endl;
		switch (_getch())
		{
		case '1':
			AddBook();
			break;
		case '2':
			Search();
			break;
		case'3':
			DeleteBook();
			break;
		case '4':
			UpdateBook();
			break;
		case'0':
			cout<<"谢谢使用"<<endl;
			exit(1);
		default:
			break;
		}
	}
}

void main()
{
	menu();
}

猜你喜欢

转载自blog.csdn.net/qq_28311415/article/details/81113922