将客 2024-06-08 02:13 采纳率: 96.6%
浏览 1
已结题

面向对象程序设计.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-08 02:13
    关注

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


    根据你的需求,以下是对现有C++代码的修改和扩展,以实现你所需的功能。

    1. 修改Date

    Date类需要能够设置和获取学生的出生日期。

    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; }
        void SetBirthDate() { SetDate(); }  // 可以重用SetDate方法
    private:
        int year, month, day;
    };
    

    2. 修改Student

    Student类需要添加性别和成绩的属性,并实现相关的方法。

    class Student {
    public:
        void SetStudentValues(Date &birthDate, char &gender, int scores[3]) {
            SetStuValues(birthDate);
            cin >> gender;
            for (int i = 0; i < 3; ++i) {
                cin >> scores[i];
            }
            CalculateScores(scores);
        }
        // ... 省略其他已有方法 ...
    
        void SetGender(char gender) {
            this->gender = gender;
        }
        char GetGender() {
            return gender;
        }
    
        void SetScores(int scores[3]) {
            for (int i = 0; i < 3; ++i) {
                this->scores[i] = scores[i];
            }
            CalculateScores(this->scores);
        }
        int* GetScores() {
            return scores;
        }
    
        void CalculateScores(int scores[3]) {
            int sum = 0;
            for (int i = 0; i < 3; ++i) {
                sum += scores[i];
                maxScore = max(maxScore, scores[i]);
                minScore = min(minScore, scores[i]);
            }
            averageScore = static_cast<double>(sum) / 3;
        }
    
        // ... 省略其他已有方法 ...
    
    private:
        char gender;
        int scores[3];
        double averageScore;
        int maxScore, minScore;
    };
    

    3. 修改main函数

    main函数中,你需要添加新的菜单选项,并实现相应的逻辑。

    void Select() {
        int choice;
        while (true) {
            cout << "请选择操作:\n";
            cout << "1. 浏览全部学生记录\n";
            cout << "2. 按学号查找学生记录\n";
            cout << "3. 按学号删除学生记录\n";
            cout << "4. 按学号修改学生的出生日期\n";
            cout << "5. 按学号修改学生某门课的成绩\n";
            cout << "6. 退出系统\n";
            cin >> choice;
            switch (choice) {
                // ... 省略其他已有case ...
                case 5:
                    // 实现按学号修改学生某门课的成绩
                    break;
                case 6:
                    exit(0);
                    break;
            }
        }
    }
    

    4. 文件操作

    你需要修改文件操作的代码,以支持新的数据结构。

    void InputStudent(Date &birthday, char gender, int scores[3]) {
        // ... 省略其他代码 ...
        outfile << GetNum() << " " << GetName() << " " << birthday.GetYear() << " "
                << birthday.GetMonth() << " " << birthday.GetDay() << " " << gender;
        for (int i = 0; i < 3; ++i) {
            outfile << " " << scores[i];
        }
        outfile << endl;
        // ... 省略其他代码 ...
    }
    
    void OutputStudent() {
        // ... 省略其他代码 ...
        char gender;
        int scores[3];
        // ... 省略其他代码 ...
        while (!infile.eof()) {
            infile >> num >> name >> year >> month >> day >> gender;
            for (int i = 0; i < 3; ++i) {
                infile >> scores[i];
            }
            cout << num << " " << name << " " << year << " " << month << " " << day
                 << " 性别: " << gender << " 成绩: ";
            for (int i = 0; i < 3; ++i) {
                cout << scores[i] << " ";
            }
            cout << endl;
        }
        // ... 省略其他代码 ...
    }
    

    5. 参考资料

    请注意,以上代码仅提供了一个大致的框架和示例,你可能需要根据实际情况进一步调整和完善代码。

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

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月13日
  • 已采纳回答 6月10日
  • 创建了问题 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 数据包 大概什么价