#include "ds.h"//自定义的头文件
using namespace std;//c++中指定命名空间
#include<string>; //ds.h中的string.h属于c语言里关于字符数组的函数定义的头文件,常用函数有strcmp(比较两个字符串并根据结果返回整数),strlen(求字符串长度 包含多少字符),strcpy(复制字符串)等
//string是c++字符串标准库类,定义在string头文件中,里面包含了对字符串的各种常用操作,对字符串操作更加方便快捷,可动态拓展
struct Book //定义一种单本图书结构体类型
{
string ISBN; //书号 string 字符串类型 char 字符类型
string name; //书名
string author; //作者
string publisher; //出版单位
float price;//价格
int year;//图书的年【若为1988年出版 请输入1988】
int month;//图书的月【输入方法同上】
int day; //图书的日【输入方法同上】 //中间请用空格空开
};
Book book[MAXSIZE];//图书的结构体数组
/*9787521334111 新未来大学英语综合教程 孙有中 外语教学与研究出版社 74.9 2022 7 1
9787516519516 军事理论 宋来新 航空工业出版社 35.00 2020 8 2
9787115576668 数据结构 严蔚敏 人民邮电出版社 49.80 2022 1 2
9787521326963 视听说 孙有中 外语教学与研究出版社 62.9 2022 7 1*/
void menu() //菜单函数 更好的用户体验
{
cout << "*******************************************************************************************" << endl;
cout << "*******************************欢迎进入图书管理系统****************************************" << endl;
cout << "*******************************1.图书信息输入**********************************************" << endl;
cout << "*******************************2.图书信息浏览**********************************************" << endl;
cout << "*******************************3.图书信息添加**********************************************" << endl;
cout << "*******************************4.图书信息删除(按书号删除、按书名删除)*********************" << endl;
cout << "*******************************5.图书信息修改(修改除书名号外的图书信息等)****************" << endl;
cout << "*******************************6.图书信息查询(按书名查询、按书号查询)********************" << endl;
cout << "*******************************7.统计图书的平均价格****************************************" << endl;
cout << "*******************************8.图书排序(按价格排序、按出版日期排序)********************" << endl;
cout << "*******************************9.图书信息保存到文件****************************************" << endl;
cout << "*******************************************************************************************" << endl;
}
void input(int &num) //输入图书基本信息
{
int i;
cout << "输入图书的本数num=";
cin >> num;
cout << "请输入" << num << "本图书的信息,空格分开\n";
for(i=0; i<num; i++)
{
cin >> book[i].ISBN >> book[i].name >> book[i].author >> book[i].publisher >> book[i].price >> book[i].year >> book[i].month >> book[i].day;
}
}
void output(int num) //输出图书基本信息
{
int i;
for (i=0; i<num; i++)
{
cout << "书名:" << book[i].name << " 作者:" << book[i].author << " 出版单位:" << book[i].publisher << " 价格:" << book[i].price << " 出版日期:" << book[i].year << " " << book[i].month << " " << book[i].day << " "<< endl;
}
}
void insertm(int &num) //在最后面依次添加num本图书
{
int i,m;
cout << "需要添加图书信息的本数为:";
cin >> m;
cout << "请输入其信息【依次为书号(ISBN) 书名 作者 出版单位 价格 出版日期(书号不重复,用空格空开】\n ";
for (i=0; i<m; i++)
{
cin >> book[i].ISBN >> book[i].name >> book[i].author >> book[i].publisher >> book[i].price >> book[i].year >> book[i].month >> book[i].day;
}
num = num + m;
cout << "**************************图书信息添加后为:**************************\n";
output(num);
}
void delete_books(int num) //删除图书
{
int i,choice,t=0;
string ISBN1;
string name1;
cout << "**************************进入删除图书系统****************************\n ";
cout << "**************************按书号删除请按1*****************************\n";
cout << "**************************按书名删除请按2*****************************\n";
cin >> choice;
switch(choice) //按书号删除和按书名删除
{
case 1:
cout << "请输入你想删除的图书的书号: ";
cin >> ISBN1;
for(i=0; i<num; i++)
{
if(ISBN1 == book[i].ISBN)
{
t = i; //记住要删除图书的下标
break;
}
}
if(t != num - 1) //判断要删除图书是否为最后一本 即判断t是否为最后一个值
{
for(i=t; i<num; i++)
{
book[i] = book[i+1];
break;
}
}
else
cout << "**************************删除失败,请确认书号是否正确!***************************" << endl;
num = num-1;
output(num);
break;
case 2:
cout << "请输入你想删除的图书的书名: ";
cin >> name1;
for(i=0; i<num; i++)
{
if(name1 == book[i].name)
{
t = i; //记住要删除图书的下标
break;
}
else
cout << "**************************删除失败,请确认书名是否正确!**************************" << endl;
}
if(t != num-1) //判断要删除图书是否为最后一本 即判断t是否为最后一个值
{
for(i=t; i<num; i++)
{
book[i] = book[i+1];
}
}
num = num-1;
output(num);
break;
}
}
void change_books(int num)
{
string ISBN2;
int i,t=-1,choice;
cout << "请输入要修改信息图书的书号:" << endl;
cin >> ISBN2;
for(i=0; i<num; i++)
{
if(ISBN2 == book[i].ISBN)
{
t = i;
break;
}
else
t = -1;
}
if(t != -1)
cout << "**************************已找到该本图书******************************************" << endl;
else
cout << "**************************无图书信息!请确认书号是否正确!******************************************" << endl;
cout << "请输入需要修改信息的序号:" << endl;
cout << "**************************1.书名*********************************************" << endl;
cout << "**************************2.作者*********************************************" << endl;
cout << "**************************3.出版单位*****************************************" << endl;
cout << "**************************4.价格*********************************************" << endl;
cout << "**************************5.出版日期*****************************************" << endl;
cin >> choice;
switch(choice)
{
case 1:
cout << "请输入修改后的书名:";
cin >> book[t].name;
break;
case 2:
cout << "请输入修改后的作者:";
cin >> book[t].author;
break;
case 3:
cout << "请输入修改后的出版单位:";
cin >> book[t].publisher;
break;
case 4:
cout << "请输入修改后的价格:";
cin >> book[t].price;
break;
case 5:
cout << "请输入修改后的出版日期:";
cin >> book[t].year >> book[t].month >> book[t].day;
break;
default:
cout << "***************************输入错误!请重新输入1-4*************************" << endl;
}
output(num);
}
void book_search(int num)
{
int i,choice,t=-1;
string ISBN3;
string name3;
cout << "**************************进入查询图书系统****************************\n";
cout << "**************************按书号查询请按1*****************************\n";
cout << "**************************按书名查询请按2*****************************\n";
cin >> choice;
switch(choice)
{
case 1:
cout << "请输入你想查询的图书的书号:";
cin >> ISBN3;
for(i=0; i<num; i++)
{
if(ISBN3 == book[i].ISBN)
{
t = i;
break;
}
}
if(t != -1)
{
cout << "*************************查询成功*************************\n";
cout << "该书基本信息为:\n";
cout << "书名:" << book[i].name << " 作者;" << book[i].author << " 出版单位:" << book[i].publisher << " 价格:" << book[i].price << " 出版日期:" << book[i].year << " " << book[i].month << " " << book[i].day << " "<< endl;
}
else
cout << "查询失败!请确认书号是否正确!";
break;
case 2:
cout << "请输入你想查询的图书的书名:";
cin >> name3;
for(i=0; i<num; i++)
{
if(name3 == book[i].name)
{
t = i;
break;
}
}
if(t != -1)
{
cout << "*************************查询成功*************************\n";
cout << "该书基本信息为:\n";
cout << "书号:" << book[i].ISBN << " 作者;" << book[i].author << " 出版单位:" << book[i].publisher << " 价格:" << book[i].price << " 出版日期:" << book[i].year << " " << book[i].month << " " << book[i].day << " "<< endl;
}
else
cout << "查询失败!请确认书名是否正确!";
break;
}
}
int main()
{
int num=0;
int choice;
menu();
input(num);
cout << "进行图书信息浏览: " << endl;
output(num);
cout<<"请输入需要进入的系统3-9:";
cin>>choice;
switch(choice)
{
case 3:
insertm(num);
break;
case 4:
delete_books(num);
break;
case 5:
change_books(num);
break;
case 6:
book_search(num);
break;
}
system("pause");
}