C語言實現圖書管理系統
阿新 • • 發佈:2021-12-15
前言
用C語言實現一個圖書管理系統
簡單的那種。
簡單
。
執行環境:vs2013
效果
1.主介面
2.檢視庫存
3.錄入書籍
4.刪除書籍
5.查詢書籍
6.價格排序
7.修改資訊
8.退出
完整程式碼
#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
。
本文來自部落格園,作者:拾荒荒,轉載請註明原文連結:https://www.cnblogs.com/lvvou/p/15692687.html