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

(关键词-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;
}
private:
int year;
int month;
int 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 ("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");
}
//2.浏览全部学生记录
void OutputStudent ()
{
int count= 0;
int year;
int month;
int day;
ifstream infile ("student.txt", ios::in);
if (! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>year>>month>>day;
cout<<num<<""<<name<<""<<year<<""<<month<<""<<day<<endl;
infile. get ();
if (infile.peek ()=='\n') break;
}
infile. close ();
system ("pause");
}
//3. 按学号查找学生记录
void FindByNum ()
{
int count = 0;
int year;
int month;
int day;
int findnum;
bool find = false;
cout<<"请输入要查找学生的学号";
cin>>findnum;
ifstream infile ("student. txt" , ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile. eof ())
{
infile>>num>>name>>year>>month>>day;
if (num ==findnum)
{
cout<<num<<""<<name<<""<<year<<""<<month<<""<<day<<endl;
find = true;
}
infile. get ( );
if (infile. peek ()=='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile. close ();
system ("pause");
}
//4. 按学号删除学生记录
void DeleteByNum ()
{
int count = 0;
int year;
int month;
int day;
int findnum;
bool find = false;
cout<<"请输入要删除学生的学号";    
cin>>findnum;
ifstream infile ("student.txt", ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("studentcopy. 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
{
cout<<num<<""<<name<<""<<year<<""<<month<<""<<day<<endl; 
find = true;
cout<<"已经删除该学生"<<endl;
}
infile.get ();
if (infile. peek ()=='\n') break;
}
if (! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove ("student, txt" ) ; //删除文件
rename ("studentcopy, txt" , " student. txt" ) ;
}
//5.按学号修改学生的姓名
void AlterNameByNum ()
{
int count =0;
int year;
int month;
int day; 
int findnum;
string altername;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的姓名";
cin>>altername;
ifstream infile ("student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("studentcopy. 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<<""<<altername<<""<<year<<""<<month<<""<<day<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
infile. get ();
if (infile. peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile. close ();
outfile. close ();
remove ("student, txt" ) ; //删除文件
rename ("studentcopy, txt" , " student. txt" ) ;
}
//5.按学号修改学生的姓名
void AlterNameByNum ()
{
int count =0;
int year;
int month;
int day; 
int findnum;
string altername;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的姓名";
cin>>altername;
ifstream infile ("student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("studentcopy. 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<<""<<altername<<""<<year<<""<<month<<""<<day<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
infile. get ();
if (infile. peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile. close ();
outfile. close ();
Select ();
break;
case 6:
cout<<"系统结束,谢谢再见"<<endl;
exit (0);
break;
}
}
int main ()
{
MainShow ();
Select ();
return 0;
}

如果把代码修改为学生的数据属性有:学号、姓名、性别.出生日期、课程成绩。先把学生的数据属性存入到student.txt文件,其中每个学生有三门课成绩,先求三门课的平均分、三门课的最高分和最低分,再存入student.txt文件。并且把代码原有的功能修改学生姓名改成修改学生的出生日期,然后学生管理系统需要实现的功能如下:
(1)浏览全部学生记录;
(2)按学号查找学生记录;
(3)按学号删除学生记录;
(4)按学号修改学生的出生日期。
(5)按学号修改学生某门课的成绩。

  • 写回答

2条回答 默认 最新

  • 阿里嘎多学长 2024-06-04 23:36
    关注

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


    要实现您所要求的功能,我们需要对现有的C++代码进行一些扩展和修改。以下是一些建议和代码示例,帮助您实现所需的功能。

    1. 扩展学生数据属性

    首先,我们需要在Student类中添加性别和课程成绩的数据成员,并提供相应的设置和获取方法。

    class Student {
        // ... 现有代码 ...
        // 添加性别和成绩的数据成员
        string sex;
        float scores[3]; // 假设有三门课的成绩
    
    public:
        // 添加设置和获取性别的方法
        void SetSex(const string& s) { sex = s; }
        string GetSex() const { return sex; }
    
        // 添加设置和获取成绩的方法
        void SetScores(const float* scoresArray) {
            for (int i = 0; i < 3; ++i) {
                scores[i] = scoresArray[i];
            }
        }
        float GetScore(int index) const { return scores[index]; } // 获取指定课程的成绩
    };
    

    2. 计算平均分、最高分和最低分

    我们可以在Student类中添加一个方法来计算成绩的统计信息。

    void CalculateScores() {
        float sum = 0;
        for (int i = 0; i < 3; ++i) {
            sum += scores[i];
        }
        float average = sum / 3;
        // 计算最高分和最低分
        scores[0] = max(scores[0], max(scores[1], scores[2]));
        scores[2] = min(scores[0], min(scores[1], scores[2]));
        scores[1] = average; // 将平均分放在中间位置
    }
    

    3. 更新student.txt文件

    我们需要修改InputStudent方法,以便将新的数据属性写入文件,并添加一个新的方法来更新成绩统计信息。

    void InputStudent(Date &birthday) {
        // ... 现有代码 ...
        // 添加性别和成绩的输入
        cout << "请输入学生的性别: ";
        cin >> sex;
        cout << "请输入三门课的成绩: ";
        float scoresArray[3];
        for (int i = 0; i < 3; ++i) {
            cin >> scoresArray[i];
        }
        SetScores(scoresArray);
        CalculateScores(); // 计算成绩统计信息
    
        // 更新文件写入格式
        outfile << GetNum() << " " << name << " " << sex << " "
                << birthday.GetYear() << " " << birthday.GetMonth() << " " << birthday.GetDay()
                << " " << scores[0] << " " << scores[1] << " " << scores[2] << endl;
        // ... 现有代码 ...
    }
    

    4. 实现成绩修改功能

    我们需要添加一个新的方法来修改指定课程的成绩,并重新计算成绩统计信息。

    void AlterScoreByNum(int index, float newScore) {
        if (index >= 0 && index <= 2) {
            scores[index] = newScore;
            CalculateScores(); // 更新成绩统计信息
            // 这里可以添加将修改后的信息写回文件的代码
        } else {
            cout << "错误:无效的成绩索引。" << endl;
        }
    }
    

    代码优化建议

    • 使用std::vector来代替固定大小的数组,以便更灵活地处理不同数量的成绩。
    • 使用异常处理来代替exit(0),以便更优雅地处理错误情况。
    • 考虑使用更现代的文件处理方式,如std::stringstream,以提高代码的可读性和可维护性。

    参考资料

    请注意,上述代码仅为示例,您可能需要根据您的具体需求进行调整和测试。如果您需要更详细的帮助,或者有特定的问题,请随时提问。

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

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月10日
  • 已采纳回答 6月9日
  • 修改了问题 6月8日
  • 修改了问题 6月8日
  • 展开全部

悬赏问题

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