C/C++项目开发之图书管理系统

嵌入式技术

1343人已加入

描述

 

hello,各位小伙伴们大家好!你们今天有敲代码嘛?

管理系统一直是我们计算机专业同学都要经历的项目设计,而管理系统的核心知识点其实都是一样的,无法就是换了个马甲,今天我们就来看看很多同学都会遇到的管理项目之一:图书管理系统!

 

用于图书信息的管理。包括图书信息的创建、图书信息的打印、图书信息的查询、图书信息的修改、图书信息的删除。方便用户整理图书,查询图书。

这个图书管理系统是由单链表这一数据结构实现的,板块包括图书信息的创建、打印、查询、修改、删除、以及图书价格的排序等组成。

代码后面也有注释的,基本很好理解的。

源代码

下面为源代码:

 

#include 
#include 
#include 
//3.数据的设计
  //3.1程序的数据存储--->容器
  //3.2数据的结构 --->图书的信息


struct bookInfo
{
  char name[20];  //书名
  float price;    //书籍的价格
  int num;    //书籍的数量
};


//定义链表
struct Node
{
  struct bookInfo data;
  struct Node* 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 data)
{
  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)
{
  struct Node* newNode = createNode(data);
  newNode->next = headNode->next;
  headNode->next = newNode;
}


//尾插法
/*struct insertNodeByTall(struct Node* headNode, int data)
{
  struct Node* pMove = headNode;
  while (pMove != NULL)
  {
    pMove = pMove->next;
  }
  struct Node* newNode = createHead(data);
  pMove->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("删除成功!
");
    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("书名	价格	数量
");
  while (pMove != NULL)
  {
    printf("%s	%.1f	%d
", pMove->data.name, pMove->data.price, pMove->data.num);
    pMove = pMove->next;
  }
}


//直接文件操作
//文件写操作
void saveInfoToFile(const char* filename, struct Node* headNode)
{
  FILE* fp = fopen(filename, "w");
  struct Node* pMove = headNode->next;
  while (pMove != NULL)
  {
    fprintf(fp, "%s	%.1f	%d
", 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	%f	%d
", tempData.name, &tempData.price, &tempData.num) != EOF)
  {
    insertNodeByHead(list, tempData);
  }
  fclose(fp);
}
//冒泡排序(链表)
void bubbleSortList(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 = 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(" 【 登记 】 
");
    printf("输入书籍的信息(name,price,num):");
    scanf("%s%f%d", tempBook.name, &tempBook.price, &tempBook.num);
    insertNodeByHead(list, tempBook);
    saveInfoToFile("bookinfo.txt", list);
    break;
  case 1:
    printf(" 【 浏览 】 
");
    printList(list);
    break;
  case 2:
    printf(" 【 借阅 】 
"); 
    printf("请输入你要借阅的书籍:");
    scanf("%s", tempBook.name);
    result = searchByName(list,tempBook.name);
    if (result == NULL)
      printf("没有相关书籍无法借阅!
");
    else
    {
      if (result->data.num > 0)
      {
        result->data.num--;
        printf("借阅成功
");
        saveInfoToFile("bookinfo.txt", list);
      }
      else
      {
        printf("当前书籍无库存,借阅失败!
");
      }
    }
    break;
  case 3:
    printf(" 【 归还 】 
");
    printf("请输入你要归还的书籍:");
    scanf("%s", tempBook.name);
    result = searchByName(list, tempBook.name);
    if (result == NULL)
      printf("书籍来源非法!
");
    else
    {
      result->data.num++;
      printf("书籍归还成功!
");
      saveInfoToFile("bookinfo.txt", list);
    }
    break;
  case 4:
    printf(" 【 查找 】 
");
    printf("你要查询的书名:");
    scanf("%s", tempBook.name);
    result = searchByName(list, tempBook.name);
    if (result == NULL)
    {
      printf("未找到相关结果!
");
    }
    else
    {
      printf("书名	价格	数量
");
      printf("%s	%.1f	%d
", result->data.name, result->data.price, result->data.num);
    }
    break;
  case 5:
    printf(" 【 排序 】 
");
    bubbleSortList(list);
    break;
  case 6:
    printf(" 【 删除 】 
");
    printf("输入想要删除的书名:");
    scanf("%s", tempBook.name);
    deleteNodeByName(list, tempBook.name);
    saveInfoToFile("bookinfo.txt", list);
    break;
  case 7:
    printf(" 【 退出 】 
");
    printf(" 退出成功 
");
    system("pause");
    exit(0);   //关掉整个程序
    break;
  default:
    printf(" 【 error 】 
");
    break;
  }
}


//1.界面--->菜单--->模块
void makeMenu()
{
  printf("----------------------------------
");
  printf("  Eugeo图书管理借阅系统
");
  printf("t0.登记书籍
");
  printf("t1.浏览书籍
");
  printf("t2.借阅书籍
");
  printf("t3.归还书籍
");
  printf("t4.查找书籍
");
  printf("t5.排序书籍
");
  printf("t6.删除书籍
");
  printf("t7.退出系统
");
  printf("----------------------------------
");
  printf("请输入(0~7):");
}


int main()
{
  list = createHead();  //链表初始化
  readInfoFromFile("bookinfo.txt", list);
  while (1)
  {
    makeMenu();
    keyDown();
    system("pause");
    system("cls");
  }


}

 

图书管理系统分享就到此结束啦,大家赶紧试试吧!

  审核编辑:汤梓红

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分