温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++实现图书馆管理系统的代码怎么写

发布时间:2022-03-14 09:18:48 来源:亿速云 阅读:159 作者:iii 栏目:开发技术

这篇文章主要介绍“C++实现图书馆管理系统的代码怎么写”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C++实现图书馆管理系统的代码怎么写”文章能帮助大家解决问题。

总体思想

用C++开发图书馆管理系统需要对学生和图书分别建立class,调用class中的方法实现学生登陆账号借书,还书,图书馆管理员查看信息等操作。

Student.h

#pragma once
#include<string>
#include<vector>
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;

class Student
{
private:
    int id;                          //student's ID number
    int year;                        //student's grade
    string name;
    string password;
    string gender;
    string telephone;
    string address;                  //student's name, password, gender, telephone number and 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);
    //constructors
    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();          //get the variables of class
    void change(int a, int b, string c, string d, string e, string f, string g);
    //change the information
    void display();                  //display the information on the screen
    int length();                    //get the number of all students
    bool canopen();                  //check whether the file 'student.txt' can be opened
    void write();                    //write the information current into file 'student.txt'
    void read(int n);                //read the information of the number n student from file 'student.txt'
    void write_book();               //write the books information of the student into file 'mybook.txt' 
    void read_book();                //read the infomation of the student from file 'mybook.txt'
    void change_book(int a, int b);  //change the information of vector book and grade
    void add_book(int a);            //add a new book
    void display_book();             //display the information of books on the screen
    void add_student();              //add a student into the file 'mybook.txt'
    void sub_student();              //subtract a student in the file 'mybook.txt'
    bool is_same_book(int a);        //check whether there exist a same book in the file 'mybook.txt'
};

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 << "Name:   " << name << endl;
    cout << "ID number:   " << id << endl;
    cout << "Grade:   " << year << endl;
    cout << "Gender:   " << gender << endl;
    cout << "Telephone:   " << telephone << endl;
    cout << "Address:   " << address << endl << 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;
}

Book.h

#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;

class Book
{
private:
    int id;
    string name;
    string professor;                //the information of a book
    int place;                       //left seats
    int year;                        //available to which grades
public:
    Book();
    ~Book();
    Book(int a, string b, string c, int d, int e);
    int get_id();
    string get_name();
    string get_prof();
    int get_place();
    int get_year();                  //get the variables of class
    void change(int a, string b, string c, int d, int e);
                                     //change the information
    void display();                  //display the information on the screen
    int length();                    //get the number of all the Books
    bool canopen();                  //check whether the file 'book.txt' can be opened
    void write();                    //write the information into the file 'book.txt'
    void read(int n);                //read the information of number n book form the file 'book.txt'
};


Book::Book()
{
    name = "not given";
    id = 0;
    professor = "not given";
    place = 0;
    year = 0;                         //difine the default constructor
}


Book::~Book() {}

Book::Book(int a, string b, string c, int d, int e)
{
    id = a;
    name = b;
    professor = c;
    place = d;
    year = e;                         //define the normal constructor
}

int Book::get_id()
{
    return id;
}
string Book::get_name()
{
    return name;
}
string Book::get_prof()
{
    return professor;
}
int Book::get_place()
{
    return place;
}
int Book::get_year()
{
    return year;
}

void Book::change(int a, string b, string c, int d, int e)
{
    id = a;
    name = b;
    professor = c;
    place = d;
    year = e;
}

void Book::display()
{
    cout << "Name:   " << name << endl;
    cout << "ID number:   " << id << endl;
    cout << "Professor:   " << professor << endl;
    cout << "Left seats:   " << place << endl;
    cout << "Available grade:   ";
    if (year > 0 && year < 5)
        cout << "year " << year;
    else if (year == 5)
        cout << "All of students.";
    else
        cout << "error in data.";
    cout << endl << 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 fout("book.txt", ios::app);
    fout << id << "\t" << place << "\t" << year << "\t" << name << "\t" << professor << "\t" << endl;
    fout.close();
}

void Book::read(int n)
{
    int i = 0;
    string temp, data[999], a[4];
    ifstream fin("book.txt");
    while (getline(fin, temp))
    {
        data[i] = temp;
        i += 1;
    }
    fin.close();
    istringstream stream(data[n]);
    for (i = 0; i < 4; i++)
    {
        data[n].erase(0, data[n].find("\t") + 1);
        a[i] = data[n].substr(0, data[n].find("\t"));
    }
    name = a[2];
    professor = a[3];
    stream >> id >> place >> year;
}

main.cpp

#include<iostream>
#include<string>
#include<vector>
#include"Book.h"
#include"Student.h"
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);

