|
2 | 2 | // Created by 贾智超 on 2024/7/22.
|
3 | 3 | //
|
4 | 4 | #include "Functions.h"
|
| 5 | + |
| 6 | +#include <algorithm> |
5 | 7 | #include <iostream>
|
6 | 8 | #include <memory>
|
7 | 9 | #include <vector>
|
@@ -42,7 +44,7 @@ void SaveInfo(const std::vector<std::unique_ptr<Person>> &objects) {
|
42 | 44 | obj->write(ofs);
|
43 | 45 | }
|
44 | 46 | ofs.close();
|
45 |
| -}3 |
| 47 | +} |
46 | 48 |
|
47 | 49 |
|
48 | 50 | void AddInformation(std::vector<std::unique_ptr<Person>> &WInfo) {
|
@@ -242,3 +244,79 @@ void ModifyInformation(std::vector<std::unique_ptr<Person>> &Objects) {
|
242 | 244 |
|
243 | 245 | }
|
244 | 246 |
|
| 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 | + |
0 commit comments