C++数组实现简单的图书管理系统

前言

本项目中只包含简单的增、删、改、查及插入、排序等基础功能,适合有一定编辑语言基础的同学,本文中出现的代码都为基础的知识,由于是本人临时编写,其中有一定的错误的个人理解及代码,若有问题,请指正。本人发布该博客的意图在于基础的学习交流,希望该文章能够以一种交流工具的形式存在。☆*: .。. o(≧▽≦)o .。.:*☆  ☆*: .。. o(≧▽≦)o .。.:*☆  ╰(*°▽°*)╯  ╰(*°▽°*)╯

类的创建

在这个示例中,我们首先创建了一个名为 Book_List 的类,它具有一个动态分配的数组和一个表示数组大小的变量。

//书本类
class Books
{
public:
	int IBSN;//书籍编号
	string Title;//书名
	string Author;//作者
	float Price;//价格
};

//书的数组类
class Book_List
{
public:
	int size = 10;//动态数组初始大小
	Books* book_list = new Books[size];//用指针指向动态分配的书本数组
	int List_Size;//数组中数据个数
};

数组初始化

 

void Init_List(Book_List* L)
{
	L->List_Size = 0;//初始化数组的数据长度
}

void Add_List(Book_List* L , Books B)
{
	while (1)
	{
		if (L->List_Size < L->size)//当数组容量未满时,进行数据添加
		{
			cout << "请输入你想要添加的图书信息(IBSN、书名、作者、价格):" << endl;
			cin >> B.IBSN >> B.Title >> B.Author >> B.Price;
			L->book_list[L->List_Size] = B;//插入数据
			L->List_Size++;//更新数组中数据量
			cout << "添加书籍成功!" << endl;
			return;
		}
		while (1)//当数组空间满时,进入选择扩容的循环
		{
			cout << "该图书系统书库已满,是否增大书库库存?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选择:";
			int choice;
			cin >> choice;
			if (choice == 1)
			{
				int temp = L->size + 1;
				Books* New_list = new Books[temp];//创建一个数据容量大于原数组的数组
				for (int i = 0; i < L->size; i++)
				{
					New_list[i] = L->book_list[i];//将原数组数据存入新数组
				}
				delete[] L->book_list;//清空原数组
				L->book_list = New_list;//将新数组赋予原数组
				L->size = temp;//更新数组容量
				cout << "扩容成功!" << endl;
				break;//跳出while循环,进行数据添加
			}
			else if (choice == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确的选项!" << endl;
			}
		}
	}
}

void Delete_List(Book_List* L, Books B)
{
	while (1)
	{
		cout << "请输入你要删除的图书的ISBN:" << endl;
		int delete_ISBN;
		cin >> delete_ISBN;
		int temp = 0;//定义一个判断是否删除的变量
		for (int i = 0; i < L->size; i++)
		{
			if (L->book_list[i].IBSN == delete_ISBN)//遍历查找
			{
				for (int j = i; j < L->size; j++)//使删除项开始的所有数据前移
				{
					L->book_list[j] = L->book_list[j + 1];
				}
				L->List_Size--;//更新数组数据量
				cout << "删除成功!" << endl;
				temp = 1;//表已删除
				break;//跳出while
			}
		}
		if (temp == 0)//未删除时输出
		{
			cout << "未在书库中找到要删除的图书!" << endl;
		}
		while (1)//判断是否继续运行该功能
		{
			cout << "是否继续删除?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选项:" << endl;
			int choice;
			cin >> choice;
			if (choice == 1)
			{
				break;
			}
			else if (choice == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确的选项!" << endl;
			}
		}
	}
}

