栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

基于单链表的图书管理系统(C语言)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于单链表的图书管理系统(C语言)

GCC校友交作业记得改一下我还没交!!!珞珞珞 Visual Studio编译环境 功能:

            0、退出。
            1、基于单链表存储结构的图书信息表的创建和输出。
            2、基于单链表存储结构的图书信息表的新图书的入库。
            3、基于单链表存储结构的图书信息表的旧图书的出库。
            4、基于单链表存储结构的图书信息表按书号查找。
            5、基于单链表存储结构的图书信息表按价格区间查找。
            6、基于单链表存储结构的按图书价格升序排序。
            7、基于单链表存储结构的按图书价格修改。
            8、基于单链表存储结构的按图书价格普调。
            9、基于单链表存储结构的按最贵图书查找。
            10、基于单链表存储结构的图书去重。
            11、基于单链表存储结构的图书遍历。
            12、更新写入文本


Dec C++和VS编译器创建项目都要创建C++项目:

 VS:
VS2019如何创建C++项目?_Gemini-zero的博客-CSDN博客_vs2019怎么创建c++项目打开VS2019,选择空项目后,点击下一步(如果没有找到空项目,说明你下载的时候没勾选C++模块)输入项目名,更改项目存放路径后,选择下一步选择C++文件,修改程序名称,点击添加找到“解决方案资源管理器”,右键点击源文件,选择添加新建项此时可以开始编写C++程序了...https://blog.csdn.net/qq_44364832/article/details/105820626

Dec c++: https://jingyan.baidu.com/article/fd8044fae80db55031137a07.htmlhttps://jingyan.baidu.com/article/fd8044fae80db55031137a07.html


这是我vs的框架


 创建头文件 struct.h 定义数据类型和存储结构

#ifndef __STRUCH_H__
#define __STRUCH_H__

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型

struct Book {
	char id[15];//ISBN
	char name[20];//书名
	double price;//定价
};
typedef struct LNode {
	Book data; //结点的数据域
	struct LNode* next; //结点的指针域,存储后继节点位置 
} LNode, *linkList; //linkList为指向结构体LNode的指针类型

#endif

创建 function_declare.h 头文件把所有的方法函数声明

#pragma once
#ifndef __FUNCTION_H__
#define __FUNCTION_H__

//初始化 
Status InitList_L(linkList& L, bool& staMain);
Status Updata_books(linkList& L);
Status Read_books(linkList& L);
Status Traverse(linkList L);
Status Cr_List(linkList& L);
Status Sch_List(linkList& L);
Status Cz_List(linkList L);
Status SearchBookPriceRange(linkList L);
Status PriceAscendingSort(linkList& L);
Status ReviseAccordingBookPrice(linkList& L);
Status PriceIncrease(linkList& L);
Status SearchBookPriceBest(linkList& L);
Status DeleteDuplicateBooks(linkList& L);
#endif

创建 file_operation.cpp 源文件读取本程序文件夹目录中名为book的txt文本文件

#include
#include
#include "struct.h"

Status Read_books(linkList& L) {

	LNode* r;
	r = L;
	FILE* fp;
	if ((fp = fopen("book.txt", "r")) == NULL) {
		puts("Fail to open file!an");
		return ERROR;
	}

	while (!feof(fp)) {
		LNode* p = new LNode;
		fscanf(fp, "%s%s%lfn", p->data.id, p->data.name, &p->data.price);
		p->next = NULL;
		r->next = p;
		r = p;
	}
	r->next = NULL;
	fclose(fp);
	return OK;
}

//修改txt文件
Status Updata_books(linkList& L) {
	linkList p = L;
	FILE* fp;
	if ((fp = fopen("book.txt", "w+")) == NULL) {
		puts("Fail to open file!an");
		return ERROR;
	}

	//数据写入文件
	rewind(fp);

	while (p->next != NULL) {
		p = p->next;
		fprintf(fp, "%s %s %.2lfn", p->data.id, p->data.name, p->data.price);
	}
	fclose(fp);
	printf("Text update succeeded!n");
}

