使用链表写了一个C语言的图书管理系统
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int an = 0;
struct SHU {
int hao;//书号
char name[100];//书名
char zuo_zhe[100];//作者
char jie_yue[32];//借阅标记
};
struct Node {
struct SHU data;
struct Node* next;
};
void jie_mian();//界面菜单
struct Node* chu_shi_hua();//初始化,创建表头
void deng_ji1(struct Node*);//登记书籍(头插)
void deng_ji2(struct Node*);//登记书籍(尾插)
void liu_lang(struct Node*);//浏览书籍
void jie_yue(struct Node*);//借阅书籍
void gui_huan(struct Node*);//归还书籍
void shang_chu(struct Node*);//删除书籍
void chazhao(struct Node*);//查找书籍
///
int main()
{
struct Node* list = chu_shi_hua();
jie_mian();
while (1) {
scanf("%d", &an);
switch (an) {
case 0:
printf("\t\t 【退出】\n");
break;
case 1:
printf("\t\t 【登记】\n");
int count = 1;
int shu_zi = 0;
printf("请输入要登记几本书籍:");
scanf("%d", &shu_zi);
deng_ji1(list);
while (count < shu_zi) {
deng_ji2(list);
count++;
}
break;
case 2:
printf("\t\t 【浏览】\n");
liu_lang(list);
break;
case 3:
printf("\t\t 【借阅】\n");
jie_yue(list);
break;
case 4:
printf("\t\t 【归还】\n");
gui_huan(list);
break;
case 5:
printf("\t\t 【删除】\n");
shang_chu(list);
break;
case 6:
printf("\t\t 【查找】\n");
chazhao(list);
break;
default:
printf("\t\t 【输入错误】!!!\n");
}
if (an == 0) {
printf("\n");
printf("+++++++++++");
printf("退出成功!");
printf("+++++++++++");
printf("\n");
break;
}
}
return 0;
}
void jie_mian() {
printf("-----------------------------------------------------\n");
printf("-----------------------------------------------------\n");
printf(" 图书管理系统 \n ");
printf("\t\t 1.登记书籍\n");
printf("\t\t 2.浏览书籍\n");
printf("\t\t 3.借阅书籍\n");
printf("\t\t 4.归还书籍\n");
printf("\t\t 5.删除书籍\n");
printf("\t\t 6.查找书籍\n");
printf("\t\t 0.退出系统\n");
printf("-----------------------------------------------------\n");
}
struct Node* chu_shi_hua() {
struct Node* list = (struct Node*)malloc(sizeof(struct Node));
list->next = NULL;
return list;
}
void deng_ji1(struct Node* list) {
int Hao = 0;
char Name[100] = { 0 };
char ZUO_zhe[100] = { 0 };
printf("输入要登记书籍的编号,书名和作者:");
scanf("%d%s%s", &Hao, &Name, &ZUO_zhe);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data.hao = Hao;
strcpy(newNode->data.name, Name);
strcpy(newNode->data.zuo_zhe, ZUO_zhe);
strcpy(newNode->data.jie_yue, "未借阅");
newNode->next = list->next;
list->next = newNode;
printf("%s--登记成功!\n", newNode->data.name);
}
void deng_ji2(struct Node* list) {
int Hao = 0;
char Name[100] = { 0 };
char ZUO_zhe[100] = { 0 };
printf("输入要登记书籍的编号,书名和作者:");
scanf("%d%s%s", &Hao, &Name, &ZUO_zhe);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data.hao = Hao;
strcpy(newNode->data.name, Name);
strcpy(newNode->data.zuo_zhe, ZUO_zhe);
strcpy(newNode->data.jie_yue, "未借阅");
newNode->next = NULL;
list = list->next;
while ((list->next) != NULL) {
list = list->next;
}
list->next = newNode;
printf("%s--登记成功!\n", newNode->data.name);
}
void liu_lang(struct Node* list) {
list = list->next;
while (list != NULL) {
printf("%d %s----%s\t%s\n", list->data.hao, list->data.name, list->data.zuo_zhe, list->data.jie_yue);
list = list->next;
}
printf("\n");
}
void jie_yue(struct Node* list) {
char name[100] = { 0 };
printf("输入要借阅的书名:");
scanf("%s", &name);
list = list->next;
while (list != NULL) {
if (strcmp(list->data.name, name) == 0) {
if (strcmp(list->data.jie_yue,"未借阅") == 0) {
strcpy(list->data.jie_yue, "已被借");
printf("%s借阅成功\n", list->data.name);
break;
}
else {
printf("%s已被借\n", list->data.name);
break;
}
}
if ( !(strcmp(list->data.name,name) == 0) && list->next == NULL) {
printf("%s借阅失败,没有找到此书籍!\n", name);
break;
}
list = list->next;
}
}
void gui_huan(struct Node* list) {
char name[100] = { 0 };
printf("输入要归还的书名:");
scanf("%s", &name);
list = list->next;
while (list != NULL) {
if (strcmp(list->data.name, name) == 0) {
if (strcmp(list->data.jie_yue,"已被借") == 0) {
strcpy(list->data.jie_yue, "未借阅");
printf("%s成功归还\n", name);
break;
}
else {
printf("%s归还失败,此书籍未被借出\n", name);
break;
}
}
if (!(strcmp(list->data.name, name) == 0) && list->next == NULL) {
printf("%s归还失败,没有找到此书籍!\n", name);
break;
}
list = list->next;
}
}
void shang_chu(struct Node* list) {
struct Node* p = list;
struct Node* pmove = list->next;
char name[100] = { 0 };
printf("输入要删除的书名:");
scanf("%s", &name);
while (pmove != NULL) {
if (strcmp(pmove->data.name,name) == 0) {
p->next = pmove->next;
printf("%s删除成功\n", pmove->data.name);
free(pmove);
break;
}
if (!(strcmp(list->data.name, name) == 0) && list->next == NULL) {
printf("删除失败,没有此书籍!\n");
break;
}
p = pmove;
pmove = pmove->next;
}
list = list->next;
for (int i = 1;; i++) {
list->data.hao = i;
if (list->next == NULL) {
break;
}
list = list->next;
}
printf("\n");
}
void chazhao(struct Node* list) {
char name[100] = { 0 };
printf("输入要查找的书名:");
scanf("%s", &name);
list = list->next;
while (list != NULL) {
if (strcmp(list->data.name,name) == 0) {
printf("查询成功:");
printf("%d %s----%s\t%s\n", list->data.hao, list->data.name, list->data.zuo_zhe, list->data.jie_yue);
break;
}
if (!(strcmp(list->data.name, name) == 0) && list->next == NULL) {
printf("查询失败,没有此书籍!\n");
break;
}
list = list->next;
}
}