基于单链表的图书管理系统

上个月在复习链表知识时,本来想动手运用下,但是我没想到我上一篇关于链表的文章写的程序漏洞百出,加上其他班要求写一个基于单链表的图书管理系统,我的班没有布置,并且大二要学数据结构,又听说链表是数据结构里最简单的一部分,只得花了一个月来查阅各种资料、阅读各种博文,在综合网友们的代码后,自己整理了下,写了这个图书管理系统(一定还有bug我没发现),代码如下:

#include<iostream>
#include<cstring>
#include<conio.h>
#include<cstdlib>
#include<algorithm>
#include<iomanip>
#include<Windows.h>
using namespace std;

struct mInfo {
	unsigned long long ID;
	char name[21];
	int score;
};

struct mList {
	struct mInfo data;
	struct mList* next;
};

mList* mCreate(mList* mHead)
{
	//initialize the list
	mHead = (mList*)malloc(sizeof(mList));
	if (mHead == NULL) {
		return 0;
	}
	mHead->next = NULL;
	return mHead;
}

mList* add_m(mList* mHead)
{
	mList* s, * p;//p->pointer to next,s->pointer to data
	p = mHead;
	int num = 0;
	cout << "How many memberships do you want to registry: ";
	cin >> num;
	for (int i = 0; i < num; i++) {
		s = (mList*)malloc(sizeof(mList));
		cout << "memership No." << i+1 << " 's info" << endl;
		cout << "enter the num: ";
		cin >> s->data.ID;
		cout << "enter the name: ";
		cin >> s->data.name;
		cout << "enter the score: ";
		cin >> s->data.score;
		p->next = s;
		p = s;
		system("cls");
	}
	p->next = NULL;
	return mHead;
}

void print_m(mList* L)
{
	cout << "num" << '\t' << "name" << '\t' << "score\n";
	mList* p;
	p = L->next;
	while (p != NULL) {
		cout.setf(ios::fixed);
		cout << p->data.ID << '\t' << p->data.name << "\t" << p->data.score << endl;
		p = p->next;
		//system("pause");
	}
}

void insert_m(mList* L)
{
	mList* p, * s;//p->pointer to next,s->pointer to data
	p = L->next;
	s = (mList*)malloc(sizeof(mList));
	while (p->next != NULL) {
		p = p->next;
	}
	cout << "enter the ID for membership: ";
	cin >> s->data.ID;
	cout << "enter the name: ";
	cin >> s->data.name;
	cout << "enter the score: ";
	cin >> s->data.score;
	s->next = p->next;
	p->next = s;
}

void del_m(mList* L)
{
	mList* p, *pre;
	p = L->next;
	pre = p;
	unsigned long long num = 0;
	cout << "enter the ID: ";
	cin >> num;
	while (p != NULL) {
		if (p->data.ID == num) {
			pre->next = p->next;
			free(p);
			cout << "delete successfully\n";
			return;
		}
		pre = p;
		p = p->next;
	}
	cout << "could not find the book that you want to delete\n";
}

void edit_m(mList* L) 
{
	mList* p;
	unsigned num = 0;
	p = L->next;
	cout << "enter the num of the book that you want to delete: ";
	cin >> num;
	while (p != NULL) {
		if (p->data.ID == num) {
			cout << "you can only edit the name\n";
			/*cout << "enter the new num: ";
			cin >> p->data.ID;*/
			cout << "enter the new name: ";
			cin >> p->data.name;
			//cout << "enter the new score: "
			return;
		}
		p = p->next;
	}
	cout << "could not find the book that you want to edit\n";
	p = p->next;
}

void search_m(mList* L)
{
	mList* p;
	unsigned long long num = 0;
	p = L->next;
	cout << "enter the book that you want to search: ";
	cin >> num;
	while (p != NULL) {
		if (p->data.ID == num) {
			cout << "num" << '\t' << "name" << '\t' << "price\n";
			cout << p->data.ID << '\t' << p->data.name << '\t' << p->data.score << endl;
			//system("pause");
			return;
		}
		p = p->next;
	}
	cout << "not found\n";
	system("pause");
}

struct info_b {
	unsigned long long ID;
	char name[21];
	double price;
};

struct bList {
	struct info_b data;
	struct bList* next;
};

