-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cpp
More file actions
185 lines (158 loc) · 4.55 KB
/
Copy pathMain.cpp
File metadata and controls
185 lines (158 loc) · 4.55 KB
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
#include <iostream>
#include <vector>
#include <regex>
using namespace std;
class Student {
public:
string first_name;
string last_name;
string id;
Student(string sid)
{
id = sid;
}
void print()
{
cout << "student: " << id << " with name: " << first_name << ", " << last_name;
}
};
class MySet {
public:
vector<Student> mySet;
int find(string studentId)
{
vector<Student>::iterator it;
for (it = mySet.begin(); it != mySet.end(); it++)
{
if((*it).id == studentId) {
cout << "Found: ";
(*it).print();
return it - mySet.begin();
break;
}
}
cout << "Can't find: " << studentId << ".";
return -1;
}
//Insert new students, you should not accept duplicates!
void insert(Student s)
{
if(find(s.id) == -1) {
cout << " inserting." << endl;
mySet.push_back(s);
}
}
//Remove a student from the set given her G#
void remove(string studentId)
{
vector<Student>::iterator it;
int index = find(studentId);
if(index != -1) {
mySet.erase(mySet.begin() + index);
cout << " removing." << endl;
return;
}
cout << " no found no remove." << endl;
}
/*find a student given her G# (it can return several students with similar G#s.
Make sure you can accept regular expression, e.g., G39*)*/
void findSimilarId(string regexId)
{
regex myRegex(regexId);
vector<Student>::iterator it;
for (it = mySet.begin(); it != mySet.end(); it++)
{
if(regex_match((*it).id, myRegex)) {
cout << "Found: ";
(*it).print();
cout << endl;
}
}
}
/*List all the student (print them!) that match similar name (inputs: 1st name and/or last name).
Again regular expressions should be used, e.g., 1st Name: Y*, last name: Kim*.
So all students having the 1st name starting with Y and last name starting with Kim should be printed.*/
void findFirstName(string firstRegex)
{
return findSimilarName(firstRegex, ".*");
}
void findLastName(string lastRegex)
{
return findSimilarName(".*",lastRegex);
}
void findSimilarName(string firstRegex, string lastRegex)
{
regex firstname(firstRegex);
regex lastname(lastRegex);
vector<Student>::iterator it;
for (it = mySet.begin(); it != mySet.end(); it++)
{
if(regex_match((*it).first_name, firstname) && regex_match((*it).last_name, lastname))
{
cout << "Found: ";
(*it).print();
cout << endl;
}
}
}
void printSet()
{
vector<Student>::iterator it;
for (it = mySet.begin(); it != mySet.end(); it++)
{
(*it).print();
cout << endl;
}
}
};
int main()
{
// you must test with 10 students
Student s1("G319817");
s1.first_name = "jolie"; s1.last_name = "zhu";
Student s2("G308030");
s2.first_name = "leo"; s2.last_name = "liao";
Student s3("G397515");
s3.first_name = "tuanzi"; s3.last_name = "zhu";
Student s4("G886210");
s4.first_name = "hebe"; s4.last_name = "brown";
Student s5("G886256");
s5.first_name = "peiyao"; s5.last_name = "teng";
Student s6("G986298");
s6.first_name = "hebe"; s6.last_name = "teng";
Student s7("G186298");
s7.first_name = "rele"; s7.last_name = "wang";
Student s8("G886222");
s8.first_name = "jack"; s8.last_name = "williams";
Student s9("G654398");
s9.first_name = "tom"; s9.last_name = "smith";
Student s10("G863598");
s10.first_name = "jerry"; s10.last_name = "lee";
MySet studentSet;
studentSet.insert(s1);
studentSet.insert(s2);
studentSet.insert(s3);
studentSet.insert(s4);
studentSet.insert(s5);
studentSet.insert(s6);
studentSet.insert(s7);
studentSet.insert(s8);
studentSet.insert(s9);
studentSet.insert(s10);
cout << endl;
studentSet.find("G123");
cout << endl << endl;
studentSet.find("G886222");
cout << endl << endl;
studentSet.remove("G319817");
cout << endl;
studentSet.findSimilarId("G3.*");
cout << endl;
studentSet.findFirstName("hebe.*");
cout << endl;
studentSet.findLastName("will.*");
cout << endl;
studentSet.findSimilarName("je.*","le.*");
cout << endl;
return 0;
}