c语言实现图书管理系统

前言

用C语言实现一个图书管理系统

简单的那种。

简单

运行环境:vs2013

效果

1.主界面

2.查看库存

3.录入书籍

4.删除书籍

5.查询书籍

6.价格排序

7.修改信息

8.退出

完整代码

/**************************************
* Author:拾荒荒					  
* Note:more...              
***************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define EMPTY     -1		// 初始标志
#define DEL_EMPTY  0        // 删除标志
#define _MAX       200      //库存最大数量

typedef struct BookInfo {
	int _count;
	int _price;
	char _id[10];
	char _author[20];
	char _name[50];
	char _introduction[100];
}BookData;

BookData books_house[_MAX] = { -1, 0,"","", "", "", };

// 书仓信息
void books_house_info()
{
	printf("--------------------书籍信息--------------------\n");
	printf("ID\t书名\t作者\t价格\t数量\t简介\n");
	for (int i = 0; i < _MAX; ++i) {
		if (books_house[i]._count == EMPTY)
			break;
		else if (books_house[i]._count == DEL_EMPTY)
			continue;
		printf("%s\t", books_house[i]._id);
		printf("%s\t", books_house[i]._name);
		printf("%s\t", books_house[i]._author);
		printf("%d\t", books_house[i]._price);
		printf("%d\t", books_house[i]._count);
		printf("%s\t", books_house[i]._introduction);
		printf("\n");
	}
	printf("------------------------------------------------\n");
}

// 增添书
bool books_house_add(BookData info)
{
	for (int i = 0; i < _MAX; ++i) {
		if (books_house[i]._count == EMPTY ||
			books_house[i]._count == DEL_EMPTY) {
			for (int j = 0; j < _MAX; ++j) {
				if (strcmp(books_house[j]._id, info._id) == 0 ||
					strcmp(books_house[j]._name, info._name) == 0) {
					return false;
				}
			}
			books_house[i] = info; break;
		}
	}
	books_house_info();
	return true;
}

// 删除书
void books_house_del(BookData info)
{
	for (int i = 0; i < _MAX; ++i) {
		if (strcmp(books_house[i]._name, info._name) == 0) {
			books_house[i]._count = DEL_EMPTY;
			break;
		}
	}
	books_house_info();
}

// 价格排序(升序)
void books_house_sort()
{
	for (int i = 0; i < _MAX - 1; ++i) {
		if (books_house[i]._count == EMPTY) break;
		else if (books_house[i]._count == DEL_EMPTY) continue;
		for (int j = 0; j < _MAX - i - 1; ++j) {
			if (books_house[j]._price > books_house[j + 1]._price) {
				BookData tmp = books_house[j];
				books_house[j] = books_house[j + 1];
				books_house[j + 1] = tmp;
			}
		}
	}

	books_house_info();
}

// 修改书籍信息
void books_house_modify(BookData info)
{
	for (int i = 0; i < _MAX; ++i) {
		if (strcmp(books_house[i]._name, info._name) == 0) {
			books_house[i] = info; break;
		}
	}
	books_house_info();
}

// 根据书ID查找
bool books_house_search(BookData info)
{
	printf("\n--------------------查询结果--------------------\n");
	printf("ID\t书名\t作者\t价格\t数量\t简介\n");
	for (int i = 0; i < _MAX; ++i) {
		if (strcmp(books_house[i]._id, info._id) == 0) {
			printf("%s\t", books_house[i]._id);
			printf("%s\t", books_house[i]._name);
			printf("%s\t", books_house[i]._author);
			printf("%d\t", books_house[i]._price);
			printf("%d\t", books_house[i]._count);
			printf("%s\t", books_house[i]._introduction);
			printf("\n");
			return true;
			break;
		}
	}
	return false;
}

// 书籍信息
void set_book_info(BookData *book)
{
	printf("输入ID:");
	scanf_s("%s", book->_id, sizeof(book->_id));

	printf("输入书名:");
	scanf_s("%s", book->_name, sizeof(book->_name));

	printf("输入作者:");
	scanf_s("%s", book->_author, sizeof(book->_author));

	printf("输入简介:");
	scanf_s("%s", book->_introduction, sizeof(book->_introduction));

	printf("输入数量:");
	scanf_s("%d", &book->_count);

	printf("输入价格:");
	scanf_s("%d", &book->_price);
}

void books_house_manage_menu()
{
	system("cls");
	printf("				图书管理系统	\n\n");
	printf("			|	1.查看库存	|\n");
	printf("				2.录入书籍	 \n");
	printf("			|	3.删除书籍	|\n");
	printf("				4.查询书籍	 \n");
	printf("			|	5.价格排序	|\n");
	printf("				6.修改信息	 \n");
	printf("			|	7.退出		|\n");
	printf("\n-----\n");
	printf("请问你想操作什么: ");
}

int main(int argc, char **argv)
{
	do {
		int select;
		books_house_manage_menu();
		scanf_s("%d", &select);
		switch (select)
		{
		case 1: {
			books_house_info();
			break;
		}
		case 2: {
			BookData book;
			set_book_info(&book);
			books_house_add(book) ? NULL : printf("**该书籍已存在!请勿重复输入!\n");
			break;
		}
		case 3: {
			BookData book;
			printf("输入需要删除的书名:");
			scanf_s("%s", book._name, sizeof(book._name));
			books_house_del(book);
			break;
		}
		case 4: {
			BookData book;
			printf("输入需要查询的ID号:");
			scanf_s("%s", book._id, sizeof(book._id));
			books_house_search(book);
			break;
		}
		case 5: {
			books_house_sort();
			break;
		}
		case 6: {
			BookData book;
			printf("输入需要修改书籍的ID:");
			scanf_s("%s", book._id, sizeof(book._id));
			if (books_house_search(book)) {
				printf("请输入以下提示的修改信息\n");
				set_book_info(&book);
				books_house_modify(book);
			}
			break;
		}
		case 7: {
			return 0;
		}
		default:
			break;
		}
		printf("\n");
		system("pause");
	} while (true);

	return 0;
}


_End

posted @ 2021-12-15 14:37  想想就很离谱  阅读(1580)  评论(0编辑  收藏  举报