-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl.cpp
200 lines (180 loc) · 4.01 KB
/
stl.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
/*************************************************************************
> File Name: stl.cpp
> Author: LoveZJT
> Mail: [email protected]
> Created Time: 2017年05月19日 星期五 21时11分52秒
************************************************************************/
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<locale>
using namespace std;
static const char *ZH_CN_LOCALE_STRING="zh_CN.utf8";
static const locale zh_CN_locale = locale(ZH_CN_LOCALE_STRING);
static const collate<char>& zh_CN_collate = use_facet<collate<char> >(zh_CN_locale);
enum Sex{M,F};
class Student
{
public:
int stuid;
string stuname;
Sex stusex;
string stumajor;
int stuage;
void display()
{
cout<<stuid<<" "<<stuname<<" ";
if(stusex==F)
cout<<"female ";
else if(stusex==M)
cout<<"male ";
cout<<stumajor<<" "<<stuage<<endl;
}
};
class MatchMajor
{
public:
string stumajor;
MatchMajor(string major)
{
stumajor=major;
}
bool operator ()(Student &stu)
{
return stumajor==stu.stumajor;
}
};
void display(Student &stu);
double compute_age(vector<Student> v);
int add_age(int sum,Student stu);
int countmajor(vector<Student> v,string major);
void save(vector<Student> v);
void save_computer(Student &stu);
void sort_by_id(vector<Student> v);
bool compare_id(Student &stu1,Student &stu2);
void sort_by_name(vector<Student> v);
bool compare_name(Student &stu1,Student &stu2);
bool zh_CN_less_than(const string &s1, const string &s2);
bool compare_major(Student &stu1,Student &stu2);
void sort_by_major(vector<Student> v);
int main()
{
vector<Student> v;
ifstream fin("student.txt");
if(!fin)
{
cout<<"can't open the student.txt"<<endl;
return -1;
}
Student stu;
char ch;
fin>>stu.stuid>>stu.stuname;
fin>>ch;
if(ch=='F')
stu.stusex=F;
else
stu.stusex=M;
fin>>stu.stumajor>>stu.stuage;
while(fin)
{
v.push_back(stu);
fin>>stu.stuid>>stu.stuname;
fin>>ch;
if(ch=='F')
stu.stusex=F;
else
stu.stusex=M;
fin>>stu.stumajor>>stu.stuage;
//fin>>stu.stuid>>stu.stuname>>stu.stusex>>stu.stumajor>>stu.stuage;
}
fin.close();
for_each(v.begin(),v.end(),display);
//cout<<v.size();
cout<<compute_age(v)<<endl;
cout<<countmajor(v,"计算机")<<endl;
save(v);
sort_by_id(v);
sort_by_name(v);
sort_by_major(v);
return 0;
}
void display(Student &stu)
{
stu.display();
}
double compute_age(vector<Student> v)
{
int sum=accumulate(v.begin(),v.end(),0,add_age);
//cout<<sum/v.size()<<endl;
return sum/v.size();
}
int add_age(int sum,Student stu)
{
return sum+stu.stuage;
}
int countmajor(vector<Student> v,string major)
{
return count_if(v.begin(),v.end(),MatchMajor(major));
}
void save(vector<Student> v)
{
ofstream fout("students_computer.txt");
if(!fout)
fout.close();
for_each(v.begin(),v.end(),save_computer);
}
void save_computer(Student &stu)
{
ofstream fout("students_computer.txt",ios::app);
if(!fout)
{
cout<<"can't open the students_computer.txt"<<endl;
}
if(stu.stumajor=="计算机")
{
fout<<stu.stuid<<" "<<stu.stuname;
if(stu.stusex=='F')
fout<<" F ";
else
fout<<" M ";
fout<<stu.stumajor<<" "<<stu.stuage<<endl;
}
fout.close();
}
void sort_by_id(vector<Student> v)
{
sort(v.begin(),v.end(),compare_id);
for_each(v.begin(),v.end(),display);
}
bool compare_id(Student &stu1,Student &stu2)
{
return stu1.stuid<stu2.stuid;
}
void sort_by_name(vector<Student> v)
{
sort(v.begin(),v.end(),compare_name);
for_each(v.begin(),v.end(),display);
}
bool compare_name(Student &stu1,Student &stu2)
{
//return stu1.stuname<stu2.stuname;
return zh_CN_less_than(stu1.stuname,stu2.stuname);
}
bool zh_CN_less_than(const string &s1, const string &s2)
{
const char *pb1 = s1.data();
const char *pb2 = s2.data();
return (zh_CN_collate.compare(pb1, pb1+s1.size(), pb2, pb2+s2.size()) < 0);
}
void sort_by_major(vector<Student> v)
{
sort(v.begin(),v.end(),compare_major);
for_each(v.begin(),v.end(),display);
}
bool compare_major(Student &stu1,Student &stu2)
{
return zh_CN_less_than(stu1.stumajor,stu2.stumajor);
}