21xrx.com
2024-11-22 08:07:39 Friday
登录
文章检索 我的文章 写文章
C++课程设计: 图书管理系统题目及代码
2023-06-28 09:27:34 深夜i     --     --
C++ 课程设计 图书管理系统 题目 代码

C++课程设计是每个计算机科学专业学生必不可少的一项任务,它不仅可以帮助学生巩固所学的知识,还可以锻炼学生的编程能力。而作为一门实用性较强的课程,图书管理系统是C++课程设计的常见题目。下面我们来分享一下一份图书管理系统的题目及代码。

题目:设计一个图书管理系统,要求实现以下功能:

1. 图书馆藏书量的查询、增加、删除、修改等功能。

2. 图书借阅、归还的管理,可以记录借阅者的信息,包括姓名、借阅时间、归还时间等。

3. 借阅者信息的管理,可以查询、增加、删除、修改借阅者的信息。

4. 生成图书借阅、归还记录。

5. 界面友好,操作简便。

代码如下:

#include

#include

#include

#include

#include

using namespace std;

struct Book

  string name;

  string author;

  string id;

  int count; // 图书馆藏书数量

;

struct Borrower

  string name;

  string id;

  string phone;

;

struct BorrowRecord

  string borrower_id;

  string book_id;

  time_t start_time;

  time_t end_time;

;

vector library; // 图书馆藏书

vector borrowers; // 借阅者信息

vector records; // 借阅记录

// 从文件中读取图书馆藏书信息

void readLibrary() {

  ifstream file("library.txt");

  Book book;

  while (file >> book.name >> book.author >> book.id >> book.count) {

    library.push_back(book);

  }

  file.close();

}

// 保存图书馆藏书信息到文件中

void saveLibrary() {

  ofstream file("library.txt");

  for (int i = 0; i < library.size(); i++) {

    file << library[i].name << " " << library[i].author << " " << library[i].id << " " << library[i].count << endl;

  }

  file.close();

}

// 从文件中读取借阅者信息

void readBorrowers() {

  ifstream file("borrowers.txt");

  Borrower borrower;

  while (file >> borrower.name >> borrower.id >> borrower.phone) {

    borrowers.push_back(borrower);

  }

  file.close();

}

// 保存借阅者信息到文件中

void saveBorrowers() {

  ofstream file("borrowers.txt");

  for (int i = 0; i < borrowers.size(); i++) {

    file << borrowers[i].name << " " << borrowers[i].id << " " << borrowers[i].phone << endl;

  }

  file.close();

}

// 从文件中读取借阅记录

void readRecords() {

  ifstream file("records.txt");

  BorrowRecord record;

  while (file >> record.borrower_id >> record.book_id >> record.start_time >> record.end_time) {

    records.push_back(record);

  }

  file.close();

}

// 保存借阅记录到文件中

void saveRecords() {

  ofstream file("records.txt");

  for (int i = 0; i < records.size(); i++) {

    file << records[i].borrower_id << " " << records[i].book_id << " " << records[i].start_time << " " << records[i].end_time << endl;

  }

  file.close();

}

// 添加图书

void addBook() {

  Book book;

  cout << "请输入书名:";

  cin >> book.name;

  cout << "请输入作者:";

  cin >> book.author;

  cout << "请输入书号:";

  cin >> book.id;

  cout << "请输入数量:";

  cin >> book.count;

  library.push_back(book);

  saveLibrary();

}

// 删除图书

void deleteBook() {

  string id;

  cout << "请输入要删除的书号:";

  cin >> id;

  for (int i = 0; i < library.size(); i++) {

    if (library[i].id == id) {

      library.erase(library.begin() + i);

      saveLibrary();

      cout << "删除成功!" << endl;

      return;

    }

  }

  cout << "未找到对应的书号!" << endl;

}

// 修改图书信息

void modifyBook() {

  string id;

  cout << "请输入要修改信息的书号:";

  cin >> id;

  for (int i = 0; i < library.size(); i++) {

    if (library[i].id == id) {

      cout << "当前书名为:" << library[i].name << endl;

      cout << "请输入新的书名:";

      cin >> library[i].name;

      cout << "当前作者为:" << library[i].author << endl;

      cout << "请输入新的作者:";

      cin >> library[i].author;

      cout << "当前数量为:" << library[i].count << endl;

      cout << "请输入新的数量:";

      cin >> library[i].count;

      saveLibrary();

      cout << "修改成功!" << endl;

      return;

    }

  }

  cout << "未找到对应的书号!" << endl;

}

// 查看图书信息

void showLibrary() {

  cout << "书名\t作者\t书号\t数量" << endl;

  for (int i = 0; i < library.size(); i++) {

    cout << library[i].name << "\t" << library[i].author << "\t" << library[i].id << "\t" << library[i].count << endl;

  }

}

// 添加借阅者

void addBorrower() {

  Borrower borrower;

  cout << "请输入姓名:";

  cin >> borrower.name;

  cout << "请输入学号/工号:";

  cin >> borrower.id;

  cout << "请输入电话号码:";

  cin >> borrower.phone;

  borrowers.push_back(borrower);

  saveBorrowers();

}

// 删除借阅者

void deleteBorrower() {

  string id;

  cout << "请输入要删除的学号/工号:";

  cin >> id;

  for (int i = 0; i < borrowers.size(); i++) {

    if (borrowers[i].id == id) {

      borrowers.erase(borrowers.begin() + i);

      saveBorrowers();

      cout << "删除成功!" << endl;

      return;

    }

  }

  cout << "未找到对应的学号/工号!" << endl;

}