int main()
{
    int user;
    char choice;
    bool success = 0;
    initialize();
    do {
        menu1();
        cin >> choice;
        switch (choice)
        {
        case'1':
        {
            if (is_administrator()) {
                do {
                    menu2();
                    cin >> choice;
                    getchar();
                    switch (choice)
                    {
                    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':give_mark(); success = 0; break;
                    case'6':change_password(); success = 0; break;
                    case'9':success = 1; break;
                    case'0':success = 1; break;
                    default:wrong_input(); break;
                    }
                } while (!success);
            }
            else
            {
                cout << "The password is incorrect." << endl;
                system("pause");
            }
        }
        break;
        case'2':
        {
            if (is_student(&user))
            {
                do {
                    menu3();
                    cin >> choice;
                    switch (choice)
                    {
                    case'1':choose_book(user); success = 0; break;
                    case'2':my_book(user); success = 0; break;
                    case'3':check_info(user); success = 0; break;
                    case'9':success = 1; break;
                    case'0':success = 1; break;
                    default:wrong_input(); break;
                    }
                } while (!success);
            }
            else
            {
                cout << "Your name or password is incorrect." << endl;
                system("pause");
            }
        }
        break;
        case'0':success = 1; break;
        default:wrong_input(); break;
        }
    } while (choice != '0');
    return 0;
}

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();
}
bool is_administrator()
{
    string p1, p2;
    getchar();
    cout << "Please input the password:";
    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;
}
bool is_student(int *n)
{
    Student a[100];
    Student s;
    string p1, p2;
    int i;
    bool success = 0;
    getchar();
    cout << "Please input your name:";
    getline(cin, p1);
    cout << "Please input your password:";
    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;
}
void menu1()
{
    system("cls");
    cout << endl << endl << endl;
    cout << "-------------------------------------------------------------------" << endl;
    cout << "                University Student Management System               " << endl << endl;
    cout << "      1.Administrator System." << endl << endl;
    cout << "      2.Student System." << endl << endl;
    cout << "      0.Exit." << endl;
    cout << "-------------------------------------------------------------------" << endl;
    cout << "Please input your choice:";
}
void menu2()
{
    system("cls");
    cout << endl << endl << endl;
    cout << "-------------------------------------------------------------------" << endl;
    cout << "                          Administrator System   " << endl << endl;
    cout << "      1.Book Management." << endl << endl;
    cout << "      2.Student Management." << endl << endl;
    cout << "      3.Show the Book List." << endl << endl;
    cout << "      4.Show the Student List." << endl << endl;
    cout << "      5.Give Marks to Students." << endl << endl;
    cout << "      6.Change Administrator Password." << endl << endl;
    cout << "      9.Return to the main menu." << endl << endl;
    cout << "      0.Exit." << endl;
    cout << "-------------------------------------------------------------------" << endl;
    cout << "Please input your choice:";
}
void menu3()
{
    system("cls");
    cout << endl << endl << endl;
    cout << "-------------------------------------------------------------------" << endl;
    cout << "                          Student System" << endl << endl;
    cout << "      1.Choose My Books" << endl << endl;
    cout << "      2.Check My Books and Grades" << endl << endl;
    cout << "      3.Check and Change My Infomation" << endl << endl;
    cout << "      9.Return to the main menu." << endl << endl;
    cout << "      0.Exit." << endl;
    cout << "-------------------------------------------------------------------" << endl;
    cout << "Please input your choice:";
}
void wrong_input()
{
    system("cls");
    cout << endl << endl << endl << endl << endl << endl;
    cout << "                        Wrong input! Please input again." << endl;
    system("pause");
}
void mag_book()
{
    int i, id, plac, year;
    char choice;
    bool success = 0, success2 = 0;
    string name, prof;
    Book a[50];
    Book c;
    do {
        fflush(stdin);
        system("cls");
        cout << endl << "                     Book Management" << endl << endl;
        cout << "Which operation do you want about the list of book?" << endl;
        cout << "1.Browse a book\n\n2.Add a book\n\n3.Modify a book\n\n4.Delete a book\n\n";
        cin >> choice;
        cout << "Please input the ID number of book:";
        cin >> id;
        getchar();
        can_open(c);
        if (choice == '1')
        {
            success2 = 1;
            fflush(stdin);
            for (i = 0; i < c.length(); i++)
                a[i].read(i);
            for (i = 0; i < c.length(); i++)
                if (id == a[i].get_id())
                {
                    system("cls");
                    a[i].display();
                    success = 1;
                }
            if (!success)
                cout << "The book cannot be found.";
        }
        else if (choice == '2')
        {
            success2 = 1;
            fflush(stdin);
            for (i = 0; i < c.length(); i++)
            {
                a[i].read(i);
                if (id == a[i].get_id())
                    success = 1;
            }
            if (success)
                cout << "The book is exist";
            else
            {
                do {
                    cout << "Please input the name of book:";
                    getline(cin, name);
                } while (is_same_book_name(name));
                cout << "Please input the professor's name:";
                getline(cin, prof);
                cout << "Please input the maximum quota of people(connot change later):";
                cin >> plac;
                cout << "Which grades are available?" << endl << "1. year 1\n2. year 2\n3. year 3\n4. year 4\n5. all of students\n";
                cin >> year;
                c.change(id, name, prof, plac, year);
                c.write();
                system("cls");
                cout << "The book has been saved." << endl << endl;
                c.display();
            }
        }
        else if (choice == '3')
        {
            success2 = 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;
                    success = 1;
                }
            }
            if (success)
            {
                do {
                    cout << "Please input the new name of book " << id << ":";
                    getline(cin, name);
                } while (is_same_book_name(name));
                cout << "Please input the new professor's name of book " << id << ":";
                getline(cin, prof);
                cout << "Which grades are available?" << endl << "1. year 1\n2. year 2\n3. year 3\n4. year 4\n5. all of students\n";
                cin >> year;
                a[n].change(id, name, prof, a[n].get_place(), year);
                ofstream fout("book.txt");
                fout.close();
                for (i = 0; i < l; i++)
                    a[i].write();
                system("cls");
                cout << "The book has been changed." << endl << endl;
                a[n].display();
            }
            else
                cout << "The book " << id << " cannot be found.";
        }
        else if (choice == '4')
        {
            success2 = 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;
                    success = 1;
                }
            }
            if (success)
            {
                ofstream fout("book.txt");
                fout.close();
                for (i = 0; i < l - 1; i++)
                    if (i != n)
                        a[i].write();
                system("cls");
                cout << "The book has been deleted." << endl << endl;
                a[n].display();
            }
            else
                cout << "The book " << id << " cannot be found.";
        }
        else
        {
            cout << "wrong input, please input again." << endl;
            system("pause");
        }
    } while (!success2);
    cout << endl;
    system("pause");
}
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 << "                     Student Management" << endl << endl;
        cout << "Which operation do you want about the list of student?" << endl;
        cout << "1.Browse a student\n2.Add a student\n3.Modify a student\n4.Delete a student\n";
        cin >> choice;
        cout << "Please input the ID number of student:";
        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");
                    a[i].display();
                    success = 1;
                }
            if (!success)
                cout << "The student cannot be found.";
        }
        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 << "The student is exist";
            else
            {
                do {
                    cout << "Please input the name of student:";
                    getline(cin, name);
                } while (is_same_student_name(name));
                cout << "Please input the password:";
                getline(cin, pass);
                do {
                    cout << "What grade is the student in? (1-4)";
                    cin >> year;
                } while (year < 1 || year>4);
                fflush(stdin);
                do {
                    cout << "Please input the student's gender:" << endl << "Please enter 'male' or 'female'  :";
                    getline(cin, gend);
                } while (!(gend == "male" || gend == "female"));
                do {
                    cout << "Please input the telephone number:";
                    getline(cin, tele);
                } while (is_same_student_tele(tele));
                do {
                    cout << "Please input the address:";
                    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 << "The information of student has been saved." << 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 << "Please input the new name of student " << id << ":";
                    getline(cin, name);
                } while (is_same_student_name(name));
                pass = a[n].get_pass();
                do {
                    cout << "What grade is the student in? (1-4)";
                    cin >> year;
                } while (year < 1 || year>4);
                fflush(stdin);
                do {
                    cout << "Please input the student's gender:" << endl << "Please enter 'male' or 'female'  :";
                    getline(cin, gend);
                } while (!(gend == "male" || gend == "female"));
                do {
                    cout << "Please input the new telephone number:";
                    getline(cin, tele);
                } while (is_same_student_tele(tele));
                do {
                    cout << "Please input the new address:";
                    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 << "The student has been changed." << 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 << "The student has been deleted." << endl << endl;
                a[n].display();
            }
            else
                cout << "The student " << id << " cannot be found.";
        }
        else
        {
            cout << "Wrong input, please input again." << endl;
            system("pause");
        }
    } while (!success2);
    cout << endl;
    system("pause");
}
void show_book_list()
{
    Book a[100];
    Book c;
    int i;
    system("cls");
    cout << endl << "                     Books List" << endl << endl;
    can_open(c);
    for (i = 0; i < c.length(); i++)
    {
        a[i].read(i);
        a[i].display();
    }
    cout << endl;
    system("pause");
}
void show_student_list()
{
    Student a[100];
    Student s;
    int i;
    system("cls");
    cout << endl << "                     Students List" << endl << endl;
    can_open(s);
    for (i = 0; i < s.length(); i++)
    {
        a[i].read(i);
        a[i].display();
    }
    cout << endl;
    system("pause");
}
void give_mark()
{
    int i, j, k = 0, id, temp;
    bool success = 0;
    vector<int> student, mark;
    Student a[999];
    Student s;
    Book b[999];
    Book c;
    system("cls");
    cout << endl << "                     Give Marks" << endl << endl;
    cout << "Please input the ID number of book:";
    cin >> id;
    for (i = 0; i < c.length(); i++)
    {
        b[i].read(i);
        if (b[i].get_id() == id)
            success = 1;
    }
    if (!success)
        cout << "The book " << id << " is not exist." << endl;
    else
    {
        cout << "These student(s) are your student(s):";
        for (i = 0; i < s.length(); i++)
        {
            a[i].read(i);
            a[i].read_book();
            for (j = 0; j < a[i].get_book().size(); j++)
                if (id == a[i].get_book()[j])
                {
                    k += 1;
                    cout << endl << k << ". " << a[i].get_name();
                    student.push_back(i);
                    break;
                }
        }
        cout << endl << "Please give marks;" << endl;
        for (i = 0; i < k; i++)
        {
            cout << a[student[i]].get_name() << ":   ";
            cin >> temp;
            a[student[i]].change_book(id, temp);
        }
        for (i = 0; i < s.length(); i++)
            a[i].write_book();
        cout << endl << "Giving marks successfully!";
    }
    cout << endl;
    system("pause");
}
void change_password()
{
    string p1, p2, p3;
    system("cls");
    cout << endl << "                 Change Administrator Password." << endl << endl;
    cout << "Please input the password:";
    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 << "Please input the new password:";
        getline(cin, p3);
        ofstream outfile("password.txt");
        outfile << p3;
        outfile.close();
        cout << "The administrator password has been changed.";
    }
    else
        cout << "Wrong password.";
    cout << endl;
    system("pause");
}
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 << "                     Choose My Books" << 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();
    }
    cout << "                                                   Welcome," << a[n].get_name() << endl << endl;
    for (i = 0; i < l; i++)
    {
        b[i].read(i);
        if (((b[i].get_year() == a[n].get_year()) || b[i].get_year() == 5) && (b[i].get_place() > 0))
        {
            can_choose[i] = 1;
            cout << "Status:   Available" << endl;
            b[i].display();
        }
        else
        {
            can_choose[i] = 0;
            cout << "Status:   Unavailable" << endl;
            b[i].display();
        }
    }

    do {
        cout << "Please input the ID number of the book you want to choose:";
        cin >> id;
        success = 0;
        for (i = 0; i < l; i++)
            if (b[i].get_id() == id)
            {
                m = i;
                success = 1;
            }
    } while (!success);
    system("cls");
    cout << endl << endl << endl;
    if (can_choose[m])
    {
        if (a[n].is_same_book(id))
            cout << "                  You have selected the book " << id << endl;
        else
        {
            b[m].change(b[m].get_id(), b[m].get_name(), b[m].get_prof(), b[m].get_place() - 1, b[m].get_year());
            ofstream outfile("book.txt");
            outfile.close();
            for (i = 0; i < l; i++)
                b[i].write();
            a[n].add_book(id);
            a[n].write_book();
            cout << "Here is the list of your books now:" << endl << endl << "ID\t\tGrade" << endl;
            a[n].display_book();
        }
    }
    else
        cout << "               The book '" << b[m].get_name() << "' cannot be selected." << endl;
    system("pause");
}
void my_book(int n)
{
    int i, l;
    Student a[999];
    Student s;
    system("cls");
    cout << endl << "                     Check My Books ang Grades" << 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");
}
void check_info(int n)
{
    int i, l;
    char choice;
    bool success = 0;
    string tele, addr, pass;
    Student a[999];
    Student s;
    system("cls");
    cout << endl << "                     Check and Change My Information" << 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].display();
    cout << endl << "Enter 1: Change my information." << endl;
    cout << "Enter 2: Change my password." << endl;
    cout << "Enter else: Return to the student menu:";
    cin >> choice;
    getchar();
    if (choice == '1')
    {
        do {
            cout << "Please input the new telephone number:";
            getline(cin, tele);
        } while (is_same_student_tele(tele));
        do {
            cout << "Please input the new address:";
            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 << "The information has been changed:" << endl;
        a[n].display();
        system("pause");
    }
    else if (choice == '2')
    {
        cout << "Please input the new password.";
        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 << "The password has been changed." << endl;
        system("pause");
    }
}
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);
    }
}
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;
}
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 << "There exist the same telephone number." << endl;
            success = 1;
        }
    }
    return success;
}
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;
}
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 << "There exist the same name." << endl;
            success = 1;
        }
    }
    return success;
}

关于“C++实现图书馆管理系统的代码怎么写”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI