图书馆管理系统模型轮廓(C++)
最近在学习C++,主要是跟着黑马程序员的课程学习,在看完第一部分之后,想着利用学习的知识点实现一个简单的图书馆管理系统,花费两个小时做出了简单的轮廓,没想到效果不错。
基本情况
使用软件:VS
涉及知识点:switch 数组 函数 goto txt文件的读取写入(参考GPT) 头文件 结构体
效果展示
在存放代码的位置创建一个txt文件,存放每一本书的信息。每一本书占用5行,如下图。共有25行,有5本书。每本书由name,type,time,number,rest这5部分组成。
运行代码,根据提示操作之后,打开txt文件。
需要按照我的流程来才行,因为大部分内容还没有完全建立。
发现确实可以更改其中的数据,如第二本书傲慢与偏见的类型,由003变成022。
代码
MAIN.cpp
#include "USER.h"
using namespace std;
int main()
{
Print1();
int a;
cin >> a;
switch (a)
{
case 0: user_Administrator(); break;
case 1: user_registered(); break;
case 2: user_visitor(); break;
case 3: return main(); break;
default:return main();break;
}
system("pause");
return 0;
}
USER.h
#include<iostream>
using namespace std;
void Print1();
void user_Administrator();
void user_registered();
void user_visitor();
USER.cpp
#include "USER.h"
#include "BOOK.h"
void Print1()
{
cout << "Welcome to Library Management System" << endl;
cout << "Follow the prompts to select the appropriate number" << endl;
cout << "Administrator login please press number 0" << endl;
cout << "For registered user login, please press number 1" << endl;
cout << "For visitor login, please press number 2" << endl;
cout << "To return to the main screen, please press 3" << endl;
}
void user_Administrator()
{
cout << " Login successfully" << endl;
cout << "Welcome to the administrator interface" << endl;
BOOK();
}
void user_registered()
{
cout << " Login successfully" << endl;
cout << "Welcome to the registered user interface" << endl;
}
void user_visitor()
{
cout << " Login successfully" << endl;
cout << "Welcome to the visitor interface" << endl;
}
BOOK.h
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int BOOK();
BOOK.cpp
#include "BOOK.h"
struct BOOK
{
string name;
string type;
string time;
string number;
string residue;
};
int BOOK()
{
ifstream input_file("input.txt");
if (!input_file.is_open())
{
cout << "Failed to open input file!" << endl;
return 1;
}
string line;
string Line[25];
struct BOOK book;
int i = 0;
while (getline(input_file, line))
{
//cout << line << endl;
Line[i] = line;
i++;
}
FLAG2:
cout << "The following books are now available"<< endl;
cout << "[1]: " << Line[0] << endl;
cout << "[2]: " << Line[5] << endl;
cout << "[3]: " << Line[10] << endl;
cout << "[4]: " << Line[15] << endl;
cout << "[5]: " << Line[20] << endl;
FLAG:
int j = 1;
cout << "Please enter the book you want to search " << j << endl;
cin >> j;
cout << "Information about book " << j << endl << endl;
j = (j - 1) * 5;
book.name = Line[j];
book.type = Line[j+1];
book.time = Line[j+2];
book.number = Line[j+3];
book.residue = Line[j+4];
cout << "book's name: ";
cout << book.name << endl;
cout << "book's type: ";
cout << book.type << endl;
cout << "book's time: ";
cout << book.time << endl;
cout << "book's number: ";
cout << book.number << endl;
cout << "book's residue: ";
cout << book.residue << endl;
cout << "Continue query (please press 1), modify (2) " << endl;
int p = 0;
cin >> p;
if (p == 1)
goto FLAG;
if (p == 2)
{
int r;
string rr;
FLAG1:
cout << "Please enter the sequence to be modified (name is 1, remaining is 5)" << endl;
cin >> r;
if(r == 1)
{
cout << "The content you want to modify is name" << endl;
cout << "Please enter the content you want to enter" << endl;
cin >> rr;
}
if (r == 2)
{
cout << "The content you want to modify is type" << endl;
cout << "Please enter the content you want to enter" << endl;
cin >> rr;
}
if (r == 3)
{
cout << "The content you want to modify is time" << endl;
cout << "Please enter the content you want to enter" << endl;
cin >> rr;
}
if (r == 4)
{
cout << "The content you want to modify is book's number" << endl;
cout << "Please enter the content you want to enter" << endl;
cin >> rr;
}
if (r == 5)
{
cout << "The content you want to modify is book's residue" << endl;
cout << "Please enter the content you want to enter" << endl;
cin >> rr;
}
if (r > 5 || r <1)
{
cout << "The input is incorrect, return to the previous operation" << endl;
goto FLAG1;
}
Line[j + r - 1] = rr;
goto FLAG2;
}
std::ofstream output_file("input.txt");
if (!output_file.is_open())
{
std::cout << "Failed to open output file!" << std::endl;
return 1;
}
for (int i = 0; i < 25; i++)
{
output_file << Line[i] << endl;
}
input_file.close();
return 0;
}
注意
以上代码纯属自娱自乐,逻辑可能比较混乱,不适合想高效学习C++知识的人。
utput file!" << std::endl;
return 1;
}
for (int i = 0; i < 25; i++)
{
output_file << Line[i] << endl;
}
input_file.close();
return 0;
}
## 注意
以上代码纯属自娱自乐,逻辑可能比较混乱,不适合想高效学习C++知识的人。
有需要的同学可以拷贝下来在此基础上进行修改,对其进行扩充过程中相信您也会学到很多东西。