数据结构课程设计图书管理系统,C语言版。


一、功能描述

      设计一个图书管理程序满足图书馆基本业务需求。


二、设计要求

  1. 每种书的登记内容包括书号、书名、著作者、现存量和库存量等;
  2. 对书号建立索引表(线性表)以提高查找效率;
  3. 实现图书管理系统的主要功能描述。
  4. 用数据文件存储。

三、实现的功能

对图书信息进行增、删、改、差查询所有
运行结果大概就是这个样子:
在这里插入图片描述


四、代码

Tip:主要运用的是链表和文件的读写,注释很详细。
#include<stdio.h>
#include<stdlib.h>
#include "book.h"
#define len 20
#define size 20

/*
	1、建立一个链表
	2、将.dat文件中读出的数据放入链表中
	3、进行业务操作
	4、业务操作后将链表中数据存回.dat文件中
	*/



//定义一个图书类型
struct oneBook
{
	char isbn[size];
	char bookname[size];
	char author[size];
	int stocknum;
	float price;
	int flag;
}bookarr[len];

//定义链表
typedef struct Book
{
	char isbn[size];
	char bookname[size];
	char author[size];
	int stocknum;
	float price;
	int flag;
	struct Book* next;
} ListBook;


//定义一个输入字符,判断使用者需要的功能
char inNeed;


//编写程序首页方法
void indexPage() {
	//首先写出程序的首页面
	printf("|***********************************************************|\n");
	printf("|---------welcome to the Library Management System----------|\n");
	printf("|-------------------添加图书信息请输入1---------------------|\n");
	printf("|-------------------删除图书信息请输入2---------------------|\n");
	printf("|-------------------修改图书信息请输入3---------------------|\n");
	printf("|-------------------查询图书信息请输入4---------------------|\n");
	printf("|-------------------查询书目信息请输入5---------------------|\n");
	printf("|-----------------退出图书管理系统请输入6-------------------|\n");
	printf("|***********************************************************|\n");
	printf("请输入要进行的操作:");
}

//编写读取文件内容的方法  ,每次用程序之前将数据从文件中读取,放入链表中
ListBook* readFile(ListBook *head) {
	//定义一个指向文件的指针
	FILE* fp;
	char content;
	ListBook* temp,*book;
	//这里的r是只读的意思
	if ((fp = fopen("file1.dat", "r")) == NULL) {
		//打开文件失败
		printf("cannot open this file\n");
		exit(0);
	}
	//将文件中数据拿出来,方进书的结构体数组中
	for (int i = 0; i < len; i++) {
		fread(&bookarr[i], sizeof(struct oneBook), 1, fp);
		//加一个判断跳出循环
		if (bookarr[i].flag == 0) {
			break;
		}

		//把结构体数据方进链表,success,用的是头插法
		if (head == NULL) {
			book = (ListBook*)malloc(sizeof(ListBook));
			//字符串赋值给字符数组用strncpy
			strncpy(book->isbn, bookarr[i].isbn, size);
			strncpy(book->bookname, bookarr[i].bookname, size);
			strncpy(book->author, bookarr[i].author, size);
			book->stocknum = bookarr[i].stocknum;
			book->price = bookarr[i].price;
			book->flag = bookarr[i].flag;
			head = book;;
			head->next = NULL;
		}
		else {
			temp = head;
			book = (ListBook*)malloc(sizeof(ListBook));
			strncpy(book->isbn, bookarr[i].isbn, size);
			strncpy(book->bookname, bookarr[i].bookname, size);
			strncpy(book->author, bookarr[i].author, size);
			book->stocknum = bookarr[i].stocknum;
			book->price = bookarr[i].price;
			book->flag = bookarr[i].flag;
			head = book;
			head->next = temp;
		}
		
	}
	//关闭文件输入流
	fclose(fp);
	return head;
	
}

