• 学院教学信息管理系统(c++)


    每一条记录包括一位教师的职工号、姓名、职称、性别、3门课程,教学效果综合评分

    系统实现了以下功能:

    1、输入:输入每一位教师记录,将其信息写入文件中。

    2、显示:显示每位教师记录。

    3、排序:按职工号或教学效果综合评分进行排序,并显示。

    4、查找:完成按姓名或课程查找教师的相关记录,并显示。

    1. #include <iostream>
    2. #include <fstream>
    3. #include <vector>
    4. #include <algorithm>
    5. using namespace std;
    6. // 定义教师结构体
    7. struct Teacher {
    8. int id; //职工号
    9. string name; //姓名
    10. string title; //职称
    11. char gender; //性别
    12. string courses[3]; //课程
    13. double teachingEffectiveness; //教学效果综合评分
    14. // 显示教师信息,声明为 const 成员函数
    15. void display() const {
    16. cout << "教师信息:" << endl;
    17. cout << "职工号: " << id << endl;
    18. cout << "姓名: " << name << endl;
    19. cout << "职称: " << title << endl;
    20. cout << "性别: " << gender << endl;
    21. cout << "课程: ";
    22. for (int i = 0; i < 3; ++i) {
    23. cout << courses[i] << " ";
    24. }
    25. cout << endl;
    26. cout << "教学效果综合评分: " << teachingEffectiveness << endl;
    27. cout << "-----------------------------" << endl;
    28. }
    29. };
    30. // 比较函数,用于排序
    31. bool compareById(const Teacher& a, const Teacher& b) {
    32. return a.id < b.id;
    33. }
    34. bool compareByEffectiveness(const Teacher& a, const Teacher& b) {
    35. return a.teachingEffectiveness > b.teachingEffectiveness;
    36. }
    37. // 函数声明
    38. void writeToFile(const vector<Teacher>& teachers, const string& filename);
    39. void readFromFile(vector<Teacher>& teachers, const string& filename);
    40. void displayTeachers(const vector<Teacher>& teachers);
    41. void sortByOption(vector<Teacher>& teachers, int option);
    42. void searchTeachers(const vector<Teacher>& teachers, const string& key);
    43. int main() {
    44. vector<Teacher> teachers;
    45. string filename = "teachers.txt"; //文件 teachers.txt 将保存在程序的当前工作目录中
    46. int choice;
    47. // 读取文件中的数据
    48. readFromFile(teachers, filename);
    49. while (true) {
    50. cout << "欢迎使用学院教学信息管理系统:" << endl;
    51. cout << "1. 输入教师记录" << endl;
    52. cout << "2. 显示所有教师记录" << endl;
    53. cout << "3. 按职工号排序并显示" << endl;
    54. cout << "4. 按教学效果评分排序并显示" << endl;
    55. cout << "5. 按姓名或课程查找教师记录" << endl;
    56. cout << "0. 退出系统" << endl;
    57. cout << "请选择操作:";
    58. cin >> choice;
    59. switch (choice) {
    60. case 1: {
    61. Teacher newTeacher;
    62. cout << "请输入教师信息:" << endl;
    63. cout << "职工号: ";
    64. cin >> newTeacher.id;
    65. cout << "姓名: ";
    66. cin.ignore(); // 忽略之前的回车符
    67. getline(cin, newTeacher.name);
    68. cout << "职称: ";
    69. getline(cin, newTeacher.title);
    70. cout << "性别 (M/F): ";
    71. cin >> newTeacher.gender;
    72. cout << "输入3门课程名称:" << endl;
    73. for (int i = 0; i < 3; ++i) {
    74. cout << "课程 " << i + 1 << ": ";
    75. cin >> newTeacher.courses[i];
    76. }
    77. cout << "教学效果综合评分: ";
    78. cin >> newTeacher.teachingEffectiveness;
    79. teachers.push_back(newTeacher);
    80. // 写入文件
    81. writeToFile(teachers, filename);
    82. break;
    83. }
    84. case 2:
    85. displayTeachers(teachers);
    86. break;
    87. case 3:
    88. sortByOption(teachers, 1); // 1 表示按职工号排序
    89. displayTeachers(teachers);
    90. break;
    91. case 4:
    92. sortByOption(teachers, 2); // 2 表示按教学效果评分排序
    93. displayTeachers(teachers);
    94. break;
    95. case 5: {
    96. string key;
    97. cout << "请输入要查找的姓名或课程名称:";
    98. cin.ignore(); // 忽略之前的回车符
    99. getline(cin, key);
    100. searchTeachers(teachers, key);
    101. break;
    102. }
    103. case 0:
    104. cout << "感谢使用!再见!" << endl;
    105. return 0;
    106. default:
    107. cout << "无效的选项,请重新选择。" << endl;
    108. break;
    109. }
    110. }
    111. return 0;
    112. }
    113. // 将教师信息写入文件
    114. void writeToFile(const vector<Teacher>& teachers, const string& filename) {
    115. ofstream outFile(filename);
    116. if (outFile.is_open()) {
    117. for (const auto& teacher : teachers) {
    118. outFile << teacher.id << "," << teacher.name << "," << teacher.title << ","
    119. << teacher.gender << "," << teacher.courses[0] << "," << teacher.courses[1] << ","
    120. << teacher.courses[2] << "," << teacher.teachingEffectiveness << endl;
    121. }
    122. outFile.close();
    123. cout << "数据已成功写入文件 " << filename << endl;
    124. } else {
    125. cout << "无法打开文件 " << filename << " 进行写入。" << endl;
    126. }
    127. }
    128. // 从文件中读取教师信息
    129. void readFromFile(vector<Teacher>& teachers, const string& filename) {
    130. ifstream inFile(filename);
    131. if (inFile.is_open()) {
    132. teachers.clear();
    133. Teacher teacher;
    134. char comma;
    135. while (inFile >> teacher.id >> comma >> teacher.name >> comma >> teacher.title >> comma
    136. >> teacher.gender >> comma >> teacher.courses[0] >> comma >> teacher.courses[1] >> comma
    137. >> teacher.courses[2] >> comma >> teacher.teachingEffectiveness) {
    138. teachers.push_back(teacher);
    139. }
    140. inFile.close();
    141. cout << "成功从文件 " << filename << " 中读取数据。" << endl;
    142. } else {
    143. cout << "无法打开文件 " << filename << " 进行读取。" << endl;
    144. }
    145. }
    146. // 显示所有教师记录
    147. void displayTeachers(const vector<Teacher>& teachers) {
    148. if (teachers.empty()) {
    149. cout << "暂无教师记录。" << endl;
    150. } else {
    151. for (const auto& teacher : teachers) {
    152. teacher.display();
    153. }
    154. }
    155. }
    156. // 按选项排序教师信息
    157. void sortByOption(vector<Teacher>& teachers, int option) {
    158. switch (option) {
    159. case 1:
    160. sort(teachers.begin(), teachers.end(), compareById);
    161. cout << "按职工号排序结果:" << endl;
    162. break;
    163. case 2:
    164. sort(teachers.begin(), teachers.end(), compareByEffectiveness);
    165. cout << "按教学效果评分排序结果:" << endl;
    166. break;
    167. default:
    168. cout << "无效的排序选项。" << endl;
    169. return;
    170. }
    171. }
    172. // 按姓名或课程查找教师记录
    173. void searchTeachers(const vector<Teacher>& teachers, const string& key) {
    174. bool found = false;
    175. for (const auto& teacher : teachers) {
    176. if (teacher.name == key) {
    177. teacher.display();
    178. found = true;
    179. } else {
    180. for (int i = 0; i < 3; ++i) {
    181. if (teacher.courses[i] == key) {
    182. teacher.display();
    183. found = true;
    184. break;
    185. }
    186. }
    187. }
    188. }
    189. if (!found) {
    190. cout << "未找到与 " << key << " 相关的教师记录。" << endl;
    191. }
    192. }

  • 相关阅读:
    FL Studio21.2宿主软件中文免费版下载
    论文阅读之Discrete Opinion Tree Induction for Aspect-based Sentiment Analysis
    c++类与对象
    Linux—— ansible循环
    Base64编码相关知识总结
    《Deep Convolution Neural Networks for Twitter Sentiment Analysis》文献研读
    Acwing算法心得——现代艺术(统计遍历)
    Python学习七:数据库编程接口
    Pythonmock基本使用
    方程求根之二分法
  • 原文地址:https://blog.csdn.net/zeyeqianli/article/details/140407909