小言_互联网的博客

图书管理程序(c语言实现)

204人阅读  评论(0)

使用c语言模仿图书管理系统

实现系统的初始化
定义程序中需要使用的数据结构
定义菜单内容
书籍的管理方法
关于成语的操作方法
main主程序
init.h
bookSys.h
menu.h
bookManage.h
stuManage.h

这里简述一下自己在写这个程序的时候的一些感悟吧,首先就是对于文件的读写操作一定要熟练,不要害怕,还有就是在动手写一个程序之前一定要明确好自己的思路,一定要事先将自己的想法写下来,然后一步一步的进行调试,编写。
总结一下这个程序中需要的知识点:

  • 对于头文件定义的了解和使用,首先在定义头文件的时候首先加上预处理判断语句,用来防止一个头文件被多次引用。

  • 对于结构指针的使用,通过使用结构指针可以大大减少自己对于文件的读写操作。这也是自己感觉刚开始的时候所欠缺的。

  • 对于文件操作的一些自己的感想

  • 在进行数据的读取的时候,可以将文件中的数据进行读取到一个全局变量中,一般使用的是一个指针,因为指针可以更方便进行操作,这样当程序需要文件中的数据的时候就可以使用指针来获取数据,简化操作,同时使用指针的另外的好处是,当需要进行对数据进行修改的时候,可以直接修改,而不是每一次都需要写入文件,然后再从文件中读出。

下面是程序的源码:欢迎大家,一起交流指正,一起进步
链接: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;	//待删除数据的index
	FILE* 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 20

struct 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;	//待删除数据的index
	FILE* 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_

转载:https://blog.csdn.net/qq_42330141/article/details/102247326
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场