创建 books_operation.cpp 源文件编写系统的功能

#include
#include
#include "struct.h"
#include 
#include "function_declare.h"

//初始化状态
bool sta = false;

//11、遍历
Status Traverse(linkList L) {
	linkList p = L;
	int sum = 0;
	printf("The book information is as follows:n");
	while (p->next != NULL) {
		p = p->next;
		printf("%s %s %.2f元n", p->data.id, p->data.name, p->data.price);
	}
	return OK;
}

//1、初始化 
Status InitList_L(linkList& L,bool &staMain) { //算法2.6 单链表的初始化
	//构造一个空的单链表L
	if (sta) {
		printf("ERROR:Do not repeat initialization!an");
		return ERROR;
	}
	L = new LNode; //生成新结点作为头结点,用头指针L指向头结点
	L->next = NULL; //头结点的指针域置空
	Read_books(L);
	printf("Create table succeeded complete.n");
	Traverse(L);
	sta = true;
	staMain = true;
	return OK;
}

//2、基于链表存储结构的图书信息表的新图书的入库。
Status Cr_List(linkList& L) {
	Book e;
	int i;
	linkList p, s;
	p = L;
	if (!sta) {
		printf("ERROR:The linked list is not initialized!an");
		return ERROR;
	}
	int j = 0;                          //判断是否有头结点,无头节点则创建失败。
	printf("Please enter the position you want to insert:");
	scanf("%d", &i);
	while (p && (j < i - 1)) {
		p = p->next;
		++j;
	}
	if (!p || j > i - 1) {
		printf("ERROR:On failure!an");
		return ERROR;
	}
	printf("Isbn:");
	scanf("%s", &e.id);
	printf("title:");
	scanf("%s", &e.name);
	printf("price:");
	scanf("%lf", &e.price);
	while (p && (j < i - 1)) {
		p = p->next;
		++j;
	}
	if (!p || j > i - 1) {
		printf("ERROR:Incorrect insertion position!an");
		return ERROR;
	}
	s = new LNode;
	//s = (linkList)malloc(sizeof(Book));//插入时先新建头结点
	s->next = NULL;                 //定义p=L用p代替L进行指针遍历,防止丢失头结点
	s->data = e;
	s->next = p->next;
	p->next = s;
	Traverse(L);
	printf("On a successful!n");
	return OK;
}

//3、基于链表存储结构的图书信息表的旧图书的出库。
Status Sch_List(linkList& L) {
	if (!sta) {
		printf("ERROR::The linked list is not initialized!an");
		return ERROR;
	}
	if (L->next == NULL) {
		printf("ERROR:The list is empty!na");
		return ERROR;
	}
	char name[15];
	linkList q = L;
	linkList p = L->next;
	printf("Enter the isbn number to delete:n");
	scanf("%s", &name);
	while (p) {
		if (!strcmp(name, p->data.id)) {
			q->next = p->next;
			free(p);
			p = q->next;
			Traverse(L);
			printf("The successful!n");
			return OK;
		}
		q = p;
		p = p->next;
	}
	printf("ERROR:There is no book!an");
}

//4、基于链表存储结构的图书信息表按书号查找。
Status Cz_List(linkList L) {
	if (!sta) {
		printf("ERROR:The linked list is not initialized!an");
		return ERROR;
	}
	if (L->next == NULL) {
		printf("ERROR:The list is empty!na");
		return ERROR;
	}
	char name[15];
	linkList p = L;
	printf("Enter the isbn you are looking for:n");
	scanf("%s", &name);
	while (p->next) {
		if (!strcmp(name, p->next->data.id)) {
			p = p->next;
			printf("%s %s %.2f元n", p->data.id, p->data.name, p->data.price);
			return OK;
		}
		p = p->next;
	}
	printf("ERROR:There is no book!an");
	return ERROR;
}

