21xrx.com
2024-11-10 00:58:25 Sunday
登录
文章检索 我的文章 写文章
C++ 图书馆管理系统源代码
2023-06-22 17:47:21 深夜i     --     --
C++ 图书馆管理系统 源代码 图书 借阅记录

C++是一门非常流行的编程语言,被广泛应用于各种领域,尤其是软件开发领域。图书馆管理系统是C++应用的一个重要领域之一,也是需要高效、可靠的程序实现的。

下面我们介绍一份C++图书馆管理系统的源代码,这份源代码基于C++语言编写,是一个较为完整的图书馆管理系统。

该系统的功能主要包括:添加图书、删除图书、查询图书、借阅图书、归还图书、图书管理等功能。

以下是该系统的源代码:

(代码中注释中的内容解释了源代码的作用)


#include<iostream>

#include<cstdio>

#include<cstring>

using namespace std;

struct st{ //图书结构体

  int bookID; //图书ID

  char bookName[100]; //图书名字

  char author[100]; //作者名字

  char publisher[100]; //出版社名字

  int publishYear; //出版年份

  int borrowTime; //借出时间

  int returnTime; //归还时间

  bool hasBorrowed; //是否借出

}book[100]; //图书数组

int cnt=0; //图书个数

void output(const st& x){ //输出一个图书

  printf("ID:%d Name:%s Author:%s Publisher:%s Publish Year:%d ",x.bookID,x.bookName,x.author,x.publisher,x.publishYear);

  if(x.hasBorrowed==1) printf("Has been borrowed. Borrow time:%d Return time:%d \n",x.borrowTime,x.returnTime);

  else printf("Hasn't been borrowed. \n");

  return;

}

int find(int _id){ //根据ID寻找图书,返回所在的编号,没有则返回-1

  int res=-1;

  for(int i=0;i<cnt;i++){

    if(book[i].bookID==_id) res=i;

  }

  return res;

}

void add(){ //添加图书

  if(cnt==100){printf("Sorry, the library is full.\n");return;}

  printf("Please input the book's name:");

  char tmp[100];

  cin>>tmp;

  strcpy(book[cnt].bookName,tmp);

  printf("Please input the book's author:");

  cin>>tmp;

  strcpy(book[cnt].author,tmp);

  printf("Please input the book's publisher:");

  cin>>tmp;

  strcpy(book[cnt].publisher,tmp);

  printf("Please input the book's publish year:");

  cin>>book[cnt].publishYear;

  book[cnt].hasBorrowed=0;

  book[cnt].borrowTime=book[cnt].returnTime=-1;

  book[cnt].bookID=cnt+1;

  cnt++;

  printf("Successfully added the book. The book's ID is %d.\n",cnt);

  return;

}

void del(){ //删除图书

  printf("Please input the ID of the book you want to delete:");

  int _id;

  cin>>_id;

  int pos=find(_id);

  if(pos==-1){printf("The book does not exsit.\n");return;}

  else{

    for(int i=pos;i<cnt-1;i++){

      book[i]=book[i+1];

      book[i].bookID--;

    }

    cnt--;

    printf("The book has been deleted.\n");

  }

  return;

}

void query(){ //查询图书

  printf("Please input the ID of the book you want to query:");

  int _id;

  cin>>_id;

  int pos=find(_id);

  if(pos==-1){printf("The book does not exsit.\n");return;}

  else output(book[pos]);

  return;

}

void borrow(){ //借出图书

  printf("Please input the ID of the book you want to borrow:");

  int _id;

  cin>>_id;

  int pos=find(_id);

  if(pos==-1){printf("The book does not exsit.\n");return;}

  else if(book[pos].hasBorrowed==1){printf("The book has been borrowed.\n");return;}

  else{ //借出图书

    book[pos].hasBorrowed=1;

    printf("Please input the borrow time:");

    cin>>book[pos].borrowTime;

    printf("Please input the return time:");

    cin>>book[pos].returnTime;

    printf("The book has been borrowed.\n");

  }

  return;

}

void getback(){ //归还图书

  printf("Please input the ID of the book you want to get back:");

  int _id;

  cin>>_id;

  int pos=find(_id);

  if(pos==-1){printf("The book does not exsit.\n");return;}

  else if(book[pos].hasBorrowed==0){printf("The book has not been borrowed.\n");return;}

  else{ //归还图书

    book[pos].hasBorrowed=0;

    book[pos].borrowTime=book[pos].returnTime=-1;

    printf("The book has been returned.\n");

  }

  return;

}

void menu(){ //输出主菜单

  printf("---------------------\nLibrary Manager\n---------------------\n");

  printf("1.Add a book.\n2.Delete a book.\n3.Query a book.\n4.Borrow a book.\n5.Get back a book.\n6.Manage books.\n7.Exit.\n");

  printf("---------------------\n");

  return;

}

int main(){

  while(1){

    menu();

    int opt;

    cin>>opt;

    if(opt==1) add();

    else if(opt==2) del();

    else if(opt==3) query();

    else if(opt==4) borrow();

    else if(opt==5) getback();

    else if(opt==6){

      printf("There are %d books in the library.\n",cnt);

      printf("Do you want to see all the books?('y' or 'n')\n");

      char tmp;

      cin>>tmp;

      if(tmp=='y'){

        for(int i=0;i<cnt;i++) output(book[i]);

      }

    }

    else if(opt==7) break;

    else printf("Invalid option.\n");

  }

  return 0;

}

总体来说,这份源代码相对比较简单,但基本实现了一个图书馆管理系统应有的功能。这份源代码能够帮助初学者了解C++编程语言的基本语法,并且能够对图书馆管理系统有一个相对完整的认识。由于代码长度较短,适合初学者进行边学边改进,不断提升代码质量和功能完整性。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复