将客 2024-06-08 10:55 采纳率: 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)按学号修改学生某门课的成绩。
完整的代码,不省略
  • 写回答

1条回答 默认 最新

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

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

    在给定的代码基础上,我们需要对类进行扩展,并添加新的功能。首先,我们需要更新Date类以包含性别,然后创建一个新的Score类来存储学生的每门课程成绩。接着,我们将更新Student类并实现所需的功能。

    这里是你需要的完整代码:

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <vector>
    using namespace std;
    
    enum class Gender { Male, Female };
    
    // 定义分数类表示学生的单科成绩
    class Score {
    public:
        string course;
        int grade;
    
        Score(string c, int g) : course(c), grade(g) {}
    };
    
    // 更新时间日期类表示学生的出生年月日和性别
    class Date {
    public:
        void SetDate() {
            cout << "请输入出生年份:" << endl;
            cin >> year;
            cout << "请输入出生月份:" << endl;
            cin >> month;
            cout << "请输入出生日:" << endl;
            cin >> day;
        }
    
        int GetYear() const {
            return year;
        }
    
        int GetMonth() const {
            return month;
        }
    
        int GetDay() const {
            return day;
        }
    
        void SetGender(Gender gender) {
            this->gender = gender;
        }
    
        Gender GetGender() const {
            return gender;
        }
    
    private:
        int year;
        int month;
        int day;
        Gender gender;
    };
    
    // 更新学生类,包含学生的学号、姓名、出生年月日和性别,以及三门课成绩
    class Student {
    public:
        void SetStuValues(Date &birthday) {
            cout << "请输入学生的学号:" << endl;
            cin >> num;
            cout << "请输入学生的姓名:" << endl;
            cin >> name;
            birthday.SetDate();
            cout << "请输入学生的性别(M/F):" << endl;
            char genderChoice;
            cin >> genderChoice;
            if (genderChoice == 'M' || genderChoice == 'm')
                birthday.SetGender(Gender::Male);
            else if (genderChoice == 'F' || genderChoice == 'f')
                birthday.SetGender(Gender::Female);
            else {
                cout << "无效的性别选择,默认为男" << endl;
                birthday.SetGender(Gender::Male);
            }
        }
    
        void AddScore(Score score) {
            scores.push_back(score);
        }
    
        double CalculateAverageGrade() const {
            double sum = 0;
            for (const auto &score : scores) {
                sum += score.grade;
            }
            return sum / scores.size();
        }
    
        int GetMaxGrade() const {
            int maxGrade = scores[0].grade;
            for (const auto &score : scores) {
                if (score.grade > maxGrade) {
                    maxGrade = score.grade;
                }
            }
            return maxGrade;
        }
    
        int GetMinGrade() const {
            int minGrade = scores[0].grade;
            for (const auto &score : scores) {
                if (score.grade < minGrade) {
                    minGrade = score.grade;
                }
            }
            return minGrade;
        }
    
        void UpdateBirthDate(Date &newDate) {
            year = newDate.GetYear();
            month = newDate.GetMonth();
            day = newDate.GetDay();
        }
    
        void InputStudent(Date &birthday) {
            char choice;
            cout << "输入学生记录:" << endl;
            ofstream outfile("student.txt", ios::app); // 打开文件
            if (!outfile) {
                cout << "文件打开失败" << endl;
                exit(0);
            }
            SetStuValues(birthday);
            for (const auto &score : scores) {
                outfile << num << "" << name << "" << birthday.GetYear() << ""
                        << birthday.GetMonth() << "" << birthday.GetDay() << ""
                        << score.course << "" << score.grade << endl;
            }
            outfile.close();
            cout << "是否继续输入学生信息?输入y(继续)或者n(停止)" << endl;
            cin >> choice;
            if (choice == 'y') {
                InputStudent(birthday);
            }
            system("pause");
        }
    
        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;
                int gradeSum = 0;
                int gradeCount = 0;
                while (!infile.eof()) {
                    string course;
                    int grade;
                    infile >> course >> grade;
                    gradeSum += grade;
                    gradeCount++;
                    if (infile.peek() == '\n') break;
                }
                cout << "平均分: " << gradeSum / gradeCount << ", 最高分: "
                     << GetMaxGrade() << ", 最低分: " << GetMinGrade() << endl;
                infile.get();
                if (infile.peek() == '\n') break;
            }
            infile.close();
            system("pause");
        }
    
        void FindByNum() {
            int count = 0;
            int year;
            int month;
            int day;
            int findnum;
            bool found = false;
            cout << "请输入要查找学生的学号:" << endl;
            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;
                    cout << "平均分: " << CalculateAverageGrade() << ", 最高分: "
                         << GetMaxGrade() << ", 最低分: " << GetMinGrade() << endl;
                    found = true;
                }
                infile.get();
                if (infile.peek() == '\n') break;
            }
            if (!found) {
                cout << "查无此人" << endl;
            }
            infile.close();
            system("pause");
        }
    
        void DeleteByNum() {
            int count = 0;
            int year;
            int month;
            int day;
            int findnum;
            bool found = false;
            cout << "请输入要删除学生的学号:" << endl;
            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 << "";
                    for (const auto &score : scores) {
                        outfile << score.course << "" << score.grade << endl;
                    }
                } else {
                    cout << num << "" << name << "" << year << "" << month << ""
                         << day << endl;
                    found = true;
                    cout << "已经删除该学生" << endl;
                }
                infile.get();
                if (infile.peek() == '\n') break;
            }
            if (!found) {
                cout << "查无此人" << endl;
            }
            infile.close();
            outfile.close();
            remove("student.txt"); // 删除文件
            rename("studentcopy.txt", "student.txt");
        }
    
        void UpdateGradeByNum(int targetCourse, int newGrade) {
            int findnum;
            cout << "请输入要修改成绩的学生学号:" << endl;
            cin >> findnum;
            cout << "请输入要修改的课程名称:" << endl;
            string targetCourseStr;
            cin >> targetCourseStr;
            for (auto &score : scores) {
                if (score.course == targetCourseStr) {
                    score.grade = newGrade;
                    cout << "已将" << targetCourseStr << "的成绩修改为" << newGrade
                         << endl;
                    break;
                }
            }
            OutputStudent();
        }
    
    private:
        int num;
        string name;
        Date birthdate;
        vector<Score> scores;
    };
    
    void MainShow() {
        cout << "欢迎使用学生管理系统" << endl;
        cout << "1. 输入学生信息" << endl;
        cout << "2. 浏览所有学生记录" << endl;
        cout << "3. 按学号查找学生记录" << endl;
        cout << "4. 按学号删除学生记录" << endl;
        cout << "5. 按学号修改学生的出生日期" << endl;
        cout << "6. 按学号修改学生某门课的成绩" << endl;
        cout << "7. 系统退出" << endl;
    }
    
    void Select() {
        int choice;
        cout << "请选择操作:" << endl;
        cin >> choice;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

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