图书管理系统(简单版)面向对象程序设计c++

大一期末作业,第一个博客,多多指教

图书信息的组成部分为:书号、书名、作者名、出版社、出版时间、价格

1) 新书上架:添加图书信息

2) 图书浏览:输出所有图书信息

3) 图书查询:可按书号、书名或作者名查询

4)图书下架:删除指定图书信息,如按书号

一、各部分构成(后面有完整代码)

#include<iostream>
#include<cstring>
#include<stdlib.h>
#include <fstream>
using namespace std;
#define max 1000

书类

class book{
private:
	string num;//书号 
    string name;//书名
    string author;//作者名 
    string publisher;//出版社
	string time;//出版时间 
    double price;//图书价格
public:
    book() {}; //构造函数 
    void fuzhibook(string num1, string name1,string author1,string publisher1,string time1,double price1);//给整体赋值 
    void Num(string num2);// 给书号 赋值 
    void Name(string name2);//给书名赋值
    void Author(string author2);//给作者名赋值
    void Publisher(string publisher2);//给出版社赋值
    void Time(string time2);//给出版时间赋值
    void Price(double price2);//给图书价格赋值
    string getNum();//获取书号  
    string getName();//获取书名
    string getAuthor();//获取作者名 
    string getPublisher();//获取出版社
    string getTime();//获取出版时间 
    double getPrice();//获取图书价格
    bool isName(string name3);//判断书名 
    bool isNum(string num3);//判断书号
	bool isAuthor(string author3);//判断作者名  
	void display();//
};

操作类

class library{
private:
	book arr[max];//存书的数组
	int count;//书的数量
public:
    void add();//添加
	void show();//显示
	void deletebook();//删除
	void findbynum();//按书号查找
	void findbyname();//按书名查找
	void findbyauthor();//按作者名查找
	//void modifyPerson();//修改联系人
	void infile();//读文件
	void outfile();//写文件
};

添加操作

void library::add()
{
	if (count>=max)
	{
		cout << "书籍已满,无法添加" << endl;
		return;
	}
	else
	{
		string num4,name4,author4,publisher4,time4;
		double price4;
		cout << "书号\t书名\t作者名\t出版社\t出版时间\t价格" << endl;
		cin >>num4>>name4>>author4>>publisher4>>time4>>price4;
		arr[count].fuzhibook(num4,name4,author4,publisher4,time4,price4);
		count++;
		system("pause");
	}
}

浏览

void library::add()
{
	if (count>=max)
	{
		cout << "书籍已满,无法添加" << endl;
		return;
	}
	else
	{
		string num4,name4,author4,publisher4,time4;
		double price4;
		cout << "书号\t书名\t作者名\t出版社\t出版时间\t价格" << endl;
		cin >>num4>>name4>>author4>>publisher4>>time4>>price4;
		arr[count].fuzhibook(num4,name4,author4,publisher4,time4,price4);
		count++;
		system("pause");
	}
}

删除

void library::deletebook()
{
	cout << "请输入您要删除图书的书号" << endl;
	string num5;
	cin >> num5;
	int j = 0;//未知
	for (int i = 0; i < count; i++)
	{
		if (arr[i].isNum(num5))
			break;
		else j++;	
	}
	if (j== count)
	{
		cout << "没有您要删除的书" << endl;
		system("pause");
		return;
	}
	for (int i =j ; i < count; i++)
	{
		arr[i] =arr[i + 1];
	}
    count--;
	system("pause");
}

查询

void library::findbynum()
{
	cout << "请输入您要查找的书号" << endl;
	string num6;
	cin >> num6;

	for (int i = 0;i < count;i++)
	{
		if (arr[i].isNum(num6))
		{
			arr[i].display();
			system("pause");
			return;
		}
		else continue;
	}
	cout << "没有您要查找的书" << endl;
	system("pause");
}
void library::findbyname()
{
	cout << "请输入您要查找的书名" << endl;
	string name6;
	cin >> name6;

	for (int i = 0;i < count;i++)
	{
		if (arr[i].isName(name6))
		{
			arr[i].display();
			system("pause");
			return;
		}
		else continue;
	}
	cout << "没有您要查找的书" << endl;
	system("pause");
}
void library::findbyauthor()
{
	cout << "请输入您要查找的作者名" << endl;
	string author6;
	cin >> author6;

	for (int i = 0;i < count;i++)
	{
		if (arr[i].isAuthor(author6))
		{
			arr[i].display();
			system("pause");
			return;
		}
		else continue;
	}
	cout << "没有您要查找的书" << endl;
	system("pause");
}

文件

