将客 2024-06-14 23:49 采纳率: 96.6%
浏览 3
已结题

关于#c++#的问题:先把学生的数据属性存入到student.txt文件,其中每学生有三门课成绩,先求三门课的平均分、三门课的最高分和最低分,再存7student.txt文件(相关搜索:学生管理系统)

#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
//定义时间日期类表示学生的出生年月日
class Date
{
public:
void SetDate () //设置年月
{
cout<<"请输入出生年份"<<endl;
cin>>year;
cout<<"请输入出生月份"<<endl;
cin>>month;
cout<<"请输入出生日"<<endl;
cin>>day;
}
int GetYear()
 {return year;}//获取年份
int GetMonth() 
{return month;}//获取月
int GetDay()
 {return day;} //获取日
void SetBirthDate()
{SetDate();}
private:
int year,month,day;
};
//定义学生类,包含学生的学号、姓名、出生年月日
class Student
{
public:
void SetStuValues (Date &birthday)
{
cout<<"请输入学生的学号"<<endl;
cin>>num;
cout<<"请输入学生的姓名"<<endl;
cin>>name;
birthday.SetDate ();
}
void SetNum () //设置学生的学号
{
cin>>num;
}
int GetNum () //获得学生的学号
{
return num;
}
void SetName ()//设置学生的姓
{
cin>>name;
}
string GetName ()//获得学生的姓名
{
return name;
}
void SetBirthday (Date &birthday) //设置年月日
{
 birthday.SetDate();
}
int GetBirYear (Date &birthday) //获得年
{return birthday.GetYear();
}
int GetBirMonth (Date &birthday) //获得月
{
  return birthday.GetMonth();
}
int GetBirDay (Date &birthday) //获得日
{
return birthday.GetDay();    
}
void InputStudent (Date &birthday)
{
char choice;
cout<<"输入学生记录"<<endl;
ofstream outfile ("d:\\student.txt", ios::app);//打开文件
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
SetStuValues (birthday);
outfile <<GetNum ()<<" "<<GetName()<<" "<<birthday.GetYear() <<" "<<birthday.GetMonth ()
 <<" "<<birthday. GetDay ()<<endl;
cout<<"是否继续输入学生信息输入y(继续)或者n(停止)";
cin>>choice;
if(choice=='y')
{
InputStudent (birthday);
}
outfile.close ();
system ("pause");
}
//5.按学号修改学生的出生日期
void AlterNameByNum ()
{
int count =0;
int year;
int month;
int day; 
int findnum;
int alteryear;
int altermonth;
int alterday;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的出生日期";
cin>>alteryear>>altermonth>>alterday;
ifstream infile ("d:\\student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("copy.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>year>>month>>day;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<endl;
}
else
{ 
outfile <<num<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<name<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
infile.get ();
if (infile.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove (" d:\\student.txt" ) ; //删除文件
rename ("copy.txt", "student.txt");
}
private:
int num;
string name;
Date birthday; 
};
void MainShow ()
{
cout<<"\t\t ***学生信息系统***"<<endl;
cout<<"\t\t ***1.输入学生记录**"<<endl;

cout<<"\t\t ***5.按学号修改学生的成绩*** "<<endl;
cout<<"\t\t ***6.退出系统***"<<endl;
}
void Select()
{
int num;
Student stu;
Date birthday;
cout<<"请选择功能"<<endl;
cin>>num;
switch(num)
{
case 1:
stu.InputStudent (birthday);
MainShow();
Select ();
break;
case 5:
stu.AlterNameByNum();
MainShow ();
Select ();
break;
case 6:
cout<<"系统结束,谢谢再见"<<endl;
exit (0);
break;
}
}
int main ()
{
MainShow ();
Select ();
return0;
}如果在原有的代码里面添加实现:使用文件知识建立一个学生管理系统。学生的数据属性有:学号、姓名、性别出生日期、课程成绩。先把学生的数据属性存入到student.txt文件,其中每学生有三门课成绩,先求三门课的平均分、三门课的最高分和最低分,再存7student.txt文件。然后学生管理系统需要实现的功能如下:
(5)按学号修改学生某门课的成绩。

应该怎么改,写出修改后的具体代码
  • 写回答

3条回答 默认 最新

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

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

    为了满足你的需求,我们可以在原有代码的基础上进行扩展,增加对学生成绩的处理,并实现按学号修改成绩的功能。下面是对原有代码的扩展部分,包括读取和计算成绩,以及修改成绩的功能。

    首先,我们需要在Student类中增加成绩相关的属性,并在构造函数中初始化这些变量。然后,在InputStudent函数中增加输入成绩的部分,同时计算并存储平均分、最高分和最低分。接下来,我们将增加一个新的函数AlterGradeByNum来修改特定学号的学生的课程成绩。最后,我们需要更新MainShowSelect函数以反映新的菜单选项。

    这里是修改后的代码:

    #include<fstream>
    #include<iostream>
    #include<string>
    #include<cstdlib>
    #include<vector>
    #include<algorithm> // For std::max and std::min
    using namespace std;
    
    class Date {
        // ... (same as before)
    };
    
    class Student {
    public:
        void SetStuValues(Date &birthday);
        // ... (same as before)
    
        void SetGrades(vector<int> grades); // New function to set grades
        vector<int> GetGrades() const;      // New function to get grades
    
        double GetAverageGrade() const;     // New function to calculate average grade
        int GetMaxGrade() const;            // New function to get max grade
        int GetMinGrade() const;            // New function to get min grade
    
        void InputStudentWithGrades(Date &birthday); // New function for input with grades
        void AlterGradeByNum(int num, int courseIndex, int newGrade); // New function to alter grade by number
    
    private:
        // ... (same as before)
        vector<int> grades; // New member variable to store grades
    };
    
    // Implement new functions here
    void Student::SetGrades(vector<int> grades) {
        this->grades = grades;
    }
    
    vector<int> Student::GetGrades() const {
        return grades;
    }
    
    double Student::GetAverageGrade() const {
        if (grades.empty()) return 0;
        int sum = 0;
        for (int grade : grades) {
            sum += grade;
        }
        return static_cast<double>(sum) / grades.size();
    }
    
    int Student::GetMaxGrade() const {
        if (grades.empty()) return 0;
        return *max_element(grades.begin(), grades.end());
    }
    
    int Student::GetMinGrade() const {
        if (grades.empty()) return 0;
        return *min_element(grades.begin(), grades.end());
    }
    
    void Student::InputStudentWithGrades(Date &birthday) {
        // ... (same as before)
        vector<int> grades(3); // Assume 3 courses
        cout << "请输入三门课的成绩" << endl;
        cin >> grades[0] >> grades[1] >> grades[2];
        SetGrades(grades);
    
        ofstream outfile("d:\\student.txt", ios::app);
        // ... (same as before)
        outfile << GetNum() << " " << GetName() << " "
                << birthday.GetYear() << " " << birthday.GetMonth() << " " << birthday.GetDay() << " "
                << GetAverageGrade() << " " << GetMaxGrade() << " " << GetMinGrade() << endl;
        // ... (same as before)
    }
    
    void Student::AlterGradeByNum(int num, int courseIndex, int newGrade) {
        // Implement the logic to alter a grade by student number and course index
        // This requires reading from file, modifying the grade, and writing back to a new file
        // Similar logic to AlterNameByNum but focused on grades
    }
    
    // Update MainShow function to include new option
    void MainShow() {
        // ... (same as before)
        cout << "\t\t ***4.按学号修改学生某门课的成绩***" << endl;
        // ... (same as before)
    }
    
    // Update Select function to handle new option
    void Select() {
        // ... (same as before)
        switch(num) {
            // ... (same as before)
            case 4: {
                Student stu;
                int num, courseIndex, newGrade;
                cout << "请输入要修改学生的学号和课程索引及新成绩:" << endl;
                cin >> num >> courseIndex >> newGrade;
                stu.AlterGradeByNum(num, courseIndex, newGrade);
                MainShow();
                Select();
                break;
            }
            // ... (same as before)
        }
    }
    
    int main() {
        // ... (same as before)
    }
    

    请注意,AlterGradeByNum函数的实现逻辑与AlterNameByNum类似,但涉及到成绩的读取、修改和写回,这需要根据文件内容进行适当调整。由于这部分代码较为复杂,涉及文件操作的具体细节,这里没有提供完整的实现,但你可以参照AlterNameByNum的结构来进行实现。确保在修改成绩后正确更新文件中的平均分、最高分和最低分。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 如何改进这个简易的模数转换程序
  • ¥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编写领子和褶裥的模型的方法