void Change_List(Book_List* L, Books B)
{
	while (1)
	{
		cout << "请输入你想要修改的图书的ISBN:" << endl;
		int change_ISBN;
		cin >> change_ISBN;
		int temp = 0;//定义一个判断是否修改的变量
		for (int i = 0; i < L->size; i++)
		{
			if (L->book_list[i].IBSN == change_ISBN)//遍历查找
			{
				while (1)//选择单项修改
				{
					cout << "已经找到该书籍,请选择你要修改的内容:"
						<< "1.书名"
						<< "2.作者"
						<< "3.价格"
						<< "请输入你的选项:" << endl;
					int choice_change;
					cin >> choice_change;
					if (choice_change == 1)
					{
						cout << "请输入要修改的书名:";
						string change_title;
						cin >> change_title;
						L->book_list[i].Title = change_title;
						break;
					}
					else if (choice_change == 2)
					{
						cout << "请输入要修改的作者名:";
						string change_author;
						cin >> change_author;
						L->book_list[i].Author = change_author;
						break;
					}
					else if (choice_change == 3)
					{
						cout << "请输入要修改的价格:";
						float change_price;
						cin >> change_price;
						L->book_list[i].Price = change_price;
						break;
					}
					else
					{
						cout << "请输入正确选项!" << endl;
					}
				}
				temp = 1;//表已修改
				cout << "修改成功!" << endl;
				break;
			}
		}
		if (temp == 0)//未修改时输出
        {
			cout << "未在书库中找到要删除的图书!" << endl;
		}
        while (1)//选择是否继续该功能
		{
			cout << "是否继续修改?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选项:" << endl;
			int choice_continue;
			cin >> choice_continue;
			if (choice_continue == 1)
			{
				break;
			}
			else if (choice_continue == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确选项!" << endl;
			}
		}
	}
}

这里使用了效率低,但更基础的冒泡排序

void Find_List(Book_List* L, Books B)
{
	while (1)
	{
		while (1)//进入选择查询方式的循环
		{
			cout << "请输入查找图书的方式:"
				<< "1.ISBN"
				<< "2.书名"
				<< "请输入你的选项:" << endl;
			int choice_find;
			cin >> choice_find;
			int temp = 0;//定义一个判断是否查询到的变量
			if (choice_find == 1)
			{
				cout << "请输入你要查找的图书的ISBN:";
				int find_ISBN;
				cin >> find_ISBN;
				for (int i = 0; i < L->size; i++)
				{
					if (L->book_list[i].IBSN == find_ISBN)//遍历查找并输出其信息
					{
						cout << "已找到该书,其信息如下:"
							<< L->book_list[i].IBSN << L->book_list[i].Title << L->book_list[i].Author << L->book_list[i].Price;
						temp = 1;//表已查询
						break;
					}
				}
				if (temp == 0)
				{
					cout << "未在书库中找到要删除的图书!" << endl;
				}
				break;
			}
			else if (choice_find == 2)
			{
				cout << "请输入你要查找的图书的书名:";
				string find_title;
				cin >> find_title;
				for (int j = 0; j < L->size; j++)
				{
					if (L->book_list[j].Title == find_title)
					{
						cout << "已找到该书,其信息如下:"
							<< L->book_list[j].IBSN << L->book_list[j].Title << L->book_list[j].Author << L->book_list[j].Price;
						temp = 1;
						break;
					}
				}
				if (temp == 0)
				{
					cout << "未在书库中找到要删除的图书!" << endl;
				}
				break;
			}
			else
			{
				cout << "请输入正确选项!" << endl;
			}
		}
		while (1)//选择是否继续该功能
		{
			cout << "是否继续查找?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选项:" << endl;
			int choice_continue;
			cin >> choice_continue;
			if (choice_continue == 1)
			{
				break;
			}
			else if (choice_continue == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确选项!" << endl;
			}
		}
	}
}

插入

void Insert_List(Book_List* L, Books B)
{
	while (1)
	{
		while (1)//判断数组容量是否为满
		{
			int temp = 0;//定义一个判断是否插入的变量
			if (L->List_Size < L->size)//判断数组容量是否为满
			{
				while (1)
				{
					cout << "请输入要插入的位置:" << endl;
					int location;
					cin >> location;
					if (location > 0 && location < L->size + 1)//判断插入位置是否合法
					{
						cout << "请输入要插入的书籍信息(IBSN、书名、作者、价格):" << endl;
						cin >> B.IBSN >> B.Title >> B.Author >> B.Price;
						if (location > L->List_Size)//在已有数据后插入时
						{
							L->book_list[location - 1] = B;//直接插入
							L->List_Size++;//更新数组数据量
							temp = 1;//表已插入
							break;
						}
						else//在已有数据间插入时
						{
							for (int i = L->List_Size; i > location; i--)
							{
								L->book_list[i] = L->book_list[i - 1];//从插入项开始数据后移
							}
							L->book_list[location - 1] = B;
							L->List_Size++;
							temp = 1;
							break;
						}
					}
					else
					{
						cout << "请输入正确的插入位置" << endl;
					}
				}
			}
			if (temp == 1)//已插入时跳出循环
			{
				break;
			}
			while (1)
			{
				cout << "该图书系统书库已满,是否增大书库库存?"
					<< "1.是"
					<< "2.否"
					<< "请输入你的选择:";
				int choice;
				cin >> choice;
				if (choice == 1)
				{
					int temp = L->size + 1;
					Books* New_list = new Books[temp];
					for (int i = 0; i < L->size; i++)
					{
						New_list[i] = L->book_list[i];
					}
					delete[] L->book_list;
					L->book_list = New_list;
					L->size = temp;
					cout << "扩容成功!" << endl;
					break;
				}
				else if (choice == 2)
				{
					return;
				}
				else
				{
					cout << "请输入正确的选项!" << endl;
				}
			}
		}
		cout << "插入成功!" << endl;
		while (1)
		{
			cout << "是否继续插入?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选项:" << endl;
			int choice_continue;
			cin >> choice_continue;
			if (choice_continue == 1)
			{
				break;
			}
			else if (choice_continue == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确选项!" << endl;
			}
		}
	}
}

