holyya.com
2025-12-03 10:26:02 Wednesday
登录
文章检索 我的文章 写文章
C++图书管理系统简单代码
2023-07-13 10:06:40 深夜i     --     --
C++ 图书管理系统 简单代码

C++图书管理系统简单代码是绝大多数学习C++语言的学生所需要掌握的技能,因为它是基础、实用的代码之一。下面我们就来介绍一下C++图书管理系统简单代码。

C++图书管理系统简单代码分为以下几个模块:添加图书、删除图书、查看图书、借出图书和归还图书。这些模块的实现需要定义不同的函数实现,具体代码如下:

1. 添加图书模块:


#include <iostream>

#include <string>

#include <fstream>

using namespace std;

void add_book(string file_name) {

  string name, author, publisher;

  int number;

  cout << "请输入要添加的图书信息:\n";

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

  cin >> name;

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

  cin >> author;

  cout << "请输入出版社:";

  cin >> publisher;

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

  cin >> number;

  ofstream ofile(file_name, ios::app);//追加模式打开文件

  ofile << name << " " << author << " " << publisher << " " << number << endl;

  ofile.close();

}

2. 删除图书模块:


#include <iostream>

#include <string>

#include <fstream>

using namespace std;

void delete_book(string file_name) {

  string name, author;

  cout << "请输入要删除的图书信息:\n";

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

  cin >> name;

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

  cin >> author;

  ifstream ifile(file_name);//打开文件

  ofstream ofile("temp.txt");//临时文件

  string file_data;

  while (getline(ifile, file_data)) {

    if (file_data.find(name) != string::npos && file_data.find(author) != string::npos)

      continue;

    

    ofile << file_data << endl;

  }

  ifile.close();//关闭文件

  ofile.close();//关闭文件

  remove(file_name.c_str());//删除原文件

  rename("temp.txt", file_name.c_str());//将临时文件重命名为原文件

}

3. 查看图书模块:


#include <iostream>

#include <string>

#include <fstream>

using namespace std;

void show_book(string file_name) {

  ifstream ifile(file_name);//打开文件

  string file_data;

  cout << "所有图书如下:\n";

  while (getline(ifile, file_data))

    cout << file_data << endl;

  

  ifile.close();//关闭文件

}

4. 借出图书模块:


#include <iostream>

#include <string>

#include <fstream>

using namespace std;

void lend_book(string file_name) {

  string name, author;

  int number;

  cout << "请输入要借出的图书信息:\n";

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

  cin >> name;

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

  cin >> author;

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

  cin >> number;

  ifstream ifile(file_name);//打开文件

  ofstream ofile("temp.txt");//临时文件

  string file_data;

  bool found = false;

  while (getline(ifile, file_data)) {

    if (file_data.find(name) != string::npos && file_data.find(author) != string::npos) {

      found = true;

      int total_num = stoi(file_data.substr(file_data.find_last_of(' ') + 1));

      int left_num = total_num - number;

      if (left_num < 0) {

        cout << "借出图书数量错误!\n";

        ofile << file_data << endl;

        continue;

      }

      file_data = file_data.substr(0, file_data.find_last_of(' ') + 1) + to_string(left_num);

    }

    ofile << file_data << endl;

  }

  ifile.close();//关闭文件

  ofile.close();//关闭文件

  remove(file_name.c_str());//删除原文件

  rename("temp.txt", file_name.c_str());//将临时文件重命名为原文件

  if (!found) {

    cout << "没有找到该图书!\n";

  }

}

5. 归还图书模块:


#include <iostream>

#include <string>

#include <fstream>

using namespace std;

void return_book(string file_name) {

  string name, author;

  int number;

  cout << "请输入要归还的图书信息:\n";

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

  cin >> name;

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

  cin >> author;

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

  cin >> number;

  ifstream ifile(file_name);//打开文件

  ofstream ofile("temp.txt");//临时文件

  string file_data;

  bool found = false;

  while (getline(ifile, file_data)) {

    if (file_data.find(name) != string::npos && file_data.find(author) != string::npos) {

      found = true;

      int total_num = stoi(file_data.substr(file_data.find_last_of(' ') + 1));

      int left_num = total_num + number;

      if (left_num > total_num) {

        cout << "归还图书数量错误!\n";

        ofile << file_data << endl;

        continue;

      }

      file_data = file_data.substr(0, file_data.find_last_of(' ') + 1) + to_string(left_num);

    }

    ofile << file_data << endl;

  }

  ifile.close();//关闭文件

  ofile.close();//关闭文件

  remove(file_name.c_str());//删除原文件

  rename("temp.txt", file_name.c_str());//将临时文件重命名为原文件

  if (!found) {

    cout << "没有找到该图书!\n";

  }

}

以上就是C++图书管理系统简单代码的实现,它是一种非常实用的C++编程技能,对于想要了解C++语言的人来说,掌握这些技能是非常必要的。如果您现在正在学习C++,那么一定要多多练习,并掌握以上技能,这对您未来的编程学习将有很大的帮助。

  
  

评论区

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