图书管理系统顺序表实现

顺序表实现

#include
#include
#include
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef  int Status;
typedef struct book
{
	char no[20];
	char name[30];
	float price;
}Book;
typedef struct Node
{
	Book* elem;
	int length;
	int caption;
}Sqlist;
Status Init(Sqlist& L)
{
	L.elem = (Book*)malloc(sizeof(Book));
	if (!L.elem) exit(OVERFLOW);
	L.length = 0;
	L.caption = MAXSIZE;
	return OK;
}
Status Get(Sqlist L, int i, Book& m)
{
	if (i<0 || i>L.length)
		return ERROR;
	m = L.elem[i - 1];
	return OK;
}



Status Great(Sqlist& L)
{
	int n;
	printf("请输入图书数量:");
	scanf("%d", &n);
	printf("请输入图书的编号、名字、价格:");
	int i;
	for (i = 0; i < n; i++)
		scanf("%s %s %f", &L.elem[i].no, &L.elem[i].name, &L.elem[i].price);
	L.length = i;
	return OK;
}
Status Dele(Sqlist& L, int i, Book& e)
{
	if (i<0 || i>L.length)
		return ERROR;
	e = L.elem[i - 1];
	for (int j = i - 1; j < L.length; j++)
	{

		L.elem[j] = L.elem[j + 1];
	}
	--L.length;
	return OK;
}
Status local(Sqlist L, char a[20])
{
	for (int i = 0; i < L.length; i++)
	{
		if (strcmp(L.elem[i].name, a) == 0)
			return i + 1;
	}
			printf("没有这本书");

}

Status Insert(Sqlist& L, int i, Book e)
{
	if (i<0 || i>L.length)
		return ERROR;
	for(int j = L.length; j >=i; j--)
		L.elem[j] = L.elem[j-1];
	L.elem[i - 1] = e;
	++L.length;  
}
void print(Sqlist L)
{
	for (int i = 0; i < L.length; i++)
		printf("%-5s %-5s %.1f\n", L.elem[i].no, L.elem[i].name, L.elem[i].price);
}
int main()
{
	Sqlist S;
	Init(S);
	Great(S);
	printf("----------------------------------------");
	printf("\n1.查找图书\n 2.插入一本书\n 3.打印图书\n");
	printf("4.取值\n");
	printf("5.删除\n");
    printf("6.退出系统\n");
	printf("----------------------------------------\n");
	Book e, k;
	int flat, i, m;
	char a[20];


	while (flat != 6)
	{
		scanf("%d", &flat);
		switch (flat)
		{
		case 1:
		printf("请输入要查找图书的名字:");
			scanf("%s", a);
			if (local(S, a))
			m = local(S, a);
			printf("%d\n", m);
			break;
		case 2:
			printf("请输入要插入图书的编号、名字、价格、位置:");
			scanf("%s%s%f%d", &e.no, &e.name, &e.price, &i);
			Insert(S, i, e);
			break;
		case 3:
			print(S);
			break;
		case 4:
		printf("请输入要取值的位置:");
			int n;
			scanf("%d", &n);
			Get(S, n, k);
			printf("%-5s %-5s %.1f\n", k.no, k.name, k.price);
			break;
		case 5:
		printf("请输入要删除图书的位置:");
			int d;
			Book q;
			scanf("%d", &d);
			Dele(S, d, q);
			printf("您删除的书是:%s %s %f\n", q.no, q.name, q.price);
			break;

		default:
			printf("已退出系统!\n");
			break;
		}
	}
	printf("操作后的图书:\n");
	print(S);
	return 0;
}

你可能感兴趣的:(数据结构笔记)