生了!


         昨天下班回家接了个电话,吃了一惊,天啊,老姐生了,生了个男孩.真是没想到,前天我还在她那,虽然我估计会早产,但没想到提前这么多.看样子还比较顺利.总算好了. 
#include<iostream> #include<string> #include<fstream> #include<iomanip> using namespace std; // 学信息结构体 struct Student { int id; // 学号 string name; // 姓名 float math; // 数学成绩 float english; // 英语成绩 float computer; // 计算机成绩 float totalScore; // 总分 float averageScore; // 平均分 Student* next; // 指向下一个节点的指针 }; // 学管理系统类 class StudentManager { private: Student* head; // 链表头指针 int studentCount; // 学数量 public: // 构造函数 StudentManager() { head == NULL; studentCount = 0; } // 析构函数 ~StudentManager() { clearAllStudents(); } // 录入学信息 void insertStudent() { Student* newStudent = new Student; cout << "\n请输入学信息:" << endl; cout << "学号: "; cin >> newStudent->id; // 检查学号是否已存在 if (findStudentById(newStudent->id) != NULL) { cout << "错误:该学号已存在!" << endl; delete newStudent; return; } cout << "姓名: "; cin >> newStudent->name; // 成绩输入验证 do { cout << "数学成绩(0-100): "; cin >> newStudent->math; if (newStudent->math < 0 || newStudent->math > 100) { cout << "成绩必须在0-100之间,请重新输入!" << endl; } } while (newStudent->math < 0 || newStudent->math > 100); do { cout << "英语成绩(0-100): "; cin >> newStudent->english; if (newStudent->english < 0 || newStudent->english > 100) { cout << "成绩必须在0-100之间,请重新输入!" << endl; } } while (newStudent->english < 0 || newStudent->english > 100); do { cout << "计算机成绩(0-100): "; cin >> newStudent->computer; if (newStudent->computer < 0 || newStudent->computer > 100) { cout << "成绩必须在0-100之间,请重新输入!" << endl; } } while (newStudent->computer < 0 || newStudent->computer > 100); // 计算总分和平均分 newStudent->totalScore = newStudent->math + newStudent->english + newStudent->computer; newStudent->averageScore = newStudent->totalScore / 3.0; // 插入到链表 newStudent->next = head; head = newStudent; studentCount++; cout << "学信息录入成功!" << endl; } // 修改学信息 void updateStudent() { if (head == NULL) { cout << "没有学记录!" << endl; return; } int id; cout << "请输入要修改的学学号: "; cin >> id; Student* student = findStudentById(id); if (student == NULL) { cout << "未找到该学号的学!" << endl; return; } cout << "当前学信息:" << endl; displayStudent(student); cout << "\n请输入新的学信息:" << endl; cout << "姓名: "; cin >> student->name; do { cout << "数学成绩(0-100): "; cin >> student->math; if (student->math < 0 || student->math > 100) { cout << "成绩必须在0-100之间,请重新输入!" << endl; } } while (student->math < 0 || student->math > 100); do { cout << "英语成绩(0-100): "; cin >> student->english; if (student->english < 0 || student->english > 100) { cout << "成绩必须在0-100之间,请重新输入!" << endl; } } while (student->english < 0 || student->english > 100); do { cout << "计算机成绩(0-100): "; cin >> student->computer; if (student->computer < 0 || student->computer > 100) { cout << "成绩必须在0-100之间,请重新输入!" << endl; } } while (student->computer < 0 || student->computer > 100); // 更新总分和平均分 student->totalScore = student->math + student->english + student->computer; student->averageScore = student->totalScore / 3.0; cout << "学信息修改成功!" << endl; } // 删除学信息 void deleteStudent() { if (head == NULL) { cout << "没有学记录!" << endl; return; } int id; cout << "请输入要删除的学学号: "; cin >> id; Student* current = head; Student* previous = NULL; // 查找学 while (current != NULL && current->id != id) { previous = current; current = current->next; } if (current == NULL) { cout << "未找到该学号的学!" << endl; return; } // 删除学 if (previous == NULL) { head = current->next; } else { previous->next = current->next; } delete current; studentCount--; cout << "学信息删除成功!" << endl; } // 插入新学(在指定位置之后) void insertNewStudent() { if (head == NULL) { cout << "当前没有学记录,将创建第一条记录。" << endl; insertStudent(); return; } int positionId; cout << "请输入在哪个学之后插入新学(输入学号): "; cin >> positionId; Student* positionStudent = findStudentById(positionId); if (positionStudent == NULL) { cout << "未找到该学号的学,将作为第一条记录插入。" << endl; insertStudent(); return; } Student* newStudent = new Student; cout << "\n请输入新学信息:" << endl; cout << "学号: "; cin >> newStudent->id; // 检查学号是否已存在 if (findStudentById(newStudent->id) != NULL) { cout << "错误:该学号已存在!" << endl; delete newStudent; return; } cout << "姓名: "; cin >> newStudent->name; do { cout << "数学成绩(0-100): "; cin >> newStudent->math; if (newStudent->math < 0 || newStudent->math > 100) { cout << "成绩必须在0-100之间,请重新输入!" << endl; } } while (newStudent->math < 0 || newStudent->math > 100); do { cout << "英语成绩(0-100): "; cin >> newStudent->english; if (newStudent->english < 0 || newStudent->english > 100) { cout << "成绩必须在0-100之间,请重新输入!" << endl; } } while (newStudent->english < 0 || newStudent->english > 100); do { cout << "计算机成绩(0-100): "; cin >> newStudent->computer; if (newStudent->computer < 0 || newStudent->computer > 100) { cout << "成绩必须在0-100之间,请重新输入!" << endl; } } while (newStudent->computer < 0 || newStudent->computer > 100); // 计算总分和平均分 newStudent->totalScore = newStudent->math + newStudent->english + newStudent->computer; newStudent->averageScore = newStudent->totalScore / 3.0; // 插入到链表 newStudent->next = positionStudent->next; positionStudent->next = newStudent; studentCount++; cout << "学信息插入成功!" << endl; } // 按学号查询学 void findStudentById() { if (head == NULL) { cout << "没有学记录!" << endl; return; } int id; cout << "请输入要查询的学学号: "; cin >> id; Student* student = findStudentById(id); if (student == NULL) { cout << "未找到该学号的学!" << endl; return; } cout << "\n查询结果:" << endl; displayStudent(student); } // 按姓名查询学 void findStudentByName() { if (head == NULL) { cout << "没有学记录!" << endl; return; } string name; cout << "请输入要查询的学姓名: "; cin >> name; bool found = false; Student* current = head; while (current != NULL) { if (current->name == name) { if (!found) { cout << "\n查询结果:" << endl; displayHeader(); found = true; } displayStudentSimple(current); } current = current->next; } if (!found) { cout << "未找到该姓名的学!" << endl; } } // 按学号排序 void sortById() { if (head == NULL || head->next == NULL) { cout << "学记录为空或只有一条记录,无需排序!" << endl; return; } // 冒泡排序 bool swapped; Student* ptr1; Student* lptr = NULL; do { swapped = false; ptr1 = head; while (ptr1->next != lptr) { if (ptr1->id > ptr1->next->id) { // 交换数据 swap(ptr1->id, ptr1->next->id); swap(ptr1->name, ptr1->next->name); swap(ptr1->math, ptr1->next->math); swap(ptr1->english, ptr1->next->english); swap(ptr1->computer, ptr1->next->computer); swap(ptr1->totalScore, ptr1->next->totalScore); swap(ptr1->averageScore, ptr1->next->averageScore); swapped = true; } ptr1 = ptr1->next; } lptr = ptr1; } while (swapped); cout << "按学号排序完成!" << endl; } // 按总分排序 void sortByTotalScore() { if (head == NULL || head->next == NULL) { cout << "学记录为空或只有一条记录,无需排序!" << endl; return; } // 冒泡排序 bool swapped; Student* ptr1; Student* lptr = NULL; do { swapped = false; ptr1 = head; while (ptr1->next != lptr) { if (ptr1->totalScore < ptr1->next->totalScore) { // 交换数据 swap(ptr1->id, ptr1->next->id); swap(ptr1->name, ptr1->next->name); swap(ptr1->math, ptr1->next->math); swap(ptr1->english, ptr1->next->english); swap(ptr1->computer, ptr1->next->computer); swap(ptr1->totalScore, ptr1->next->totalScore); swap(ptr1->averageScore, ptr1->next->averageScore); swapped = true; } ptr1 = ptr1->next; } lptr = ptr1; } while (swapped); cout << "按总分排序完成!" << endl; } // 按平均分排序 void sortByAverageScore() { if (head == NULL || head->next == NULL) { cout << "学记录为空或只有一条记录,无需排序!" << endl; return; } // 冒泡排序 bool swapped; Student* ptr1; Student* lptr = NULL; do { swapped = false; ptr1 = head; while (ptr1->next != lptr) { if (ptr1->averageScore < ptr1->next->averageScore) { // 交换数据 swap(ptr1->id, ptr1->next->id); swap(ptr1->name, ptr1->next->name); swap(ptr1->math, ptr1->next->math); swap(ptr1->english, ptr1->next->english); swap(ptr1->computer, ptr1->next->computer); swap(ptr1->totalScore, ptr1->next->totalScore); swap(ptr1->averageScore, ptr1->next->averageScore); swapped = true; } ptr1 = ptr1->next; } lptr = ptr1; } while (swapped); cout << "按平均分排序完成!" << endl; } // 显示所有学信息 void displayAllStudents() { if (head == NULL) { cout << "没有学记录!" << endl; return; } cout << "\n所有学信息:" << endl; displayHeader(); Student* current = head; while (current != NULL) { displayStudentSimple(current); current = current->next; } cout << "\n共 " << studentCount << " 条记录" << endl; } private: // 查找学内部使用) Student* findStudentById(int id) { Student* current = head; while (current != NULL) { if (current->id == id) { return current; } current = current->next; } return NULL; } // 显示学信息(详细) void displayStudent(Student* student) { displayHeader(); displayStudentSimple(student); } // 显示表头 void displayHeader() { cout << setw(5) << "学号" << setw(10) << "姓名" << setw(10) << "班级" << setw(8) << "数学" << setw(8) << "英语" << setw(10) << "计算机" << setw(8) << "总分" << setw(8) << "平均分" << endl; cout << string(70, '-') << endl; } // 显示学信息(简洁) void displayStudentSimple(Student* student) { cout << setw(5) << student->id << setw(10) << student->name << setw(8) << fixed << setprecision(1) << student->math << setw(8) << fixed << setprecision(1) << student->english << setw(10) << fixed << setprecision(1) << student->computer << setw(8) << fixed << setprecision(1) << student->totalScore << setw(8) << fixed << setprecision(1) << student->averageScore << endl; } // 清空所有学记录 void clearAllStudents() { Student* current = head; while (current != NULL) { Student* next = current->next; delete current; current = next; } head = NULL; studentCount = 0; } }; // 主菜单 void displayMenu() { cout << "\n========== 学成绩管理系统 ==========" << endl; cout << "1. 录入学信息" << endl; cout << "2. 修改学信息" << endl; cout << "3. 删除学信息" << endl; cout << "4. 插入新学" << endl; cout << "5. 按学号查询学" << endl; cout << "6. 按姓名查询学" << endl; cout << "7. 按学号排序" << endl; cout << "8. 按总分排序" << endl; cout << "9. 按平均分排序" << endl; cout << "10. 显示所有学信息" << endl; cout << "0. 退出系统" << endl; cout << "=====================================" << endl; cout << "请输入您的选择: "; } int main() { StudentManager manager; int choice; do { displayMenu(); cin >> choice; switch (choice) { case 1: manager.insertStudent(); break; case 2: manager.updateStudent(); break; case 3: manager.deleteStudent(); break; case 4: manager.insertNewStudent(); break; case 5: manager.findStudentById(); break; case 6: manager.findStudentByName(); break; case 7: manager.sortById(); break; case 8: manager.sortByTotalScore(); break; case 9: manager.sortByAverageScore(); break; case 10: manager.displayAllStudents(); break; case 0: cout << "感谢使用学成绩管理系统,再见!" << endl; break; default: cout << "无效的选择,请重新输入!" << endl; } } while (choice != 0); return 0; }简化代码保持功能不变
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值