void library::infile()//读文件 
{
	fstream infile;
	infile.open("book.txt", ios::in);
	string s;
	while (infile >> s)
		cout << s << endl; 
	infile.close();
}
void library::outfile()//写文件 
{
	fstream outfile;
	outfile.open("book.txt", ios::out | ios::app);
	for (int i = 0; i < count; i++)
	{
		outfile << arr[i].getNum()<<endl;
		outfile << arr[i].getName() << endl;
		outfile << arr[i].getAuthor() << endl;
		outfile << arr[i].getPublisher()<<endl;
		outfile << arr[i].getTime() << endl;
		outfile << arr[i].getPrice() << endl;
	}
	cout<<"成功保存到文件"<<endl; 
	outfile.close();
}

菜单

void menu()
{
    cout << "\t\t\t***************************************************************" << endl;
    cout << "\t\t\t*******************欢迎光临图书管理系统************************" << endl;
    cout << "\t\t\t****************    1.输入1   新书上架      *******************" << endl;
    cout << "\t\t\t****************    2.输入2   图书浏览      *******************" << endl;
    cout << "\t\t\t****************    3.输入3   图书下架    *******************" << endl;
    cout << "\t\t\t****************    4.输入4   按书名查询    *******************" << endl;
    cout << "\t\t\t****************    5.输入5   按作者名查询  *******************" << endl;
    cout << "\t\t\t****************    6.输入6   按书号查询     *******************" << endl;
    cout << "\t\t\t****************    6.输入7   按书号查询     *******************" << endl;
    cout << "\t\t\t****************    7.输入0   退出程序      *******************" << endl;
    cout << "\t\t\t***************************************************************" << endl;
}

主函数