struct bList* bCreate(bList* bHead)
{
	//initialize the list
	bHead = (struct bList*)malloc(sizeof(struct bList));
	if (bHead == NULL) {
		return 0;
	}
	bHead->next = NULL;
	return bHead;
}

bList* add_b(bList* bHead)
{
	bList* p = NULL, * s = NULL;
	int num = 0;
	p = bHead;
	cout << "How many books that you want to registry: ";
	cin >> num;
	for (int i = 0; i < num; i++) {
		cout << "book No." << i + 1 << " 's info\n";
		s = (bList*)malloc(sizeof(bList));
		cout << "enter the book ID: ";
		cin >> s->data.ID;
		cout << "enter the book name: ";
		cin >> s->data.name;
		cout << "enter the book price: ";
		cin >> s->data.price;
		p->next = s;
		p = s;
		system("cls");
	}
	p->next = NULL;
	return bHead;
}

void print_b(bList* L)
{
	bList* p;
	p = L->next;
	cout << "book num" << '\t' << "book name" << '\t' << "book price\n";
	while (p != NULL) {
		cout.setf(ios::fixed);
		cout << p->data.ID << '\t' << p->data.name << '\t' << setprecision(2) << p->data.price << endl;;
		p = p->next;
	}
}

void insert_b(bList* L)
{
	bList* p, * s;
	p = L->next;
	while (p->next != NULL) {
		p = p->next;
	}
	s = (bList*)malloc(sizeof(bList));
	cout << "enter the book ID: ";
	cin >> s->data.ID;
	cout << "enter the book name: ";
	cin >> s->data.name;
	cout << "enter the book price: ";
	cin >> s->data.price;
	s->next = p->next;
	p->next = s;//pointer point to the next data
}

void del_b(bList* L)
{
	bList* p,* pre;
	unsigned long long num = 0;
	p = L->next;
	pre = p;
	cout << "enter the book ID that you want to delete: ";
	cin >> num;
	while (p != NULL) {
		if (p->data.ID == num) {
			pre->next = p->next;
			free(p);
			cout << "delete successfully\n";
			return;
		}
		pre = p;
		p = p->next;
	}
}

void edit_b(bList* L)
{
	bList* p;
	p = L->next;
	unsigned long long num = 0;
	cout << "enter the book ID that you want to edit: ";
	cin >> num;
	while (p != NULL) {
		if (p->data.ID == num) {
			cout << "enter the book ID: ";
			cin >> p->data.ID;
			cout << "enter the book name: ";
			cin >> p->data.name;
			cout << "enter the book price: ";
			cin >> p->data.price;
			cout << "edit successfully\n";
			return;
		}
		p = p->next;
	}
}

void search_b(bList* L)
{
	bList* p;
	p = L->next;
	unsigned long long num;
	cout << "enter the book ID that you want to search: ";
	cin >> num;
	while (p != NULL) {
		if (p->data.ID == num) {
			cout.setf(ios::fixed);
			cout << "book ID" << '\t' << "book name" << '\t' << "book price\n";
			cout << p->data.ID << '\t' << p->data.name << '\t' <<setprecision(2) << p->data.price << endl;
			return;
		}
		p = p->next;
	}
	cout << "opps, could not find the book\n";
}

void total_b(bList* L)
{
	double sum = 0;
	bList* p = L->next;
	while (p != NULL) {
		sum += p->data.price;
		p = p->next;
	}
	cout << "calculating...\n";
	Sleep(1500);
	cout.setf(ios::fixed);
	cout << "the total cost: " << setprecision(2) << sum << '$' << endl;
}

void login()
{

	string account, password;
	int index = 0;
	char ch;
	while (1) {
		cout << "account: ";
		cin >> account;
		cout << "password: ";
		while ((ch = _getch()) != '\r') {
			if (ch == 8) {
				password[index] = '\0';
				break;
			}
			password += ch;
			index++;
			if (ch != '\b') {
				cout << "*";
			}
			else {
				if (index != 0) {
					cout << '\b' << " " << '\b';
					index--;
				}
			}
		}
		if ((account == "admin") && (password == "123456")) {
			break;
		}
		else {
			cout << "\nerror,check your account or password" << endl;
			system("pause");
			fflush(stdin);
			password.clear();
			system("cls");
		}
	}
}

