需求文档
产品原型图
产品流程图
核心代码
预备知识
- 指针如何变成变量
- 什么是结构体
- 什么是链表
代码语言:javascript
复制#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//3.数据的设计
/*
1.程序用什么东西处理数据-- 数组、链表
2.数据的结构 -- 图书的处理
*/
//创建图书信息
struct bookInfo
{
char name[20];
float price;
int num;
};
//创建容器-有表头的链表-表头不存放数据
struct Node()
{
struct bookInfo data;
struct *next;
};
//创建全局链表表头
struct Node* list = NULL;
//创建表头:
struct Node* createHead(){
//动态内存分配
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//初始化:通常需要初始化指针和数据
//但是表头不需要存放数据所以只需要初始化指针即可
headNode->next = NULL;
return headNode;
}
//创建节点:为插入做准备
/*
把用户的数据变为结构体变量
*/
struct Node* createNode(struct bookInfo date)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//打印链表
/*
打印以当前节点为头节点的链表。
需要定义一个指针从当前头节点的下一个节点开始打印
*/
void printList(struct Node* headNode){
struct Node* pMove = headNode->next;
while(pMove)//完整写法pMove != NULL
{
printf("%s/t%.1fn%dn",pMove->data.name,pMove->data.price,pMove->data.num);
pMove = pMove->next;
}
}
//插入:只需要一种插入方式
/*
表头插入
*/
void insertNodeByHead(struct Node* headNode,struct bookInfo data){
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
//插入:尾插
/*
关键是找到表尾:当pMove-next == NULL时
*/
void insertNodeByTail(struct Node* headNode,struct bookInfo 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)
{
struct Node* posLeftNode = NULL;
struct Node* posNode = headNode->next;
//书籍名字时是字符串,需要采用strcmp函数比较字符串
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* fileName){
struct Node* posNode = headNode->next;
while(posNode != NULL && strcmp(posNode->data->name,fileName)){
posNode = posNode->next;
}
return posNode;
}
//1.写界面--》菜单--》模块
void makeMeun()
{
printf("图书管理系统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");
}
//直接文件操作
/*
在程序中我们通过list链表存储数据,但是在计算机中我们通过文件存储数据,所以我们需要通过从文件中读取数据到链表里。
*/
//文件写操作
void saveInforToFile(const char* fileName,struct Node* headNode){
FILE *fp = fopen(filename,"w");
//从第二个节点开始存入文件
struct Node* pMove = headNode->next;
while(pMove != NULL){
fprintf(fp,"%st%ft%dn",pMove->data.name,pMove->data.price,pMove->data.num);
pMove = pMove->next;
}
fclose(fileName);
}
//文件读操作
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,"%st%ft%dn",tempData.name,tempData.price,tempData.num) != EOF){
insertNodeByHead(list,tempData);
}
fclose(fileName);
}
//冒泡排序-对链表进行排序-按照书籍的价格排序
void bubbleSortList(struct Node* headNode){
for(struct Node* p = headNode->next;p != NULL;p = p->next){
for(struct Node* p = headNode->next;q->next != NULL;q = q->next){
if(q->data.price > q->data.price){
struct bookInfo tempData = q->data;
q->data = q->next->data;
q->next->data = tempData;
}
}
}
}
//2.做交互
void keyDown()
{
int userKey = 0;
struct bookInfo tempBook;
struct Node* resualt = NULL;
scanf("%d",userKey);
switch(userKey)
{
case 0:
exit(0);
break;
case 1:
printf("登记n");
printf("请输入书籍信息:(名称,价格,数量)");
scanf("%s%f%d",);
insertNoteByhead(list,);
saveInfoToFile("bookFile.txt",list);
break;
case 2:
printf("浏览n");
printList(list);
break;
case 3:
printf("借阅n");
scanf("%s",tempBook.name);
resualt = searchByName(list,tempBook.name);
if(resualt == NULL){
printf("Book Not Found!");
}
else{
if(resualt->data.num > 0){
resualt->data.num --;
printf("借阅成功n");
}
}
break;
case 4:
break;
case 5:
bubbleSortList(list);
break;
case 6:
printf("删除");
scanf("%s"'tempData.name');
deleteNoteByName(list,tempBook.name);
saveInfoToFile("bookInfo.txt",list);
break;
case 7:
Printf("查找");
scanf("%s",tempBook.name);
resualt = searchByName(list,tempBook.name);
if(resualt == NULL){
printf("Not Found!")
}
else{
printf("%st%.1ft%dn",resualt->data.name,resualt->data.price,resualt->data.num);
}
break;
default:
printf("errorn");
break;
}
}
int main()
{
int userKey = 0;
list = createHead();
while(1){
makeMenu();
keyDown();
system("pause");
system("cls");
}
return 0;
}