//编写   添加图书信息方法   
ListBook* insertMessage(ListBook* head) {
	printf("输入想要录入的图书信息,并以#结束:\n");
	printf("    *录入的模板为:ISBN码 书名 著作者 库存量 金额\n");
	ListBook* book,*temp;
	//把添加的数据放入链表,用的是头插法
	for (int i = 0; i < len; i++) {
		if (head == NULL) {
			book = (ListBook*)malloc(sizeof(ListBook));
			scanf("%s %s %s %d %f", &book->isbn, &book->bookname, &book->author, &book->stocknum, &book->price);
			book->flag = 1;
			head = book;;
			head->next = NULL;
			if (getchar() == '#') {
				break;
			}
		}
		else {
			temp = head;
			book = (ListBook*)malloc(sizeof(ListBook));
			scanf("%s %s %s %d %f", &book->isbn, &book->bookname, &book->author, &book->stocknum, &book->price);
			book->flag = 1;
			head = book;
			head->next = temp;
			if (getchar() == '#') {
				break;
			}
		}
		
	}

	//打印链表,没啥用
	printf("这是刚刚添加的数据\n");
	temp = head;
	while (temp != NULL) {
		printf("isbn:%s\t", temp->isbn);
		printf("bookname:%s\t", temp->bookname);
		printf("author:%s\t", temp->author);
		printf("stocknum:%d\t", temp->stocknum);
		printf("price:%.2f\t", temp->price);
		printf("flag:%d\n", temp->flag);
		temp = temp->next;
	}
	printf("\n添加成功!\n");
	return head;
}

//编写储存数据方法,   每次程序使用结束,将数据存入文件中
void saveFile(ListBook *book) {
	//先将链表中数据放回结构体数组,成功
	int i = 0;
	while (book != NULL) {
		strncpy(bookarr[i].isbn, book->isbn, size);
		strncpy(bookarr[i].bookname, book->bookname, size);
		strncpy(bookarr[i].author, book->author, size);
		bookarr[i].stocknum = book->stocknum;
		bookarr[i].price = book->price;
		bookarr[i].flag = book->flag;
		book = book->next;
		i++;
	}

	//重新写入文件
	FILE* fp;
	if ((fp = fopen("file1.dat", "wb")) == NULL) {
		printf("cannot open file\n");
		return;
	}
	for (int j = 0; j < i; j++) {
		if (fwrite(&bookarr[j], sizeof(struct oneBook), 1, fp) != 1) {
			printf("file write error\n");
		}
	}
	fclose(fp);	
	printf("~程序已退出,欢迎下次使用~");
	return;
}

//编写查询所有图书信息的方法
void selectAll(ListBook* book) {
	ListBook* print;
	print = book;
	while (print != NULL) {
		printf("isbn:%s\t", print->isbn);
		printf("bookname:%s\t", print->bookname);
		printf("author:%s\t", print->author);
		printf("stocknum:%d\t", print->stocknum);
		printf("price:%.2f\n", print->price);
		print = print->next;
	}
}

//编写删除图书信息方法
ListBook* deleteMessage(ListBook* head,char isbn[size]) {
	ListBook* temp, * pre,*book;
	book = temp = head;
	pre = head->next;
	while (head != NULL) {
		//在头上
		if (0 == strcmp(head->isbn, isbn)) {
			head = head->next;
			book = head;
			free(temp);
			printf("删除成功\n");
			break;
		}
		//如果链表中只有一个值,但是不是要找的值,pre指针的strcmp会空指针
		if (head->next == NULL) {
			printf("没有isbn码为%s的图书\n", isbn);
			break;
		}
		//不在头上
		if (0 == strcmp(pre->isbn, isbn)) {
			temp = pre;
			head->next = pre->next;
			free(temp);
			printf("删除成功\n");
			break;
		}
		pre = pre->next;
		head = head->next;
		if (head == NULL || pre == NULL) {
			printf("没有isbn码为%s的图书\n", isbn);
		}
	}
	return book;
}

//编写修改图书信息的方法
ListBook* updataMessage(ListBook* head,char isbn[size]) {
	ListBook* temp;
	temp = head;
	//首先把要修改的图书找出来
	while (head != NULL) {
		if (0 == strcmp(head->isbn, isbn)) {
			printf("请输入要修改的值:");
			scanf("%s %s %s %d %f", &head->isbn, &head->bookname, &head->author, &head->stocknum, &head->price);
			printf("修改成功");
			break;
		}
		head = head->next;
		if (head == NULL) {
			printf("没有isbn码为:%s的图书\n", isbn);
		}
	}
	return temp;
}