int menu()
{
	int mode = 0;
	cout << "**********************************************" << "\n" << endl;
	cout << "* book sales management system(admin edition)" << '\n' << endl;
	cout << "*     1.membership management  " << '\n' << endl;
	cout << "*     2.books management       " << '\n' << endl;
	cout << "*     3.clear the bill         " << '\n' << endl;
	cout << "*     0.sign out               " << '\n' << endl;
	cout << "**********************************************" << "\n" << endl;
	cout << "choose a mode: ";
	cin >> mode;
	return mode;
}

int main()
{
	login();
	mList* mHead = NULL, * M = NULL;
	bList* bHead = NULL, * B = NULL;
	int mode = 0;
	system("cls");
	while (1) {
		mode = menu();
		if (mode == 1) {
			while (1) {
				system("cls");
				int mode1;
				cout << "book sales management system > membership management " << endl;
				cout << "******************************" << "\n" << endl;
				cout << "* 1.initialize the system" << '\n' << endl;
				cout << "* 2.show all the memberships " << '\n' << endl;
				cout << "* 3.add membership           " << '\n' << endl;
				cout << "* 4.edit membership          " << '\n' << endl;
				cout << "* 5.delete membership        " << '\n' << endl;
				cout << "* 6.search the membership    " << '\n' << endl;
				cout << "* 0.back to main menu        " << '\n' << endl;
				cout << "******************************" << '\n' << endl;
				cout << "select a mode: ";
				cin >> mode1;
				if (mode1 == 1) {
					mHead = mCreate(mHead);
					M = add_m(mHead);
					system("pause");
				}
				else if (mode1 == 2) {
					print_m(M);
					system("pause");
				}
				else if (mode1 == 3) {
					insert_m(M);
					system("pause");
				}
				else if (mode1 == 4) {
					edit_m(M);
					system("pause");
				}
				else if (mode1 == 5) {
					del_m(M);
					system("pause");
				}
				else if (mode1 == 6) {
					search_m(M);
					system("pause");
				}
				else if (mode1 == 0) {
					system("pause");
					system("cls");
					break;
				}
				else {
					cout << "opps, please check what you inputted\n";
					system("pause");
					system("cls");
				}
			}
		}
		else if (mode == 2) {
			while (1) {
				system("cls");
				cout << "book sales management system > book management" << endl;
				cout << "***************************" << '\n' << endl;
				cout << "* 1.initialize the system" << '\n' << endl;
				cout << "* 2.browse the book   " << "\n" << endl;
				cout << "* 3.add book          " << '\n' << endl;
				cout << "* 4.edit book         " << '\n' << endl;
				cout << "* 5.delete book       " << '\n' << endl;
				cout << "* 6.search the book   " << '\n' << endl;
				cout << "* 0.back to main menu " << '\n' << endl;
				cout << "***************************" << '\n' << endl;
				int mode2;
				cout << "select one mode: ";
				cin >> mode2;
				if (mode2 == 1) {
					bHead = bCreate(bHead);
					B = add_b(bHead);
					system("pause");
				}
				else if(mode2 == 2) {
					print_b(B);
					system("pause");
				}
				else if(mode2 == 3) {
					insert_b(B);
					system("pause");
				}
				else if (mode2 == 4) {
					edit_b(B);
					system("pause");
				}
				else if (mode2 == 5) {
					del_b(B);
					system("pause");
				}
				else if (mode2 == 6) {
					search_b(B);
					system("pause");
				}
				else if (mode2 == 0) {
					system("pause");
					system("cls");
					break;
				}
				else {
					cout << "opps, please check what you inputted\n";
					system("pause");
					system("cls");
				}
			}
		}
		else if (mode == 3) {
			double sum = 0,temp = 0;
			while (1) {
				cout << "Do you want to clear the bill?(y/n) ";
				char ch;
				cin >> ch;
				if ((ch == 'y') || (ch == 'Y')) {
					total_b(B);
					//system("pause");
				}
				else if ((ch == 'n') || (ch == 'N')) {
					cout << "exiting...\n";
					Sleep(2000);
					//system("pause");
					break;
				}
				system("pause");
				system("cls");
				break;
			}
		}
		else if (mode == 0) {
			system("pause");
			system("cls");
			break;
		}
		else {
			cout << "opps, please check what you inputted\n";
			system("pause");
			system("cls");
		}
	}
}

如果各位读者能发现我的代码中的bug的话,欢迎指出,也可和我私信一同讨论!