C/C++知識項目教程:圖書管理系統(tǒng)!手把手教你寫出大學C語言圖書管理系統(tǒng)...

老師的源代碼??
/*VS軟件的內擴增問題*/#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <list>
/*用戶信息
struct student
{
char name[20];
char tel[20];
int curNum;
struct bookInfo userBook[3];
};
list<student> myList;*/
//書籍信息
struct bookInfo//圖書信息結構體
{
char name[20];
float price;
int num;
};
struct Node//定義結構體變量
{
struct bookInfo data;
struct Node* next;
};
struct Node* listBook = NULL;//全局變量聲明
struct Node* createHead()//創(chuàng)建表頭(有表頭鏈表)
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//動態(tài)內存申請
headNode->next = NULL;//初始化
return headNode;
}
struct Node* createNode(struct bookInfo data)//創(chuàng)建節(jié)點,用戶數(shù)據(jù)變?yōu)榻Y構體變量
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;//必須先連接后斷開
newNode->next = NULL;
return newNode;
}
void insertNodeByHead(struct Node* headNode, struct bookInfo data)//頭插法插入數(shù)據(jù)
{
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
/*void insertNodeByTail(struct Node* headNode, int data)//尾插法
{
struct Node* pMove = headNode;
while (pMove->next != NULL)
{
pMove = pMove->next;
}
struct Node* newNode = createNode(data);
pMove->next = newNode;
}*/
void deleteNodeByName(struct Node* headNode,char *bookName)//刪除函數(shù)
{
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;
}
}
void printlistBook(struct Node* headNode)//打印數(shù)據(jù)
{
struct Node* pMove = headNode->next;
printf("書名\t價格\t數(shù)量\n");
while (pMove)
{
//剝洋蔥
printf("%s\t%.1f\t%d\n", pMove->data.name,pMove->data.price,pMove->data.num);
pMove = pMove->next;
}
}
void bubbleSortlistBook(struct Node* headNode)//排序
{
for (struct Node* p = headNode->next; p != NULL; 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 = q->data;
q->data = q->next->data;
q->next->data = tempData;
}
}
}
printlistBook(headNode);
}
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 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)//創(chuàng)建文件
{
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 makeMenu()//顯示初始界面
{
printf("----------------------\n");
printf("圖書管理系統(tǒng)\n");
printf("0.退出\n");
printf("1.登記\n");
printf("2.瀏覽\n");
printf("3.借閱\n");
printf("4.歸還\n");
printf("5.排序\n");
printf("6.刪除\n");
printf("7.查找\n");
printf("----------------------\n");
printf("請輸入(0-7)\n");
}
void keyDown()//Switch函數(shù)選擇界面
{
int userkey = 0;
struct bookInfo tempBook;//產生一個臨時的變量儲存書籍信息
struct Node* result = NULL;
scanf("%d", &userkey);
switch (userkey)
{
case 0:
printf("【 退出成功 】\n");
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");
printf("請輸入借閱的書名:");
scanf("%s", tempBook.name);
result = searchByName(listBook, tempBook.name);
if (result == NULL)
{
printf("沒有找到相關書籍,無法借閱。\n");
}
else
{
if (result->data.num >= 1)
{
result->data.num--;
printf("借閱成功。\n");
}
else
{
printf("當前書本暫無庫存,借閱失敗!\n");
}
}
break;
case 4:
printf("【 歸還 】\n");
printf("請輸入歸還的書名:");
scanf("%s", tempBook.name);
result = searchByName(listBook, tempBook.name);
if (result == NULL)
{
printf("該書不屬于圖書館!");
}
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數(shù)量\n");
printf("%s\t%.1f\t%d\n",result->data.name,result->data.price,result->data.num);
}
break;
default:
printf("【 請輸入有效數(shù)字。 】\n");
break;
}
}
int main()//主函數(shù)
{
listBook = createHead();
readInfoFromFile("bookinfo.txt",listBook);
while (1)
{
makeMenu();
keyDown();
system("pause");
system("cls");
}
system("pause");
return 0;
}