//编写查询图书信息方法
void selectOne(ListBook* head,char isbn[size]) {
	ListBook* temp;
	temp = head;
	while (temp != NULL) {
		//printf("打印:%s\n", temp->isbn);
		if (0 == strcmp(temp->isbn, isbn)) {
			printf("查询结果为:\n");
			printf("isbn:%s\t", temp->isbn);
			printf("bookname:%s\t", temp->bookname);
			printf("author:%s\t", temp->author);
			printf("stocknum:%d\t", temp->stocknum);
			printf("price:%.2f\n", temp->price);
			break;
		}
		temp = temp->next;
		if (temp == NULL) {
			printf("没有isbn码为:%s的图书\n", isbn);
		}
	}

}

void main() {
	//定义一个链表
	ListBook* head=NULL;
	//调用程序首页方法
	indexPage();
	//调用读取文件内容的方法
	head = readFile(head);
	inNeed = getchar();
	//编写一个大循环,使程序一直跑下去
	while (inNeed!='6') {
		if (inNeed == '1') {
			head = insertMessage(head);
			//输入下一步操作
			printf("请输入下一步操作:\n");
			//用来接收最后输入的回车符
			inNeed = getchar();
			//接收下一步操作的信号
			inNeed = getchar();
		}
		else if (inNeed == '2') {
			printf("请输入要删除图书的ISBN码:\n");
			//调用删除方法,需要输入要删除图书的isbn
			char isbn[size];
			scanf("%s", isbn);
			head = deleteMessage(head,isbn);
			//输入下一步操作
			printf("请输入下一步操作:\n");
			//用来接收最后输入的回车符
			inNeed = getchar();
			//接收下一步操作的信号
			inNeed = getchar();
		}
		else if (inNeed == '3') {
			printf("请输入要修改的图书的ISBN码:\n");
			//调用删除方法,需要输入要删除图书的isbn
			char isbn[size];
			scanf("%s", isbn);
			head = updataMessage(head, isbn);
			//输入下一步操作
			printf("请输入下一步操作:\n");
			//用来接收最后输入的回车符
			inNeed = getchar();
			//接收下一步操作的信号
			inNeed = getchar();
		}
		else if (inNeed == '4') {
			printf("请输入要查询的图书的ISBN码:\n");
			//调用删除方法,需要输入要删除图书的isbn
			char isbn[size];
			scanf("%s", isbn);
			selectOne(head, isbn);
			//输入下一步操作
			printf("请输入下一步操作:\n");
			//用来接收最后输入的回车符
			inNeed = getchar();
			//接收下一步操作的信号
			inNeed = getchar();
		}
		else if(inNeed == '5'){
			printf("查询书目信息\n");
			//编写查询书目方法
			selectAll(head);
			//输入下一步操作
			printf("请输入下一步操作:\n");
			//用来接收最后输入的回车符
			inNeed = getchar();
			//接收下一步操作的信号
			inNeed = getchar();
		}
	}
	//调用存数据方法,结束程序	
	saveFile(head);
	return;
}

