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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
| #include "app.h" #include <list> #include <vector> #include <string> #include <locale> #include<fstream> #include <iostream> #include <sstream> using namespace std;
void menu() { cout << "\n 学生信息管理系统\n\n" << "*******************************************************************************\n\n" << "\t\t\t\t0.退出系统\n\n" << "\t\t\t\t1.录入学生信息\n\n" << "\t\t\t\t2.导出学生信息\n\n" << "\t\t\t\t3.修改学生信息\n\n" << "\t\t\t\t4.删除学生信息\n\n" << "\t\t\t\t5.查询学生信息\n\n" << "*******************************************************************************\n"; }
class Student { private: string id; string name; string sex; int age; string adder; string profession; int num_delete;
public:
Student() { num_delete = 0; }
Student(string studentId, string studentName, string studentSex, int studentAge, string studentAdder, string studentProfession) { id = studentId; name = studentName; sex = studentSex; age = studentAge; adder = studentAdder; profession = studentProfession; num_delete = 0; }
bool matchesKeyword(const string& keyword) const { bool idMatch = id.find(keyword) != string::npos; bool nameMatch = name.find(keyword) != string::npos; bool sexMatch = sex.find(keyword) != string::npos; bool adderMatch = adder.find(keyword) != string::npos; bool professionMatch = profession.find(keyword) != string::npos;
return nameMatch || sexMatch || idMatch || adderMatch || professionMatch; }
std::string getId() const { return id; }
std::string getName() const { return name; }
std::string getSex() const { return sex; }
int getAge() const { return age; }
std::string getAdder() const { return adder; }
std::string getProfession() const { return profession; }
int getNumDelete() const { return num_delete; }
void setId(const string& studentId) { id = studentId; }
void setName(const string& studentName) { name = studentName; }
void setSex(const string& studentSex) { sex = studentSex; }
void setAge(int studentAge) { age = studentAge; }
void setAdder(const string& studentAdder) { adder = studentAdder; }
void setProfession(const string& studentProfession) { profession = studentProfession; }
void setNumDelete(int deleteStatus) { num_delete = deleteStatus; } };
list<Student> stulist; list<Student>::iterator stu;
void init() { ifstream inputFile("students.txt");
std::string line; while (getline(inputFile, line)) { Student student; std::istringstream iss(line); std::string field;
getline(iss, field, ','); student.setId(field);
getline(iss, field, ','); student.setName(field);
getline(iss, field, ','); student.setSex(field);
getline(iss, field, ','); student.setAge(stoi(field));
getline(iss, field, ','); student.setAdder(field);
getline(iss, field); student.setProfession(field);
stulist.push_back(student); } inputFile.close(); }
Student checkStudent(string name,string sex,int age,string adder,string profession) { string ids; string a, b, c, d,e; bool validInput = false; bool validSex = false;
cout << "是否想添加姓名?(是/否)"; cin >> a; if (a == "是") { cout << "请输入姓名:" << endl; cin >> name; } cout << "是否想添加性别?(是/否)"; cin >> b; if (b == "是") { while (!validSex) { cout << "请输入性别(男/女):" << endl; cin >> sex; if (sex == "男" || sex == "女") { validSex = true; } else { cout << "性别输入不合理,重新输入"; } } } cout << "是否想添加年龄?(是/否)"; cin >> c; if (c == "是") { while (!validInput) { cout << "请输入年龄:" << endl; if (cin >> age) { if (age > 0 && age < 100) { validInput = true; } else { cout << "请输入一个合法的年龄" << endl; } } else { cout << "输入无效,请输入一个有效的数字年龄。" << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } } } cout << "是否想添加地址?(是/否)"; cin >> d; if (d == "是") { cout << "请输入地址:" << endl; cin >> adder; } cout << "是否想添加专业名称?(是/否)"; cin >> e; if (e == "是") { cout << "请输入专业名称:" << endl; cin >> profession; } Student data(ids, name, sex, age, adder, profession);
return data; }
void inputStudent() { cout << "请输入学生信息\n"; string ids, name, sex, adder, profession; int age = 0; bool validInput = false; bool validId = false; int i = 0; while (!validId) { cout << "请输入必选学号:(只可尝试三次)" << endl; cin >> ids; string yearString = ids.substr(0, 4); int year = stoi(yearString); if (year >= 1935 && year <= 2024 && ids.length() == 12) { validId = true; } else { cout << "输入无效,请输入符合规定的学号(12位数字并且数字含义正确)。" << endl; i++; if (i == 3) { cout << "输入学号多次失败,自动返回系统界面" << endl; return; } } for (stu = stulist.begin(); stu != stulist.end(); stu++) { if (ids == (*stu).getId()) { cout << "学号已存在,请重新回初始界面选择" << endl; return; } } } Student student = checkStudent(name, sex, age, adder, profession);
Student data(ids, student.getName(), student.getSex(), student.getAge(), student.getAdder(), student.getProfession()); stulist.push_back(data); cout << "添加信息成功" << endl; return; }
void outputStudent() { ofstream outputFile1("students.txt"); ofstream outputFile2("DeleteStudents.txt"); for (stu = stulist.begin(); stu != stulist.end(); stu++) { if (outputFile1.is_open() && outputFile2.is_open()) { if ((*stu).getNumDelete() != 1) { outputFile1 << (*stu).getId() << ", " << (*stu).getName() << ", " << (*stu).getSex() << ", " << (*stu).getAge() << ", " << (*stu).getAdder() << ", " << (*stu).getProfession() << endl;
cout << "学生信息导出成功!" << endl; } else { outputFile2 << (*stu).getId() << ", " << (*stu).getName() << ", " << (*stu).getSex() << ", " << (*stu).getAge() << ", " << (*stu).getAdder() << ", " << (*stu).getProfession() << endl;
cout << "学生信息导出成功!" << endl; } } else { cerr << "无法打开文件" << endl; } }
outputFile1.close(); outputFile2.close(); }
bool matchStudentId(const Student& student, const std::string& id) { return student.getId() == id; }
void updateStudent() { string strId, name, sex, adder, profession; int age = 0; bool found = false;
cout << "请输入需要修改信息的学生的学号:"; cin >> strId;
for (auto& student : stulist) { if (student.getId() == strId && student.getNumDelete() != 1) { found = true; cout << "请输入你希望修改的学生信息:" << endl; Student data = checkStudent(name, sex, age, adder, profession);
student.setName(data.getName()); student.setSex(data.getSex()); student.setAge(data.getAge()); student.setAdder(data.getAdder()); student.setProfession(data.getProfession()); break; } }
if (found) { ofstream outputFile("students.txt", ios::out | ios::trunc);
if (outputFile.is_open()) { for (const auto& student : stulist) { if (student.getNumDelete() != 1) { outputFile << student.getId() << ", " << student.getName() << ", " << student.getSex() << ", " << student.getAge() << ", " << student.getAdder() << ", " << student.getProfession() << endl; } }
cout << "修改成功" << endl; } else { cerr << "无法打开文件" << endl; }
outputFile.close(); } else { cout << "该学号不存在或输入错误,请选择添加学生信息或再次输入:" << endl; cout << "1.添加学生信息" << endl; cout << "2.再次输入修改学号" << endl; cout << "3.返回主菜单" << endl; int choice; cin >> choice; switch (choice) { case 1: inputStudent(); break; case 2: updateStudent(); break; default: cout << "自动回到初始界面\n"; return; break; } } }
void deleteStudentPhysically(string id) { ifstream inputFile("students.txt"); vector<Student> students;
std::string line; while (getline(inputFile, line)) { Student student; std::istringstream iss(line); std::string field;
getline(iss, field, ','); student.setId(field);
getline(iss, field, ','); student.setName(field);
getline(iss, field, ','); student.setSex(field);
getline(iss, field, ','); student.setAge(stoi(field));
getline(iss, field, ','); student.setAdder(field);
getline(iss, field); student.setProfession(field);
students.push_back(student); }
students.erase(remove_if(students.begin(), students.end(), [id](const Student& s) { return s.getId() == id; }), students.end());
inputFile.close();
ofstream outputFile("students.txt"); for (const auto& s : students) { outputFile << s.getId() << ", " << s.getName() << ", " << s.getSex() << ", " << s.getAge() << ", " << s.getAdder() << ", " << s.getProfession() << endl; }
outputFile.close();
cout << "已从文本文件中删除已逻辑删除的学生数据。" << endl; }
void deldeteStudent() { string strId; bool f = true; while (true) { cout << "请输入需要删除信息的学生的学号"; cin >> strId; if (strId.length() != 12) { cout << "输入的学号有误,请重新输入" << endl; } else { for (stu = stulist.begin(); stu != stulist.end(); stu++) { if (strId == (*stu).getId()) { (*stu).setNumDelete(1); deleteStudentPhysically(strId); cout << "删除成功" << endl; f = !f; } } if (f) { cout << "该同学的学号不存在无法删除" << endl; } break; } } }
list<Student> fuzzySearch(const list<Student>& students, const string& keyword) { list<Student> results;
for (const Student& student : students) { if (student.matchesKeyword(keyword)) { results.push_back(student); } } return results; }
void fuzzySearchResult() { string keyword; int i = 0; cout << "请输入你想要查询的关键字"; cin >> keyword; list<Student> searchResults = fuzzySearch(stulist, keyword); if (searchResults.empty()) { cout << "你输入的关键字无对应的匹配结果" << endl; } else { cout << "含有关键字的学生对象输出如下:" << endl; for (const Student& result : searchResults) { if (result.getNumDelete() != 1) { cout << result.getId() << ", " << result.getName() << ", " << result.getSex() << ", " << result.getAge() << ", " << result.getAdder() << ", " << result.getProfession() << endl; i++; } } cout << "满足条件的人数为:" << i << "人" << endl; } }
bool compare(const Student& student1, const Student& student2) { string stringYear1 = student1.getId().substr(0, 4); string stringYear2 = student2.getId().substr(0, 4); int year1 = stoi(stringYear1); int year2 = stoi(stringYear2); return year1 < year2; }
void decList() { stulist.sort(compare); int i = 0; for (const Student& student : stulist) { if (student.getNumDelete() != 1) { cout << student.getId() << ", " << student.getName() << ", " << student.getSex() << ", " << student.getAge() << ", " << student.getAdder() << ", " << student.getProfession() << endl; i++; } } cout << "满足条件的人数为:" << i << "人" << endl; }
void getStudent() { int choice{ 0 }; cout << "请输入您要操作的序号" << endl; cout << "1.模糊查询" << endl; cout << "2.按照入学年份由小到大查询所有" << endl; cin >> choice; switch (choice) { case 1: fuzzySearchResult(); break; case 2: decList(); break; default: cout << "选择的序号不存在,请重新选择\n"; getStudent(); break; } }
void keydown() { int choice{ 0 }; cout << "请输入您要操作的序号"; cin >> choice; switch (choice) { case 0: cout << "期待再次与你相遇..."; exit(0); break; case 1: system("cls"); cout << "\n*****************************【录入学生信息】*********************************\n"; inputStudent(); break; case 2: system("cls"); cout << "\n*****************************【导出学生信息】*********************************\n"; outputStudent(); break; case 3: system("cls"); cout << "\n*****************************【修改学生信息】*********************************\n"; updateStudent(); break; case 4: system("cls"); cout << "\n*****************************【删除学生信息】*********************************\n"; deldeteStudent(); break; case 5: system("cls"); cout << "\n*****************************【查询学生信息】*********************************\n"; getStudent(); break; default: cout << "选择的序号不存在,请重新输入\n"; keydown(); break; } }
int main() { init(); while (true) { menu(); keydown(); } return 0; }
|