基于线性表的图书信息管理(C语言)

线性表头文件(linklist.h):

typedef struct Booklinklist {
	char BookNumber[50];
	char BookName[50];
	double price;
	struct Booklinklist* next;
}book, * Sbook;

Sbook CreateBook();
int ListLength(Sbook S);
void PrintBook(Sbook S);
void PrintBook1(Sbook S);
void DeclineBook(Sbook S);
void InsertBooki(Sbook S, Sbook SNew, int i);
void InsertBooki1(Sbook S, Sbook SNew, int i);
void InsertBook(Sbook S);
void InsertBook1(Sbook S);
void DeleteBook(Sbook S, int i);

线性表中函数实现(linklist.cpp):

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include"linklist.h"

Sbook CreateBook() {          //创建链表
	Sbook S;
	S = (Sbook)malloc(sizeof(book));
	S->next = NULL;
	return S;
}

int ListLength(Sbook S) {    //获取链表长度
	int length = 0;
	Sbook STemp = S->next;
	while (STemp) {
		length++;
		STemp = STemp->next;
	}
	return length;
}

void PrintBook(Sbook S) {    //输出图书(含输出图书总数)
	printf("%d\n", ListLength(S));
	Sbook STemp = S->next;
	while (STemp) {
		printf("%s %s %.2lf\n", STemp->BookNumber, STemp->BookName, STemp->price);
		STemp = STemp->next;
	}
}

void PrintBook1(Sbook S) {    //输出图书(不含输出图书总数)
	Sbook STemp = S->next;
	while (STemp) {
		printf("%s %s %.2lf\n", STemp->BookNumber, STemp->BookName, STemp->price);
		STemp = STemp->next;
	}
}

void DeclineBook(Sbook S) {   //图书按照价格降序
	Sbook STemp = S->next;
	Sbook STemp1;
	int length = ListLength(S);
	for (int i = 1; i < length; i++) {
		for (int j = 1; j < length; j++) {
			if (STemp->price < STemp->next->price) {
				STemp1 = STemp->next->next;
				InsertBooki(S, STemp->next, j);
				STemp->next = STemp1;
			}
			else {
				STemp = STemp->next;
			}
		}
		STemp = S->next;
	}
}

void InsertBooki(Sbook S, Sbook SNew, int i) {    //将节点SNew插入到第i位置
	Sbook STemp = S;
	for (int j = 1; j < i; j++) {
		STemp = STemp->next;
	}
	SNew->next = STemp->next;
	STemp->next = SNew;
}

void InsertBooki1(Sbook S, Sbook SNew, int i) {    //将节点SNew插入到第i位置(扫描插入)
	Sbook STemp = S;
	for (int j = 1; j < i; j++) {
		STemp = STemp->next;
	}
	SNew = (Sbook)malloc(sizeof(book));
	scanf("%s%s%lf", &SNew->BookNumber, &SNew->BookName, &SNew->price);
	SNew->next = STemp->next;
	STemp->next = SNew;
}

void InsertBook(Sbook S) {           //插入图书(0 0 0结尾)
	int j = 0;
	Sbook STemp = S;
	while (1) {
		Sbook SNew;
		SNew = (Sbook)malloc(sizeof(book));
		scanf("%s%s%lf", &SNew->BookNumber, &SNew->BookName, &SNew->price);
		if (strcmp(SNew->BookNumber, "0") == 0 && strcmp(SNew->BookName, "0") == 0 && SNew->price == 0)
			break;
		SNew->next = STemp->next;
		STemp->next = SNew;
		STemp = SNew;
	}
}

void InsertBook1(Sbook S) {         //插入图书(先获取图书总数)
	Sbook STemp = S;
	int number;
	scanf("%d", &number);
	for (int i = 0; i < number; i++) {
		Sbook SNew;
		SNew = (Sbook)malloc(sizeof(book));
		scanf("%s%s%lf", &SNew->BookNumber, &SNew->BookName, &SNew->price);
		SNew->next = STemp->next;
		STemp->next = SNew;
		STemp = SNew;
	}
}