不知道是因为我自己的电脑还是vs什么的原因,第一次运行特别慢 卡,但是运行成功之后,下次运行就飞快。希望对做这个课设的小伙伴有帮助吧。
欢迎指导纠错~
  • 15
    点赞
  • 108
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
题目采自 《数据结构题集》(c语言)(严蔚敏`吴伟民)6.3 图书管理,最大的难度在于要求用B树对书进行索引。 设计语言:C语言 编译环境: VC++6.0 里面包含完整的源程序和报告文档,程序为dos界面,有彩色菜单,对数据显示实现格化……本课程设计成绩为优秀。 源程序有注释,报告文档完全按要求,包括所用数据结构的描述与实现、算法的时空分析等都包括在内。 程序所能达到的功能 1图书采编入库(用B树对书建立索引) 2清除库存 3图书借阅 4图书归还 5图书预约 6列出某著者全部著作名 7列出某种书的状态(包括图书基本信息和该书借者名单、 预约者名单) 8每次插入或删除一个关键字后以凹入显示B树的状态 9把一次会话过程中的全部人机对话记录入一个日志文件中 10在程序主界面显示当前系统时间 一、 需求分析 1. 书和借阅证、库存量、出年份用整型示;书名用20位字符型数组,著者和借阅者姓名用30位字符型数组示;图书价格用浮点型示。图书入库时输入图书的书、书名、著者、总量等完整信息,清除库存时输入图书书,借阅和归还时输入书和借阅者证,姓名等信息,并记录系统时间为借书日期。 2. 借书和归还时显示图书的信息。插入、删除后用凹入显示以书建立的B树状态。查看图书状态,以格显示图书的基本信息,借阅者名单和预约者名单。 ……………… 课程设计清单: base.h //全程常量、全局变量和公共函数等 btree.h //B树类型单元 library.h //书库类型单元 main.cpp //主程序 bookiofo.dat //图书信息文件 borrower.dat //借书者姓名文件 bespeaker.dat //预约者姓名文件 main.exe //编译得到的可执行文件 数据结构课程设计实验报告-图书管理.doc
以下是使用C语言数据结构设计图书管理系统的简要介绍: 该图书管理系统基于单链存储结构,适用于大一、大二需要进行《数据结构》、《C语言程序设计》等课程设计和实训的同学。代码中有详细注释,逻辑非常清晰。如果已经安装了CodeBlocks 13及以后的本,可以直接运行。 该图书管理系统具有以下功能: 1. 添加图书:可以添加新的图书到系统中,包括图书的编、名称、作者等信息。 2. 删除图书:可以根据图书的编或名称删除系统中的图书。 3. 修改图书信息:可以修改系统中图书的信息,包括图书的名称、作者等。 4. 查询图书:可以根据图书的编或名称查询系统中的图书信息。 5. 显示所有图书:可以显示系统中所有图书的信息。 6. 保存和加载数据:可以将系统中的图书信息保存到文件中,并在需要时加载数据。 以下是一个简单的示例代码,用于演示如何使用C语言数据结构设计图书管理系统: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义图书结构体 typedef struct Book { int id; char name[100]; char author[100]; struct Book* next; } Book; // 全局变量,指向链头节点 Book* head = NULL; // 添加图书 void addBook(int id, char name[], char author[]) { Book* newBook = (Book*)malloc(sizeof(Book)); newBook->id = id; strcpy(newBook->name, name); strcpy(newBook->author, author); newBook->next = NULL; if (head == NULL) { head = newBook; } else { Book* current = head; while (current->next != NULL) { current = current->next; } current->next = newBook; } } // 删除图书 void deleteBook(int id) { if (head == NULL) { printf("图书管理系统为空\n"); return; } Book* current = head; Book* previous = NULL; while (current != NULL) { if (current->id == id) { if (previous == NULL) { head = current->next; } else { previous->next = current->next; } free(current); printf("成功删除图书\n"); return; } previous = current; current = current->next; } printf("未找到指定图书\n"); } // 修改图书信息 void modifyBook(int id, char name[], char author[]) { if (head == NULL) { printf("图书管理系统为空\n"); return; } Book* current = head; while (current != NULL) { if (current->id == id) { strcpy(current->name, name); strcpy(current->author, author); printf("成功修改图书信息\n"); return; } current = current->next; } printf("未找到指定图书\n"); } // 查询图书 void searchBook(int id) { if (head == NULL) { printf("图书管理系统为空\n"); return; } Book* current = head; while (current != NULL) { if (current->id == id) { printf("图书编:%d\n", current->id); printf("图书名称:%s\n", current->name); printf("图书作者:%s\n", current->author); return; } current = current->next; } printf("未找到指定图书\n"); } // 显示所有图书 void displayAllBooks() { if (head == NULL) { printf("图书管理系统为空\n"); return; } Book* current = head; while (current != NULL) { printf("图书编:%d\n", current->id); printf("图书名称:%s\n", current->name); printf("图书作者:%s\n", current->author); printf("\n"); current = current->next; } } int main() { // 添加图书示例 addBook(1, "图书1", "作者1"); addBook(2, "图书2", "作者2"); addBook(3, "图书3", "作者3"); // 显示所有图书示例 displayAllBooks(); // 删除图书示例 deleteBook(2); // 修改图书信息示例 modifyBook(1, "新图书1", "新作者1"); // 查询图书示例 searchBook(1); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值