数据结构文章推荐: |
---|
多种方式实现英文单词词频统计和检索系统 |
指针如何赋值?关于指针的理解 |
深度优先搜索判断有向图路径是否存在 |
待更新 |
本次记录的是数据结构课程的实验,实验内容如下:
图书信息表包括如下常用的基本操作:图书信息表的创建和输出、排序(分别按图书名称、按图书价格排序)、修改、按名查找、最贵图书的查找、新书入库、旧书出库等八项。
其中,图书信息包括如下6部分:
ISBN、作者姓名、图书名称、出版社、出版年份、图书价格
1. 代码实现
2. 运行效果
3. 实验总结
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define maxsize 50 //图书库存最大数量
//函数结果状态代码
#define OK 1 //正确标志
#define ERROR 0 //错误标志
//函数结果状态代码
typedef int Status;
typedef struct{
int id; //管理系统中的id
char isbn[20]; //ISBN
char authorName[30]; //作者姓名
char bookName[50]; //图书名称
char publisher[40]; //出版社
char publishYear[10]; //出版年份
float price; //图书价格
}Book;
//顺序表数据结构
typedef struct{
Book *elem; //储存空间的基地址
int length; //数据结构的长度
}SqList;
SqList BookSystem;
//初始化顺序表基本算法
Status InitList(SqList *BookSystem){
//构造一个空的顺序表BookSystem
BookSystem->elem = (Book*)malloc(sizeof(Book)*maxsize); //分配内存空间
if(!BookSystem->elem)exit(-1); //存储分配失败
BookSystem->length = 0; //当前长度为0
return OK;
}
//用于测试数据,更易看出实验效果
void insertBook(SqList *BookSystem){
Book book[] = {{1,"5447395","余小华","《这么玩才赚钱》","人民邮电出版社","2017-01",49.50},
{2,"4375152","达芙妮","《高情商心理学》","现代出版社","2016-11",20.00},
{3,"6644188","郝言言","《悦己》","新华出版社","2007-04",25.00},
{4,"0211267","路瑶","《平凡的世界》","北京文艺出版社","1986-12",30.00},
{5,"6075642","张汝","《小时候》","海燕出版社","2013-06",33.00},
{6,"1553645","陈果","《好的爱情》","人民日报出版社","2010-09",28.00}};
int i = 0; //循环变量
for(i;i<6;i++){
BookSystem->elem[i] = book[i]; //初始化图书
BookSystem->length++;
}
}
//8项操作的实现:创建、输出、排序、修改、按名查找、最贵图书的查找、新书入库、旧书出库
int showFunctions(){//显示图书管理系统功能
printf("%-30s***************欢迎来到图书管理系统***************\n","");
printf("%-33s1、创建系统\t2、输出图书\t3、图书排序\n%-33s4、图书修改\t5、按名查找\t6、最贵图书\n%-33s7、新书入库\t8、旧书出库\t9、退出系统","","","");
printf("\n%-33s请选择相关功能操作:","");
int choice = -1; //定义用户的选择并初始化
scanf("%d",&choice);
printf("%-30s**************************************************\n","");
return choice;
}
void creatBookSystem(SqList *BookSystem){//第一项功能:创建
int i = 0;
/*for(i;i<30;i++){
printf("*");
}printf("\n");*/
printf("%-30s系统:图书管理系统初始化中...\n","");
//SqList BookSystem; //定义SqList类型的图书库
InitList(BookSystem); //初始化图书管理系统
insertBook(BookSystem); //添加一些初始化图书
printf("%-30s系统:图书管理系统初始化成功!\n","");
}
void showBook(SqList *BookSystem){//第二项功能:输出
int j = 0;
for(j;j<115;j++){
printf("=");
}
printf("\n");
printf(" -id- ---ISBN--- --作者姓名-- ---图书名称--- -----出版社----- -出版年份- -图书价格-\n");
if(BookSystem->length == 0){
printf("%-30s系统:当前图书系统内无图书!","");
}
int i = 0;
for(i;i<BookSystem->length;i++){
Book book = BookSystem->elem[i];
printf("%-5s%-8d","",book.id);
printf("%-15s",book.isbn);
printf("%-13s",book.authorName);
printf("%-23s",book.bookName);
printf("%-24s",book.publisher);
printf("%-14s",book.publishYear);
printf("%.3f\n",book.price);
}
for(j=0;j<115;j++){
printf("=");
}
printf("\n");
}
void sortBook(SqList *BookSystem){//第三项功能:排序
int i = 0;int j = 0;int max_Index = -1;int choice = -1;Book temp;
printf("%-33s1、按名称排序\t2、按价格排序\t请选择:","");
scanf("%d",&choice);
if(choice==1){
for(i;i<BookSystem->length-1;i++){
max_Index = i;
for(j=i+1;j<BookSystem->length;j++){
if(strcmp(BookSystem->elem[max_Index].bookName,BookSystem->elem[j].bookName)>0){
max_Index=j;
}
}
temp = BookSystem->elem[i];
BookSystem->elem[i] = BookSystem->elem[max_Index];
BookSystem->elem[max_Index] = temp;
}
}
if(choice==2){
for(i;i<BookSystem->length-1;i++){
max_Index = i;
for(j=i+1;j<BookSystem->length;j++){
if(BookSystem->elem[max_Index].price<BookSystem->elem[j].price){
max_Index=j;
}
}
temp = BookSystem->elem[i];
BookSystem->elem[i] = BookSystem->elem[max_Index];
BookSystem->elem[max_Index] = temp;
}
}
printf("%-30s系统:排序完毕!\n","");
i = 0;
for(i=0;i<BookSystem->length;i++){
BookSystem->elem[i].id = i+1;
}
}
void updateBook(SqList *BookSystem){//第四项功能:修改
printf("%-30s系统:请输入您要修改的书籍名称或id:","");
char ch[50];
scanf("%s",&ch);
int id = atoi(ch);int i = 0;
if(id==0){
for(i;i<BookSystem->length;i++){
if(strcmp(ch,BookSystem->elem[i].bookName)==0){
printf("%-30s系统:","");
Book book = BookSystem->elem[i];
printf("(%s-%s-%s-%s-[%s]-%.3f)\n",book.isbn,book.authorName,
book.bookName,book.publisher,book.publishYear,book.price);
printf("%-30s修改格式:(ISBN 作者姓名 图书名称 出版社 出版年份 图书价格)\n%-30s请输入:","","");
scanf("%s %s %s %s %s %f",&BookSystem->elem[i].isbn,&BookSystem->elem[i].authorName,&BookSystem->elem[i].bookName,
&BookSystem->elem[i].publisher,&BookSystem->elem[i].publishYear,&BookSystem->elem[i].price);
printf("%-30s系统:修改成功!\n","");
break;
}
}
}else{
for(i;i<BookSystem->length;i++){
if(BookSystem->elem[i].id == id){
printf("%-30s系统:","");
Book book = BookSystem->elem[i];
printf("(%s-%s-%s-%s-[%s]-%.3f)\n",book.isbn,book.authorName,
book.bookName,book.publisher,book.publishYear,book.price);
printf("%-30s修改格式:(ISBN 作者姓名 图书名称 出版社 出版年份 图书价格)\n%-30s请输入:","","");
scanf("%s %s %s %s %s %f",&BookSystem->elem[i].isbn,&BookSystem->elem[i].authorName,&BookSystem->elem[i].bookName,
&BookSystem->elem[i].publisher,&BookSystem->elem[i].publishYear,&BookSystem->elem[i].price);
printf("%-30s系统:修改成功!\n","");
break;
}
}
}
}
void findByName(SqList *BookSystem){//第五项功能:按名查找
printf("%-30s系统:请输入您要查找的书籍名称:","");
char ch[50];
scanf("%s",&ch);
int i = 0;
for(i;i<BookSystem->length;i++){
if(strcmp(ch,BookSystem->elem[i].bookName)==0){
printf("%-30s系统:","");
Book book = BookSystem->elem[i];
printf("(%s-%s-%s-%s-[%s]-%.3f)\n",book.isbn,book.authorName,
book.bookName,book.publisher,book.publishYear,book.price);
break;
}else if(i==BookSystem->length-1&&strcmp(ch,BookSystem->elem[i].bookName)!=0){
printf("%-30s系统:查无此书!\n","");
}
}
}
void findByExpensive(SqList *BookSystem){//第六项:最贵图书查找
int j = 0;int max_Index = 0;
for(j=max_Index+1;j<BookSystem->length;j++){
if(BookSystem->elem[max_Index].price<BookSystem->elem[j].price){
max_Index=j;
}
}
printf("%-30s系统:","");
Book book = BookSystem->elem[max_Index];
printf("(%s-%s-%s-%s-[%s]-%.3f)\n",book.isbn,book.authorName,
book.bookName,book.publisher,book.publishYear,book.price);
}
void addBook(SqList *BookSystem){//第七项:新书入库
if(BookSystem->length==maxsize){
printf("%-30s系统:库存已满!","");
}
else{
printf("%-30s添加格式:(ISBN 作者姓名 图书名称 出版社 出版年份 图书价格)\n%-30s请输入:","","");
int index = ++BookSystem->length;
Book newBook;
scanf("%s %s %s %s %s %f",&newBook.isbn,&newBook.authorName,
&newBook.bookName,&newBook.publisher,
&newBook.publishYear,&newBook.price);
newBook.id = index;
BookSystem->elem[index-1] = newBook;
//9787115447395 黄鹏飞 《代码是如何敲成的》 人民出版社 2019-10 33->00 用于测试
printf("%-30s系统:入库成功!\n","");
}
}
void outBook(SqList *BookSystem){//第八项:旧书出库
printf("%-30s系统:请输入要出库的书籍名称或id:","");
char ch[50];
scanf("%s",&ch);
int id = atoi(ch);int i = 0;
if(id==0){
for(i;i<BookSystem->length;i++){
if(strcmp(ch,BookSystem->elem[i].bookName)==0){
int j = i;
for(j=i;j<BookSystem->length-1;j++){
BookSystem->elem[j] = BookSystem->elem[j+1];
}
BookSystem->length--;
printf("%-30s系统:出库成功!\n","");
break;
}else if(i==BookSystem->length-1&&strcmp(ch,BookSystem->elem[i].bookName)!=0){
printf("%-30s系统:查无此书!\n","");
}
}
}else{
for(i;i<BookSystem->length;i++){
if(BookSystem->elem[i].id==id){
int j = i;
for(j=i;j<BookSystem->length-1;j++){
BookSystem->elem[j] = BookSystem->elem[j+1];
}
BookSystem->length--;
printf("%-30s系统:出库成功!\n","");
break;
}else if(i==BookSystem->length-1&&strcmp(ch,BookSystem->elem[i].bookName)!=0){
printf("%-30s系统:查无此书!\n","");
}
}
}
for(i=0;i<BookSystem->length;i++){
BookSystem->elem[i].id = (i+1);
}
}
int main()
{
bool isCreated = false;
while(true){
int choice = showFunctions(); //调用函数取得用户选择的功能
if(choice == 1){
creatBookSystem(&BookSystem);
isCreated = true;
continue;
}
if(choice == 2&&isCreated){
system("cls");
showBook(&BookSystem);
continue;
}
if(choice==3&&isCreated){
sortBook(&BookSystem);
continue;
}
if(choice==4&&isCreated){
updateBook(&BookSystem);
continue;
}
if(choice==5&&isCreated){
findByName(&BookSystem);
continue;
}
if(choice==6&&isCreated){
findByExpensive(&BookSystem);
continue;
}
if(choice==7&&isCreated){
addBook(&BookSystem);
continue;
}
if(choice==8&&isCreated){
outBook(&BookSystem);
continue;
}
if(choice==9){
printf("%-30s系统:欢迎下次使用!\n\n\n","");
break;
}
if(choice>9||choice<1){
printf("%-30s系统:请输入正确选项!\n","");
continue;
}
printf("%-30s系统:您还未创建图书管理系统!\n","");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//函数结果状态代码
#define OK 1 //正确标志
#define ERROR 0 //错误标志
//函数结果状态代码
typedef int Status;
typedef struct{
int id; //管理系统中的id
char isbn[20]; //ISBN
char authorName[30]; //作者姓名
char bookName[50]; //图书名称
char publisher[40]; //出版社
char publishYear[10]; //出版年份
float price; //图书价格
}Book;
//顺序表数据结构
typedef struct LNode{
Book book; //当前节点中的Book结构体
struct LNode *next; //next指向下一个LNode
}LNode,*LinkList;
LinkList BookSystem;
//8项操作的实现:创建、输出、排序、修改、按名查找、最贵图书的查找、新书入库、旧书出库
int showFunctions(){//显示图书管理系统功能
printf("%-30s***************欢迎来到图书管理系统***************\n","");
printf("%-33s1、创建系统\t2、输出图书\t3、图书排序\n%-33s4、图书修改\t5、按名查找\t6、最贵图书\n%-33s7、新书入库\t8、旧书出库\t9、退出系统","","","");
printf("\n%-33s请选择相关功能操作:","");
int choice = -1; //定义用户的选择并初始化
scanf("%d",&choice);
printf("%-30s**************************************************\n","");
return choice;
}
void creatBookSystem(){//第一项功能:创建
BookSystem = (LinkList)malloc(sizeof(LNode));
BookSystem->next = NULL;
int i = 0; //初始给6个节点
Book book[] = {{1,"5447395","余小华","《这么玩才赚钱》","人民邮电出版社","2017-01",49.50},
{2,"4375152","达芙妮","《高情商心理学》","现代出版社","2016-11",20.00},
{3,"6644188","郝言言","《悦己》","新华出版社","2007-04",25.00},
{4,"0211267","路瑶","《平凡的世界》","北京文艺出版社","1986-12",30.00},
{5,"6075642","张汝","《小时候》","海燕出版社","2013-06",33.00},
{6,"1553645","陈果","《好的爱情》","人民日报出版社","2010-09",28.00}};
for(i;i<6;i++){
LNode *node = (LinkList)malloc(sizeof(LNode)); //生成新节点
node->book = book[5-i];
node->next = BookSystem->next;
BookSystem->next = node;
}
printf("%-30s系统:图书管理系统初始化成功!\n","");
}
void showBook(){//第二项功能:输出
int j = 0;LNode *temp = BookSystem->next;
for(j;j<115;j++){
printf("=");
}
printf("\n");
printf(" -id- ---ISBN--- --作者姓名-- ---图书名称--- -----出版社----- -出版年份- -图书价格-\n");
if(temp == 0){
printf("%-30s系统:当前图书系统内无图书!","");
}else{
while(temp){
Book book = temp->book;
printf("%-5s%-8d","",book.id);
printf("%-15s",book.isbn);
printf("%-13s",book.authorName);
printf("%-23s",book.bookName);
printf("%-24s",book.publisher);
printf("%-14s",book.publishYear);
printf("%.3f\n",book.price);
temp = temp->next;
}
}
int i = 0;
for(j=0;j<115;j++){
printf("=");
}
printf("\n");
}
void sortBook(){//第三项功能:排序
int choice = -1;bool isCompared = false;
printf("%-33s1、按名称排序\t2、按价格排序\t请选择:","");
scanf("%d",&choice);
if(choice==1){
LNode *node = BookSystem->next; //循环体条件
LNode *temp; //临时变量
Book maxBook; //比较的书
LNode *temp_book = node; //临时变量
while(node->next){
maxBook = node->book;
temp_book = node;
while(temp_book){
if(strcmp(maxBook.bookName,temp_book->book.bookName)>0){
maxBook = temp_book->book;
temp = temp_book;
isCompared = true;
}
temp_book = temp_book->next;
}
if(isCompared){
temp->book = node->book;
node->book = maxBook;
isCompared = false; //*突破点
}
node = node->next;
}
}
if(choice==2){
LNode *node = BookSystem->next; //循环体条件
LNode *temp = BookSystem->next; //临时变量
Book maxBook; //比较的书
LNode *temp_book; //临时变量
while(node->next){
maxBook = node->book;
temp_book = node;
while(temp_book){
if(maxBook.price<temp_book->book.price){
maxBook = temp_book->book;
temp = temp_book;
isCompared = true;
}
temp_book = temp_book->next;
}
if(isCompared){
temp->book = node->book;
node->book = maxBook;
isCompared = false;
}
node = node->next;
}
}
printf("%-30s系统:排序完毕!\n","");
int i = 0;
LNode *node = BookSystem->next;
while(node){
node->book.id = ++i;
node = node->next;
}
}
void updateBook(){//第四项功能:修改
printf("%-30s系统:请输入您要修改的书籍名称或id:","");
char ch[50];
scanf("%s",&ch);
int id = atoi(ch);int i = 0;
LNode *p = BookSystem->next;
if(id==0){
while(p){
if(strcmp(ch,p->book.bookName)==0){
printf("%-30s系统:","");
Book book = p->book;
printf("(%s-%s-%s-%s-[%s]-%.3f)\n",book.isbn,book.authorName,
book.bookName,book.publisher,book.publishYear,book.price);
printf("%-30s修改格式:(ISBN 作者姓名 图书名称 出版社 出版年份 图书价格)\n%-30s请输入:","","");
scanf("%s %s %s %s %s %f",&p->book.isbn,&p->book.authorName,&p->book.bookName,
&p->book.publisher,&p->book.publishYear,&p->book.price);
printf("%-30s系统:修改成功!\n","");
break;
}
p=p->next;
}
}else{
while(p){
if(p->book.id == id){
printf("%-30s系统:","");
Book book = p->book;
printf("(%s-%s-%s-%s-[%s]-%.3f)\n",book.isbn,book.authorName,
book.bookName,book.publisher,book.publishYear,book.price);
printf("%-30s修改格式:(ISBN 作者姓名 图书名称 出版社 出版年份 图书价格)\n%-30s请输入:","","");
scanf("%s %s %s %s %s %f",&p->book.isbn,&p->book.authorName,&p->book.bookName,
&p->book.publisher,&p->book.publishYear,&p->book.price);
printf("%-30s系统:修改成功!\n","");
break;
}
p=p->next;
}
}
}
void findByName(){//第五项功能:按名查找
printf("%-30s系统:请输入您要查找的书籍名称:","");
char ch[50];
scanf("%s",&ch);
int i = 0;
LNode *p = BookSystem->next;
while(p){
if(strcmp(ch,p->book.bookName)==0){
printf("%-30s系统:","");
Book book = p->book;
printf("(%s-%s-%s-%s-[%s]-%.3f)\n",book.isbn,book.authorName,
book.bookName,book.publisher,book.publishYear,book.price);
break;
}
p=p->next;
}
}
void findByExpensive(){//第六项:最贵图书查找
LNode *node = BookSystem->next; //循环体条件
Book maxBook; //比较的书
LNode *temp_book; //临时变量
while(node->next){
maxBook = node->book;
temp_book = node->next;
while(temp_book->next){
if(maxBook.price<temp_book->book.price){
maxBook = temp_book->book;
}
temp_book = temp_book->next;
}
break;
}
printf("%-30s系统:","");
Book book = maxBook;
printf("(%s-%s-%s-%s-[%s]-%.3f)\n",book.isbn,book.authorName,
book.bookName,book.publisher,book.publishYear,book.price);
}
void addBook(){//第七项:新书入库
LNode *p = BookSystem->next;
LNode *book = (LinkList)malloc(sizeof(LNode));
int index = 0;
while(p->next){
index++;
p = p->next;
}
printf("%-30s添加格式:(ISBN 作者姓名 图书名称 出版社 出版年份 图书价格)\n%-30s请输入:","","");
Book newBook;
scanf("%s %s %s %s %s %f",&newBook.isbn,&newBook.authorName,
&newBook.bookName,&newBook.publisher,
&newBook.publishYear,&newBook.price);
newBook.id = index;
book->book = newBook;
p->next = book;
book->next = 0;
printf("%-30s系统:入库成功!\n","");
}
void outBook(){//第八项:旧书出库
printf("%-30s系统:请输入您要修改的书籍名称或id:","");
char ch[50];
scanf("%s",&ch);
int id = atoi(ch);
LNode *p = BookSystem->next;
LNode *book = BookSystem;
if(id==0){
while(p){
if(strcmp(ch,p->book.bookName)==0){
book->next = p->next;
p->next = 0;
printf("%-30s系统:出库成功!\n","");
break;
}else if(strcmp(ch,p->book.bookName)!=0&&!p->next){
printf("%-30s系统:查无此书!\n","");
}
book=book->next;
p=p->next;
}
}else{
while(p){
if(id==p->book.id){
book->next = p->next;
p->next = 0;
printf("%-30s系统:出库成功!\n","");
break;
}else if(strcmp(ch,p->book.bookName)!=0&&!p->next){
printf("%-30s系统:查无此书!\n","");
}
book=book->next;
p=p->next;
}
}
int i = 0;
LNode *node = BookSystem->next;
while(node){
node->book.id = ++i;
node = node->next;
}
}
int main()
{
bool isCreated = false;
while(true){
int choice = showFunctions(); //调用函数取得用户选择的功能
if(choice == 1){
creatBookSystem();
isCreated = true;
continue;
}
if(choice == 2&&isCreated){
system("cls");
showBook();
continue;
}
if(choice==3&&isCreated){
sortBook();
continue;
}
if(choice==4&&isCreated){
updateBook();
continue;
}
if(choice==5&&isCreated){
findByName();
continue;
}
if(choice==6&&isCreated){
findByExpensive();
continue;
}
if(choice==7&&isCreated){
addBook();
continue;
}
if(choice==8&&isCreated){
outBook();
continue;
}
if(choice==9){
printf("%-30s系统:欢迎下次使用!\n\n\n","");
break;
}
if(choice>9||choice<1){
printf("%-30s系统:请输入正确选项!\n","");
continue;
}
printf("%-30s系统:您还未创建图书管理系统!\n","");
}
return 0;
}
具体运行效果请看视频:项目演示视频
最后:如果有问题的话欢迎交流!有帮助的话别忘了支持一下哦!