//5、基于链表存储结构的图书信息表按价格区间查找。
Status SearchBookPriceRange(linkList L) {
	if (!sta) {
		printf("ERROR:The linked list is not initialized!an");
		return ERROR;
	}
	if (L->next == NULL) {
		printf("ERROR:The list is empty!na");
		return ERROR;
	}
	linkList p = L;
	int num = 0;
	bool exist = false;
	double min_price;
	double max_price;
	printf("The lowest price:");
	getchar();
	scanf("%lf", &min_price);
	printf("The highest price:");
	getchar();
	scanf("%lf", &max_price);
	while (p->next) {
		if (p->next->data.price >= min_price && p->next->data.price <= max_price) {
			printf("%st%st%.2lfn", p->next->data.id, p->next->data.name, p->next->data.price);
			exist = true;
		}
		p = p->next;
	}
	if (exist == false) {
		printf("ERROR:Books are not available at this price!na");
		return ERROR;
	}
	printf("There are %d books altogether.n", num);
	return OK;
}

//6、基于链表存储结构的按图书价格升序排序。
Status PriceAscendingSort(linkList& L) {
	if (!sta) {
		printf("ERROR:The linked list is not initialized!an");
		return ERROR;
	}
	if (L->next == NULL) {
		printf("ERROR:The list is empty!na");
		return ERROR;
	}
	linkList pfirst;
	linkList psecond;
	linkList pend = NULL;
	pfirst = L;
	psecond = L;
	Book temp;
	while (pfirst != pend){    
		while (pfirst->next != pend){
			if (pfirst->data.price > pfirst->next->data.price){
				temp = pfirst->data;
				pfirst->data = pfirst->next->data;
				pfirst->next->data = temp;
			}
			pfirst = pfirst->next;
		}
		pend = pfirst;
		pfirst = L;
	}
	Traverse(L);
	printf("Order to complete.n");
	return OK;
}

//7、基于链表存储结构的按图书价格修改。

Status ReviseAccordingBookPrice(linkList& L) {
	if (!sta) {
		printf("ERROR:The linked list is not initialized!an");
		return ERROR;
	}
	if (L->next == NULL) {
		printf("ERROR:The list is empty!na");
		return ERROR;
	}
	linkList p=L;
	char name[15];
	double price;
	printf("isbn:");
	getchar();
	scanf("%s", &name);
	printf("price:");
	getchar();
	scanf("%lf", &price);
	while (p->next) {
		if (!strcmp(name, p->next->data.id)) {
			p = p->next;
			p->data.price = price;
			printf("%s %s %.2f元n", p->data.id, p->data.name, p->data.price);
			printf("Modify the success.n");
			return OK;
		}
		p = p->next;
	}
	printf("ERROR:Books don't exist!na");
	return ERROR;
}

//8、基于链表存储结构的按图书价格普调。
Status PriceIncrease(linkList& L) {
	if (!sta) {
		printf("ERROR:The linked list is not initialized!an");
		return ERROR;
	}
	if (L->next == NULL) {
		printf("ERROR:The list is empty!na");
		return ERROR;
	}
	linkList p = L->next;
	int len = 0;
	double sum_price = 0;
	double average;
	while (p) {
		sum_price += p->data.price;
		p=p->next;
		len++;
	}
	average = sum_price / len;
	p = L->next;
	while (p) {
		if (average > p->data.price) {
			p->data.price += (p->data.price * 0.2);
		}
		else {
			p->data.price += (p->data.price * 0.1);
		}
		p = p->next;
	}
	Traverse(L);
	printf("The average price: %lf 元n", average);
	printf("The price has been raised successfully.n");
	return OK;
}

//9、基于链表存储结构的按最贵图书查找。
Status SearchBookPriceBest(linkList& L) {
	if (!sta) {
		printf("ERROR:The linked list is not initialized!an");
		return ERROR;
	}
	if (L->next == NULL) {
		printf("ERROR:The list is empty!na");
		return ERROR;
	}
	int num = 0;
	linkList p = L->next;
	double expensive = 0;
	while (p) {
		expensive = expensive < p->data.price ? p->data.price : expensive;
		p = p->next;
	}
	p = L->next;
	while (p) {
		if (p->data.price >= expensive) {
			printf("%s %s %.2lfn", p->data.id, p->data.name, p->data.price);
			num++;
		}
		p = p->next;
	}
	printf("There are %d books altogether.n", num);
	return OK;
}

