服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现图书馆管理系统源码

C++实现图书馆管理系统源码

2022-10-18 12:57Complicated321 C/C++

这篇文章主要为大家详细介绍了C++实现图书馆管理系统源码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现图书馆管理系统的具体代码,供大家参考,具体内容如下

总体思想

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

Student.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
#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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_40930675/article/details/84202582

延伸 · 阅读

精彩推荐
  • C/C++使用C语言求二叉树结点的最低公共祖先的方法

    使用C语言求二叉树结点的最低公共祖先的方法

    这篇文章主要介绍了使用C语言求二叉树结点的最低公共祖先的方法,文中还给出了ACM的练习题目,需要的朋友可以参考下...

    zinss269145202021-03-05
  • C/C++C语言中的内存泄露 怎样避免与检测

    C语言中的内存泄露 怎样避免与检测

    堆经常会出现两种类型的问题:1.释放或改写仍在使用的内存(称为:“内存损坏”)。2.未释放不再使用的内存(称为:“内存泄露”)。这是最难被调试发现...

    C语言教程网6182020-12-25
  • C/C++c++基础语法:构造函数与析构函数

    c++基础语法:构造函数与析构函数

    构造函数用来构造一个对象,主要完成一些初始化工作,如果类中不提供构造函数,编译器会默认的提供一个默认构造函数(参数为空的构造函数就是默认构...

    C++教程网7022021-01-01
  • C/C++OpenCV实现车牌定位(C++)

    OpenCV实现车牌定位(C++)

    这篇文章主要为大家详细介绍了OpenCV实现车牌定位,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Steven·简谈11762021-10-07
  • C/C++C语言数据结构顺序表中的增删改(尾插尾删)教程示例详解

    C语言数据结构顺序表中的增删改(尾插尾删)教程示例详解

    这篇文章主要为大家介绍了C语言数据结构顺序表中的增删改教程示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步...

    乔乔家的龙龙3842022-09-22
  • C/C++C语言结构体指针案例解析

    C语言结构体指针案例解析

    这篇文章主要介绍了C语言结构体指针案例解析,本文通过例子来解释说明了C语言的结构体概念和如何用指针去操作结构体,文章标明了详细的代码,需要的朋...

    梁光林5922021-11-21
  • C/C++C++虚析构函数的使用分析

    C++虚析构函数的使用分析

    本篇文章是对C++虚析构函数的使用进行了详细的分析介绍,需要的朋友参考下...

    C++教程网3952020-12-12
  • C/C++详解Visual Studio 2019(VS2019) 基本操作

    详解Visual Studio 2019(VS2019) 基本操作

    这篇文章主要介绍了详解Visual Studio 2019(VS2019) 基本操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    (≯^ω^≮)喵毛6152021-08-23