欢迎光临散文网 会员登陆 & 注册

C/C++知识项目教程:图书管理系统!手把手教你写出大学C语言图书管理系统...

2023-06-25 13:23 作者:粉烧鱼  | 我要投稿

老师的源代码😘


/*VS软件的内扩增问题*/#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <iostream>

#include <list>

/*用户信息

struct student

{

char name[20];

char tel[20];

int curNum;

struct bookInfo userBook[3];

};

list<student> myList;*/

//书籍信息

struct bookInfo//图书信息结构体

{

char name[20];

float price;

int num;

};

struct Node//定义结构体变量

{

struct bookInfo data;

struct Node* next;

};

struct Node* listBook = NULL;//全局变量声明

struct Node* createHead()//创建表头(有表头链表)

{

struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//动态内存申请

headNode->next = NULL;//初始化

return headNode;

}

struct Node* createNode(struct bookInfo data)//创建节点,用户数据变为结构体变量

{

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;//必须先连接后断开

newNode->next = NULL;

return newNode;

}

void insertNodeByHead(struct Node* headNode, struct bookInfo data)//头插法插入数据

{

struct Node* newNode = createNode(data);

newNode->next = headNode->next;

headNode->next = newNode;

}

/*void insertNodeByTail(struct Node* headNode, int data)//尾插法

{

struct Node* pMove = headNode;

while (pMove->next != NULL)

{

pMove = pMove->next;

}

struct Node* newNode = createNode(data);

pMove->next = newNode;

}*/

void deleteNodeByName(struct Node* headNode,char *bookName)//删除函数

{

struct Node* posLeftNode = headNode;

struct Node* posNode = headNode->next;

while (posNode != NULL && strcmp(posNode->data.name,bookName))

{

posLeftNode = posNode;

posNode = posLeftNode->next;

}

if (posNode == NULL)

return;

else

{

printf("删除成功。\n");

posLeftNode->next = posNode->next;

free(posNode);

posNode = NULL;

}

}

void printlistBook(struct Node* headNode)//打印数据

{

struct Node* pMove = headNode->next;

printf("书名\t价格\t数量\n");

while (pMove)

{

//剥洋葱

printf("%s\t%.1f\t%d\n", pMove->data.name,pMove->data.price,pMove->data.num);

pMove = pMove->next;

}

}

void bubbleSortlistBook(struct Node* headNode)//排序

{

for (struct Node* p = headNode->next; p != NULL; p->next)

{

for (struct Node* q = headNode->next; q->next != NULL; q = q->next)

{

if (q->data.price > q->next->data.price)

{

struct bookInfo tempData = q->data;

q->data = q->next->data;

q->next->data = tempData;

}

}

}

printlistBook(headNode);

}

struct Node* searchByName(struct Node* headNode, char* bookName)

{

struct Node* posNode = headNode->next;

while (posNode != NULL && strcmp(posNode->data.name, bookName))

{

posNode = posNode->next;

}

return posNode;

}

//文件操作

void saveInfoToFile(const char* fileName, struct Node* headNode)//文件储存

{

FILE* fp = fopen(fileName, "w");

struct Node* pMove = headNode-> next;

while (pMove != NULL)

{

fprintf(fp, "%s\t%.1f\t%d\n", pMove->data.name, pMove->data.price, pMove->data.num);

pMove = pMove->next;

}

fclose(fp);

}

void readInfoFromFile(const char* fileName, struct Node* headNode)//文件浏览

{

FILE* fp = fopen(fileName, "r");

if (fp == NULL)//创建文件

{

fp = fopen(fileName, "w+");

}

struct bookInfo tempData;

while (fscanf(fp, "%s\t%f\t%d\n", tempData.name, &tempData.price, &tempData.num) != EOF)

{

insertNodeByHead(listBook, tempData);

}

fclose(fp);

}

void makeMenu()//显示初始界面

{

printf("----------------------\n");

printf("图书管理系统\n");

printf("0.退出\n");

printf("1.登记\n");

printf("2.浏览\n");

printf("3.借阅\n");

printf("4.归还\n");

printf("5.排序\n");

printf("6.删除\n");

printf("7.查找\n");

printf("----------------------\n");

printf("请输入(0-7)\n");

}

void keyDown()//Switch函数选择界面

{

int userkey = 0;

struct bookInfo tempBook;//产生一个临时的变量储存书籍信息

struct Node* result = NULL;

scanf("%d", &userkey);

switch (userkey)

{

case 0:

printf("【 退出成功 】\n");

exit(0);

break;

case 1:

printf("【 登记 】\n");

printf("输入书籍的信息(name,price,num):");

scanf("%s%f%d", tempBook.name, &tempBook.price, &tempBook.num);

insertNodeByHead(listBook, tempBook);

saveInfoToFile("bookinfo.txt", listBook);

break;

case 2:

printf("【 浏览 】\n");

printlistBook(listBook);

break;

case 3:

printf("【 借阅 】\n");

printf("请输入借阅的书名:");

scanf("%s", tempBook.name);

result = searchByName(listBook, tempBook.name);

if (result == NULL)

{

printf("没有找到相关书籍,无法借阅。\n");

}

else

{

if (result->data.num >= 1)

{

result->data.num--;

printf("借阅成功。\n");

}

else

{

printf("当前书本暂无库存,借阅失败!\n");

}

}

break;

case 4:

printf("【 归还 】\n");

printf("请输入归还的书名:");

scanf("%s", tempBook.name);

result = searchByName(listBook, tempBook.name);

if (result == NULL)

{

printf("该书不属于图书馆!");

}

else

{

result->data.num++;

printf("归还成功。\n");

}

break;

case 5:

printf("【 排序 】\n");

bubbleSortlistBook(listBook);

break;

case 6:

printf("【 删除 】\n");

printf("请输入需要删除的书名:");

scanf("%s", tempBook.name);

deleteNodeByName(listBook, tempBook.name);

saveInfoToFile("bookinfo.txt", listBook);

break;

case 7:

printf("【 查找 】\n");

printf("请输入要查找的书籍:");

scanf("%s", tempBook.name);

result = searchByName(listBook, tempBook.name);

if (result == NULL)

{

printf("未找到相关信息。\n");

}

else

{

printf("书名\t价格\t数量\n");

printf("%s\t%.1f\t%d\n",result->data.name,result->data.price,result->data.num);

}

break;

default:

printf("【 请输入有效数字。 】\n");

break;

}

}

int main()//主函数

{

listBook = createHead();

readInfoFromFile("bookinfo.txt",listBook);

while (1)

{

makeMenu();

keyDown();

system("pause");

system("cls");

}

system("pause");

return 0;

}

C/C++知识项目教程:图书管理系统!手把手教你写出大学C语言图书管理系统...的评论 (共 条)

分享到微博请遵守国家法律