//10、基于链表存储结构的图书去重。
Status DeleteDuplicateBooks(linkList& L) {
	if (!sta) {
		printf("ERROR:The linked list is not initialized!an");
		return ERROR;
	}
	if (L->next == NULL) {
		printf("ERROR:The list is empty!na");
		return ERROR;
	}
	linkList p;
	linkList mark;
	linkList q;
	for (mark = L->next; mark != NULL; mark = mark->next)
	{
		q = mark;
		p = mark->next;
		while (p)
		{
			if (!strcmp(mark->data.id, p->data.id))
			{
				q->next = p->next;
				free(p);
				p = q->next;
			}
			else
			{
				q = p;
				p = p->next;
			}
		}
	}
	Traverse(L);
	printf("Duplicate books have been successfully deleted.n");
	return OK;
}


创建主函数 main.cpp 源文件编写界面并执行程序

#include
#include 
#include "struct.h"
#include "function_declare.h"

int main() {
	bool staMain = false;
	linkList L;
	int init, choose;
	printf("Press any key to access the main menu.n");
	getchar();
	while (true) {
		system("cls");
		printf("============The main menu=========n");
		printf("0、exitn");
		printf("1、Create a order table.n");
		printf("2、New books are put in storage.n");
		printf("3、Old books go out of stock.n");
		printf("4、Search by isbn.n");
		printf("5、Search by price range.n");
		printf("6、Sort by price in ascending order.n");
		printf("7、Revise according to book price.n");
		printf("8、According to the book price general adjustment.n");
		printf("9、Search by most expensive book.n");
		printf("10、Delete duplicate books.n");
		printf("11、View all books.n");
		printf("12、Updates are saved to text.n");
		printf("nPlease enter the required function number:");
		scanf("%d", &choose);
		printf("--------------------------------------------------n");
		switch (choose) {
		case 0:
			system("cls");
			printf("Exiting the system successfully.n");
			system("pause");
			return 0;
		case 1:
			InitList_L(L,staMain);
			system("pause");
			break;
		case 2:
			Cr_List(L);
			system("pause");
			break;
		case 3:
			Sch_List(L);
			system("pause");
			break;
		case 4:
			Cz_List(L);
			system("pause");
			break;
		case 5:
			SearchBookPriceRange(L);
			system("pause");
			break;
		case 6:
			PriceAscendingSort(L);
			system("pause");
			break;
		case 7:
			ReviseAccordingBookPrice(L);
			system("pause");
			break;
		case 8:
			PriceIncrease(L);
			system("pause");
			break;
		case 9:
			SearchBookPriceBest(L);
			system("pause");
			break;
		case 10:
			DeleteDuplicateBooks(L);
			system("pause");
			break;
		case 11:
			if (!staMain) {
				printf("ERROR:The linked list is not initialized!an");
				system("pause");
			}
			else {
				Traverse(L);
				system("pause");
			}
			break; 
		case 12:
			if (!staMain) {
				printf("ERROR:The linked list is not initialized!an");
				system("pause");
			}
			else {
				Updata_books(L);
				system("pause");
			}
			break;
		default:
			printf("Error:Command error!na");
			system("pause");
			break;
		}
	}
	return 0;
}

本程序添加了防误操作的代码,防止误输入。 

 


附上测试文本(放到本项目的目录上即可)

9787512100831 计算机操作系统 35.25
9787811234923 汇编语言程序设计教程 43.55
9787302219972 单片机技术及应用 46.85
9787302257646 程序设计基础 47.52
9787302203513 编译原理 67.35
 

自行创建book.txt文件即可直接运行

提示:如果读写文本出现乱码,可能是你的文本编码格式跟编译器对应不上,新建一个新的.txt再尝试。

如果有用的话可以给一个小赞吗來

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/767223.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号