将客 2024-06-19 16:57 采纳率: 96.6%
浏览 3
已结题

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

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 << "请输入3门课成绩:";
        cin >> score1 >> score2 >> score3;
        double average = (score1 + score2 + score3) / 3.0;
        int maxscore = max({score1, score2, score3});
        int minscore = min({score1, score2, score3});
        outfile << num << " " << name << " " << birthday.GetYear() << " " << birthday.GetMonth() 
        << " " << birthday.GetDay()<< " " << gender << " " << score1 << " " << score2 << " " << 
        score3 << " " << average << " " << maxscore << " " << minscore << endl;
        cout << "学生信息录入成功!" << endl;
        outfile.close();
    }
    //2.浏览学生全部信息
    void OutputStudents()
    {
        ifstream infile("student.txt");
        string line;
        while (getline(infile, line))
        {
            cout << line << endl;
        }
        infile.close();
    }
    //3.按学号查找学生记录
    void FindByNum(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;
        }
    }
        //4.按学号删除学生记录
       void DeleteByID(int findnum)
        {
        fstream file("student.txt");
        ofstream temp("temp.txt");
        string line;
        int num;
        bool found = false;
        while (file >> num)
         {
            getline(file, line);
            if (num != findnum) 
            {
                temp << num << " " << line << endl;
            } 
            else 
            {
                found = true; 
            }
        }
        file.close();
        temp.close();
        if (found) 
        {
            remove("student.txt");
            rename("temp.txt", "student.txt");
            cout << "已成功删除学号为 " << findnum << " 的学生记录!" << endl;
        } 
        else 
        {
            cout << "未找到学号为 " << findnum << " 的学生记录。" << endl;
        }
    }
    //5.按学号修改出生日期
    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;
    }
    }
    //6. 按学号修改学生成
    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;
};

每一行代码的注释,特别是关于文件部分的

  • 写回答

1条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-06-19 16:57
    关注

    引自免费微信小程序:皆我百晓生

    对于你提供的C++代码,下面是对每段与文件操作相关代码的详细解释及注释:

    // 定义Student类的InputStudent方法,用于输入学生信息并将其写入到"student.txt"文件中
    void InputStudent(Date &birthday)
    {
        ofstream outfile("student.txt", ios::app); // 创建一个ofstream对象outfile,打开"student.txt"文件,并以追加模式(ios::app)打开
        // 下面的代码从标准输入读取数据
        cout << "请输入学号:";
        cin >> num;
        cout << "请输入姓名:";
        cin >> name;
        // 设置生日日期,这里假设Date类有SetDate()成员函数
        birthday.SetDate();
        cout << "请输入性别:";
        cin >> gender;
        cout << "请输入3门课成绩:";
        cin >> score1 >> score2 >> score3;
        
        // 计算平均分、最高分和最低分
        double average = (score1 + score2 + score3) / 3.0;
        int maxscore = max({score1, score2, score3});
        int minscore = min({score1, score2, score3});
    
        // 将数据写入文件
        outfile << num << " " << name << " " << birthday.GetYear() << " " << birthday.GetMonth() 
                << " " << birthday.GetDay() << " " << gender << " " << score1 << " " << score2 << " " 
                << score3 << " " << average << " " << maxscore << " " << minscore << endl;
        
        // 输出确认信息
        cout << "学生信息录入成功!" << endl;
        outfile.close(); // 关闭文件
    }
    
    // 定义OutputStudents方法,用于输出所有学生的信息
    void OutputStudents()
    {
        ifstream infile("student.txt"); // 创建ifstream对象infile,打开"student.txt"文件
        string line;
        // 循环读取文件中的每一行,并输出到控制台
        while (getline(infile, line))
        {
            cout << line << endl;
        }
        infile.close(); // 关闭文件
    }
    
    // 定义FindByNum方法,用于按学号查找学生记录
    void FindByNum(int findnum)
    {
        ifstream infile("student.txt"); // 打开"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; // 输出未找到信息
        }
    }
    
    // 定义DeleteByID方法,用于按学号删除学生记录
    void DeleteByID(int findnum)
    {
        fstream file("student.txt"); // 打开"student.txt"文件,读写模式
        ofstream temp("temp.txt"); // 创建临时文件"temp.txt"
        string line;
        int num;
        bool found = false; // 是否找到学号的标志
        // 遍历文件中的每条记录
        while (file >> num)
        {
            getline(file, line); // 读取整行
            if (num != findnum) // 如果学号不等于要删除的学号
            {
                temp << num << " " << line << endl; // 将该行写入临时文件
            }
            else
            {
                found = true; // 找到了要删除的学号
            }
        }
        file.close(); // 关闭原文件
        temp.close(); // 关闭临时文件
        if (found) // 如果找到了学号
        {
            remove("student.txt"); // 删除原文件
            rename("temp.txt", "student.txt"); // 将临时文件重命名为原文件名
            cout << "已成功删除学号为 " << findnum << " 的学生记录!" << endl; // 输出删除成功信息
        }
        else // 如果没有找到学号
        {
            cout << "未找到学号为 " << findnum << " 的学生记录。" << endl; // 输出未找到信息
        }
    }
    
    // 定义AlterDateByNum方法,用于按学号修改学生的出生日期
    void AlterDateByNum(int findnum)
    {
        fstream file("student.txt"); // 打开"student.txt"文件,读写模式
        ofstream temp("temp.txt"); // 创建临时文件"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; // 创建新的Date对象
                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; // 输出未找到信息
        }
    }
    
    // 定义AlterScoreByNum方法,用于按学号修改学生的成绩
    void AlterScoreByNum(int findnum)
    {
        fstream file("student.txt"); // 打开"student.txt"文件,读写模式
        ofstream temp("temp.txt"); // 创建临时文件"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; // 输出未找到信息
        }
    }
    

    以上代码展示了如何在C++中使用文件流(fstream, ifstream, ofstream)进行文件的读写操作,包括添加、查找、删除和修改文件中的记录。在这些方法中,使用了临时文件策略来实现记录的删除和修改,这是一种常见的文件处理技巧,可以避免直接修改源文件时可能产生的数据丢失风险。

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

报告相同问题?

问题事件

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

悬赏问题

  • ¥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 数据包 大概什么价