将客 2024-06-17 23:24 采纳率: 96.6%
浏览 1
已结题

关于#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 << "请输入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;
};
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 << "7. 退出系统" << endl;
        cout << "请选择功能:";
        cin >> choice;
        switch (choice)
        {
        case 1:
            stu.InputStudent(birthday);
            break;
        case 2:
            stu.OutputStudents();
            break;
        case 3:
            cout << "请输入要查找的学生学号:";
            cin >> findnum;
            stu.FindByNum(findnum);
            break;
        case 4:
            cout << "请输入要删除的学生学号:";
            cin >> findnum;
            stu.DeleteByID(findnum);
            break;
        case 5:
            cout << "请输入要修改日期的学生学号:";
            cin >> findnum;
            stu.AlterDateByNum(findnum);
            break;
        case 6:
            cout << "请输入要修改成绩的学生学号:";
            cin >> findnum;
            stu.AlterScoreByNum(findnum);
            break;
        case 7:
            cout << "感谢使用,再见!" << endl;
            return 0;   
        default:
            cout << "无效的选项,请重新选择。" << endl;
            break;
        }
    }
    return 0;
}

写出这个代码的每一句注释,并实现这个代码的编码步骤是什么,分析这个代码

  • 写回答

3条回答 默认 最新

  • 关注

    晚上好🌙🌙🌙
    本答案参考ChatGPT-3.5

    该代码是一个基于C++的学生信息管理系统。下面是每一句代码的注释:

    #include <iostream>      // 引入输入输出流库
    #include <fstream>       // 引入文件流库
    #include <string>        // 引入字符串库
    #include <vector>        // 引入向量库
    #include <algorithm>     // 引入算法库
    using namespace std;    // 使用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)   // 输入学生信息函数,参数为birthday对象的引用
        {
            ofstream outfile("student.txt", ios::app);   // 打开student.txt文件并以追加模式写入
            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();    // 关闭文件
        }
      
        void OutputStudents()   // 浏览学生全部信息函数
        {
            ifstream infile("student.txt");   // 打开student.txt文件并以输入模式读取
            string line;
            while (getline(infile, line))     // 循环读取每一行信息并输出
            {
                cout << line << endl;
            }
            infile.close();   // 关闭文件
        }
    
        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;
            }
        }
    
        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");   // 将临时文件重命名为student.txt
                cout << "已成功删除学号为 " << findnum << " 的学生记录!" << endl;
            }
            else
            {
                cout << "未找到学号为 " << findnum << " 的学生记录。" << endl;
            }
        }
    
        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;
                    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");   // 将临时文件重命名为student.txt
            if (!found)
            {
                cout << "未找到学号为 " << findnum << " 的学生记录。" << endl;
            }
        }
    };
    

    实现该代码的编码步骤:

    1. 引入所需的头文件。
    2. 定义Date类,其中包含设置日期、获取年月日的函数和私有的年月日变量。
    3. 定义Student类,其中包含输入学生信息、浏览学生全部信息、按学号查找学生记录、按学号删除学生记录、按学号修改出生日期的函数。
    4. main函数中创建Student对象,并根据用户的输入选择对应的功能调用对应的函数。
    5. 根据用户输入的不同,执行相应的操作,并在操作完成后返回相应的结果或提示。

    为了更好地展示解决方案,以下是修改后的代码实现:

    #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 << "请输入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();
        }
    
        void OutputStudents()
        {
            ifstream infile("student.txt");
            string line;
            while (getline(infile, line))
            {
                cout << line << endl;
            }
            infile.close();
        }
    
        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;
            }
        }
    
        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;
            }
        }
    
        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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月26日
  • 已采纳回答 6月18日
  • 创建了问题 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 数据包 大概什么价