排序

void Sort_List(Book_List* L)
{
	while (1)
	{
		cout << "请输入要排序的方式:"
			<< "1.ISBN"
			<< "2.价格"
			<< "请输入你的选项:" << endl;
		int choice_sort;
		cin >> choice_sort;
		if (choice_sort == 1)
		{
			for (int i = 0; i < L->List_Size - 1; i++)
			{
				for (int j = 0; j < L->List_Size - i - 1; j++)
				{
					if (L->book_list[j].IBSN > L->book_list[j + 1].IBSN)
					{
						Books temp = L->book_list[j];
						L->book_list[j] = L->book_list[j + 1];
						L->book_list[j + 1] = temp;
					}
				}
			}
			break;
		}
		else if (choice_sort == 2)
		{
			for (int i = 0; i < L->List_Size - 1; i++)
			{
				for (int j = 0; j < L->List_Size - i - 1; j++)
				{
					if (L->book_list[j].Price > L->book_list[j + 1].Price)
					{
						Books temp = L->book_list[j];
						L->book_list[j] = L->book_list[j + 1];
						L->book_list[j + 1] = temp;
					}
				}
			}
			break;
		}
		else
		{
			cout << "请输入正确选项!" << endl;
		}
	}
	cout << "排序成功!" << endl;
}

完整代码如下:

#include"iostream"
#include"string"
using namespace std;

class Books
{
public:
	int IBSN;
	string Title;
	string Author;
	float Price;
};

class Book_List
{
public:
	int size = 10;
	Books* book_list = new Books[size];
	int List_Size;
};

void Init_List(Book_List* L)
{
	L->List_Size = 0;
}

void Add_List(Book_List* L , Books B)
{
	while (1)
	{
		if (L->List_Size < L->size)
		{
			cout << "请输入你想要添加的图书信息(IBSN、书名、作者、价格):" << endl;
			cin >> B.IBSN >> B.Title >> B.Author >> B.Price;
			L->book_list[L->List_Size] = B;
			L->List_Size++;
			cout << "添加书籍成功!" << endl;
			return;
		}
		while (1)
		{
			cout << "该图书系统书库已满,是否增大书库库存?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选择:";
			int choice;
			cin >> choice;
			if (choice == 1)
			{
				int temp = L->size + 1;
				Books* New_list = new Books[temp];
				for (int i = 0; i < L->size; i++)
				{
					New_list[i] = L->book_list[i];
				}
				delete[] L->book_list;
				L->book_list = New_list;
				L->size = temp;
				cout << "扩容成功!" << endl;
				break;
			}
			else if (choice == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确的选项!" << endl;
			}
		}
	}
}

void Delete_List(Book_List* L, Books B)
{
	while (1)
	{
		cout << "请输入你要删除的图书的ISBN:" << endl;
		int delete_ISBN;
		cin >> delete_ISBN;
		int temp = 0;
		for (int i = 0; i < L->size; i++)
		{
			if (L->book_list[i].IBSN == delete_ISBN)
			{
				for (int j = i; j < L->size; j++)
				{
					L->book_list[j] = L->book_list[j + 1];
				}
				L->List_Size--;
				cout << "删除成功!" << endl;
				temp = 1;
				break;
			}
		}
		if (temp == 0)
		{
			cout << "未在书库中找到要删除的图书!" << endl;
		}
		while (1)
		{
			cout << "是否继续删除?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选项:" << endl;
			int choice;
			cin >> choice;
			if (choice == 1)
			{
				break;
			}
			else if (choice == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确的选项!" << endl;
			}
		}
	}
}

