将客 2024-06-17 00:40 采纳率: 96.6%
浏览 3
已结题

如何在里面添加一个功能按学号删除学生记录?(语言-c++)


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

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;
}

在里面添加一个功能按学号删除学生记录另外使这个代码更加简洁

  • 写回答

1条回答 默认 最新

  • 阿里嘎多学长 2024-06-17 08:38
    关注

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


    要在现有的C++学生管理系统中添加一个按学号删除学生记录的功能,并优化现有代码,以下是一些建议和代码示例:

    1. 添加删除学生记录的功能

    你可以添加一个新的成员函数DeleteStudentByNumStudent类中,这个函数将遍历文件,将不匹配指定学号的学生记录写入一个临时文件,然后替换原文件。

    void DeleteStudentByNum(int delnum) {
        fstream file("student.txt");
        ofstream temp("temp.txt");
        string line;
        bool shouldDelete = false;
    
        while (getline(file, line)) {
            int num;
            istringstream iss(line);
            iss >> num;
            if (num != delnum) {
                temp << line << endl;
            } else {
                shouldDelete = true;
            }
        }
    
        file.close();
        temp.close();
    
        if (shouldDelete) {
            remove("student.txt");
            rename("temp.txt", "student.txt");
            cout << "已成功删除学号为 " << delnum << " 的学生记录!" << endl;
        } else {
            cout << "未找到学号为 " << delnum << " 的学生记录" << endl;
        }
    }
    

    2. 优化AlterDateByNumAlterScoreByNum函数

    你可以创建一个通用的函数AlterStudentRecord来避免代码重复,这个函数可以接收一个修改函数作为参数,然后应用这个函数到找到的学生记录上。

    template<typename Func>
    void AlterStudentRecord(int findnum, Func modifyFunc) {
        fstream file("student.txt");
        ofstream temp("temp.txt");
        bool found = false;
    
        while (getline(file, line)) {
            istringstream iss(line);
            int num;
            if (!(iss >> num)) break; // 错误的数据格式
    
            if (num == findnum) {
                modifyFunc(iss, temp);
                found = true;
            } else {
                temp << line << endl;
            }
        }
    
        file.close();
        temp.close();
    
        if (found) {
            remove("student.txt");
            rename("temp.txt", "student.txt");
        } else {
            cout << "未找到学号为 " << findnum << " 的学生" << endl;
        }
    }
    
    // 修改日期的函数
    void ModifyDate(istringstream &iss, ofstream &temp) {
        Date newDate;
        newDate.SetDate();
        // 构造新的记录并写入temp
    }
    
    // 修改成绩的函数
    void ModifyScore(istringstream &iss, ofstream &temp) {
        // 读取当前成绩并提示用户输入新成绩
        // 构造新的记录并写入temp
    }
    

    然后在AlterDateByNumAlterScoreByNum中调用AlterStudentRecord

    void AlterDateByNum(int findnum) {
        AlterStudentRecord(findnum, ModifyDate);
    }
    
    void AlterScoreByNum(int findnum) {
        AlterStudentRecord(findnum, ModifyScore);
    }
    

    3. 改进文件操作

    确保在文件操作中使用异常处理来处理可能的I/O错误,并且在删除和重命名文件之前检查操作是否成功。

    4. 优化用户界面和交互流程

    提供一个清晰的菜单,让用户可以容易地选择他们想要的操作。同时,确保所有的输入都有错误检查,以避免无效或恶意的输入。

    最后,确保你的代码风格一致,使用适当的变量名和函数名,这样可以提高代码的可读性和可维护性。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月25日
  • 已采纳回答 6月17日
  • 创建了问题 6月17日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价