大學C語言課程設計——圖書管理系統(C語言版本)
必不可缺的頭檔案:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <list>
using namespace std;
原始碼分享:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <list> using namespace std; //3.資料的設計? //3.1 程式用什么東西處理資料---->容器--->鏈表 //3.2 資料的結構 ---->圖書的資訊 struct bookInfo { char name[20]; //書名 float price; //書籍的價格 int num; //書的數量 }; struct Node { struct bookInfo data; struct Node* next; }; struct Node* listBook = NULL; //創建表頭: 表頭就是一個結構體變數 //另一個部分:用戶資訊 struct student { char name[20]; char tel[20]; int curNum; struct bookInfo userBook[3]; }; list<student> myList; struct Node* createHead() { //動態記憶體申請 struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); //變數的基本規則>? 使用前必須初始化 headNode->next = NULL; return headNode; } //創建節點: 為插入做準備 //把用戶的資料變為結構體變數 struct Node* createNode(struct bookInfo data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data =https://www.cnblogs.com/112Q/p/ data; newNode->next = NULL; return newNode; } //插入:只需要一種插入方式 void insertNodeByHead(struct Node* headNode, struct bookInfo data) { struct Node* newNode = createNode(data); //必須先連接后斷開 newNode->next = headNode->next; headNode->next = newNode; } //指定洗掉 //posLeftNode->next=posNode->next; //free(posNode); void deleteNodeByName(struct Node* headNode, char *bookName) { struct Node* posLeftNode = headNode; struct Node* posNode = headNode->next; //書籍名字是字串,字串比較函式 while (posNode != NULL && strcmp(posNode->data.name,bookName)) { posLeftNode = posNode; posNode = posLeftNode->next; } //討論查找的結果 if (posNode == NULL) return; else { printf("洗掉成功!\n"); posLeftNode->next = posNode->next; free(posNode); posNode = NULL; } } struct Node* searchByName(struct Node* headNode, char* bookName) { struct Node* posNode = headNode->next; while (posNode != NULL && strcmp(posNode->data.name, bookName)) { posNode = posNode->next; } return posNode; } void printlistBook(struct Node* headNode) { struct Node* pMove = headNode->next; printf("書名\t價格\t數量\n"); while (pMove!=NULL) { //剝洋蔥 printf("%s\t%.1f\t%d\n", pMove->data.name, pMove->data.price, pMove->data.num); pMove = pMove->next; } } //1.寫界面--->選單--->模塊 void makeMenu() { printf("---------------------------------------\n"); printf(" xxoo圖書管理系統\n"); printf("\t0.退出系統\n"); printf("\t1.登記書籍\n"); printf("\t2.瀏覽書籍\n"); printf("\t3.借閱書籍\n"); printf("\t4.歸還書籍\n"); printf("\t5.書籍排序\n"); printf("\t6.洗掉書籍\n"); printf("\t7.查找書籍\n"); printf("---------------------------------------\n"); printf("請輸入(0~7):"); } //直接檔案操作? //寫操作 void saveInfoToFile(const char* fileName, struct Node* headNode) { FILE* fp = fopen(fileName, "w"); struct Node* pMove = headNode->next; while (pMove != NULL) { fprintf(fp, "%s\t%.1f\t%d\n", pMove->data.name, pMove->data.price, pMove->data.num); pMove = pMove->next; } fclose(fp); } //檔案讀操作 void readInfoFromFile(const char* fileName, struct Node* headNode) { FILE* fp = fopen(fileName, "r"); //第一次打開檔案肯定不存在 if (fp == NULL) { //不存在就創建出來這個檔案 fp = fopen(fileName, "w+"); } struct bookInfo tempData; while (fscanf(fp, "%s\t%f\t%d\n", tempData.name, &tempData.price, &tempData.num) != EOF) { insertNodeByHead(listBook, tempData); } fclose(fp); } //演算法是一種思想 void bubbleSortlistBook(struct Node* headNode) { for (struct Node* p = headNode->next; p != NULL; p = p->next) { for (struct Node* q = headNode->next; q->next != NULL; q = q->next) { if (q->data.price > q->next->data.price) { //交換值 struct bookInfo tempData = https://www.cnblogs.com/112Q/p/q->data; q->data = https://www.cnblogs.com/112Q/p/q->next->data; q->next->data =https://www.cnblogs.com/112Q/p/ tempData; } } } printlistBook(headNode); } //2.做互動 void keyDown() { int userKey = 0; struct bookInfo tempBook; //產生一個臨時的變數存盤書籍資訊 struct Node* result = NULL; scanf("%d", &userKey); switch (userKey) { case 0: printf("【 退出 】\n"); printf("退出成功\n"); system("pause"); exit(0); //關閉掉整個程式 break; case 1: printf("【 登記 】\n"); printf("輸入書籍的資訊(name,price,num):"); scanf("%s%f%d", tempBook.name, &tempBook.price, &tempBook.num); insertNodeByHead(listBook, tempBook); saveInfoToFile("bookinfo.txt", listBook); break; case 2: printf("【 瀏覽 】\n"); printlistBook(listBook); break; case 3: printf("【 借閱 】\n"); //書籍存在可以借閱 ,書籍的總數量-1 不存在借閱失敗 printf("請輸入借閱的書名:"); scanf("%s", tempBook.name); result = searchByName(listBook, tempBook.name); if (result==NULL) { printf("沒有相關書籍無法借閱!\n"); } else { if (result->data.num > 0) { result->data.num--; printf("借閱成功!\n"); } else { printf("當前書籍無庫存在,借閱失敗!\n"); } } break; case 4: printf("【 歸還 】\n"); //當前書籍的數量+1 printf("請輸入歸還的書名:"); scanf("%s", tempBook.name); result = searchByName(listBook, tempBook.name); if (result == NULL) { printf("該書來源非法!\n"); } else { result->data.num++; printf("書籍歸還成功!\n"); } break; case 5: printf("【 排序 】\n"); bubbleSortlistBook(listBook); break; case 6: printf("【 洗掉 】\n"); printf("請輸入洗掉書名:"); scanf("%s", tempBook.name); deleteNodeByName(listBook, tempBook.name); saveInfoToFile("bookinfo.txt", listBook); break; case 7: printf("【 查找 】\n"); printf("請輸入要查詢的書名:"); scanf("%s", tempBook.name); result = searchByName(listBook, tempBook.name); if (result == NULL) { printf("未找到相關資訊!\n"); } else { printf("書名\t價格\t數量\n"); printf("%s\t%.1f\t%d\n", result->data.name, result->data.price, result->data.num); } break; default: printf("【 error 】\n"); break; } } //easyx inputBox //自己封裝一個Edit int main() { listBook= createHead(); readInfoFromFile("bookinfo.txt", listBook); while (1) { makeMenu(); keyDown(); system("pause"); system("cls"); } system("pause"); return 0; }
簡單效果展示:
希望對大家有幫助哦!
另外如果你想更好的提升你的編程能力,學好C語言C++編程!彎道超車,快人一步!
C語言C++編程學習交流圈子,QQ群1095293493【點擊進入】微信公眾號:C語言編程學習基地
分享(原始碼、專案實戰視頻、專案筆記,基礎入門教程)
歡迎轉行和學習編程的伙伴,利用更多的資料學習成長比自己琢磨更快哦!
編程學習軟體分享:

編程學習視頻分享:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/162311.html
標籤:C++
下一篇:Java學習day10