void Change_List(Book_List* L, Books B)
{
	while (1)
	{
		cout << "请输入你想要修改的图书的ISBN:" << endl;
		int change_ISBN;
		cin >> change_ISBN;
		int temp = 0;
		for (int i = 0; i < L->size; i++)
		{
			if (L->book_list[i].IBSN == change_ISBN)
			{
				while (1)
				{
					cout << "已经找到该书籍,请选择你要修改的内容:"
						<< "1.书名"
						<< "2.作者"
						<< "3.价格"
						<< "请输入你的选项:" << endl;
					int choice_change;
					cin >> choice_change;
					if (choice_change == 1)
					{
						cout << "请输入要修改的书名:";
						string change_title;
						cin >> change_title;
						L->book_list[i].Title = change_title;
						break;
					}
					else if (choice_change == 2)
					{
						cout << "请输入要修改的作者名:";
						string change_author;
						cin >> change_author;
						L->book_list[i].Author = change_author;
						break;
					}
					else if (choice_change == 3)
					{
						cout << "请输入要修改的价格:";
						float change_price;
						cin >> change_price;
						L->book_list[i].Price = change_price;
						break;
					}
					else
					{
						cout << "请输入正确选项!" << endl;
					}
				}
				temp = 1;
				cout << "修改成功!" << endl;
				break;
			}
		}
		if (temp == 0)
		{
			cout << "未在书库中找到要删除的图书!" << endl;
		}
		while (1)
		{
			cout << "是否继续修改?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选项:" << endl;
			int choice_continue;
			cin >> choice_continue;
			if (choice_continue == 1)
			{
				break;
			}
			else if (choice_continue == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确选项!" << endl;
			}
		}
	}
}

void Find_List(Book_List* L, Books B)
{
	while (1)
	{
		while (1)
		{
			cout << "请输入查找图书的方式:"
				<< "1.ISBN"
				<< "2.书名"
				<< "请输入你的选项:" << endl;
			int choice_find;
			cin >> choice_find;
			int temp = 0;
			if (choice_find == 1)
			{
				cout << "请输入你要查找的图书的ISBN:";
				int find_ISBN;
				cin >> find_ISBN;
				for (int i = 0; i < L->size; i++)
				{
					if (L->book_list[i].IBSN == find_ISBN)
					{
						cout << "已找到该书,其信息如下:"
							<< L->book_list[i].IBSN << L->book_list[i].Title << L->book_list[i].Author << L->book_list[i].Price;
						temp = 1;
						break;
					}
				}
				if (temp == 0)
				{
					cout << "未在书库中找到要查找的图书!" << endl;
				}
				break;
			}
			else if (choice_find == 2)
			{
				cout << "请输入你要查找的图书的书名:";
				string find_title;
				cin >> find_title;
				for (int j = 0; j < L->size; j++)
				{
					if (L->book_list[j].Title == find_title)
					{
						cout << "已找到该书,其信息如下:"
							<< L->book_list[j].IBSN << L->book_list[j].Title << L->book_list[j].Author << L->book_list[j].Price;
						temp = 1;
						break;
					}
				}
				if (temp == 0)
				{
					cout << "未在书库中找到要查找的图书!" << endl;
				}
				break;
			}
			else
			{
				cout << "请输入正确选项!" << endl;
			}
		}
		while (1)
		{
			cout << "是否继续查找?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选项:" << endl;
			int choice_continue;
			cin >> choice_continue;
			if (choice_continue == 1)
			{
				break;
			}
			else if (choice_continue == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确选项!" << endl;
			}
		}
	}
}

void Sort_List(Book_List* L)
{
	while (1)
	{
		cout << "请输入要排序的方式:"
			<< "1.ISBN"
			<< "2.价格"
			<< "请输入你的选项:" << endl;
		int choice_sort;
		cin >> choice_sort;
		if (choice_sort == 1)
		{
			for (int i = 0; i < L->List_Size - 1; i++)
			{
				for (int j = 0; j < L->List_Size - i - 1; j++)
				{
					if (L->book_list[j].IBSN > L->book_list[j + 1].IBSN)
					{
						Books temp = L->book_list[j];
						L->book_list[j] = L->book_list[j + 1];
						L->book_list[j + 1] = temp;
					}
				}
			}
			break;
		}
		else if (choice_sort == 2)
		{
			for (int i = 0; i < L->List_Size - 1; i++)
			{
				for (int j = 0; j < L->List_Size - i - 1; j++)
				{
					if (L->book_list[j].Price > L->book_list[j + 1].Price)
					{
						Books temp = L->book_list[j];
						L->book_list[j] = L->book_list[j + 1];
						L->book_list[j + 1] = temp;
					}
				}
			}
			break;
		}
		else
		{
			cout << "请输入正确选项!" << endl;
		}
	}
	cout << "排序成功!" << endl;
}

