题目概述: 图书管理系统。 编程: #include<stdio.h> #include<stdlib.h> #include<string.h> //1.写界面—>菜单—>模块 //2.数据设计 struct bookInfo//图书信息 { char name[20];//书名 float price;//书的价格 int num;//书的数量 }; struct Node { struct bookInfo data; struct Node* next; }; struct Node* list = NULL; //创建表头:表头是一个结构体变量
//另一部分:用户信息(C++(类))
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 = data; newNode->next = NULL; return newNode; } //插入:只需要一种插入方式(表头法插入) void inserNodeByHead(struct Node* headNode, struct bookInfo data) { struct Node* newNode = createNode(data); //必须先连接后断开 newNode->next = headNode->next; headNode->next = newNode; } 表尾插入 //void inserNodeByTail(struct Node* headNode, int data) //{ // struct Node* pMove = headNode; // while (pMove->next != NULL) // { // pMove = pMove->next; // } // struct Node* newNode = createNode(data); // pMove->next = newNode; //} //指定删除 //posLeftNode->nest=posNode->next; //free(posNode); void deleteNodeByName(struct Node* headNode,char* bookName)//指定书名删除 { struct Node* posLeftNode = headNode;//删除节点Left struct Node* posNode = headNode->next;//删除节点right //书名是字符串,要用字符串比较函数 while (posNode != NULL && strcmp(posNode->data.name,bookName))//指定位置name和传入参数bookName比较 { posLeftNode = posNode; posNode = posLeftNode->next;//posNode和posLeftNode并排往下 } //讨论查找结果 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 printList(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; } } void makeMuLu() { printf("------------------------\n"); printf(" xx00图书管理系统\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):\n”); } //文件操作(对list做文件操作) //文件存操作 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%.1f\t%d\n”, tempData.name, &tempData.price, &tempData.num) != EOF) {//读一个就插入链表中 inserNodeByHead(list, tempData); } fclose(fp); } //排序(冒泡) void bubbleSortList(struct Node* headNode) { for (struct Node* p = headNode->next; p!=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 = q->data; q->data = q->next->data; q->next->data = tempData; } } } printList(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%.1f%d", tempBook.name, &tempBook.price, &tempBook.num); inserNodeByHead(list, tempBook); //同步到文件 saveInfoToFile(“bookinfo.txt”,list); break; case 2:printf("【浏览】\n"); printList(list); break; case 3:printf("【借阅】\n"); printf(“输入书籍名称:”); scanf("%s", tempBook.name); result = searchByName(list, 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"); printf(“输入归还书籍名称:”); scanf("%s", tempBook.name); result = searchByName(list, tempBook.name); if (result == NULL) { printf(“该书来源非法!\n”); } else { result->data.num++; printf(“书籍归还成功!\n”); } break; case 5:printf("【排序】\n"); bubbleSortList(list); break; case 6:printf("【删除】\n"); printf(“输入删除书籍名称:”); scanf("%s", tempBook.name); deleteNodeByName(list, tempBook.name); //同步到文件 saveInfoToFile(“bookinfo.txt”, list); break; case 7:printf("【查找】\n"); printf(“输入查找书籍名称:”); scanf("%s", tempBook.name); result = searchByName(list, tempBook.name); if (result == NULL) { printf(“未找到相关信息”); } 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; } } void main() { list = createHead();//创建列表 readInfoFromFile(“bookinfo.txt”, list); while (1) { makeMuLu(); keyDown(); system(“pause”); //锁屏 } } 上机实践: 感想: 我第一次写这么长的C代码实在是不容易,虽然看着视频讲解但是自己编译问题和困难还是不少,总之弄完还是感觉挺不错的。
|