Skip to content

Commit df4155e

Browse files
committed
✨ Add Find,Sort,Clear functions
1 parent 97d7962 commit df4155e

File tree

4 files changed

+89
-9
lines changed

4 files changed

+89
-9
lines changed

Functions.cpp

+79-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Created by 贾智超 on 2024/7/22.
33
//
44
#include "Functions.h"
5+
6+
#include <algorithm>
57
#include <iostream>
68
#include <memory>
79
#include <vector>
@@ -42,7 +44,7 @@ void SaveInfo(const std::vector<std::unique_ptr<Person>> &objects) {
4244
obj->write(ofs);
4345
}
4446
ofs.close();
45-
}3
47+
}
4648

4749

4850
void AddInformation(std::vector<std::unique_ptr<Person>> &WInfo) {
@@ -242,3 +244,79 @@ void ModifyInformation(std::vector<std::unique_ptr<Person>> &Objects) {
242244

243245
}
244246

247+
void FindInformation(std::vector<std::unique_ptr<Person>> &Objects) {
248+
try {
249+
std::cout << "Find Information"
250+
<<"\n1. Find by Name"
251+
<< "\n2. Find by Number"<<std::endl;
252+
int choice;
253+
std::cout << "Input choice: ";
254+
std::cin >> choice;
255+
switch (choice) {
256+
case 1: {
257+
std::cout << "Input the num you want to find: ";
258+
int num;
259+
bool found=false;
260+
std::cin >> num;
261+
for (const auto& obj : Objects) {
262+
if (obj->Number() == num) {
263+
found=true;
264+
obj->ShowNumber();
265+
obj->ShowName();
266+
obj->ShowSpace();
267+
obj->ShowDuty();
268+
}
269+
}
270+
if (!found) {
271+
std::cout << "No person with that number found." << std::endl;
272+
}
273+
break;
274+
}
275+
case 2: {
276+
bool found=false;
277+
std::cout << "Input the name you want to find: ";
278+
std::string Name;
279+
std::cin >> Name;
280+
for (const auto& obj : Objects) {
281+
if (obj->Name() == Name) {
282+
found=true;
283+
obj->ShowNumber();
284+
obj->ShowName();
285+
obj->ShowSpace();
286+
obj->ShowDuty();
287+
}
288+
}
289+
if (!found) {
290+
std::cout << "No person with that name and number found." << std::endl;
291+
}
292+
break;
293+
}
294+
default:
295+
std::cout << "Invalid choice!" << std::endl;
296+
break;
297+
}
298+
299+
}catch (const std::exception&e) {
300+
std::cerr << "Error"<<e.what()<<std::endl;
301+
}
302+
}
303+
304+
bool compareByNumber(const std::unique_ptr<Person>& p1, const std::unique_ptr<Person>& p2) {
305+
return p1->Number() < p2->Number();
306+
}
307+
308+
void SortInformation(std::vector<std::unique_ptr<Person>> &Objects) {
309+
std::cout << "Sort Information By Number"<<std::endl;
310+
std::sort(Objects.begin(), Objects.end(), compareByNumber);
311+
ShowInformation(Objects);
312+
}
313+
314+
void ClearAlltxt(std::vector<std::unique_ptr<Person>> &Objects) {
315+
Objects.clear();
316+
std::cout << "All information has been cleared." << std::endl;
317+
}
318+
319+
320+
321+
322+

Functions.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ void AddInformation(std::vector<std::unique_ptr<Person>> &WInfo);
1414
void ShowInformation(std::vector<std::unique_ptr<Person>> &loadedObjects);
1515
void DeleteInformation(std::vector<std::unique_ptr<Person>> &Objects);
1616
void ModifyInformation(std::vector<std::unique_ptr<Person>> &Objects);
17-
void FindInformation();
18-
void SortInformation();
19-
void ClearAlltxt();
17+
void FindInformation(std::vector<std::unique_ptr<Person>> &Objects);
18+
bool compareByNumber(const std::unique_ptr<Person>& p1, const std::unique_ptr<Person>& p2);
19+
void SortInformation(std::vector<std::unique_ptr<Person>> &Objects);
20+
void ClearAlltxt(std::vector<std::unique_ptr<Person>> &Objects);
2021
#endif //FUNCTIONS_H
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Start testing: Aug 12 10:26 �й���׼ʱ��
1+
Start testing: Aug 12 19:46 �й���׼ʱ��
22
----------------------------------------------------------
3-
End testing: Aug 12 10:26 �й���׼ʱ��
3+
End testing: Aug 12 19:46 �й���׼ʱ��

main.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ int main() {
2323
break;
2424
case 5:ModifyInformation(WInfo);
2525
break;
26-
case 6:
26+
case 6:FindInformation((WInfo));
2727
break;
28-
case 7:
28+
case 7:SortInformation(WInfo);
2929
break;
3030
case 8:
31+
ClearAlltxt(WInfo);
3132
break;
3233
default:std::cout<<"Error,please try again!"<<std::endl;
33-
break;
34+
break;
3435
}
3536
}while(number!=1);
3637

0 commit comments

Comments
 (0)