-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
61 lines (54 loc) · 1.37 KB
/
database.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
#include "database.h"
using namespace std;
ostream& operator << (ostream& os, const Database &db){
for(int i=0;i<db.size_;i++)
os << db.students_[i];
return os;
}
void Database::Add(const Student& stu){
students_[size_++] = stu;
}
bool Database::ReadFile(string filename){
Student temp;
ifstream ifile(filename);
if(ifile){
size_ = 0;//Reset the size of the Database
while(ifile){
if(isFull()){
cout << "The database is full. ";
return false;
}
temp.Input(ifile);
if(ifile)
Add(temp);
}
ifile.close();
return true;
}else{
return false;
}
}
void Database::sort(){
stable_sort(this->students_,this->students_+size_);
}
void Database::Search(string name)const{
int result_ind[DB_MAX_SIZE] = {0};
int count = 0;
for(int i=0;i<size_;++i){
if( name == this->students_[i].GetName() )
result_ind[count++] = i;
}
cout << endl << "There is/are " << count << " \"" << name << "\"" << endl << endl;
for(int i=0;i<count;++i)
cout << this->students_[result_ind[i]];
}
bool Database::OutputFile(string filename)const{
ofstream ofile(filename);
if(ofile){
ofile << (*this);
ofile.close();
return true;
}else{
return false;
}
}