目录
1 图书信息管理系统的设计与实现
实验内容:
设计并实现一个图书信息管理系统。根据实验要求设计该系统的菜单和交互逻辑,并编码实现增删改查的各项功能。 该系统至少包含以下功能:
- 根据指定图书个数,逐个输入图书信息;
- 逐个显示图书表中所有图书的相关信息;
- 能根据指定的待入库的新图书的位置和信息,将新图书插入到图书表中指定的位置;
- 根据指定的待出库的旧图书的位置,将该图书从图书表中删除;
- 能统计表中图书个数;
- 实现图书信息表的图书去重;
- 实现最爱书籍查询,根据书名进行折半查找,要求使用非递归算法实现,成功返回此书籍的书号和价格;
- 图书信息表按指定条件进行批量修改;
- 利用快速排序按照图书价格降序排序;
- 实现最贵图书的查找;
实现流程:
源代码:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int MaxSize = 100;
struct Book
{
char no[10]; //8位书号
char name[40]; //书名
double price; //价格
};
class BookSeqList
{
public:
BookSeqList() { length = 0; } //无参构造函数
~BookSeqList() { } //析构函数为空
void InputBook();
int GetLength()
{
return length;
} //求顺序表的长度
Book Get(int i); //取顺序表的第i个元素
int Locate(Book x); //顺序表元素定位
void InsertBook(); //在顺序表中第i个位置插入值为x的元素
void DeleteBook(int pos); //删除顺序表的第i个元素
void ModBook(); //修改价格
void PrintBook(); //输出图书表
void ExpensiveBook(); //最贵书籍查找
void FavouriteBook(); //最爱书籍查找
void DeleteRepetitiveBooks();//图书去重
void QucikSortBooks(int low, int high);//用快速排序法按照书籍价格对书籍排序
private:
Book data[MaxSize];
int length; //顺序表的长度
};
void BookSeqList::InputBook()//无参构造函数初始化图书列表,创建图书信息表
{
char ans;
int n = 1;
while (true)
{
cout << "请输入第" << n << "本书的书号:";
cin >> data[n - 1].no;
cout << "请输入第" << n << "本书的书名:";
cin >> data[n - 1].name;
cout << "请输入第" << n << "本书的价格:";
cin >> data[n - 1].price;
n++;
cout << "是否继续录入?(y/n)" << endl;
cin >> ans;
if (ans != 'y' && ans != 'Y')
break;
}
length = n - 1;
}
void BookSeqList::PrintBook()
{
if (length > 0)
{
cout << "书号" << '\t' << "书名" << '\t' << '\t' << "价格" << endl;
cout << "-------------------------------------------------------" << endl;
for (int i = 0; i < length; i++)
cout << data[i].no << '\t' << data[i].name << '\t' << fixed << setprecision(2) << data[i].price << endl;
cout << "-------------------------------------------------------" << endl;
}
else
cout << "无任何信息" << endl;
cout << "0 0 0" << endl;
}
void BookSeqList::ModBook()//修改学生成绩
{
double sum = 0;
double average;
for (int i = 0; i < length; i++)
sum += data[i].price;
average = sum / length;
for (int i = 0; i < length; i++)
{
if (data[i].price < average)
data[i].price = data[i].price * 1.2;
else
data[i].price = data[i].price * 1.1;
}
PrintBook();
}
void BookSeqList::ExpensiveBook()
{
int count = 0;
double max = data[0].price;
for (int i = 0; i < length; i++)
if (max < data[i].price)
max = data[i].price;
for (int i = 0; i < length; i++)
if (data[i].price == max)
count++;
cout << "最贵的图书数目:" << count << endl;
cout << "书号" << '\t' << "书名" << '\t' << "价格" << endl;
cout << "-------------------------------------------" << endl;
for (int i = 0; i < length; i++)
if (data[i].price == max)
cout << data[i].no << '\t' << data[i].name << '\t' << fixed << setprecision(2) << data[i].price << endl;
cout << "-------------------------------------------------------" << endl;
}
void BookSeqList::FavouriteBook()
{
int count = 0;
Book Favourite;
cout << "请输入最爱图书的书名:";
cin >> Favourite.name;
for (int i = 0; i < length; i++)
{
if (strcmp(data[i].name, Favourite.name) == 0)
count++;
else
continue;
}
if(count==0)
{
cout << "查无此书!" << endl;
return;
}
cout << "最爱的图书数目:" << count << endl;
cout << "书号" << '\t' << "书名" << '\t' << "价格" << endl;
cout << "-------------------------------------------" << endl;
for (int i = 0; i < length; i++)
if (strcmp(data[i].name, Favourite.name) == 0)
cout << data[i].no << '\t' << data[i].name << '\t' << fixed << setprecision(2) << data[i].price << endl;
cout << "-------------------------------------------------------" << endl;
}
Book BookSeqList::Get(int i)//得元素
{
if (i<1 || i>length)
throw "Wrong";
return data[i - 1];
}
int BookSeqList::Locate(Book x)//元素定位
{
for (int i = 0; i < length; i++)
{
if (strcmp(data[i].no, x.no) == 0)
return i + 1;
}
return 0;
}
void BookSeqList::InsertBook()//新书入库
{
char ans;
int pos;
Book x;
bool t = true;
while (t)
{
cout << "请输入新书入库位置:";
cin >> pos;
if (pos < 1 || pos>MaxSize)
{
cout << "位置错误,请重新插入!";
continue;
}
cout << "请输入新书书号:";
cin >> x.no;
cout << "请输入新书书名:";
cin >> x.name;
cout << "请输入新书价格:";
cin >> x.price;
for (int j = length; j >= pos; j--)
data[j] = data[j - 1];
data[pos - 1] = x;
length++;
cout << "是否继续录入?(y/n)" << endl;
cin >> ans;
if (ans != 'y' && ans != 'Y')//输入y或Y表示继续插入,否则结束
break;
}
PrintBook();
}
void BookSeqList::DeleteBook(int pos)//旧书出库
{
for (int j = pos; j <= length - 1; j&