• 关于#c++#的问题,请各位专家解答!


    关注 码龄 粉丝数 原力等级 -- 被采纳 被点赞 采纳率 将客 2024-06-17 10:20 采纳率: 98.8% 浏览 6 首页/ 编程语言 / 关于#c++#的问题,请各位专家解答! c++ ``` #include #include #include #include #include using namespace std;class Date{public: void SetDate() { cout << "请输入年份:"; cin >> year; cout << "请输入月份:"; cin >> month; cout << "请输入日期:"; cin >> day; } int GetYear() { return year; } int GetMonth() { return month; } int GetDay() { return day; }private: int year; int month; int day;};class Student{public: void InputStudent(Date &birthday) { ofstream outfile("student.txt", ios::app); cout << "请输入学号:"; cin >> num; cout << "请输入姓名:"; cin >> name; birthday.SetDate(); cout << "请输入性别:"; cin >> gender; cout << "请输入三门课成绩:"; cin >> score1 >> score2 >> score3; double average = (score1 + score2 + score3) / 3.0; int max_score = max({score1, score2, score3}); int min_score = min({score1, score2, score3}); outfile << num << " " << name << " " << birthday.GetYear() << " " << birthday.GetMonth() << " " << birthday.GetDay() << " " << gender << " " << score1 << " " << score2 << " " << score3 << " " << average << " " << max_score << " " << min_score << endl; cout << "学生信息录入成功!" << endl; outfile.close(); } void DisplayAllStudents() { ifstream infile("student.txt"); string line; while (getline(infile, line)) { cout << line << endl; } infile.close(); } void FindStudentByNum(int findnum) { ifstream infile("student.txt"); int num; string line; bool found = false; while (infile >> num) { if (num == findnum) { getline(infile, line); cout << num << " " << line << endl; found = true; break; } getline(infile, line); } infile.close(); if (!found) { cout << "未找到学号为 " << findnum << " 的学生" << endl; } } void AlterDateByNum(int findnum) { // 实现修改日期的方法 // 省略具体实现代码 fstream file("student.txt"); ofstream temp("temp.txt"); int num; string name, gender, line; int year, month, day, score1, score2, score3; double average; bool found = false; while (file >> num >> name >> year >> month >> day >> gender >> score1 >> score2 >> score3 >> average) { if (num == findnum) { Date newDate; newDate.SetDate(); temp << num << " " << name << " " << newDate.GetYear() << " " << newDate.GetMonth() << " " << newDate.GetDay() << " " << gender << " " << score1 << " " << score2 << " " << score3 << " " << average << endl; found = true; cout << "已成功修改学号为 " << num << " 的学生的出生日期!" << endl; } else { temp << num << " " << name << " " << year << " " << month << " " << day << " " << gender << " " << score1 << " " << score2 << " " << score3 << " " << average << endl; } } file.close(); temp.close(); remove("student.txt"); rename("temp.txt", "student.txt"); if (!found) { cout << "未找到学号为 " << findnum << " 的学生" << endl; } } void AlterScoreByNum(int findnum) { // 实现修改成绩的方法 // 省略具体实现代码 fstream file("student.txt"); ofstream temp("temp.txt"); int num; string name, gender, line; int year, month, day, score1, score2, score3; double average; bool found = false; while (file >> num >> name >> year >> month >> day >> gender >> score1 >> score2 >> score3 >> average) { if (num == findnum) { int newScore1, newScore2, newScore3; cout << "请输入新的三门课成绩: "; cin >> newScore1 >> newScore2 >> newScore3; double newAverage = (newScore1 + newScore2 + newScore3) / 3.0; int newMax = max({newScore1, newScore2, newScore3}); int newMin = min({newScore1, newScore2, newScore3}); temp << num << " " << name << " " << year << " " << month << " " << day << " " << gender << " " << newScore1 << " " << newScore2 << " " << newScore3 << " " << newAverage << " " << newMax << " " << newMin << endl; found = true; cout << "已成功修改学号为 " << num << " 的学生成绩!" << endl; } else { temp << num << " " << name << " " << year << " " << month << " " << day << " " << gender << " " << score1 << " " << score2 << " " << score3 << " " << average << endl; } } file.close(); temp.close(); remove("student.txt"); rename("temp.txt", "student.txt"); if (!found) { cout << "未找到学号为 " << findnum << " 的学生" << endl; } }private: int num; string name; string gender; int score1, score2, score3;};int main(){ Student stu; Date birthday; int findnum; int choice; while (true) { cout << "***欢迎进入学生管理系统***" << endl; cout << "1. 录入学生信息" << endl; cout << "2. 浏览全部学生记录" << endl; cout << "3. 按学号查找学生记录" << endl; cout << "4. 按学号修改出生日期" << endl; cout << "5. 按学号修改学生成绩" << endl; cout << "6. 退出系统" << endl; cout << "请选择功能:"; cin >> choice; switch (choice) { case 1: stu.InputStudent(birthday); break; case 2: stu.DisplayAllStudents(); break; case 3: cout << "请输入要查找的学生学号:"; cin >> findnum; stu.FindStudentByNum(findnum); break; case 4: cout << "请输入要修改日期的学生学号:"; cin >> findnum; stu.AlterDateByNum(findnum); break; case 5: cout << "请输入要修改成绩的学生学号:"; cin >> findnum; stu.AlterScoreByNum(findnum); break; case 6: cout << "感谢使用,再见!" << endl; return 0; default: cout << "无效的选项,请重新选择。" << endl; break; } } return 0;} ```在现有的代码里面添加一个按学号删除学生记录的功能 展开全部 收起 写回答 好问题 0 提建议 追加酬金 关注问题 微信扫一扫 点击复制链接 分享 邀请回答 编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 当前问题酬金 ¥ 0 (可追加 ¥500) ¥ 15¥ 20¥ 50¥ 100¥ 200 支付方式 扫码支付 二维码出错 点击刷新 支付金额 15 元 提供问题酬金的用户不参与问题酬金结算和分配 支付即为同意 《付费问题酬金结算规则》 结题 再想想 删除 再等等

    1条回答 默认 最新

    • 阿里嘎多学长 2024-06-17 10:27
      关注

      以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


      很高兴帮助你添加按学号删除学生记录的功能到你的C++学生管理系统中。下面是你需要添加的代码和步骤:

      1. 在主菜单中添加删除功能的选项: 修改 main 函数中的 switch 语句,添加一个新的 case 来处理删除操作。
      case 7:
          cout << "请输入要删除的学生学号:";
          cin >> findnum;
          if (stu.DeleteStudentByNum(findnum)) {
              cout << "学生记录删除成功!" << endl;
          } else {
              cout << "未找到学号为 " << findnum << " 的学生记录" << endl;
          }
          break;
      
      1. 实现删除功能的逻辑: 在 Student 类中添加一个新的成员函数 DeleteStudentByNum,实现删除逻辑。
      bool Student::DeleteStudentByNum(int findnum) {
          ifstream file("student.txt");
          ofstream temp("temp.txt");
          string line;
          int num;
          bool found = false;
      
          // 读取原始文件,并将不匹配的记录写入临时文件
          while (file >> num) {
              if (num != findnum) {
                  // 写入不匹配的记录
                  temp << num;
                  while (file.peek() != '\n' && !file.eof()) {
                      temp << file.get();
                  }
                  temp << endl;
              } else {
                  // 找到要删除的记录,设置found标志
                  found = true;
                  // 跳过当前记录的剩余部分
                  while (file.peek() != '\n' && !file.eof()) {
                      file.get();
                  }
              }
          }
      
          file.close();
          temp.close();
      
          // 删除原始文件,并将临时文件重命名为原始文件名
          remove("student.txt");
          rename("temp.txt", "student.txt");
      
          return found;
      }
      
      1. 更新文件内容: 在上面的 DeleteStudentByNum 函数中,我们使用了一个临时文件 temp.txt 来存储不包含要删除记录的新文件内容。当读取到要删除的记录时,我们跳过它,不将其写入临时文件。然后,我们删除原始文件 student.txt 并将 temp.txt 重命名为 student.txt

      2. 向用户反馈操作结果: 在 DeleteStudentByNum 函数的最后,我们返回一个布尔值,表示是否找到了要删除的学生记录。在 main 函数中,我们根据这个返回值来向用户反馈操作结果。

      请注意,这段代码假设你的学生记录文件 student.txt 是按照一定的格式存储的,并且每条记录都是以学号开始的。如果文件格式不同,你可能需要对代码进行相应的调整。希望这能帮助你完成你的学生管理系统的新增功能!

      展开全部

      评论 编辑记录
      编辑
      预览

      报告相同问题?

    • 相关阅读:
      echarts X轴类目名太长时隐藏,hover时显示全部
      13-ES5和ES6基础
      基于ssm人事管理系统获取(java毕业设计)
      用Python在PDF文档中插入单图像水印和平铺图像水印
      打工人,这里有一份述职技巧,请查收
      学习SLAM:SLAM进阶(九)以激光点云赋色为例讲述如何自定义ROS的消息格式并实现消息的订阅与发布
      【笔记】Oracle不同数据库之间的表级别同步
      JVM内存设置与查看
      1.4.14 实验14:ospf多区域
      Java使用lowagie根据模版动态生成PDF(无需额外设置系统字体)
    • 原文地址:https://ask.csdn.net/questions/8119625