图书管理程序(c语言实现)
使用c语言模仿图书管理系统
这里简述一下自己在写这个程序的时候的一些感悟吧,首先就是对于文件的读写操作一定要熟练,不要害怕,还有就是在动手写一个程序之前一定要明确好自己的思路,一定要事先将自己的想法写下来,然后一步一步的进行调试,编写。
总结一下这个程序中需要的知识点:
对于头文件定义的了解和使用,首先在定义头文件的时候首先加上预处理判断语句,用来防止一个头文件被多次引用。
对于结构指针的使用,通过使用结构指针可以大大减少自己对于文件的读写操作。这也是自己感觉刚开始的时候所欠缺的。
对于文件操作的一些自己的感想
在进行数据的读取的时候,可以将文件中的数据进行读取到一个全局变量中,一般使用的是一个指针,因为指针可以更方便进行操作,这样当程序需要文件中的数据的时候就可以使用指针来获取数据,简化操作,同时使用指针的另外的好处是,当需要进行对数据进行修改的时候,可以直接修改,而不是每一次都需要写入文件,然后再从文件中读出。
下面是程序的源码:欢迎大家,一起交流指正,一起进步
链接:https://pan.baidu.com/s/1CjtFxi4LVpF_sOPVDtOHZQ
提取码:j5av
过期之后,请留言。
/*FILE: main.c-------------此程序实现的功能是模仿图书馆的工作,实现的功能具体如下1.对于普通用户来说:可以完成的功能1.注册账号 2.借书 3.还书 4.查找书籍2.设置了一个管理员(账号:haojie, 密码:111111)具有功能1.删除成员 2.删除书籍 3.添加书籍 4.查看所有书籍
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"bookSys.h"
#include"bookManage.h"
#include"stuManage.h"
#include"init.h"
#include"menu.h"int borrow();
int returnBook();int main(void) {//addBook();initBook();initStu();initMan();atexit(updateBook);atexit(updateStu);int mode;mode = menu1();switch (mode) {case 1: {int flag = administrator(); if (flag) {while (1) {int mode = menu3();switch (mode) {case 1:rmStu(); break;case 2:showAllStu(); break;case 3:addBook(); break;case 4:rmBook(); break;case 5:showAllBook(); break;case 6:exit(0); break;default: break;}updateBook();updateStu();initBook();initStu();}}}break;case 2: {char name[SLEN];printf("enter you name: ");gets(name);stuNow = findStu(name);if (stuNow == NULL) {printf("没有此用户\n");exit(1);}{int mode;while (1) {mode = menu2();switch (mode) {case 1:showStu(stuNow); break;case 2:borrow(); break;case 3:returnBook(); break;case 4: {bookNow = findBook();if (bookNow == NULL) {printf("没有您所查找的书籍\n");}else {showBook();}}; break;case 5:exit(0); break;default: break;}}}; break;case 3:addStu(); break;case 4:exit(1); break;}return 0;}}int borrow() {bookNow = findBook();if (bookNow == NULL) {printf("没有此书籍\n");return 1;}if (bookNow->flag) {printf("sorry, 此书已被借出\n");return 1;}stuNow->bookList[stuNow->total++] = *bookNow;if (!(bookNow->left--))bookNow->flag = 1;//此时不用更新文件,因为这时候的数组中的信息//已经改变了,只需要在最后的退出的时候进行更新//updateBook();return 0;}int returnBook() {int position = -1;bookNow = findBook();for (int i = 0; i < stuNow->total; i++) {if (!strcmp(bookNow->name, stuNow->bookList[i].name)) {position = i;bookNow->flag = 0;break;}}if (position < 0) {printf("您所借的书籍中没有%s\n", bookNow->name);return 1;}if (position == 4) {stuNow->total--;updateStu();}else {for (int i = position; i < stuNow->total; i++) {stuNow->bookList[i] = stuNow->bookList[i + 1];stuNow->total--;}}return 0;
}
/*FILE: bookManag.c----------------作用关于书籍的操作函数
*/
#include<stdio.h>
#include<string.h>
#include"bookManage.h"void addBook() {book aBook;char ch;FILE* fp;fp = fopen_a();printf("please enter the name of the book: ");gets(aBook.name);printf("please enter the code of the book: ");gets(aBook.number);printf("please enter the total number of the book: ");scanf("%d", &(aBook.left));while ((ch = getchar()) != '\n')continue;printf("please enter the author of the book: ");gets(aBook.author);aBook.flag = 0;fwrite(&aBook, sizeof(book), 1, fp);fclose(fp);initBook();
}int rmBook() {char delName[SLEN];short index = -1; //待删除数据的indexFILE* fp;printf("please enter the name of the book: ");gets(delName);for (int i = 0; i < bookNum; i++) {if (!strcmp(delName, book_ptr[i].name)) {index = i;break;}}if (index < 0) {printf("此书籍不存在\n");return 1;}else {fp = fopen_w();for (int i = 0; i < bookNum; i++) {if (i == index)continue;fwrite(book_ptr + i, sizeof(book), 1, fp);}fclose(fp);initBook();printf("书籍已删除\n");}return 0;
}book* findBook() {char bookName[SLEN];printf("please enter name of the book that you want to find: ");gets(bookName);for (int i = 0; i < bookNum; i++) {if (!(strcmp(bookName, book_ptr[i].name))) {return book_ptr + i;}}return NULL;
}void showBook() {printf("%-10s%-10s%-10s%-10s%-10s\n", "编号", "书名", "作者", "是否可借阅", "剩余书籍数目");printf("%-10s%-10s%-10s%-10s%-10d\n", bookNow->number, bookNow->name, bookNow->author, bookNow->flag == 1 ? "否" : "是", bookNow->left);
}void showAllBook() {for (int i = 0; i < bookNum; i++) {bookNow = book_ptr + i;showBook();}
}
void updateBook() {FILE* fp;fp = fopen_w();fwrite(book_ptr, sizeof(book)*bookNum, 1, fp);fclose(fp);printf("信息更新完成\n");initStu();
}/*Function: fopen_w()-------------------Usage: FILE* fopen_w()
*/FILE* fopen_w() {FILE* fp;if ((fp = fopen(bookFile, "w")) == NULL) {printf("文件写入失败,退出系统\n");exit(1);}return fp;
}/*Function: fopen_r()-------------------Usage: FILE* fopen_r()
*/FILE* fopen_r() {FILE* fp;if ((fp = fopen(bookFile, "r")) == NULL) {printf("文件打开失败,退出系统\n");exit(1);}return fp;
}/*Function: fopen_a()-------------------Usage: FILE* fopen_a()
*/FILE* fopen_a() {FILE* fp;if ((fp = fopen(bookFile, "a")) == NULL) {printf("文件添加失败,退出系统\n");exit(1);}return fp;
}
#pragma once
/*FILE: bookManage.h-----------------关于书籍管理函数的头文件
*/#ifndef _BOOKMANAGE_
#define _BOOKMANAGE_
#include"bookSys.h"
#include"init.h"//添加书籍进入文件
void addBook();
//删除文件中的书籍记录
int rmBook();
//以指定格式显示书籍信息
void showBook();
//查找特定的书籍
book* findBook();
//更新书籍的是否可借阅标志
void updateBook();
//显示所有的书籍信息
void showAllBook();
//将数据写入book文件
FILE* fopen_w();
//将book文件数据读出
FILE* fopen_r();
//将book追加到文件中
FILE* fopen_a();#endif // !1
#pragma once
/*FILE: bookSys.h---------------定义book数据结构和stu数据结构
*/#ifndef _BOOKSYS_
#define _BOOKSYS_//定义最多可以借的书籍
#define B_MAX 5
//定义编号的最大长度
#define SLEN 20struct bookStruct {char number[SLEN]; //图书编号char name[SLEN]; //书名char author[SLEN]; //作者short left; //剩余书籍数目short flag; //书籍是否借出标志位
};typedef struct bookStruct book;struct student {long number; //成员的编号,新添加的成员自动进行加一char name[SLEN];book bookList[B_MAX];short total;
};struct manager {char name[SLEN];char key[SLEN];
};typedef struct manager man;
typedef struct student stu;#endif // !_BOOKSYS_
/*FILE: init.c
*/
#include<stdio.h>
#include"init.h"/*Function: init()----------------对系统进行初始化,读取文件内容
*/int initBook() {FILE* fp;book book1;long position;if ((fp = fopen(bookFile, "r")) == NULL) {fp = fopen_w();printf("文件已经新建\n");}else {fclose(fp);printf("文件读入完成\n");/*addBook(&book1);fp = fopen_w();fwrite(&book1, sizeof(book), 1, fp);fclose(fp);*/fp = fopen_r();fseek(fp, 0, SEEK_END);position = ftell(fp);fseek(fp, 0, SEEK_SET);bookNum = position / sizeof(book);book_ptr = (book*)malloc(sizeof(book) * bookNum);if (book_ptr == NULL) {printf("获取空间失败\n");exit(2);}fread(book_ptr, sizeof(book), bookNum, fp);fclose(fp);}return 0;
}int initStu() {FILE* fp;stu stu1;long position;if ((fp = fopen(stuFile, "r")) == NULL) {fp = fopen_wStu();printf("文件已经新建\n");}else {fclose(fp);printf("文件读入完成\n");/*addBook(&book1);fp = fopen_w();fwrite(&book1, sizeof(book), 1, fp);fclose(fp);*/fp = fopen_rStu();fseek(fp, 0, SEEK_END);position = ftell(fp);fseek(fp, 0, SEEK_SET);stuNum = position / sizeof(stu);//将文件中的数据成员读取到数组中stu_ptr = (stu*)malloc(sizeof(stu) * stuNum);if (stu_ptr == NULL) {printf("获取空间失败\n");exit(2);}fread(stu_ptr, sizeof(stu), stuNum, fp);fclose(fp);}return 0;
}void initMan() {FILE* fp;man man1 = {"haojie","111111"};fp = fopen(manageFile, "r");if (fp == NULL) {fp = fopen(manageFile, "w");printf("文件已经新建\n");fwrite(&man1, sizeof(man), 1, fp);}fclose(fp);fp = fopen(manageFile, "r");manNow = (man*)malloc(sizeof(man));fread(manNow, sizeof(man), 1, fp);fclose(fp);
}
#pragma once
/*FILE: init.h------------完成图书系统的初始化工作,进行对全局变量的初始化操作使用book_ptr和stu_ptr指向文件中的数据。
*/#ifndef _INIT_
#define _INIT_
#include"bookSys.h"//定义文件名的最大长度
#define FLEN 20
//book.dat中含有的书籍数目
long bookNum;
//stu.dat中含有的学生数目
long stuNum;//指向stu的指针
stu* stu_ptr;
//指向book的指针
book* book_ptr;//当前用户对象的指针
stu* stuNow;
//当前需要操作的特定书籍的指针
book* bookNow;
//管理员指针
man* manNow;#define bookFile "book.dat" //书籍数据文件的名称
#define stuFile "stu.dat" //成员数据文件的名称
#define manageFile "man.dat" //管理员文件名称
int initBook();
int initStu();
void initMan();
#endif // _INIT_
/*FILE: menu.c------------作用显示系统的界面,包括管理员界面和普通界面
*/
#include"menu.h"
#include<string.h>
int menu1() {static short flag = 0;int mode = 1;char ch;if (!flag++)printf("Welcome to this bookSystem!!!\n");printf("please enter the number of the options: \n");printf("\t1. 管理员登录\n");printf("\t2. 普通用户登录\n");printf("\t3. 新用户注册\n");printf("\t4. 退出\n");scanf("%d", &mode);while ((ch = getchar()) != '\n')continue;return mode;
}int administrator() {char name[SLEN];char key[SLEN];printf("please enter the name of manager: ");gets(name);printf("please enter the key of manager: ");gets(key);if (!(strcmp(name, manNow->name)) && !(strcmp(key, manNow->key))) {printf("欢迎管理员登录\n");return 1;}else {printf("账号或者密码错误\n");}return 0;
}int menu2() {int mode = 1;char ch;printf("please enter the number of the options: \n");printf("\t1. 显示当前用户信息\n");printf("\t2. 借书\n");printf("\t3. 还书\n");printf("\t4. 查找书籍\n");printf("\t5. 退出\n");scanf("%d", &mode);while ((ch = getchar()) != '\n')continue;return mode;
}int menu3() {int mode = 1;char ch;printf("please enter the number of the options: \n");printf("\t1. 删除成员\n");printf("\t2. 显示所有成员\n");printf("\t3. 添加书籍\n");printf("\t4. 删除书籍\n");printf("\t5. 显示所有书籍\n");printf("\t6. 退出\n");scanf("%d", &mode);while ((ch = getchar()) != '\n')continue;return mode;
}void showStatus() {printf("您的信息为:\n");printf("\t%-10s%-10s\n", "姓名", "编号");printf("\t%-10s%-10s\n", stuNow->name, stuNow->number);
}
#pragma once
/*FILE: menu.h-------------提供关于页面显示的功能函数
*/#ifndef _menu_
#define _menu_
#include<stdio.h>
#include"init.h"
int menu1();
int administrator();
int menu2();
int menu3();
void showStatus();
#endif // !_menu_
/*FILE: stuManag.c
*/#include<stdio.h>
#include"stuManage.h"int addStu() {stu stu1;FILE* fp;fp = fopen_aStu();printf("please enter the name of the student: ");gets(stu1.name);stu1.total = 0;stu1.number = stuNum++;fwrite(&stu1, sizeof(stu), 1, fp);fclose(fp);initStu();
}int rmStu() {char delName[SLEN];short index = -1; //待删除数据的indexFILE* fp;printf("please enter the name of the student: ");gets(delName);for (int i = 0; i < stuNum; i++) {if (!strcmp(delName, stu_ptr[i].name)) {index = i;break;}}if (index < 0) {printf("此用户不存在\n");return 1;}else {fp = fopen_wStu();for (int i = 0; i < stuNum; i++) {if (i == index)continue;fwrite(stu_ptr + i, sizeof(stu), 1, fp);}fclose(fp);initStu();printf("用户已删除\n");}return 0;
}/*Function: findStu()------------------Usage: findStu(char* number)
*/
stu* findStu(char* name) {for (int i = 0; i < stuNum; i++) {if (!strcmp(stu_ptr[i].name, name)) {return stu_ptr + i;}}return NULL;
}void updateStu() {FILE* fp;fp = fopen_wStu();fwrite(stu_ptr, sizeof(stu)* stuNum, 1, fp);fclose(fp);printf("信息更新完成");initStu();
}void showStu(stu* stu1) {printf("%-20s%-20s%-20s\n", "编号", "姓名", "所借数目");printf("%-20d%-20s%-20d\n", stu1->number, stu1->name, stu1->total);printf("所借书籍列表:");for (int i = 0; i < stu1->total; i++) {printf("%s\t", stu1->bookList[i].name);}printf("\n");
}void showAllStu() {for (int i = 0; i < stuNum; i++)showStu(stu_ptr + i);
}
/*Function: fopen_w()-------------------Usage: FILE* fopen_w()
*/FILE* fopen_wStu() {FILE* fp;if ((fp = fopen(stuFile, "w")) == NULL) {printf("文件写入失败,退出系统");exit(1);}return fp;
}/*Function: fopen_r()-------------------Usage: FILE* fopen_r()
*/FILE* fopen_rStu() {FILE* fp;if ((fp = fopen(stuFile, "r")) == NULL) {printf("文件打开失败,退出系统");exit(1);}return fp;
}/*Function: fopen_a()-------------------Usage: FILE* fopen_a()
*/FILE* fopen_aStu() {FILE* fp;if ((fp = fopen(stuFile, "a")) == NULL) {printf("文件添加失败,退出系统");exit(1);}return fp;
}
#pragma once
/*FILE: stuManage.h-----------------定义一系列关于对于成员进行操作的方法
*/#ifndef _STUMANAGE_
#define _STUMANAGE_
#include"bookSys.h"
#include"init.h"//添加学生进入数据文件
int addStu();
//将文件中的学生信息删除
int rmStu();
//根据学号查找当前用户
stu* findStu(char* number);
//更新信息
void updateStu();
//以指定格式显示信息
void showStu(stu* stu1);
//以指定格式显示所有信息
void showAllStu();FILE* fopen_rStu();
FILE* fopen_wStu();
FILE* fopen_aStu();
#endif // !_stuManag_
图书管理程序(c语言实现)相关推荐
- c语言学生综合测评系统_c语言图书管理系统_c语言学生综合测评系统_c语言个人账本管理系统...
c语言销售管理系统设计 c语言设计管理系统,c图书管理系统设计,学生管理系统c语言,职工管理系统c语言,c语言图书管理系统,c语言成绩管理系统,c语言管理系统,c语言银行管理系统,c语言工资管理系统, ...
- 课程设计图书管理系统c语言,图书管理系统c语言课程设计c语言课程设计作业获得95分全院第一......
> 下载中心 > > 图书管理系统 c语言课程设计 english version 图书管理系统 c语言课程设计 所属分类: 开发工具:c/c++ 文件大小:2720kb 下载 ...
- 99年毕业设计获优的程序-图书管理程序
这是99年做毕业设计时,用BC3.2做的图书管理程序(当年毕业设计获优,还不错),DOS平台,通过DOS33号中断可以支持鼠标操作,图形界面(仿Window95),不过对菜单部分由于当时技术有限,仅能 ...
- 数据结构c语言编程程序,图书管理程序(数据结构c语言实现增删改查)
本人用C语言编写的第一个完整小程序实现图书的借阅管理,不完美之处欢迎交流! PS:我很菜.QQ:997459445 #include #include #include #include #inclu ...
- c语言课设学生管理程序,c语言程序课程设计学生成绩管理程序.doc
c语言程序课程设计学生成绩管理程序 武汉科技大学 二〇一〇~二〇一一学年第一学期 信息科学与工程学院 课程设计报告书 课程名称: C语言课程设计 班 级: 自动化2009级2班 学 号: 姓 名: 指 ...
- 餐厅管理程序c语言源代码,课内资源 - 基于C++的餐厅管理程序的设计与实现
摘 要 随着计算机应用技术的快速发展和日益普及,网络也遍及到我们生活的每个角落,为我们的学习和工作带来极大的方便.很多人都使用过传统的文字,纸张管理手段,与之不同的另外一种管理方式就是利用电脑程序管理 ...
- 图书管理系统C语言程序设计课程,vs c语言图书管理平台课程设计_图书管理平台c语言程序设计_c语言课程设计 图书管理系统...
#include #include #include #include //#include #define MAX 100 struct Student { int num;/*书代号*/ char ...
- 修改图书信息c语言,图书信息管理系统c语言.doc
图书信息管理系统c语言.doc 沈阳航空航天大学理学院 C语言程序设计课程设计报告 题目: 图书信息管理系统 专 业: 信息与计算科学 班 级: 学 号: 学生姓名: 指导教师: 2010年 7月 9 ...
- C语言电话簿程序设计,2010电话簿管理程序-c语言程序设计-毕业论文.doc
沈阳航空航天大学理学院 C语言程序设计课程设计报告 题目: 电话簿管理程序 专 业: 班 级: 学 号: 学生姓名: 指导教师: 2010年 7月 9日 课程设计成绩评定表 姓 名学 号 评 语 成 ...
最新文章
- 自增主键为什么不连续_没关紧的水龙头为什么滴水不连续呢?
- PostgreSql 功能和操作
- java websocket修改为同步_初级Java程序员需要掌握哪些主流技术才能拿25K?
- boost::mp11::integer_sequence相关用法的测试程序
- LinkdedList
- oracle alter_log,在线查看alter.log文件内容
- 一个快速生成元素背景的 React 组件
- HDU2010 水仙花数【进制+入门】
- 2020年5月机器视觉工作阶段性总结
- 2019b-MATLAB-中文界面设置
- 两个平面的位置关系和判定方程组解_精品获奖教案 1.2.4平面与平面的位置关系(2)教案 苏教版必修2...
- Spyder单步调试
- SAM-BA和AT91SAM9260连接问题
- 计算机类专业毕业论文文献综述,计算机类专业毕业论文参考文献大全
- 奥斯汀大学计算机专业怎么样,德克萨斯大学奥斯汀分校计算机工程排名
- python 生成器、列表/字典/集合解析式区别
- 如何设置Ubuntu键盘输入法框架为fcitx
- OpenStack部署(图文详解)
- FL Studio 20音乐制作教程
- 微信的红包算法 了解一下两种就可以了