void DeleteBook(Sbook S, int i) {   //删除第i位置元素
	int j;
	Sbook LPre = S;
	Sbook LTemp = S->next;
	for (j = 0; j < i - 1; j++) {
		LPre = LPre->next;
		LTemp = LTemp->next;
	}
	LPre->next = LTemp->next;
	free(LTemp);
}

顺序表头文件(sqlist.h):

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef struct Booksqlist {
	char BookNumber[50];
	char BookName[50];
	double price;
}booksqlist;

typedef struct {
	booksqlist* elem;
	int length;
	int listsize;
}Sqlist;

void InitList_Sq(Sqlist* S);
void CreateBook(Sqlist* S);
void CreateBook1(Sqlist* S);
void PrintBook(Sqlist* S);
void PrintBook1(Sqlist* S);
double FindMaxPriceBook(Sqlist* S);
void PrintMaxPriceBook(Sqlist* S, double max);
void InsertBook(Sqlist* S, int i);
void DeleteBook(Sqlist* S, int i);

顺序表中函数实现(sqlist.cpp):

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include"sqlist.h"

void InitList_Sq(Sqlist* S) {        //创建顺序表 
	S->elem = (booksqlist*)malloc(LIST_INIT_SIZE * sizeof(booksqlist));
	S->length = 0;
	S->listsize = 100;
}

void CreateBook(Sqlist* S) {        //创建图书表(以0 0 0作为结束符) 
	int i = 0;
	while (1) {
		scanf("%s%s%lf", &S->elem[i].BookNumber, &S->elem[i].BookName, &S->elem[i].price);
		if (strcmp(S->elem[i].BookNumber, "0") == 0 && strcmp(S->elem[i].BookName, "0") == 0 && S->elem[i].price == 0)
			break;
		i++;
		S->length++;
	}
}

void CreateBook1(Sqlist* S) {        //创建图书表(先给出图书数量) 
	int i = 0, number;
	scanf("%d", &number);
	for (i = 0; i < number; i++) {
		scanf("%s%s%lf", &S->elem[i].BookNumber, &S->elem[i].BookName, &S->elem[i].price);
		S->length++;
	}
}

void PrintBook(Sqlist* S) {         //输出图书(含书总数)
	printf("%d\n", S->length);
	for (int i = 0; i < S->length; i++) {
		printf("%s %s %.2lf\n", S->elem[i].BookNumber, S->elem[i].BookName, S->elem[i].price);
	}
}

void PrintBook1(Sqlist* S) {        //输出图书(不含书总数)
	for (int i = 0; i < S->length; i++) {
		printf("%s %s %.2lf\n", S->elem[i].BookNumber, S->elem[i].BookName, S->elem[i].price);
	}
}

double FindMaxPriceBook(Sqlist* S) {          //返回最贵图书的价格
	double max;
	int i = 0;
	max = S->elem[i].price;
	for (i = 1; i < S->length; i++) {
		if (S->elem[i].price > max) {
			max = S->elem[i].price;
		}
	}
	return max;
}

void PrintMaxPriceBook(Sqlist* S, double max) {   //输出最贵图书的总数量和信息
	int i, j = 0;
	for (i = 0; i < S->length; i++) {
		if (S->elem[i].price == max) {
			j++;
		}
	}
	printf("%d\n", j);
	for (i = 0; i < S->length; i++) {
		if (S->elem[i].price == max) {
			printf("%s %s %.2lf\n", S->elem[i].BookNumber, S->elem[i].BookName, S->elem[i].price);
		}
	}
}

void InsertBook(Sqlist* S, int i) {  //在第i个位置插入图书
	S->length++;
	for (int j = S->length - 1; j >= i; j--) {
		S->elem[j] = S->elem[j - 1];
	}
	scanf("%s%s%lf", &S->elem[i - 1].BookNumber, &S->elem[i - 1].BookName, &S->elem[i - 1].price);
}

void DeleteBook(Sqlist* S, int i) {    //删除第i位置图书
	S->length--;
	for (int j = i - 1; j < S->length; j++) {
		S->elem[j] = S->elem[j + 1];
	}
}

