数据结构课程设计∶图书管理系统实验预习
书本结构体定义∶
typedef struct {
char no[8]; //8位书号
char name[20]; //书名
int price; //价格
}Book;
以链表为存储方法
链表结构定义∶
typedef struct LNode{
Book data; //数据域
struct LNode *next; //指针域
}LNode,*LinkList;
在主函数中用一个while语句和switch语句实现函数调用∶
int main() {
LinkList Head=getpch(LNode),Rear = Head; //链表的头结点和尾结点
Head->next = NULL;
int ch; //储存用户的输入
bool t=true; //控制程序的退出
while (t) {
system("cls"); //防止页面过于繁杂,执行一次清屏
cout << "请输入要实现的功能:\n";
cin >> ch;
switch (ch) {
case 1:inputBook(Head, Rear); break; //录入图书
case 2:printBook(Head); break; //显示所有图书的信息
case 3:insetBook(Head, Rear); break; //插入图书
case 4:deleteBook(Head, Rear); break; //删除某一书号的图书
case 5:numBook(Head); break; //统计数量
case 6:deleteRepetitionBook(Head, Rear); break; //图书去重
case 7:cout << favoriteBook(Head) << endl; break; //按书名查找
case 8:changepriceBook(Head); break; //图书信息表价格的修改
case 9:quicksortBook(Head->next, Rear); printBook(Head); break; //根据价格快速排序(降序)
case 10:expensiveBook(Head); break; //最贵图书的查找
case 11:t = false; break; //退出系统
default:cout << "输入失败,请重新输入"; //输入失败
}
system("pause");
}
cout << "程序结束。" << endl;
return 0;
}
界面设计:
程序流程:很简单的调用关系,主函数调用实现功能的函数
实验代码:#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
#define getpch(type) (type*)malloc(sizeof(type))
typedef struct {
char no[14];
char name[41];
double price;
}Book;
typedef struct LNode {
Book data;
struct LNode *next;
}*LinkList;
void inputBook(LinkList Head, LinkList &Rear) { //录入图书
LinkList test;
char ch;
int n = 1;
while (true) {
test = getpch(LNode);
cout << “请输入第” << n << “本书的书号:”;
cin >> test->data.no;
cout << “请输入第” << n << “本书的书名:”;
cin >> test->data.name;
cout << “请输入第” << n << “本书的价格:”;
cin >> test->data.price;
test->next = NULL;
Rear->next = test;
Rear = test;
n++;
cout << “是否继续录入?y/n”;
cin >> ch;
if (ch != ‘y’)break;
}
}
void printBook(LinkList Head) { //显示所有图书的信息
LinkList p = Head->next;
cout << “=图书列表=” << endl;
while (p != NULL) {
printf("%s%c%s%c%.2f%c", p->data.no, ’ ', p->data.name, ’ ', p->data.price, ‘\n’);
p = p->next;
}
}
void insetBook(LinkList Head, LinkList Rear) { //插入图书
LinkList test, p = Head;
char ch;
int n, tag = 0;
bool t = true;
while (t) {
cout << “请输入插入的位置:”;
cin >> n;
if (n < 1) {
cout << “位置错误,请重新插入!”;
continue;
}
for (int i = 1; i < n; i++) {
p = p->next;
if (p == NULL) {
cout << “抱歉,入库位置非法!” << endl;
p = Head;
tag = 1;
i = n;
}
}
if (tag == 1) {
tag = 0;
continue;
}
test = getpch(LNode);
cout << “请输入书号:”;
cin >> test->data.no;
cout << “请输入书名:”;
cin >> test->data.name;
cout << “请输入价格:”;
cin >> test->data.price;
test->next = NULL;
test->next = p->next;
p->next = test;
if (p == Rear)
Rear = test;
cout << “是否继续插入?y/n”;
cin >> ch;
if (ch != ‘y’)t = false;
}
}
void deleteBook(LinkList Head, LinkList Rear) { //删除某一书号的图书
LinkList p = Head;
char ch;
char no[41];
bool t = true;
int tag = 0;
while (t) {
if (p->next == NULL) {
cout << “暂无图书,删除失败!” << endl;
return;
}
cout << “请输入待删除图书的书号:”;
cin >> num;
while (p->next != NULL) {
if (strcmp(p->next->data.no, num) == 0) {
tag = 1;
if (p->next == Rear)
Rear = p;
p->next = p->next->next;
}
else p = p->next;
}
if (tag == 1) {
tag = 0;
printBook(Head);
}
else cout << “出库失败,未找到该图书!” << endl;
cout << “是否继续删除?y/n”;
cin >> ch;
if (ch != ‘y’)t = false;
}
}
int numBook(LinkList Head) { //统计数量
LinkList p = Head->next;
int n = 0;
while (p != NULL) {
n++;
p = p->next;
}
cout << “当前存有” << n << “本书” << endl;
return n;
}
void deleteRepetitionBook(LinkList Head, LinkList Rear) { //图书去重
LinkList p = Head->next, q;
while § {
q = p;
while (q->next) {
if (strcmp(p->data.no, q->next->data.no) == 0) {
if (q->next == Rear)
Rear = q;
q->next = q->next->next;
}
else q = q->next;
}
p = p->next;
}
numBook(Head);
printBook(Head);
}
int favoriteBook(LinkList Head) { //按书名查找
LinkList p = Head->next;
int n = 0, tag = 0;
char num[41];
cout << “请输入最爱图书的书名:”;
cin >> num;
while § {
if (strcmp(p->data.name, num) == 0) {
printf("%s%c%s%c%.2f%c", p->data.no, ’ ', p->data.name, ’ ', p->data.price, ‘\n’);
tag = 1;
n++;
}
p = p->next;
}
if (tag == 0)cout << “抱歉,没有你的最爱!” << endl;
return n;
}
void changepriceBook(LinkList Head) { //图书信息表价格的修改
LinkList p = Head->next;
double allprice = 0;
while § {
allprice += p->data.price;
p = p->next;
}
allprice /= numBook(Head);
p = Head->next;
while § {
if (p->data.price < allprice)p->data.price *= 1.2;
else p->data.price *= 1.1;
p = p->next;
}
printBook(Head);
}
void quicksortBook(LinkList Head, LinkList Rear) { //根据价格快速排序(降序)
if (Head == NULL || Head == Rear)
return;
Book temp;
LinkList p = Head->next;
LinkList small = Head;
while § {
if (p->data.price > small->data.price) {
temp = small->data;
small->data = p->data;
p->data = temp;
small = small->next;
}
p = p->next;
}
quicksortBook(Head, small);
quicksortBook(small->next, Rear);
}
void expensiveBook(LinkList Head) { //最贵图书的查找
LinkList p = Head->next;
double maxprice = 0;
while § {
if (p->data.price > maxprice)maxprice = p->data.price;
p = p->next;
}
p = Head->next;
while § {
if (p->data.price == maxprice)
printf("%s%c%s%c%.2f%c", p->data.no, ’ ', p->data.name, ’ ', p->data.price, ‘\n’);
p = p->next;
}
}
int main() {
LinkList Head = getpch(LNode), Rear = Head; //链表的头结点和尾结点
Head->next = NULL;
int ch;
bool t = true;
while (t) {
system(“cls”);
cout << “请输入要实现的功能:1/2/3…\n”;
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 << “10.最贵图书的查找” << endl;
cout << “11.退出系统” << endl;
cin >> ch;
switch (ch) {
case 1:inputBook(Head, Rear); break; //录入图书
case 2:printBook(Head); break; //显示所有图书的信息
case 3:insetBook(Head, Rear); break; //插入图书
case 4:deleteBook(Head, Rear); break; //删除某一书号的图书
case 5:numBook(Head); break; //统计数量
case 6:deleteRepetitionBook(Head, Rear); break; //图书去重
case 7:cout << favoriteBook(Head) << endl; break; //按书名查找
case 8:changepriceBook(Head); break; //图书信息表价格的修改
case 9:quicksortBook(Head->next, Rear); printBook(Head); break; //根据价格快速排序(降序)
case 10:expensiveBook(Head); break; //最贵图书的查找
case 11:t = false; break; //退出系统
default:cout << “输入失败,请重新输入”;
}
system(“pause”);
}
cout << “程序结束。” << endl;
return 0;
}