图书信息管理系统的设计与实现
一、实验内容
设计并实现一个图书信息管理系统。根据实验要求设计该系统的菜单和交互逻辑,并编码实现增删改查的各项功能。 该系统至少包含以下功能:
(1) 根据指定图书个数,逐个输入图书信息;
(2) 逐个显示图书表中所有图书的相关信息;
(3) 能根据指定的待入库的新图书的位置和信息,将新图书插入到图书表中指定的位置;
(4) 根据指定的待出库的旧图书的位置,将该图书从图书表中删除;
(5) 能统计表中图书个数;
(6) 实现图书信息表的图书去重;
(7) 实现最爱书籍查询,根据书名进行折半查找,要求使用非递归算法实现,成功返回此书籍的书号和价格;
(8) 图书信息表按指定条件进行批量修改;
(9) 利用快速排序按照图书价格降序排序;
(10) 实现最贵图书的查找;
二、实验设计过程
本实验的图书信息采用结构体定义书号、书名、图书价格,设计如下:
typedef struct
{
char booknum[13]; /*13位书号*/
char bookname[20]; /*书名*/
float bookprice; /*图书价格*/
}Book;
采用顺序表来存储图书信息,设计如下:
typedef struct
{
Book *elem; /*指向数据元素的基地址*/
int length; /*表长*/
}Seqlist;
模块设计
1.图书信息表的创建和输出:
定义一个包含图书信息(书号、书名、价格)的顺序表。读入相应的图书数据来完成图书信息表的创建,然后统计图书表中的图书个数,同时逐行输出每本图书的信息。
/*顺序表初始化*/
void InitList(Seqlist &l)
{
l.elem = new Book[MaxSize]; /*分配空间*/
l.length = 0;
}
/*输入图书信息*/
void Input(Seqlist &l)
{
cout << "请输入图书个数:" << endl;
int n;
cin >> n;
for (int i = l.length; i<l.length+n; i++)
{
cout << "请输入第" << i + 1 << "本图书的信息" << endl;
cout << "书号 书名 价格" << endl;
cin >> l.elem[i].booknum >> l.elem[i].bookname >> l.elem[i].bookprice;
}
l.length = l.length + n;
}
/*输出图书信息*/
void Output(Seqlist &l)
{
for (int i = 0; i < l.length-1; i++)
{
cout << l.elem[i].booknum << " " << l.elem[i].bookname << " " << setiosflags(ios::fixed) << setprecision(2) << l.elem[i].bookprice << " " << endl;
}
cout<<l.elem[l.length-1].booknum<<" "<<l.elem[l.length-1].bookname<<" "<< setiosflags(ios::fixed) << setprecision(2) << l.elem[l.length-1].bookprice << " " << endl;
return;
}
2.图书信息表的修改
读入图书信息表,然后计算所有图书的平均价格,将所有低于平均价格的图书价格提高20%,所有高于或等于平均价格的图书价格提高10%,最后逐行输出价格修改后的图书信息。
/*计算所有图书平均价格*/
float Averageprice(Seqlist &l)
{
float average, sum;
int i ;
for (i = 0; i < l.length; i++)
{
sum += l.elem[i].bookprice;
}
average = sum / (GetLength(l));
return average;
}
/*批量改变*/
void Change(Seqlist &l)
{
int i;
for (i = 0; i < l.length; i++)
{
if (l.elem[i].bookprice < Averageprice(l))
{
l.elem[i].bookprice = 1.2*(l.elem[i].bookprice);
}
else
{
l.elem[i].bookprice = 1.1*(l.elem[i].bookprice);
}
}
Output(l);
}
3.用快速排序按照图书价格降序排序;
对图书信息表中图书价格做快速排序降序处理
/*对图书价格进行排序*/
void sort(Seqlist &l,int left,int right)
{
if (left >= right)
{
return;
}
if (left < 0 || right >=l.length)
{
cout << "error args! array bound." << endl;
return;
}//非法输入判断,防止数组越界
int i, j;
float base;
ElemType temp;
i = left;
j = right;
base = l.elem[left].bookprice;
while (i < j)
{
while (l.elem[j].bookprice <= base && i < j)
{
j--;
}
while (l.elem[i].bookprice >= base && i < j)
{
i++;
}
if (i < j)
{
temp = l.elem[i];
l.elem[i] = l.elem[j];
l.elem[j] = temp;
}
}
l.elem[left].bookprice = l.elem[i].bookprice;
l.elem[i].bookprice = base;
sort(l, left, i - 1);
sort(l, i + 1, right);
}
4.图书信息表的最爱图书的查找
读入相应的图书信息表,然后根据指定的最爱图书的名字,输出相应图书的信息。
/*最爱图书查找*/
void FavouriteBook(Seqlist &l)
{
char *name[Favourite];
int i;
cout << "输入最爱图书名称" << endl;
for (i = 0; i < Favourite; i++)
{
cin >> name[i];
}
int loc[MaxSize]; /*用loc数组配合计数器k从0存储多个目标的下标 */
int j=0,k = 0;
while (j < l.length)
{
for (int m = 0; m < Favourite; m++)
{
if (name[m]==l.elem[j].bookname)
{
loc[k] = i;
k++;
}
}
if (k == 0)
{
cout << "Sorry,there is no your favourite!" << endl;
}
else
{
cout << k << endl;
for (i = 0; i < k; i++)
{
cout << l.elem[loc[i]].booknum << " " << l.elem[loc[i]].bookname << " " << setiosflags(ios::fixed) << setprecision(2) << l.elem[loc[i]].bookprice << endl;
}
}
k = 0;
}
}
5.图书信息表的最贵图书查找
读入相应的图书信息表,然后查找价格最高的图书,输出相应图书的信息。
/*最贵图书查找*/
void OutputExpensiveBook(Seqlist &l)
{
sort(l, 0, l.length - 1);
float standard = l.elem[0].bookprice;
int i;
cout << l.elem[0].booknum << " " << l.elem[0].bookname << " "<< setiosflags(ios::fixed) << setprecision(2) << l.elem[0].bookprice<<" " << endl;
for (i = 1; i < l.length; i++)
{
if (l.elem[i].bookprice == standard)
{
cout << l.elem[i].booknum << " " << l.elem[i].bookname << " " << setiosflags(ios::fixed) << setprecision(2) << l.elem[i].bookprice << " " << endl;
}
}
}
6.图书去重
/*判断是否为同一本书*/
bool CompareElem(Book a, Book b)
{
if (strcmp(a.bookname, b.bookname) == 0 && strcmp(a.booknum, b.booknum) == 0 && a.bookprice == b.bookprice)
{
return true;
}
return false;
}
/*图书去重*/
void Remove(Seqlist &l)
{
for (int i = 0; i < l.length; i++)
{
for (int j = i + 1; j < l.length; j++)
{
if (CompareElem(l.elem[j], l.elem[i]) == 0) /*发现同一本书*/
{
for (int k = j + 1; k < l.length; k++)
{
l.elem[k - 1] = l.elem[k];
}
l.length--;
j--;
}
}
}
}
7.图书个数
/*图书个数*/
int GetLength(Seqlist &l)
{
int i = 0, count=0;
while (i < l.length)
{
count++;
}
return count;
}
8.插入和删除图书信息
/*插入图书*/
void ListInsert(Seqlist &l, int i, ElemType e)
{
if (i<1 || i>l.length + 1)
{
exit(overflow);
}
if (l.length == MaxSize)
{
return;
}
for (int j = l.length - 1; j >= i + 1; j--)
{
l.elem[j + 1] = l.elem[j];
}
l.elem[i - 1] = e;
++l.length;
}
/*删除图书*/
void ListDelete(Seqlist& l, int i)
{
if (i<1 || i>l.length)
{
exit(overflow);
}
for (int j = i; j < l.length - 1; j++)
{
l.elem[j - 1] = l.elem[j];
}
--l.length;
}
9.取出指定位置的图书信息
/*取出指定位置的图书信息*/
void GetElem(Seqlist &l, int i,ElemType &e)
{
if (i<1 || i>l.length) {
exit(overflow);
}
e = l.elem[i - 1];
}
10.菜单函数
/*菜单函数*/
void menu()
{
cout << "该图书信息管理系统包含如下功能:\n";
cout << "(1)根据指定图书个数,逐个输入图书信息;\n";
cout << "(2)逐个输出图书表中所有图书的相关信息;\n";
cout << "(3)能根据指定的待入库的新图书的位置和信息,将新图书插入到图书表中指定的位置;\n";
cout << "(4)根据指定的待出库的旧图书的位置,将该图书从图书表中删除;\n";
cout << "(5)能统计表中图书个数;\n";
cout << "(6)图书信息表的图书去重;\n";
cout << "(7)最爱书籍查询;\n";
cout << "(8)图书信息表按指定条件进行批量修改;\n";
cout << "(9)利用快速排序按照图书价格降序排序;\n";
cout << "(10)实现最贵图书的查找;\n";
cout << "(11)退出系统"<<endl;
}
三、实验完整代码
Master database.h 头文件
#pragma once
#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
#define ElemType Book
#define overflow -2
#define MaxSize 100
#define Favourite 20
typedef struct
{
char booknum[13]; /*13位书号*/
char bookname[20]; /*书名*/
float bookprice; /*图书价格*/
}Book;
typedef struct
{
Book *elem; /*指向数据元素的基地址*/
int length; /*表长*/
}Seqlist;
/*顺序表初始化*/
void InitList(Seqlist &l)
{
l.elem = new Book[MaxSize]; /*分配空间*/
l.length = 0;
}
/*输入图书信息*/
void Input(Seqlist &l)
{
cout << "请输入图书个数:" << endl;
int n;
cin >> n;
for (int i = l.length; i<l.length+n; i++)
{
cout << "请输入第" << i + 1 << "本图书的信息" << endl;
cout << "书号 书名 价格" << endl;
cin >> l.elem[i].booknum >> l.elem[i].bookname >> l.elem[i].bookprice;
}
l.length = l.length + n;
}
/*输出图书信息*/
void Output(Seqlist &l)
{
for (int i = 0; i < l.length-1; i++)
{
cout << l.elem[i].booknum << " " << l.elem[i].bookname << " " << setiosflags(ios::fixed) << setprecision(2) << l.elem[i].bookprice << " " << endl;
}
cout<<l.elem[l.length-1].booknum<<" "<<l.elem[l.length-1].bookname<<" "<< setiosflags(ios::fixed) << setprecision(2) << l.elem[l.length-1].bookprice << " " << endl;
return;
}
/*对图书价格进行排序*/
void sort(Seqlist &l,int left,int right)
{
if (left >= right)
{
return;
}
if (left < 0 || right >=l.length)
{
cout << "error args! array bound." << endl;
return;
}//非法输入判断,防止数组越界
int i, j;
float base;
ElemType temp;
i = left;
j = right;
base = l.elem[left].bookprice;
while (i < j)
{
while (l.elem[j].bookprice <= base && i < j)
{
j--;
}
while (l.elem[i].bookprice >= base && i < j)
{
i++;
}
if (i < j)
{
temp = l.elem[i];
l.elem[i] = l.elem[j];
l.elem[j] = temp;
}
}
l.elem[left].bookprice = l.elem[i].bookprice;
l.elem[i].bookprice = base;
sort(l, left, i - 1);
sort(l, i + 1, right);
}
/*取出指定位置的图书信息*/
void GetElem(Seqlist &l, int i,ElemType &e)
{
if (i<1 || i>l.length) {
exit(overflow);
}
e = l.elem[i - 1];
}
/*插入图书*/
void ListInsert(Seqlist &l, int i, ElemType e)
{
if (i<1 || i>l.length + 1)
{
exit(overflow);
}
if (l.length == MaxSize)
{
return;
}
for (int j = l.length - 1; j >= i + 1; j--)
{
l.elem[j + 1] = l.elem[j];
}
l.elem[i - 1] = e;
++l.length;
}
/*删除图书*/
void ListDelete(Seqlist& l, int i)
{
if (i<1 || i>l.length)
{
exit(overflow);
}
for (int j = i; j < l.length - 1; j++)
{
l.elem[j - 1] = l.elem[j];
}
--l.length;
}
/*图书个数*/
int GetLength(Seqlist &l)
{
int i = 0, count=0;
while (i < l.length)
{
count++;
}
return count;
}
/*判断是否为同一本书*/
bool CompareElem(Book a, Book b)
{
if (strcmp(a.bookname, b.bookname) == 0 && strcmp(a.booknum, b.booknum) == 0 && a.bookprice == b.bookprice)
{
return true;
}
return false;
}
/*图书去重*/
void Remove(Seqlist &l)
{
for (int i = 0; i < l.length; i++)
{
for (int j = i + 1; j < l.length; j++)
{
if (CompareElem(l.elem[j], l.elem[i]) == 0) /*发现同一本书*/
{
for (int k = j + 1; k < l.length; k++)
{
l.elem[k - 1] = l.elem[k];
}
l.length--;
j--;
}
}
}
}
/*最爱图书查找*/
void FavouriteBook(Seqlist &l)
{
char *name[Favourite];
int i;
cout << "输入最爱图书名称" << endl;
for (i = 0; i < Favourite; i++)
{
cin >> name[i];
}
int loc[MaxSize]; /*用loc数组配合计数器k从0存储多个目标的下标 */
int j=0,k = 0;
while (j < l.length)
{
for (int m = 0; m < Favourite; m++)
{
if (name[m]==l.elem[j].bookname)
{
loc[k] = i;
k++;
}
}
if (k == 0)
{
cout << "Sorry,there is no your favourite!" << endl;
}
else
{
cout << k << endl;
for (i = 0; i < k; i++)
{
cout << l.elem[loc[i]].booknum << " " << l.elem[loc[i]].bookname << " " << setiosflags(ios::fixed) << setprecision(2) << l.elem[loc[i]].bookprice << endl;
}
}
k = 0;
}
}
/*最贵图书查找*/
void OutputExpensiveBook(Seqlist &l)
{
sort(l, 0, l.length - 1);
float standard = l.elem[0].bookprice;
int i;
cout << l.elem[0].booknum << " " << l.elem[0].bookname << " "<< setiosflags(ios::fixed) << setprecision(2) << l.elem[0].bookprice<<" " << endl;
for (i = 1; i < l.length; i++)
{
if (l.elem[i].bookprice == standard)
{
cout << l.elem[i].booknum << " " << l.elem[i].bookname << " " << setiosflags(ios::fixed) << setprecision(2) << l.elem[i].bookprice << " " << endl;
}
}
}
/*按指定条件修改图书信息*/
/*计算所有图书平均价格*/
float Averageprice(Seqlist &l)
{
float average, sum;
int i ;
for (i = 0; i < l.length; i++)
{
sum += l.elem[i].bookprice;
}
average = sum / (GetLength(l));
return average;
}
/*批量改变*/
void Change(Seqlist &l)
{
int i;
for (i = 0; i < l.length; i++)
{
if (l.elem[i].bookprice < Averageprice(l))
{
l.elem[i].bookprice = 1.2*(l.elem[i].bookprice);
}
else
{
l.elem[i].bookprice = 1.1*(l.elem[i].bookprice);
}
}
Output(l);
}
/*菜单函数*/
void menu()
{
cout << "该图书信息管理系统包含如下功能:\n";
cout << "(1)根据指定图书个数,逐个输入图书信息;\n";
cout << "(2)逐个输出图书表中所有图书的相关信息;\n";
cout << "(3)能根据指定的待入库的新图书的位置和信息,将新图书插入到图书表中指定的位置;\n";
cout << "(4)根据指定的待出库的旧图书的位置,将该图书从图书表中删除;\n";
cout << "(5)能统计表中图书个数;\n";
cout << "(6)图书信息表的图书去重;\n";
cout << "(7)最爱书籍查询;\n";
cout << "(8)图书信息表按指定条件进行批量修改;\n";
cout << "(9)利用快速排序按照图书价格降序排序;\n";
cout << "(10)实现最贵图书的查找;\n";
cout << "(11)退出系统"<<endl;
}
main.cpp 代码
#include "Master database.h"
#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
#define ElemType Book
#define overflow -2
#define MaxSize 100
#define Favourite 20
int main() {
int choose;
bool falg = true;
Seqlist l;
menu();
int i;
ElemType e;
while (true)
{
cout << "请输入选择\n";
cin >> choose;
switch (choose)
{
case 1:
InitList(l);
Input(l);
break;
case 2:
Output(l);
break;
case 3:
cin >> i;
cin >> e.booknum;
cin >> e.bookname;
cin >> e.bookprice;
ListInsert(l, i, e);
break;
case 4:
cin >> i;
ListDelete(l, i);
break;
case 5:
GetLength(l);
break;
case 6:
Remove(l);
break;
case 7:
FavouriteBook(l);
break;
case 8:
Change(l);
break;
case 9:
sort(l,0,l.length-1);
Output(l);
break;
case 10:
OutputExpensiveBook(l);
break;
default:
falg = false;
return(true);
}
}
return 0;
}