#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");
}
//2.浏览全部学生记录
void OutputStudent ()
{
int count= 0;
int year;
int month;
int day;
ifstream infile ("d:\\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 ("d:\\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 ("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
{
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 ("d:\\student.txt" ) ; //删除文件
rename ("copy.txt" , " student.txt" ) ;
}
//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 ***2.浏览全部学生记录***"<<endl;
cout<<"\t\t ***3.按学号查找学生记录**"<<endl;
cout<<"\t\t ***4.按学号删除学生记录***"<<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 2:
stu.OutputStudent ();
MainShow ();
Select ();
break;
case 3:
stu.FindByNum ();
MainShow ();
Select ();
break;
case 4:
stu.DeleteByNum ();
MainShow ();
Select ();
break;
case 5:
stu.AlterNameByNum();
MainShow ();
Select ();
break;
case 6:
cout<<"系统结束,谢谢再见"<<endl;
exit (0);
break;
}
}
int main ()
{
MainShow ();
Select ();
return 0;
}
如果在这个代码里面添加实现这个功能要怎么改写出修改后的具体代码
关于#c++#的问题:如果在这个代码里面添加实现这个功能要怎么改写出修改后的具体代码
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
关注 引自免费微信小程序:皆我百晓生
为了满足图1中提出的需求,我们首先需要对现有的代码进行扩展,增加性别、课程成绩等属性,并计算平均分、最高分和最低分。接着,我们需要更新
Student
类和相关函数以支持这些新功能。以下是根据需求修改后的代码示例:
#include <fstream> #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Date { // ... (保持原有的Date类不变) }; class Student { public: // 添加性别和成绩向量 void SetStuValues(Date &birthday) { cout << "请输入学生的学号" << endl; cin >> num; cout << "请输入学生的姓名" << endl; cin >> name; cout << "请输入学生的性别(M/F)" << endl; cin >> gender; birthday.SetDate(); cout << "请输入三门课程的成绩" << endl; for(int i = 0; i < 3; ++i) { int score; cin >> score; scores.push_back(score); } // 计算平均分、最高分和最低分 maxScore = *max_element(scores.begin(), scores.end()); minScore = *min_element(scores.begin(), scores.end()); avgScore = accumulate(scores.begin(), scores.end(), 0.0) / scores.size(); } // ... (保持原有的其他成员函数不变) // 添加新的成员变量 char gender; vector<int> scores; double avgScore; int maxScore; int minScore; }; // 更新InputStudent函数以存储新属性 void InputStudent(Date &birthday) { // ... (保持原有逻辑不变,只需在写入文件时添加性别和成绩) ofstream outfile("d:\\student.txt", ios::app); if (!outfile) { cout << "文件打开失败" << endl; exit(0); } Student stu; stu.SetStuValues(birthday); outfile << stu.GetNum() << " " << stu.GetName() << " " << stu.gender << " " << stu.birthday.GetYear() << " " << stu.birthday.GetMonth() << " " << stu.birthday.GetDay() << " " << stu.avgScore << " " << stu.maxScore << " " << stu.minScore << endl; // ... (保持原有逻辑不变) } // 更新OutputStudent函数以显示新属性 void OutputStudent() { // ... (保持原有逻辑不变,只需在输出时添加性别和成绩) ifstream infile("d:\\student.txt", ios::in); if (!infile) { cout << "文件打开失败" << endl; } while (!infile.eof()) { int num, year, month, day; string name; char gender; double avgScore; int maxScore, minScore; infile >> num >> name >> gender >> year >> month >> day >> avgScore >> maxScore >> minScore; cout << num << " " << name << " " << gender << " " << year << " " << month << " " << day << " " << avgScore << " " << maxScore << " " << minScore << endl; // ... (保持原有逻辑不变) } } // ... (其余函数类似地更新以处理新属性) int main() { // ... (保持main函数不变) }
以上代码展示了如何扩展
Student
类以包括性别和课程成绩,以及如何在输入和输出学生信息时处理这些新属性。注意,你需要相应地更新所有与学生数据交互的函数,以确保它们可以正确处理新增的属性。此外,对于按学号修改学生成绩的功能,你可以在AlterNameByNum
函数的基础上进行修改,增加对成绩的读取和更新逻辑。本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥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 数据包 大概什么价