int main()
{
	int x= 0;
	library* librarys = new library;
	librarys->infile();
	int count = 0;
	menu();
	while (true)
	{
		cin >>x;

		switch (x)
		{
		case 1: //添加
			librarys->add();
			break;
		case 2: //显示
			librarys->show();
			break;
		case 3: //删除
			librarys->deletebook();
			break;
		case 4: //书名查找
			librarys->findbyname();
			break;
		case 5://作者名查找
			librarys->findbyauthor();
			break;
		case 6: //书号查找
			librarys->findbynum();
			break;
		case 7: //将数据保存到文件
			librarys->outfile();
			break;
		case 0: //退出程序
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	system("pause");
	return 0;
}

二、全部代码

#include<iostream>
#include<cstring>
#include<stdlib.h>
#include <fstream>
using namespace std;
#define max 1000
class book{
private:
	string num;//书号 
    string name;//书名
    string author;//作者名 
    string publisher;//出版社
	string time;//出版时间 
    double price;//图书价格
public:
    book() {}; //构造函数 
    void fuzhibook(string num1, string name1,string author1,string publisher1,string time1,double price1);//给整体赋值 
    void Num(string num2);// 给书号 赋值 
    void Name(string name2);//给书名赋值
    void Author(string author2);//给作者名赋值
    void Publisher(string publisher2);//给出版社赋值
    void Time(string time2);//给出版时间赋值
    void Price(double price2);//给图书价格赋值
    string getNum();//获取书号  
    string getName();//获取书名
    string getAuthor();//获取作者名 
    string getPublisher();//获取出版社
    string getTime();//获取出版时间 
    double getPrice();//获取图书价格
    bool isName(string name3);//判断书名 
    bool isNum(string num3);//判断书号
	bool isAuthor(string author3);//判断作者名  
	void display();//
};
void book::fuzhibook(string num1, string name1,string author1,string publisher1,string time1,double price1)
{
	num=num1;
	name=name1;
	author=author1;
	publisher=publisher1;
	time=time1;
	price=price1;
}
void book::Num(string num2)
{
	num=num2;
}
void book::Name(string name2)
{
	name=name2;
}
void book::Author(string author2)
{
	author=author2;
}
void book::Publisher(string publisher2)
{
	publisher=publisher2;
}
void book::Time(string time2)
{
	time=time2;
}
void book::Price(double price2)
{
	price=price2;
}
string book::getNum()
{
	return num;
}
string book::getName()
{
	return name;
}
string book::getAuthor()
{
	return author;
}
string book::getPublisher()
{
	return publisher;
}
string book::getTime()
{
	return time;
}
double book::getPrice()
{
	return price;
}
bool book::isName(string name3)
{
	if (name == name3) return true;
		else return false;
}
bool book::isNum(string num3)
{
	if (num3 == num) return true;
		else return false;
}
bool book::isAuthor(string author3)
{
	if (author == author3) return true;
		else return false;
}
void book::display()	
{
	cout<<" 书号:"<<num<<"书名:"<<name<<"作者名:"<<author<<"出版社:"<<publisher<<"出版时间:"<<time<<"价格:"<<price;
}
class library{
private:
	book arr[max];//存书的数组
	int count;//书的数量
public:
    void add();//添加
	void show();//显示
	void deletebook();//删除
	void findbynum();//按书号查找
	void findbyname();//按书名查找
	void findbyauthor();//按作者名查找
	//void modifyPerson();//修改联系人
	void infile();//读文件
	void outfile();//写文件
};

void library::add()
{
	if (count>=max)
	{
		cout << "书籍已满,无法添加" << endl;
		return;
	}
	else
	{
		string num4,name4,author4,publisher4,time4;
		double price4;
		cout << "书号\t书名\t作者名\t出版社\t出版时间\t价格" << endl;
		cin >>num4>>name4>>author4>>publisher4>>time4>>price4;
		arr[count].fuzhibook(num4,name4,author4,publisher4,time4,price4);
		count++;
		system("pause");
	}
}
void library::show()
{
	for (int i = 0;i < count;i++)
	{
		arr[i].display();
	}
	system("pause");
}
void library::deletebook()
{
	cout << "请输入您要删除图书的书号" << endl;
	string num5;
	cin >> num5;
	int j = 0;//未知
	for (int i = 0; i < count; i++)
	{
		if (arr[i].isNum(num5))
			break;
		else j++;	
	}
	if (j== count)
	{
		cout << "没有您要删除的书" << endl;
		system("pause");
		return;
	}
	for (int i =j ; i < count; i++)
	{
		arr[i] =arr[i + 1];
	}
    count--;
	system("pause");
}
void library::findbynum()
{
	cout << "请输入您要查找的书号" << endl;
	string num6;
	cin >> num6;

	for (int i = 0;i < count;i++)
	{
		if (arr[i].isNum(num6))
		{
			arr[i].display();
			system("pause");
			return;
		}
		else continue;
	}
	cout << "没有您要查找的书" << endl;
	system("pause");
}
void library::findbyname()
{
	cout << "请输入您要查找的书名" << endl;
	string name6;
	cin >> name6;

	for (int i = 0;i < count;i++)
	{
		if (arr[i].isName(name6))
		{
			arr[i].display();
			system("pause");
			return;
		}
		else continue;
	}
	cout << "没有您要查找的书" << endl;
	system("pause");
}
void library::findbyauthor()
{
	cout << "请输入您要查找的作者名" << endl;
	string author6;
	cin >> author6;

	for (int i = 0;i < count;i++)
	{
		if (arr[i].isAuthor(author6))
		{
			arr[i].display();
			system("pause");
			return;
		}
		else continue;
	}
	cout << "没有您要查找的书" << endl;
	system("pause");
}
void library::infile()//读文件 
{
	fstream infile;
	infile.open("book.txt", ios::in);
	string s;
	while (infile >> s)
		cout << s << endl; 
	infile.close();
}
void library::outfile()//写文件 
{
	fstream outfile;
	outfile.open("book.txt", ios::out | ios::app);
	for (int i = 0; i < count; i++)
	{
		outfile << arr[i].getNum()<<endl;
		outfile << arr[i].getName() << endl;
		outfile << arr[i].getAuthor() << endl;
		outfile << arr[i].getPublisher()<<endl;
		outfile << arr[i].getTime() << endl;
		outfile << arr[i].getPrice() << endl;
	}
	cout<<"成功保存到文件"<<endl; 
	outfile.close();
}
void menu()
{
    cout << "\t\t\t***************************************************************" << endl;
    cout << "\t\t\t*******************欢迎光临图书管理系统************************" << endl;
    cout << "\t\t\t****************    1.输入1   新书上架      *******************" << endl;
    cout << "\t\t\t****************    2.输入2   图书浏览      *******************" << endl;
    cout << "\t\t\t****************    3.输入3   图书下架    *******************" << endl;
    cout << "\t\t\t****************    4.输入4   按书名查询    *******************" << endl;
    cout << "\t\t\t****************    5.输入5   按作者名查询  *******************" << endl;
    cout << "\t\t\t****************    6.输入6   按书号查询     *******************" << endl;
    cout << "\t\t\t****************    6.输入7   按书号查询     *******************" << endl;
    cout << "\t\t\t****************    7.输入0   退出程序      *******************" << endl;
    cout << "\t\t\t***************************************************************" << endl;
}
int main()
{
	int x= 0;
	library* librarys = new library;
	librarys->infile();
	int count = 0;
	menu();
	while (true)
	{
		cin >>x;

		switch (x)
		{
		case 1: //添加
			librarys->add();
			break;
		case 2: //显示
			librarys->show();
			break;
		case 3: //删除
			librarys->deletebook();
			break;
		case 4: //书名查找
			librarys->findbyname();
			break;
		case 5://作者名查找
			librarys->findbyauthor();
			break;
		case 6: //书号查找
			librarys->findbynum();
			break;
		case 7: //将数据保存到文件
			librarys->outfile();
			break;
		case 0: //退出程序
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	system("pause");
	return 0;
}

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值