主函数(ceshi1.cpp):

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"sqlist.h"            //引用头函数
#include"linklist.h"

int main() {
	int order=1;
	while (order) {
		printf("*****************图书信息管理系统*****************\n");
		printf("菜单:\n");
		printf("1.基于顺序存储结构的图书信息表的创建和输出\n");
		printf("2.基于顺序存储结构的图书信息表的最贵图书的查找\n");
		printf("3.基于顺序存储结构的图书信息表的新图书的入库\n");
		printf("4.基于顺序存储结构的图书信息表的旧图书的出库\n");
		printf("5.基于链式存储结构的图书信息表的创建和输出\n");
		printf("6.基于链式存储结构的图书信息表的排序\n");
		printf("7.基于链式存储结构的图书信息表的新图书的入库\n");
		printf("8.基于链式存储结构的图书信息表的旧图书的出库\n");
		printf("0.退出菜单\n");
		printf("*****************图书信息管理系统*****************\n");
		printf("请输入菜单选项:");
		scanf("%d", &order);
		switch (order) {

		case 1: {
			Sqlist S;
			InitList_Sq(&S);
			printf("\n请输入图书信息(以0 0 0结束):\n");
			CreateBook(&S);
			printf("\n");
			printf("\n总图书信息:\n");
			PrintBook(&S);
			break; }

		case 2: {
			Sqlist S;
			InitList_Sq(&S);
			printf("\n请输入图书数量和信息:\n");
			CreateBook1(&S);
			double max = FindMaxPriceBook(&S);
			printf("\n最贵图书数量和信息:\n");
			PrintMaxPriceBook(&S, max);
			break; }

		case 3: {
			Sqlist S;
			InitList_Sq(&S);
			printf("\n请输入图书数量和信息:\n");
			CreateBook1(&S);
			int location;
			printf("\n请输入插入的新图书的位置:");
			scanf("%d", &location);
			printf("\n请输入插入的新图书的信息:");
			InsertBook(&S, location);
			printf("\n插入新图书后的总图书信息:\n");
			PrintBook1(&S);
			break;
		}

		case 4: {
			Sqlist S;
			InitList_Sq(&S);
			printf("\n请输入图书数量和信息:\n");
			CreateBook1(&S);
			int location;
			printf("\n请输入需要删除的旧图书的位置:");
			scanf("%d", &location);
			DeleteBook(&S, location);
			printf("\n删除旧图书后的总图书信息:\n");
			PrintBook1(&S);
			break;
		}

		case 5:
			Sbook S5;
			S5= CreateBook();
			printf("\n请输入图书信息(以0 0 0结束):\n");
			InsertBook(S5);
			printf("\n总图书信息:\n");
			PrintBook(S5);
			break;

		case 6:
			Sbook S6;
			S6 = CreateBook();
			printf("\n请输入图书信息(以0 0 0结束):\n");
			InsertBook(S6);
			DeclineBook(S6);
			printf("\n排序后的总图书信息:\n");
			PrintBook1(S6);
			break;

		case 7: {
			Sbook S7, SNew = NULL;
			S7 = CreateBook();
			printf("\n请输入图书数量和信息:\n");
			InsertBook1(S7);
			int location;
			printf("\n请输入插入的新图书的位置及信息:");
			scanf("%d", &location);
			InsertBooki1(S7, SNew, location);
			printf("\n插入新图书后的总图书信息:\n");
			PrintBook1(S7);
			break; }

		case 8:
			Sbook S8;
			S8 = CreateBook();
			printf("\n请输入图书数量和信息:\n");
			InsertBook1(S8);
			int de;
			printf("\n请输入需要删除的旧图书的位置:");
			scanf("%d", &de);
			DeleteBook(S8, de);
			printf("\n删除旧图书后的总图书信息:\n");
			PrintBook1(S8);
			break;

		case 0:
			printf("\n已退出\n");
			break;

		default:
			printf("\n无此选项,请重新输入或者退出\n");
		}
		printf("\n\n");
	}
	return 0;
}

部分功能测试结果:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值