void Insert_List(Book_List* L, Books B)
{
	while (1)
	{
		while (1)
		{
			int temp = 0;
			if (L->List_Size < L->size)
			{
				while (1)
				{
					cout << "请输入要插入的位置:" << endl;
					int location;
					cin >> location;
					if (location > 0 && location < L->size + 1)
					{
						cout << "请输入要插入的书籍信息(IBSN、书名、作者、价格):" << endl;
						cin >> B.IBSN >> B.Title >> B.Author >> B.Price;
						if (location > L->List_Size)
						{
							L->book_list[location - 1] = B;
							L->List_Size++;
							temp = 1;
							break;
						}
						else
						{
							for (int i = L->List_Size; i > location; i--)
							{
								L->book_list[i] = L->book_list[i - 1];
							}
							L->book_list[location - 1] = B;
							L->List_Size++;
							temp = 1;
							break;
						}
					}
					else
					{
						cout << "请输入正确的插入位置" << endl;
					}
				}
			}
			if (temp == 1)
			{
				break;
			}
			while (1)
			{
				cout << "该图书系统书库已满,是否增大书库库存?"
					<< "1.是"
					<< "2.否"
					<< "请输入你的选择:";
				int choice;
				cin >> choice;
				if (choice == 1)
				{
					int temp = L->size + 1;
					Books* New_list = new Books[temp];
					for (int i = 0; i < L->size; i++)
					{
						New_list[i] = L->book_list[i];
					}
					delete[] L->book_list;
					L->book_list = New_list;
					L->size = temp;
					cout << "扩容成功!" << endl;
					break;
				}
				else if (choice == 2)
				{
					return;
				}
				else
				{
					cout << "请输入正确的选项!" << endl;
				}
			}
		}
		cout << "插入成功!" << endl;
		while (1)
		{
			cout << "是否继续插入?"
				<< "1.是"
				<< "2.否"
				<< "请输入你的选项:" << endl;
			int choice_continue;
			cin >> choice_continue;
			if (choice_continue == 1)
			{
				break;
			}
			else if (choice_continue == 2)
			{
				return;
			}
			else
			{
				cout << "请输入正确选项!" << endl;
			}
		}
	}
}

void Print_List(Book_List* L)
{
	for (int i = 0; i < L->size; i++)
	{
		cout << L->book_list[i].IBSN << "\t" << L->book_list[i].Title << "\t" << L->book_list[i].Author << "\t" << L->book_list[i].Price << endl;
	}
}

void Menu()
{
	cout << "\t图书管理系统" << endl
		<< "========================"<<endl
		<< "\t1.增添书籍"<<endl
		<< "\t2.删除书籍" << endl
		<< "\t3.修改书籍" << endl
		<< "\t4.查找书籍" << endl
		<< "\t5.插入书籍" << endl
		<< "\t6.排序" << endl
		<< "\t7.浏览" << endl
		<< "\t0.退出" << endl
		<< "请输入你的选项:" << endl;
}

int main()
{
//实例化两个对象
	Book_List* L = new Book_List();
	Books *B = new Books();
	Init_List(L);
	while (1)
	{
		Menu();
		int choice;
		cin >> choice;
		switch (choice)
		{
		case 0:
            //清空空间并结束程序
			delete[] L->book_list;
			delete L;
			delete B;
			return 0;
		case 1:
			Add_List(L, *B);
			system("pause");
			system("cls");
			break;
		case 2:
			Delete_List(L, *B);
			system("pause");
			system("cls");
			break;
		case 3:
			Change_List(L, *B);
			system("pause");
			system("cls");
			break;
		case 4:
			Find_List(L, *B);
			system("pause");
			system("cls");
			break;
		case 5:
			Insert_List(L, *B);
			system("pause");
			system("cls");
			break;
		case 6:
			Sort_List(L);
			system("pause");
			system("cls");
			break;
		case 7:
			Print_List(L);
			system("pause");
			system("cls");
			break;
		default:
			cout << "请输入正确的选项!" << endl;
			system("pause");
			system("cls");
			break;
		}
	}
}

 

 

  • 13
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值