课程设计c++实现图书管理系统
## 编译环境Visual C++,此程序解决了涉及到的图书馆里基本功能。 由于程序很长,先放效果图,进入界面,通过密码进入相应的功能,学生信息可通过管理员系统录入,管理员密码要提前在password.txt中录入。管理员系统可实现的功能学生系统可实现的功能首先建立book.txt存储图书信息、password.txt存储账户密码、student.txt存储学生信息建立工程 图书管...
·
编译环境Visual C++ 6.0,此程序解决了涉及到的图书管理基本功能。 由于程序很长,先放效果图
进入界面,通过密码进入相应的功能,学生信息可通过管理员系统录入,管理员密码要提前在password.txt中录入。
管理员系统可实现的功能
学生系统可实现的功能
部分功能图示
以下为详细程序
首先建立book.txt存储图书信息、password.txt存储账户密码、student.txt存储学生信息
建立工程 图书管理系统,用来连接程序
头文件
student.h
#pragma once
#include<string>
#include<vector>
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
class Student
{
private:
int id; //学生账号
int year; //年级
string name;
string password;
string gender;
string telephone;
string address; //姓名、性别、电话、地址
vector<int> book;
vector<int> grade; //student's books and their marks
public:
Student();
~Student();
Student(int a, int b, string c, string d, string e, string f, string g);
int get_id();
int get_year();
string get_name();
string get_pass();
string get_gend();
string get_tele();
string get_addr();
vector<int> get_book();
void change(int a, int b, string c, string d, string e, string f, string g);
void display();
int length();
bool canopen();
void write();
void read(int n);
void write_book();
void read_book();
void change_book(int a, int b);
void add_book(int a);
void display_book();
void add_student();
void sub_student();
bool is_same_book(int a);
};
book.h
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include <iomanip>
using namespace std;
class Book
{
private:
string Id;
string Name;
string Author;
string Cat;
string Location;
string Addr;
string Time;
string Price;
public:
Book();
~Book();
// 编号 书名 作者 分类号 位置 出版社 出版时间 单价
Book ( string a, string b, string c, string d, string e, string f, string g, string h );
string get_Id();
string get_Name();
string get_Author();
string get_Location();
string get_Cat();
string get_Addr();
string get_Time();
string get_Price();
void change( string a, string b, string c, string d, string e, string f, string g, string h );
void display();
int length();
bool canopen(); //判断
void write(); //增添
void read(int n); //读取
};
main.h
#include<iostream>
#include<string>
#include<vector>
#include"book.h"
#include"student.h"
#include <iomanip>
using namespace std;
void initialize();
bool is_administrator();
bool is_student(int *n);
void menu1();
void menu2();
void menu3();
void wrong_input();
void mag_book();
void mag_student();
void show_book_list();
void show_student_list();
void give_mark();
void change_password();
void choose_book(int n);
void my_book(int n);
void check_info(int n);
void can_open(Book a);
void can_open(Student a);
bool is_same_student_name(string n);
bool is_same_student_tele(string n);
bool is_same_student_addr(string n);
//bool is_same_book_name(string n);
bool is_same_book_name(string n);
bool is_same_Book_id(string n);
void showbookh();
void SetScreenGrid(int x,int y);
void mag_book2();
/
类定义文件
book.cpp
#include"book.h"
#include"student.h" /*****************已改**************/
#include"main.h"
#include<string>
Book::Book()
{
Id = "0";
Name = "empty";
Author= "empty";
Cat = "empty";
Location = "empty";
Addr = "empty";
Time = "empty";
Price= "0";
}
Book::~Book() {}
Book::Book( string a, string b, string c, string d, string e, string f, string g, string h )
{
Id = a;
Name = b;
Author= c;
Cat = d;
Location = e;
Addr = f;
Time = g;
Price = h;
}
///
string Book::get_Id()
{
return Id;
}
string Book::get_Name()
{
return Name;
}
string Book::get_Author()
{
return Author;
}
string Book::get_Cat()
{
return Cat;
}
string Book::get_Location()
{
return Location;
}
string Book::get_Addr()
{
return Addr;
}
string Book::get_Time()
{
return Time;
}
string Book::get_Price()
{
return Price;
}
///
void Book::change( string a, string b, string c, string d, string e, string f, string g, string h )
{
Id = a;
Name = b;
Author= c;
Cat = d;
Location = e;
Addr = f;
Time = g;
Price = h;
}
void Book::display()
{
cout <<"*"<< setw(10) << Id;
cout <<" *"<< setw(10) << Name;
cout <<" *"<< setw(10) << Author ;
cout <<" *"<< setw(10) << Cat ;
cout <<" *"<< setw(10) << Location ;
cout <<" *"<< setw(10) << Addr ;
cout <<" *"<< setw(10) << Time;
cout <<" *"<< setw(10) << Price<<"*" ;
cout << endl;//一条纪录
///
}
int Book::length()
{
int i = 0;
string temp;
ifstream fin("book.txt");
while (getline(fin, temp))
i += 1;
fin.close();
return i;
}
bool Book::canopen()
{
ifstream fin1("book.txt");
if (fin1)
return 1;
else
return 0;
fin1.close();
}
void Book::write( )
{
ofstream outfile("book.txt", ios::app);
outfile << Id << "\t" << Name << "\t" << Author << "\t" <<Cat << "\t" << Location << "\t" <<Addr << "\t" <<Time << "\t"<<Price << "\t" << endl;
outfile.close();
}
void Book::read(int n)
{
int i = 0;
string temp, data[999], a[8];
ifstream fin("book.txt");
while (getline(fin, temp))
{
data[i] = temp;
i += 1;
}
fin.close();
istringstream stream(data[n]);
for (i = 0; i < 8; i++)
{
data[n].erase(0, data[n].find("\t") + 1);
a[i] = data[n].substr(0, data[n].find("\t"));
}
Id = a[1];
Name = a[2];
Author= a[3];
Cat = a[4];
Location = a[5];
Addr = a[6];
Time = a[7];
Price = a[8];
stream >>Id >> Name >> Author >>Cat >> Location >>Addr>>Time >>Price ;
}
student.cpp
#include"book.h"
#include"student.h"
#include"main.h"
Student::Student()
{
id = 0;
year = 0;
name = "not given";
password = "not given";
gender = "not given";
telephone = "not given";
address = "not given"; //define the default constructor
}
Student::~Student() {}
Student::Student(int a, int b, string c, string d, string e, string f, string g)
{
id = a;
year = b;
name = c;
password = d;
gender = e;
telephone = f;
address = g; //define the normal constructor
}
int Student::get_id()
{
return id;
}
int Student::get_year()
{
return year;
}
string Student::get_name()
{
return name;
}
string Student::get_pass()
{
return password;
}
string Student::get_gend()
{
return gender;
}
string Student::get_tele()
{
return telephone;
}
string Student::get_addr()
{
return address;
}
vector<int> Student::get_book()
{
return book;
}
void Student::change(int a, int b, string c, string d, string e, string f, string g)
{
id = a;
year = b;
name = c;
password = d;
gender = e;
telephone = f;
address = g;
}
void Student::display()
{
cout << setw(10) << name<< setw(10) <<id << setw(10) << year << setw(10) <<gender << setw(12) <<telephone<< setw(10) <<address <<endl;
}
int Student::length()
{
int i = 0;
string temp;
ifstream fin("student.txt");
while (getline(fin, temp))
i += 1;
fin.close();
return i;
}
bool Student::canopen()
{
ifstream fin1("student.txt");
ifstream fin2("mybook.txt");
if (fin1&&fin2)
return 1;
else
return 0;
fin1.close();
fin2.close();
}
void Student::write()
{
ofstream fout("student.txt", ios::app);
fout << id << "\t" << year << "\t" << name << "\t" << password << "\t" << gender << "\t" << telephone << "\t" << address << endl;
fout.close();
}
void Student::read(int n)
{
int i = 0;
string temp, data[999], a[6];
ifstream fin("student.txt");
while (getline(fin, temp))
{
data[i] = temp;
i += 1;
}
fin.close();
istringstream stream(data[n]);
for (i = 0; i < 6; i++)
{
data[n].erase(0, data[n].find("\t") + 1);
a[i] = data[n].substr(0, data[n].find("\t"));
}
name = a[1];
password = a[2];
gender = a[3];
telephone = a[4];
address = a[5];
stream >> id >> year;
}
void Student::write_book()
{
int i, n, l = 0;
string data[999], temp;
ifstream fin("mybook.txt");
while (getline(fin, temp))
{
data[l] = temp;
l += 1;
}
fin.close();
ofstream fout("mybook.txt");
for (i = 0; i < l; i++)
{
istringstream stream(data[i]);
stream >> n;
if (n == id)
{
fout << id;
for (int i = 0; i < book.size(); i++)
fout << "\t" << book[i] << "\t" << grade[i];
fout << endl;
}
else
fout << data[i] << endl;
}
fout.close();
}
void Student::read_book()
{
int i = 0, x, y, n;
string data[999], temp;
ifstream fin("mybook.txt");
while (getline(fin, temp))
{
data[i] = temp;
i += 1;
}
fin.close();
for (i = 0; i < 999; i++)
{
istringstream stream(data[i]);
stream >> n;
if (id == n)
while (stream >> x >> y)
{
book.push_back(x);
grade.push_back(y);
}
}
}
void Student::change_book(int a, int b)
{
int i;
for (i = 0; i < book.size(); i++)
if (book[i] == a)
grade[i] = b;
}
void Student::add_book(int a)
{
book.push_back(a);
grade.push_back(-1);
}
void Student::display_book()
{
int i;
for (i = 0; i < book.size(); i++)
{
cout << book[i] << "\t\t";
if (grade[i] == -1)
cout << "None." << endl;
else
cout << grade[i] << endl;
}
}
void Student::add_student()
{
ofstream fout("mybook.txt", ios::app);
fout << id << endl;
fout.close();
}
void Student::sub_student()
{
int i = 0, n, m, l;
string data[999], temp;
ifstream fin("mybook.txt");
while (getline(fin, temp))
{
data[i] = temp;
i += 1;
}
fin.close();
l = i;
for (i = 0; i < l; i++)
{
istringstream stream(data[i]);
stream >> n;
if (id == n)
m = i;
}
ofstream fout("mybook.txt");
for (i = 0; i < l; i++)
if (i != m)
fout << data[i] << endl;
fout.close();
}
bool Student::is_same_book(int a)
{
int i;
bool success = 0;
for (i = 0; i < book.size(); i++)
if (book[i] == a)
success = 1;
return success;
}
/
显示模块
#include"main.h"
void menu1()
{
system("cls");
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << " ***************************************" << endl;
cout << " *** 请选择登录类型 ***" << endl;
cout << " *** ***" << endl;
cout << " *** ***" << endl;
cout << " *** 1 管理人员系统 2 学生管理系统 ***" << endl;
cout << " *** ***" << endl;
cout << " *** 0 退出 ***" << endl;
cout << " ***************************************" << endl;
cout << "Please input your choice:";
}
#include"main.h"
void menu2()
{
system("cls");
cout << endl;
cout << endl;
cout <<" ***************************************" << endl;
cout <<" *** 图书管理系统 ***" << endl;
cout <<" ***************************************" << endl;
cout <<" *** 请选择功能: ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 1 图书管理 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 2 学生管理 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 3 显示图书信息 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 4 显示学生信息 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 5 修改管理员密码 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 6 返回主菜单 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 0 退出 ***" << endl;
cout <<" *** ***" << endl;
cout <<" ***************************************" << endl;
cout << "Please input your choice:";
}
#include"main.h"
void menu3()
{
system("cls");
cout << endl << endl << endl;
cout <<" ***************************************" << endl;
cout <<" *** 学生系统 ***" << endl;
cout <<" ***************************************" << endl;
cout <<" *** 请选择功能: ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 1 浏览已借阅书籍 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 2 显示图书列表 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 3 查找图书 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 4 检查我的书和成绩 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 5 检查并更改我的信息 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 6 返回主菜单 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 0 退出 ***" << endl;
cout <<" *** ***" << endl;
cout <<" ***************************************" << endl;
cout << "Please input your choice:";
}
#include"main.h"
void SetScreenGrid(int x,int y) //设置控制台大小
{
char sysSetBuf[80];
sprintf(sysSetBuf,"mode con cols=%d lines=%d",x ,y);
system(sysSetBuf);
}
#include"main.h"
void showbookh()
{
cout <<"*"<< setw(10) << "编号";
cout <<" *"<< setw(10) <<"书名";
cout <<" *"<< setw(10) << "作者" ;
cout <<" *"<< setw(10) << "分类号";
cout <<" *"<< setw(10) << "位置" ;
cout <<" *"<< setw(10) << "出版社" ;
cout <<" *"<< setw(10) << "出版时间";
cout <<" *"<< setw(10) << "价格"<<"*" ;
cout << endl;//一条纪录
}
#include"main.h"
void wrong_input()
{
system("cls");
cout << endl << endl << endl << endl << endl << endl;
cout << " 输入错误! 请重新输入" << endl;
system("pause");
}
/
管理员功能
#include"main.h"
void change_password()//修改密码
{
string p1, p2, p3;
system("cls");
cout << endl << " 修改管理员密码." << endl << endl;
cout << "请输入原密码:";
getline(cin, p1);
ifstream infile("password.txt");
if (!infile)
{
cout << endl << "Out of service" << endl;
cout << "Please press enter to exit." << endl;
system("pause");
exit(0);
}
getline(infile, p2);
infile.close();
if (p1 == p2)
{
cout << "请输入新的密码:";
cin >> p3;
ofstream outfile("password.txt",ios::out);
outfile << p3;
outfile.close();
cout << "管理员密码已修改.";
}
else
cout << "密码错误!.";
cout << endl;
system("pause");
}
#include"main.h"
void mag_book()
{
int i;
string tem;
string id, name, author, cat , location ,addr, time ,price;
char choice;
bool tempi = 0, tempi2 = 0;
Book a[50];
Book c;
do {
fflush(stdin);//清空输入缓冲区
system("cls");
cout << endl;
cout <<" ***************************************" << endl;
cout <<" *** 图书管理 ***" << endl;
cout <<" ***************************************" << endl;
cout <<" *** 请选择功能: ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 1 查找书籍 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 2 添加书籍 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 3 修改书籍 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 4 删除书籍 ***" << endl;
cout <<" *** ***" << endl;
cout <<" ***************************************" << endl;
cout << "Please input your choice:";
cin >> choice;
cout <<endl;
/*********************************************** 查找图书 */
if (choice == '1')
{
cout << "请输入图书的编号、书名或作者进行查找!";
cin >> tem;
tempi2 = 1;
fflush(stdin);
for (i = 0; i < c.length(); i++)
a[i].read(i);
for (i = 0; i < c.length(); i++)
if (tem == a[i].get_Id()||tem == a[i].get_Name()||tem == a[i].get_Author())
{
system("cls");
showbookh();
a[i].display();
tempi = 1;
}
if (!tempi)
cout << "不能找到此书.";
}
/********************************************** 添加图书 **/
else if (choice == '2')
{
cout << "请输入图书的编号";
cin >> id;
tempi2 = 1;
fflush(stdin);
for (i = 0; i < c.length(); i++)
{
a[i].read(i);
if (id == a[i].get_Id())
tempi = 1;
}
if (tempi)
cout << "已存在此编号书籍!";
else
{
cout << "请输入图书的书名:";
cin >> name;
cout << "请输入图书的作者:";
cin >> author;
cout << "请输入图书的分类号:";
cin >> cat;
cout << "请输入图书的位置:";
cin >> location;
cout << "请输入图书的出版社:";
cin >> addr;
cout << "请输入图书的出版时间:";
cin >> time;;
cout << "请输入图书的价格:";
cin >> price;
c.change(id, name, author, cat , location ,addr, time , price);
c.write();
system("cls");
cout << " 图书已保存!." << endl << endl;
showbookh();
c.display();
}
}
/********************************************** 修改图书 **/
else if (choice == '3')
{
cout << "请输入要修改的图书的编号:";
cin >> id;
tempi2 = 1;
fflush(stdin);
int l ,n ;
l = c.length();
for (i = 0; i < l; i++)
{
a[i].read(i);
if (id == a[i].get_Id())
{
n = i;
tempi = 1;
}
}
if (tempi)
{
cout << "编号修改为:";
cin >> id;
cout << "书名修改为:";
cin >> name;
cout << "作者修改为:";
cin >> author;
cout << "分类号修改为:";
cin >> cat;
cout << "位置修改为:";
cin >>location;
cout << "出版社修改为:";
cin >> addr;
cout << "出版时间修改为:";
cin >> time;
cout << "价格修改为:";
cin >> price;
a[n].change(id, name, author, cat , location ,addr, time , price);
ofstream fout("book.txt");
fout.close();
for (i = 0; i < l; i++)
a[i].write();
system("cls");
cout << "已修改此书!" << endl << endl;
a[n].display();
}
else
cout << "编号为 " << id << " 的书未找到!";
}
/********************************************** 删除图书 **/
else if (choice == '4')
{
cout << "请输入要删除的图书的编号:";
cin >> id;
tempi2 = 1;
fflush(stdin);
int n, l = c.length();
for (i = 0; i < l; i++)
{
a[i].read(i);
if (id == a[i].get_Id())
{
n = i;
tempi = 1;
}
}
if (tempi)
{
ofstream fout("book.txt");
fout.close();
for (i = 0; i < l - 1; i++)
if (i != n)
a[i].write();
system("cls");
cout << "已删除此书!" << endl << endl;
a[n].display();
}
else
cout << "编号为" << id << " 的书未发现!.";
}
/************************************************/
else
{
cout << "输入错误,请重新输入" << endl;
system("pause");
}
} while (!tempi2);
cout << endl;
system("pause");
}
#include"main.h"
void mag_student()
{
int i, id, year;
char choice;
bool success = 0, success2 = 0;
string name, pass, gend, tele, addr;
Student a[50];
Student s;
do {
system("cls");
cout << endl;
cout <<" ***************************************" << endl;
cout <<" *** 学生管理 ***" << endl;
cout <<" ***************************************" << endl;
cout <<" *** 请选择功能: ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 1 查询学生信息 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 2 添加学生信息 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 3 修改学生信息 ***" << endl;
cout <<" *** ***" << endl;
cout <<" *** 4 删除学生信息 ***" << endl;
cout <<" *** ***" << endl;
cout <<" ***************************************" << endl;
cin >> choice;
cout << "请输入学生ID:";
cin >> id;
getchar();
can_open(s);
//查询
if (choice == '1')
{
success2 = 1;
fflush(stdin);
for (i = 0; i < s.length(); i++)
a[i].read(i);
for (i = 0; i < s.length(); i++)
if (id == a[i].get_id())
{
system("cls");
cout << setw(10) <<"姓名" << setw(10) <<"账号" << setw(10) <<"年级" << setw(10) <<"性别" << setw(12) <<"电话" << setw(10) <<"地址" <<endl;
a[i].display();
success = 1;
}
if (!success)
cout << "不能找到该学生信息.";
}
//添加
else if (choice == '2')
{
success2 = 1;
fflush(stdin);
for (i = 0; i < s.length(); i++)
{
a[i].read(i);
if (id == a[i].get_id())
success = 1;
}
if (success)
cout << "已存在改学生账号信息";
else
{
do {
cout << "请输入姓名:";
getline(cin, name);
} while (is_same_student_name(name));
cout << "请输入密码:";
getline(cin, pass);
do {
cout << "输入学生是哪个年级? (1-4)";
cin >> year;
} while (year < 1 || year>4);
fflush(stdin);
do {
cout << "请输入性别:" << endl << " 输入 'male' 或 'female' :";
getline(cin, gend);
} while (!(gend == "male" || gend == "female"));
do {
cout << "请输入电话号码:";
getline(cin, tele);
} while (is_same_student_tele(tele));
do {
cout << "请输入地址:";
getline(cin, addr);
} while (is_same_student_addr(addr));
s.change(id, year, name, pass, gend, tele, addr);
s.add_student();
s.write();
system("cls");
cout << "学生信息已保存." << endl << endl;
s.display();
}
}
//修改
else if (choice == '3') //修改我的信息
{
success2 = 1;
fflush(stdin);
int l, n;
l = s.length();
for (i = 0; i < l; i++)
{
a[i].read(i);
if (id == a[i].get_id())
{
n = i;
success = 1;
}
}
if (success)
{
do {
cout << "请输入新的账户名 " << id << ":";
getline(cin, name);
} while (is_same_student_name(name));
pass = a[n].get_pass();
do {
cout << "请输入年级? (1-4)";
cin >> year;
} while (year < 1 || year>4);
fflush(stdin);
do {
cout << "请输入性别:" << endl << " 'male' or 'female' :";
getline(cin, gend);
} while (!(gend == "male" || gend == "female"));
do {
cout << "请输入新的电话号码:";
getline(cin, tele);
} while (is_same_student_tele(tele));
do {
cout << "请输入新的地址:";
getline(cin, addr);
} while (is_same_student_addr(addr));
a[n].change(id, year, name, pass, gend, tele, addr);
ofstream fout("student.txt");
fout.close();
for (i = 0; i < l; i++)
a[i].write();
system("cls");
cout << "学生信息修改完成!" << endl << endl;
a[n].display();
}
else
cout << "The student " << id << " cannot be found.";
}
//删除
else if (choice == '4')
{
success2 = 1;
fflush(stdin);
int n, l = s.length();
for (i = 0; i < l; i++)
{
a[i].read(i);
if (id == a[i].get_id())
{
n = i;
success = 1;
}
}
if (success)
{
a[n].sub_student();
ofstream fout("student.txt");
fout.close();
for (i = 0; i < l; i++)
if (i != n)
a[i].write();
system("cls");
cout << "学生信息已删除." << endl << endl;
a[n].display();
}
else
cout << "The student " << id << " cannot be found.";
}
else
{
cout << "输入错误,请重新输入." << endl;
system("pause");
}
} while (!success2);
cout << endl;
system("pause");
}
#include"main.h"
void show_book_list()
{
Book a[100];
Book c;
int i;
system("cls");
cout << endl << "*****************************************************图书信息*************************************************" << endl << endl;
can_open(c);
showbookh();
for (i = 0; i < c.length(); i++)
{
a[i].read(i);
a[i].display();
}
cout << endl;
system("pause");
}
#include"main.h"
void show_student_list()
{
Student a[100];
Student s;
int i;
system("cls");
cout << endl << " ******************学生信息*******************" << endl << endl;
can_open(s);
cout << setw(10) << "账户名" << setw(10) <<"账号" << setw(10) << "年级"<< setw(10) <<"性别" << setw(12) <<"电话"<< setw(10) <<"地址" <<endl;
for (i = 0; i < s.length(); i++)
{
a[i].read(i);
a[i].display();
}
cout << endl;
system("pause");
}
/
学生功能模块
#include"main.h"
void check_info(int n) //检查修改信息
{
int i, l;
char choice;
bool success = 0;
string tele, addr, pass;
Student a[999];
Student s;
system("cls");
can_open(s);
l = s.length();
for (i = 0; i < l; i++)
a[i].read(i);
cout << " 欢迎" << a[n].get_name() <<"用户"<< endl<< endl<< endl;
cout << setw(10) << "账户"<< setw(10) <<"账号 " << setw(10) <<" 年级" << setw(10) <<"性别" << setw(12) <<"电话"<< setw(10) <<"地址" <<endl;
a[n].display();
cout << endl ;
cout << "Enter 1: 修改我的信息." << endl;
cout << "Enter 2: 修改密码." << endl;
cout << "Enter else: 返回学生菜单:";
cin >> choice;
getchar();
if (choice == '1')
{
do {
cout << "请输入新的电话号码:";
getline(cin, tele);
} while (is_same_student_tele(tele));
do {
cout << "请输入新的地址信息:";
getline(cin, addr);
} while (is_same_student_addr(addr));
a[n].change(a[n].get_id(), a[n].get_year(), a[n].get_name(), a[n].get_pass(), a[n].get_gend(), tele, addr);
ofstream outfile("student.txt");
outfile.close();
for (i = 0; i < l; i++)
a[i].write();
cout << "信息已修改:" << endl;
a[n].display();
system("pause");
}
else if (choice == '2')
{
cout << "请输入新的密码";
getline(cin, pass);
a[n].change(a[n].get_id(), a[n].get_year(), a[n].get_name(), pass, a[n].get_gend(), a[n].get_tele(), a[n].get_addr());
ofstream outfile("student.txt");
outfile.close();
for (i = 0; i < l; i++)
a[i].write();
cout << "密码已修改." << endl;
system("pause");
}
}
#include"main.h"
void choose_book(int n)
{
int i, l, m, id;
bool success = 0;
bool can_choose[999];
Student a[999];
Student s;
Book b[999];
Book c;
system("cls");
cout << endl << " 已借阅图书" << endl << endl;
can_open(s);
can_open(c);
l = c.length();
for (i = 0; i < s.length(); i++)
{
a[i].read(i);
a[i].read_book();
}
showbookh();
for (i = 0; i < l; i++)
{
b[i].display();
}
system("pause");
}
#include"main.h"
void mag_book2()
{
int i;
string tem;
string id, name, author, cat , location ,addr, time ,price;
char choice;
bool tempi = 0, tempi2 = 0;
Book a[50];
Book c;
do {
fflush(stdin);//清空输入缓冲区
system("cls");
/*********************************************** 查找图书 */
cout << "请输入图书的编号、书名或作者进行查找!";
cin >> tem;
tempi2 = 1;
fflush(stdin);
for (i = 0; i < c.length(); i++)
a[i].read(i);
for (i = 0; i < c.length(); i++)
if (tem == a[i].get_Id()||tem == a[i].get_Name()||tem == a[i].get_Author())
{
system("cls");
showbookh();
a[i].display();
tempi = 1;
}
if (!tempi)
cout << "不能找到此书.";
} while (!tempi2);
cout << endl;
system("pause");
}
#include"main.h"
void my_book(int n)
{
int i, l;
Student a[999];
Student s;
system("cls");
cout << endl << " 检查我的书籍信息和年级" << endl << endl;
can_open(s);
l = s.length();
for (i = 0; i < l; i++)
a[i].read(i);
cout << " Welcome," << a[n].get_name() << endl << endl;
a[n].read_book();
cout << "Book ID\tGrade" << endl << endl;
a[n].display_book();
system("pause");
}
/
判断模块
#include"main.h"
bool is_administrator()//管理员密码判断
{
string p1, p2;
getchar();
cout << "初始密码为123:";
cout << "请输入密码:";
cout << endl;
getline(cin, p1);
ifstream infile("password.txt");
if (!infile)
{
cout << endl << "Out of service" << endl;
cout << "Please press enter to exit." << endl;
system("pause");
exit(0);
}
getline(infile, p2);
infile.close();
if (p1 == p2)
return 1;
else
return 0;
}
#include"main.h"
bool is_same_Book_id(string n)//判断输入的ID编号是否重复
{
int i;
bool success = 0;
Book a[999];
Book s;
for (i = 0; i < s.length(); i++)
{
a[i].read(i);
if (a[i].get_Id()== n)
{
cout << "存在相同的编号." << endl;
success = 1;
}
}
return success;
}
#include"main.h"
bool is_same_book_name(string n)//不用
{
// int i;
// bool success = 0;
// Book a[999];
// Book c;
// for (i = 0; i < c.length(); i++)
// {
// a[i].read(i);
// if (a[i].get_name() == n)
// {
// cout << "已经存在此书名." << endl;
int success;
success = 1;
// }
// }
return success;
}
#include"main.h"
bool is_same_student_addr(string n)
{
int i;
bool success = 0;
Student a[999];
Student s;
for (i = 0; i < s.length(); i++)
{
a[i].read(i);
if (a[i].get_addr() == n)
{
cout << "There exist the same address." << endl;
success = 1;
}
}
return success;
}
#include"main.h"
bool is_same_student_name(string n)
{
int i;
bool success = 0;
Student a[999];
Student s;
for (i = 0; i < s.length(); i++)
{
a[i].read(i);
if (a[i].get_name() == n)
{
cout << "There exist the same name." << endl;
success = 1;
}
}
return success;
}
#include"main.h"
bool is_same_student_tele(string n)
{
int i;
bool success = 0;
Student a[999];
Student s;
for (i = 0; i < s.length(); i++)
{
a[i].read(i);
if (a[i].get_tele() == n)
{
cout << "存在相同号码." << endl;
success = 1;
}
}
return success;
}
#include"main.h"
bool is_student(int *n)//学生登录密码判断
{
Student a[100];
Student s;
string p1, p2;
int i;
bool success = 0;
getchar();
/************************/
cout << "初始账户为111,密码为111 ";
/*********************/
cout << endl;
cout << "请输入你的账户名:";
getline(cin , p1);
cout << "请输入你的密码:";
getline(cin , p2);
can_open(s);
for (i = 0; i < s.length(); i++)
{
a[i].read(i);
if (a[i].get_name() == p1 && a[i].get_pass() == p2)
{
*n = i;
success = 1;
}
}
return success;
}
初始化程序
#include"main.h"
void initialize() //初始化信息列表
{
ifstream infile1("book.txt");
if (!infile1)
{
ofstream outfile1("book.txt");
outfile1.close();
}
infile1.close();
ifstream infile2("student.txt");
if (!infile2)
{
ofstream outfile2("student.txt");
outfile2.close();
}
infile2.close();
ifstream infile3("password.txt");
if (!infile3)
{
ofstream outfile3("password.txt");
outfile3 << "123";
outfile3.close();
}
infile3.close();
ifstream infile4("mybook.txt");
if (!infile4)
{
ofstream outfile4("mybook.txt");
outfile4.close();
}
infile4.close();
}
最后最重要的main.cpp
#include"main.h"
int main()
{
SetScreenGrid(111,30);
int user;
char choice;
int choice2;
bool success = 0;
initialize();
do {
menu1();
cin >> choice;
switch (choice)
{
/********************************************************************/
case'1':
{
if ( is_administrator() )
{
do {
menu2();
cin >> choice2;
getchar();
switch (choice2)
{
case 1 :mag_book(); //图书管理
success = 0; break;
case 2:mag_student(); //学生管理
success = 0; break;
case 3:show_book_list(); //显示图书信息
success = 0; break;
case 4:show_student_list();//显示学生信息
success = 0; break;
case 5:change_password();//修改管理员密码
success = 0; break;
case 6: //返回主菜单
success = 1; break;
case 0: //退出
success = 1; break;
default:
wrong_input(); break;
}
} while (!success);
}
else
{
cout << "密码错误!." << endl;
system("pause");
}
}
break;
/********************************************************************/
case'2':
{
if (is_student(&user))
{
do {
menu3();
cin >> choice2;
switch (choice2)
{
case 1:choose_book(user); //已借阅图书
success = 0; break;
case 2:show_book_list(); //显示图书
success = 0; break;
case 3:mag_book2(); //查找图书
success = 0; break;
case 4:my_book(user); //检查我的书和成绩
success = 0; break;
case 5:check_info(user); //检查并更改我的信息
success = 0; break;
case 6: //返回主菜单
success = 1; break;
case 0: //退出
success = 1; break;
//default:
// wrong_input(); break;
}
} while (!success);
}
else
{
cout <<endl;
cout << "你的名字或密码错误." << endl;
system("pause");
}
}
break;
case'0':success = 1; break;
default:wrong_input(); break;
}
} while (choice != '0');
return 0;
}
void can_open(Book a)
{
if (!a.canopen())
{
cout << endl << "The file cannot open.";
cout << endl << "You have to restart the program to repair the error.";
cout << endl << "Press enter to exit." << endl;
system("pause");
exit(0);
}
}
void can_open(Student a)
{
if (!a.canopen())
{
cout << endl << "The file cannot open.";
cout << endl << "You have to restart the program to repair the error.";
cout << endl << "Press enter to exit." << endl;
system("pause");
exit(0);
}
}
平时不学习,期末泪两行。
希望能帮到你们,喜欢就支持一下,
源码位置
留个痕迹 ):
更多推荐
所有评论(0)