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 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
| #include <iostream> #include <fstream> #include <vector> #include <string> #include <algorithm> #include <cctype> #include <iomanip> #include <limits> #include <map> #include <numeric> #include <stdexcept> using namespace std;
const vector<string> HEADERS = { "学号", "姓名", "学院", "专业", "语文", "数学", "英语", "总分" }; const string CSV_FILENAME = "student_info.csv";
class Student { public: string id; string name; string college; string major; string chinese; string math; string english; string total;
Student(string i = "", string n = "", string c = "", string m = "", string chi = "", string ma = "", string eng = "", string tot = "") : id(i), name(n), college(c), major(m), chinese(chi), math(ma), english(eng), total(tot) { } };
class StudentManager { private: vector<Student> students;
bool isNumeric(const string& str) { if (str.empty()) return false; int dotCount = 0; for (char c : str) { if (c == '.') { dotCount++; if (dotCount > 1) return false; } else if (!isdigit(c)) { return false; } } return true; }
void displayStudent(const Student& s) { cout << setiosflags(ios::left) << setw(10) << s.id << setiosflags(ios::left)<< setw(20) << s.name << setiosflags(ios::left)<< setw(30) << s.college << setiosflags(ios::left)<< setw(30) << s.major << setiosflags(ios::left)<< setw(10) << fixed << setprecision(1) << stof(s.chinese) << setiosflags(ios::left)<< setw(10) << fixed << setprecision(1) << stof(s.math) << setiosflags(ios::left)<< setw(10) << fixed << setprecision(1) << stof(s.english) << setiosflags(ios::left)<< setw(10) << fixed << setprecision(1) << stof(s.total) << endl; }
int findById(const string& id) { for (int i = 0; i < students.size(); i++) { if (students[i].id == id) return i; } return -1; }
int findByName(const string& name) { for (int i = 0; i < students.size(); i++) { if (students[i].name == name) return i; } return -1; }
string escapeCSVField(const string& field) { if (field.find(',') != string::npos || field.find('"') != string::npos) { string escaped = field; replace(escaped.begin(), escaped.end(), '"', '"'); return "\"" + escaped + "\""; } return field; }
string unescapeCSVField(const string& field) { if (field.size() >= 2 && field.front() == '"' && field.back() == '"') { string unescaped = field.substr(1, field.size() - 2); size_t pos = 0; while ((pos = unescaped.find("\"\"", pos)) != string::npos) { unescaped.replace(pos, 2, "\""); pos += 1; } return unescaped; } return field; }
vector<string> parseCSVLine(const string& line) { vector<string> fields; string currentField; bool inQuotes = false;
for (char c : line) { if (c == '"') { inQuotes = !inQuotes; } else if (c == ',' && !inQuotes) { fields.push_back(unescapeCSVField(currentField)); currentField.clear(); } else { currentField += c; } } fields.push_back(unescapeCSVField(currentField)); return fields; }
void autoSaveToCSV() { ofstream file(CSV_FILENAME, ios::trunc); if (!file.is_open()) { cout << "CSV文件保存失败!" << endl; return; }
for (int i = 0; i < HEADERS.size(); i++) { file << escapeCSVField(HEADERS[i]); if (i < HEADERS.size() - 1) file << ","; } file << "\n";
for (const auto& s : students) { file << escapeCSVField(s.id) << "," << escapeCSVField(s.name) << "," << escapeCSVField(s.college) << "," << escapeCSVField(s.major) << "," << escapeCSVField(s.chinese) << "," << escapeCSVField(s.math) << "," << escapeCSVField(s.english) << "," << escapeCSVField(s.total) << "\n"; } file.close(); }
bool validateScore(const string& score, const string& subject) { if (!isNumeric(score)) { cout << "错误:" << subject << "成绩必须是数字!请重新输入。" << endl; return false; } try { float scoreValue = stof(score); if (scoreValue < 0 || scoreValue > 100) { cout << "错误:" << subject << "成绩必须在0-100之间!请重新输入。" << endl; return false; } } catch (...) { cout << "错误:" << subject << "成绩格式错误!请重新输入。" << endl; return false; } return true; }
void updateTotal(Student& s) { try { float chi = stof(s.chinese); float math = stof(s.math); float eng = stof(s.english); float total = chi + math + eng; s.total = to_string(total); } catch (...) { s.total = "0.0"; } }
public: void loadFromCSV() { ifstream file(CSV_FILENAME); if (!file.is_open()) { cout << "未找到CSV数据文件,将创建新文件。" << endl; return; }
string line; getline(file, line);
while (getline(file, line)) { if (line.empty()) continue;
vector<string> tokens = parseCSVLine(line);
if (tokens.size() >= 8) { Student s; s.id = tokens[0]; s.name = tokens[1]; s.college = tokens[2]; s.major = tokens[3]; s.chinese = tokens[4]; s.math = tokens[5]; s.english = tokens[6]; s.total = tokens[7];
if (!validateScore(s.chinese, "语文")) s.chinese = "0.0"; if (!validateScore(s.math, "数学")) s.math = "0.0"; if (!validateScore(s.english, "英语")) s.english = "0.0"; updateTotal(s); students.push_back(s); } } file.close(); cout << "CSV数据加载完成,共 " << students.size() << " 名学生。" << endl; }
void addStudent() { Student s; cout << "\n===================== 添加学生 =====================" << endl;
while (true) { cout << "请输入学号:"; cin >> s.id; if (findById(s.id) != -1) { cout << "错误:该学号已存在!请重新输入。" << endl; } else if (s.id.empty()) { cout << "错误:学号不能为空!请重新输入。" << endl; } else { break; } }
cout << "请输入姓名:"; cin.ignore(); getline(cin, s.name); while (s.name.empty()) { cout << "错误:姓名不能为空!请重新输入:"; getline(cin, s.name); }
cout << "请输入学院:"; getline(cin, s.college); while (s.college.empty()) { cout << "错误:学院不能为空!请重新输入:"; getline(cin, s.college); }
cout << "请输入专业:"; getline(cin, s.major); while (s.major.empty()) { cout << "错误:专业不能为空!请重新输入:"; getline(cin, s.major); }
while (true) { cout << "请输入语文成绩:"; cin >> s.chinese; if (validateScore(s.chinese, "语文")) break; }
while (true) { cout << "请输入数学成绩:"; cin >> s.math; if (validateScore(s.math, "数学")) break; }
while (true) { cout << "请输入英语成绩:"; cin >> s.english; if (validateScore(s.english, "英语")) break; }
updateTotal(s);
students.push_back(s); autoSaveToCSV(); cout << "\n✅ 学生添加成功!数据已保存到 " << CSV_FILENAME << endl; }
void deleteStudent() { cout << "\n===================== 删除学生 =====================" << endl; if (students.empty()) { cout << "提示:当前没有学生数据!" << endl; return; }
string input; cout << "请输入学号或姓名:"; cin >> input;
int index = isNumeric(input) ? findById(input) : findByName(input);
if (index == -1) { cout << "错误:未找到该学生!" << endl; return; }
cout << "找到学生:" << endl; displayHeader(); displayStudent(students[index]);
char confirm; cout << "确定要删除吗?(y/n):"; cin >> confirm;
if (confirm == 'y' || confirm == 'Y') { students.erase(students.begin() + index); autoSaveToCSV(); cout << "\n✅ 学生删除成功!数据已保存到 " << CSV_FILENAME << endl; } else { cout << "❌ 删除操作已取消" << endl; } }
void modifyStudent() { cout << "\n===================== 修改学生 =====================" << endl; if (students.empty()) { cout << "提示:当前没有学生数据!" << endl; return; }
string input; cout << "请输入学号或姓名:"; cin >> input;
int index = isNumeric(input) ? findById(input) : findByName(input);
if (index == -1) { cout << "错误:未找到该学生!" << endl; return; }
cout << "当前学生信息:" << endl; displayHeader(); displayStudent(students[index]);
cout << "\n请选择要修改的项目:" << endl; cout << "1. 学院" << endl; cout << "2. 专业" << endl; cout << "3. 语文成绩" << endl; cout << "4. 数学成绩" << endl; cout << "5. 英语成绩" << endl; cout << "6. 取消修改" << endl; cout << "请选择(1-6):";
int choice; while (!(cin >> choice) || choice < 1 || choice > 6) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "错误:输入无效!请重新选择(1-6):"; }
cin.ignore(); switch (choice) { case 1: cout << "请输入新的学院:"; getline(cin, students[index].college); while (students[index].college.empty()) { cout << "错误:学院不能为空!请重新输入:"; getline(cin, students[index].college); } break; case 2: cout << "请输入新的专业:"; getline(cin, students[index].major); while (students[index].major.empty()) { cout << "错误:专业不能为空!请重新输入:"; getline(cin, students[index].major); } break; case 3: while (true) { cout << "请输入新的语文成绩:"; cin >> students[index].chinese; if (validateScore(students[index].chinese, "语文")) { updateTotal(students[index]); break; } } break; case 4: while (true) { cout << "请输入新的数学成绩:"; cin >> students[index].math; if (validateScore(students[index].math, "数学")) { updateTotal(students[index]); break; } } break; case 5: while (true) { cout << "请输入新的英语成绩:"; cin >> students[index].english; if (validateScore(students[index].english, "英语")) { updateTotal(students[index]); break; } } break; case 6: cout << "❌ 修改操作已取消" << endl; return; }
autoSaveToCSV(); cout << "\n✅ 学生信息修改成功!数据已保存到 " << CSV_FILENAME << endl; cout << "修改后的信息:" << endl; displayHeader(); displayStudent(students[index]); }
void searchStudent() { cout << "\n===================== 查找学生 =====================" << endl; if (students.empty()) { cout << "提示:当前没有学生数据!" << endl; return; }
string input; cout << "请输入学号或姓名:"; cin >> input;
int index = isNumeric(input) ? findById(input) : findByName(input);
if (index == -1) { cout << "错误:未找到该学生!" << endl; return; }
cout << "✅ 找到学生:" << endl; displayHeader(); displayStudent(students[index]); }
void displayAll() { if (students.empty()) { cout << "提示:当前没有学生数据!" << endl; return; }
cout << "\n===================== 所有学生信息 (" << students.size() << " 名) =====================" << endl; displayHeader(); for (const auto& s : students) { displayStudent(s); } cout << "==========================================================================" << endl; }
void sortById() { if (students.empty()) { cout << "提示:当前没有学生数据可排序!" << endl; return; }
sort(students.begin(), students.end(), [](const Student& a, const Student& b) { if (a.id.size() != b.id.size()) return a.id.size() < b.id.size(); return a.id < b.id; }); autoSaveToCSV(); cout << "\n✅ 已按学号升序排序!数据已保存到 " << CSV_FILENAME << endl; }
void sortByChinese() { if (students.empty()) { cout << "提示:当前没有学生数据可排序!" << endl; return; }
sort(students.begin(), students.end(), [](const Student& a, const Student& b) { try { return stof(a.chinese) > stof(b.chinese); } catch (...) { return false; } }); autoSaveToCSV(); cout << "\n✅ 已按语文成绩降序排序!数据已保存到 " << CSV_FILENAME << endl; }
void sortByMath() { if (students.empty()) { cout << "提示:当前没有学生数据可排序!" << endl; return; }
sort(students.begin(), students.end(), [](const Student& a, const Student& b) { try { return stof(a.math) > stof(b.math); } catch (...) { return false; } }); autoSaveToCSV(); cout << "\n✅ 已按数学成绩降序排序!数据已保存到 " << CSV_FILENAME << endl; }
void sortByEnglish() { if (students.empty()) { cout << "提示:当前没有学生数据可排序!" << endl; return; }
sort(students.begin(), students.end(), [](const Student& a, const Student& b) { try { return stof(a.english) > stof(b.english); } catch (...) { return false; } }); autoSaveToCSV(); cout << "\n✅ 已按英语成绩降序排序!数据已保存到 " << CSV_FILENAME << endl; }
void sortByTotal() { if (students.empty()) { cout << "提示:当前没有学生数据可排序!" << endl; return; }
sort(students.begin(), students.end(), [](const Student& a, const Student& b) { try { return stof(a.total) > stof(b.total); } catch (...) { return false; } }); autoSaveToCSV(); cout << "\n✅ 已按总分降序排序!数据已保存到 " << CSV_FILENAME << endl; }
void displayHeader() { cout << setiosflags(ios::left) << setw(10) << HEADERS[0] << setiosflags(ios::left)<< setw(20) << HEADERS[1] << setiosflags(ios::left)<< setw(30) << HEADERS[2] << setiosflags(ios::left)<< setw(30) << HEADERS[3] << setiosflags(ios::left)<< setw(10) << HEADERS[4] << setiosflags(ios::left)<< setw(10) << HEADERS[5] << setiosflags(ios::left)<< setw(10) << HEADERS[6] << setiosflags(ios::left)<< setw(10) << HEADERS[7] << endl; cout << string(105, '-') << endl; }
void calculateAverage() { if (students.empty()) { cout << "提示:当前没有学生数据!" << endl; return; }
float chiTotal = 0.0f, mathTotal = 0.0f, engTotal = 0.0f, totalTotal = 0.0f; int validCount = 0;
for (const auto& s : students) { try { chiTotal += stof(s.chinese); mathTotal += stof(s.math); engTotal += stof(s.english); totalTotal += stof(s.total); validCount++; } catch (...) { continue; } }
if (validCount == 0) { cout << "错误:没有有效的成绩数据可计算!" << endl; return; }
cout << fixed << setprecision(2) << "\n===================== 成绩平均分 =====================" << endl; cout << "语文平均分:" << chiTotal / validCount << endl; cout << "数学平均分:" << mathTotal / validCount << endl; cout << "英语平均分:" << engTotal / validCount << endl; cout << "总分平均分:" << totalTotal / validCount << endl; }
void findMode() { if (students.empty()) { cout << "提示:当前没有学生数据!" << endl; return; }
auto findMapMode = [](const map<float, int>& freqMap) -> pair<float, int> { if (freqMap.empty()) return { 0.0f, 0 }; auto modeIt = max_element(freqMap.begin(), freqMap.end(), [](const pair<float, int>& a, const pair<float, int>& b) { return a.second < b.second; }); return *modeIt; };
map<float, int> chiFreq; for (const auto& s : students) { try { chiFreq[stof(s.chinese)]++; } catch (...) { continue; } } auto chiMode = findMapMode(chiFreq);
map<float, int> mathFreq; for (const auto& s : students) { try { mathFreq[stof(s.math)]++; } catch (...) { continue; } } auto mathMode = findMapMode(mathFreq);
map<float, int> engFreq; for (const auto& s : students) { try { engFreq[stof(s.english)]++; } catch (...) { continue; } } auto engMode = findMapMode(engFreq);
map<float, int> totalFreq; for (const auto& s : students) { try { totalFreq[stof(s.total)]++; } catch (...) { continue; } } auto totalMode = findMapMode(totalFreq);
cout << fixed << setprecision(1) << "\n===================== 成绩众数 =====================" << endl; cout << "语文众数:" << chiMode.first << " (出现次数:" << chiMode.second << ")" << endl; cout << "数学众数:" << mathMode.first << " (出现次数:" << mathMode.second << ")" << endl; cout << "英语众数:" << engMode.first << " (出现次数:" << engMode.second << ")" << endl; cout << "总分众数:" << totalMode.first << " (出现次数:" << totalMode.second << ")" << endl; }
void findExtremeValues() { if (students.empty()) { cout << "提示:当前没有学生数据!" << endl; return; }
vector<Student> validStudents; for (const auto& s : students) { try { stof(s.chinese); stof(s.math); stof(s.english); stof(s.total); validStudents.push_back(s); } catch (...) { continue; } }
if (validStudents.empty()) { cout << "错误:没有有效的成绩数据可分析!" << endl; return; }
auto chiMin = min_element(validStudents.begin(), validStudents.end(), [](const Student& a, const Student& b) { return stof(a.chinese) < stof(b.chinese); }); auto chiMax = max_element(validStudents.begin(), validStudents.end(), [](const Student& a, const Student& b) { return stof(a.chinese) < stof(b.chinese); });
auto mathMin = min_element(validStudents.begin(), validStudents.end(), [](const Student& a, const Student& b) { return stof(a.math) < stof(b.math); }); auto mathMax = max_element(validStudents.begin(), validStudents.end(), [](const Student& a, const Student& b) { return stof(a.math) < stof(b.math); });
auto engMin = min_element(validStudents.begin(), validStudents.end(), [](const Student& a, const Student& b) { return stof(a.english) < stof(b.english); }); auto engMax = max_element(validStudents.begin(), validStudents.end(), [](const Student& a, const Student& b) { return stof(a.english) < stof(b.english); });
auto totalMin = min_element(validStudents.begin(), validStudents.end(), [](const Student& a, const Student& b) { return stof(a.total) < stof(b.total); }); auto totalMax = max_element(validStudents.begin(), validStudents.end(), [](const Student& a, const Student& b) { return stof(a.total) < stof(b.total); });
cout << fixed << setprecision(1) << "\n===================== 成绩极值 =====================" << endl; cout << "语文最低分:" << chiMin->chinese << " (学号:" << chiMin->id << ",姓名:" << chiMin->name << ")" << endl; cout << "语文最高分:" << chiMax->chinese << " (学号:" << chiMax->id << ",姓名:" << chiMax->name << ")" << endl; cout << "数学最低分:" << mathMin->math << " (学号:" << mathMin->id << ",姓名:" << mathMin->name << ")" << endl; cout << "数学最高分:" << mathMax->math << " (学号:" << mathMax->id << ",姓名:" << mathMax->name << ")" << endl; cout << "英语最低分:" << engMin->english << " (学号:" << engMin->id << ",姓名:" << engMin->name << ")" << endl; cout << "英语最高分:" << engMax->english << " (学号:" << engMax->id << ",姓名:" << engMax->name << ")" << endl; cout << "总分最低分:" << totalMin->total << " (学号:" << totalMin->id << ",姓名:" << totalMin->name << ")" << endl; cout << "总分最高分:" << totalMax->total << " (学号:" << totalMax->id << ",姓名:" << totalMax->name << ")" << endl; } };
void displayMenu() { cout << "\n==========================================================" << endl; cout << " 学生信息管理系统 " << endl; cout << "==========================================================" << endl; cout << "1. 添加学生 2. 删除学生 3. 修改学生" << endl; cout << "4. 查找学生 5. 显示所有 6. 按学号排序" << endl; cout << "7. 按语文排序 8. 按数学排序 9. 按英语排序" << endl; cout << "10. 按总分排序 11. 计算平均分 12. 查找众数" << endl; cout << "13. 查找极值 14. 退出系统 " << endl; cout << "==========================================================" << endl; cout << "提示:数据存储在 " << CSV_FILENAME << ",支持Excel直接打开" << endl; cout << "请选择功能(1-14):"; }
void clearInputBuffer() { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); }
int main() { #ifdef _WIN32 system("chcp 65001 > nul"); #endif
StudentManager manager; cout << "正在加载CSV学生数据..." << endl; manager.loadFromCSV();
int choice = 0; do { displayMenu();
if (!(cin >> choice)) { clearInputBuffer(); cout << "\n❌ 错误:输入无效!请输入数字1-14。" << endl; continue; }
clearInputBuffer();
switch (choice) { case 1: manager.addStudent(); break; case 2: manager.deleteStudent(); break; case 3: manager.modifyStudent(); break; case 4: manager.searchStudent(); break; case 5: manager.displayAll(); break; case 6: manager.sortById(); break; case 7: manager.sortByChinese(); break; case 8: manager.sortByMath(); break; case 9: manager.sortByEnglish(); break; case 10: manager.sortByTotal(); break; case 11: manager.calculateAverage(); break; case 12: manager.findMode(); break; case 13: manager.findExtremeValues(); break; case 14: cout << "\n✅ 感谢使用学生信息管理系统!数据已保存到 " << CSV_FILENAME << endl; break; default: cout << "\n❌ 错误:选择无效!请输入1-14之间的数字。" << endl; }
if (choice != 14) { cout << "\n按Enter键继续..."; cin.get(); }
} while (choice != 14); system("pause"); return 0; }
|