// 修改借阅者信息

void modifyBorrower() {

  string id;

  cout << "请输入要修改信息的学号/工号:";

  cin >> id;

  for (int i = 0; i < borrowers.size(); i++) {

    if (borrowers[i].id == id) {

      cout << "当前姓名为:" << borrowers[i].name << endl;

      cout << "请输入新的姓名:";

      cin >> borrowers[i].name;

      cout << "当前电话为:" << borrowers[i].phone << endl;

      cout << "请输入新的电话号码:";

      cin >> borrowers[i].phone;

      saveBorrowers();

      cout << "修改成功!" << endl;

      return;

    }

  }

  cout << "未找到对应的学号/工号!" << endl;

}

// 查看借阅者信息

void showBorrowers() {

  cout << "姓名\t学号/工号\t电话" << endl;

  for (int i = 0; i < borrowers.size(); i++) {

    cout << borrowers[i].name << "\t" << borrowers[i].id << "\t" << borrowers[i].phone << endl;

  }

}

// 借阅图书

void borrowBook() {

  string book_id;

  string borrower_id;

  bool book_found = false;

  bool borrower_found = false;

  int book_count;

  cout << "请输入要借的书号:";

  cin >> book_id;

  for (int i = 0; i < library.size(); i++) {

    if (library[i].id == book_id) {

      book_count = library[i].count;

      if (book_count > 0) {

        book_found = true;

        library[i].count--;

        saveLibrary();

        break;

      } else

        cout << "该书已全部借出!" << endl;

        return;

    }

  }

  if (!book_found)

    cout << "未找到对应的书号!" << endl;

    return;

  cout << "请输入借阅者学号/工号:";

  cin >> borrower_id;

  for (int i = 0; i < borrowers.size(); i++) {

    if (borrowers[i].id == borrower_id) {

      borrower_found = true;

      BorrowRecord record;

      record.borrower_id = borrower_id;

      record.book_id = book_id;

      time(&record.start_time);

      record.end_time = 0;

      records.push_back(record);

      saveRecords();

      cout << "借阅成功!" << endl;

      break;

    }

  }

  if (!borrower_found)

    cout << "未找到对应的借阅者信息!" << endl;

}

// 归还图书

void returnBook() {

  string book_id;

  bool found = false;

  cout << "请输入要归还的书号:";

  cin >> book_id;

  for (int i = 0; i < records.size(); i++) {

    if (records[i].book_id == book_id) {

      found = true;

      for (int j = 0; j < library.size(); j++) {

        if (library[j].id == book_id) {

          library[j].count++;

          saveLibrary();

          break;

        }

      }

      time(&records[i].end_time);

      saveRecords();

      cout << "还书成功!" << endl;

      break;

    }

  }

  if (!found)

    cout << "未找到对应的借阅记录!" << endl;

}

// 生成借阅记录

void generateRecords() {

  ofstream file("borrow_records.txt");

  file << "借阅者\t书名\t借阅时间\t归还时间" << endl;

  for (int i = 0; i < records.size(); i++) {

    Borrower borrower;

    Book book;

    for (int j = 0; j < borrowers.size(); j++) {

      if (borrowers[j].id == records[i].borrower_id) {

        borrower = borrowers[j];

        break;

      }

    }

    for (int j = 0; j < library.size(); j++) {

      if (library[j].id == records[i].book_id) {

        book = library[j];

        break;

      }

    }

    tm* start = localtime(&records[i].start_time);

    tm* end = localtime(&records[i].end_time);

    file << borrower.name << "\t" << book.name << "\t"

      << start->tm_year + 1900 << "-" << start->tm_mon + 1 << "-" << start->tm_mday << " " << start->tm_hour << ":" << start->tm_min << "\t";

    if (records[i].end_time == 0)

      file << "未归还" << endl;

     else {

      file << end->tm_year + 1900 << "-" << end->tm_mon + 1 << "-" << end->tm_mday << " " << end->tm_hour << ":" << end->tm_min << endl;

    }

  }

  file.close();

  cout << "借阅记录已生成!" << endl;

}

int main() {

  readLibrary();

  readBorrowers();

  readRecords();

  while (true) {

    cout << "请选择您要进行的操作:" << endl;

    cout << "1. 添加图书" << endl;

    cout << "2. 删除图书" << endl;

    cout << "3. 修改图书信息" << endl;

    cout << "4. 查看图书信息" << endl;

    cout << "5. 添加借阅者" << endl;

    cout << "6. 删除借阅者" << endl;

    cout << "7. 修改借阅者信息" << endl;

    cout << "8. 查看借阅者信息" << endl;

    cout << "9. 借阅图书" << endl;

    cout << "10. 归还图书" << endl;

    cout << "11. 生成借阅记录" << endl;

    cout << "0. 退出程序" << endl;

    int opt;

    cin >> opt;

    if (opt == 0)

      break;

     else if (opt == 1) {

      addBook();

    } else if (opt == 2) {

      deleteBook();

    } else if (opt == 3) {

      modifyBook();

    } else if (opt == 4) {

      showLibrary();

    } else if (opt == 5) {

      addBorrower();

    } else if (opt == 6) {

      deleteBorrower();

    } else if (opt == 7) {

      modifyBorrower();

    } else if (opt == 8) {

      showBorrowers();

    } else if (opt == 9) {

      borrowBook();

    } else if (opt == 10) {

      returnBook();

    } else if (opt == 11) {

      generateRecords();

    } else

      cout << "无效操作!" << endl;

  }

  return 0;

}

  
  

评论区

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