期末将至,还有人为不知道如何写课设而头疼吗,这里给大家带来一个最简单基础的课设项目,图书管理系统。

功能分析

一般来说,简单的图书管理系统有以下几个功能:

1、用户管理

2、读者管理

3、图书管理

4、图书借还管理

因此我们的菜单页面可以这样写(以用户管理为例)

Menu.c

#include "Menu.h"//提示选择int select(){int op = -1;printf("select>");scanf("%d", &op);return op;}//主菜单int mainMenu(){printf("**********************************\n");printf("* 1.用户管理*\n");printf("* 2.读者管理*\n");printf("* 3.图书管理*\n");printf("* 4.图书流通管理*\n");printf("* 5.退出系统*\n");printf("**********************************\n");return select();}//用户管理菜单int userMenu(){printf("**********************************\n");printf("* 1.用户信息输入*\n");printf("* 2.用户信息修改*\n");printf("* 3.用户信息删除*\n");printf("* 4.用户信息显示*\n");printf("* 5.用户密码修改*\n");printf("* 6.返回主菜单*\n");printf("**********************************\n");return select();}//读者管理菜单//以此类推

那我们怎么从主页面进入用户管理呢,用下面的代码

Main.c

#include"Menu.h"#include#include#include"UserManage.h"UserManage userManage;//用户管理void quit(){exit(0);}void init(){userManage_init(&userManage);}void run(){int op = mainMenu();switch (op){case 1://用户管理userManage_operation(&userManage);break;case 2:break;case 3:break;case 4:break;case 5:quit();break;}}void print(void* val){printf("%d ", (int)val);}int main(){init();while (true){run();}return 0;}

这样我们根据输入的数字即可判断下一步实现哪个功能,进入用户管理后,我们要对用户数据进行录入,代码如下

UserManage.c

#include "UserManage.h"#include"Menu.h"#includevoid userManage_init(UserManage* umage){list_init(&umage->userList);//读取文件userManage_loadData(umage,"./data/user.txt");}void userManage_loadData(UserManage* umage,const char* filename){FILE* fp = fopen(filename, "r");if (!fp){perror("file open failed\n");return;}//读取数据//把表头读出来,并丢掉char buf[BUFSIZ] = { 0 };fgets(buf, BUFSIZ, fp);//读取正式的内容while (!feof(fp)){fgets(buf, BUFSIZ, fp);//如果成功,放入链表list_pushBack(&umage->userList, user_fromString(buf));}fclose(fp);}void userManage_operation(UserManage* umage){bool isDone = false;while (!isDone){int op = userMenu();switch (op){case 1:userManage_input(umage);break;case 2:userManage_modify(umage);break;case 3:userManage_remove(umage);break;case 4:userManage_show(umage);break;case 5:userManage_modifyPassword(umage);break;case 6:isDone = true;break;}}}void userManage_input(UserManage* umage){printf("请输入用户(用户ID、密码、类型)>");User* user = createEmptyUser();scanf("%lu %s %d", &user->number, user->password, &user->type);list_pushBack(&umage->userList, user);printf("用户添加成功!\n");}void userManage_modify(UserManage* umage){}void userManage_remove(UserManage* umage){}void userManage_show(UserManage* umage){list_transfrom(&umage->userList, user_print);}void userManage_modifyPassword(UserManage* umage){}

将数据存储

User.c

#include "User.h"#include#includeUser* createEmptyUser(){User* user = calloc(1, sizeof(User));if (!user){printf("user create failed\n");return NULL;}return user;}User* user_fromString(const char* str){//创建一个空UserUser* user = createEmptyUser();//分割字符串int ret = sscanf(str, "%lu %s %d", &user->number, user->password, &user->type);if (ret number, user->password, user->type);}

最后加上

List.c

#include "List.h"#includevoid list_init(List* list){list->size = 0;list->front = list->tail = calloc(1, sizeof(Node));if (list->front == NULL){printf("head create failed\n");return;}}Node* createNode(void* val){Node* newNode = calloc(1, sizeof(Node));if (!newNode){printf("newNode create failed\n");return NULL;}newNode->data = val;return newNode;}void list_pushBack(List* list, void* val){Node* newNode = createNode(val);newNode->next = list->tail->next;list->tail->next = newNode;list->tail = newNode;list->size++;}void list_transfrom(List* list, LISTCALLBACK fun){Node* curNode = list->front->next;while (curNode){fun(curNode->data);curNode = curNode->next;}}

到这里我们源文件已经写到位了,接下来是头文件的引用

List.h

#pragma once//用户 读者 图书 借还信息有四种数据(如何定义一个链表,能够装所有的类型)//定义节点typedef struct Node{void* data;//万能指针,指向任意数据的地址 void*struct Node* next;//后继节点指针}Node;typedef struct List{int size;//节点个数Node* front;//头指针Node* tail;//尾指针}List;typedef void (*LISTCALLBACK)(void*);void list_init(List* list);void list_pushBack(List* list,void* val);void list_transfrom(List* list, LISTCALLBACK fun);

Menu.h

#ifndef _MENU_H_#define _MENU_H_//预处理指令,防止头文件重复包含#include//提示选择int select();//主菜单int mainMenu();//用户管理菜单int userMenu();#endif // !_MENU_H_

User.h

#pragma once/*@枚举用户类型*/enum UserType{UT_SysAdminUser = 1,//系统管理员Ut_BookAdminUser,//图书管理员Ut_CommonUser//普通用户};/*@用户*/typedef struct User{unsigned long number;//用户编号char password[20];//用户密码int type;//用户类型}User;User* createEmptyUser();User* user_fromString(const char* str);void user_print(User* user);

UserManage.h

#pragma once#include"List.h"#include"User.h"typedef struct UserManage{List userList;//存储所有用户}UserManage;void userManage_init(UserManage* umage);void userManage_loadData(UserManage* umage,const char* filename);//用户管理操作void userManage_operation(UserManage* umage);//用户信息输入void userManage_input(UserManage* umage);//用户信息修改void userManage_modify(UserManage* umage);//用户信息删除void userManage_remove(UserManage* umage);//用户信息显示void userManage_show(UserManage* umage);//用户密码修改void userManage_modifyPassword(UserManage* umage);

到这里这个系统已经完成了第一部分的功能,后续功能实现大家可以自己动手尝试一下哦!

进企鹅群【768440837】,为你的课设节省一点时间~

免费自学资源获取,可问问题,群友共同交流! 共同学习进步!

今天的分享到这里就结束啦,觉得此文对你有帮助的同学可以点赞支持一下~