将客 2024-06-18 00:17 采纳率: 98.8%
浏览 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;
}

写出代码的注释,只要是对文件操作的部分,总结这个代码用到文件的哪些知识点

  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间代码工作室 Java领域优质创作者 2024-06-18 10:48
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    问题:这段代码是用来实现什么功能的?请简要描述其功能。 回答: 这段代码是一个学生信息管理系统,用来实现以下功能: 1.输入学生信息并保存到文件中; 2.浏览所有学生信息; 3.按学号查找学生信息; 4.按学号删除学生信息。 举个例子: 如果我想要输入一个学生的信息,可以调用Student类的InputStudent()函数,输入学号、姓名、出生日期、性别、三门课成绩,并计算出平均分、最高分和最低分,然后将这条信息保存到文件student.txt中; 如果我想要浏览全部学生信息,可以调用Student类的OutputStudents()函数,从student.txt文件中读取所有学生信息并输出; 如果我想要按学号查找某个学生的信息,可以调用Student类的FindByNum()函数,从student.txt中查找对应学号的学生信息并输出; 如果我想要按学号删除某个学生的信息,可以调用Student类的DeleteByID()函数,从student.txt中删除相应学号的学生信息,并将剩余信息保存到temp.txt中,最后将temp.txt重命名为student.txt。
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥20 python忆阻器数字识别
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