将客 2024-06-19 21:17 采纳率: 96.6%
浏览 4
已结题

输入输出和文件,面向对象程序设计

class Student { public: void InputStudent(Date &birthday) { ofstream outfile("student.txt", ios::app); cout << "请输入学号:"; cin >> num; cout << "请输入姓名:"; cin >> name; birthday.SetDate(); cout << "请输入性别:"; cin >> gender; cout << "请输入3门课成绩:"; cin >> score1 >> score2 >> score3; double average = (score1 + score2 + score3) / 3.0; int maxscore = max({score1, score2, score3}); int minscore = min({score1, score2, score3}); outfile << num << " " << name << " " << birthday.GetYear() << " " << birthday.GetMonth() << " " << birthday.GetDay()<< " " << gender << " " << score1 << " " << score2 << " " << score3 << " " << average << " " << maxscore << " " << minscore << endl; cout << "学生信息录入成功!" << endl; outfile.close(); } //2.浏览学生全部信息 void OutputStudents() { ifstream infile("student.txt"); string line; while (getline(infile, line)) { cout << line << endl; } infile.close(); } //3.按学号查找学生记录 void FindByNum(int findnum) { ifstream infile("student.txt"); int num; string line; bool found = false; while (infile >> num) { if (num == findnum) { getline(infile, line); cout << num << " " << line << endl; found = true; break; } getline(infile, line); } infile.close(); if (!found) { cout << "未找到学号为 " << findnum << " 的学生" << endl; } } //4.按学号删除学生记录 void DeleteByID(int findnum) { fstream file("student.txt"); ofstream temp("temp.txt"); string line; int num; bool found = false; while (file >> num) { getline(file, line); if (num != findnum) { temp << num << " " << line << endl; } else { found = true; } } file.close(); temp.close(); if (found) { remove("student.txt"); rename("temp.txt", "student.txt"); cout << "已成功删除学号为 " << findnum << " 的学生记录!" << endl; } else { cout << "未找到学号为 " << findnum << " 的学生记录。" << endl; } } //5.按学号修改出生日期 void AlterDateByNum(int findnum) { fstream file("student.txt"); ofstream temp("temp.txt"); int num; string name, gender, line; int year, month, day, score1, score2, score3; double average; bool found = false; while (file >> num >> name >> year >> month >> day >> gender >> score1 >> score2 >> score3 >> average) { if (num == findnum) { Date newDate; newDate.SetDate(); temp << num << " " << name << " " << newDate.GetYear() << " " << newDate.GetMonth() << " " << newDate.GetDay() << " " << gender << " " << score1 << " " << score2 << " " << score3 << " " << average << endl; found = true; cout << "已成功修改学号为 " << num << " 的学生的出生日期!" << endl; } else { temp << num << " " << name << " " << year << " " << month << " " << day << " " << gender << " " << score1 << " " << score2 << " " << score3 << " " << average << endl; } } file.close(); temp.close(); remove("student.txt"); rename("temp.txt", "student.txt"); if (!found) { cout << "未找到学号为 " << findnum << " 的学生" << endl; } } //6. 按学号修改学生成 void AlterScoreByNum(int findnum) { fstream file("student.txt"); ofstream temp("temp.txt"); int num; string name, gender, line; int year, month, day, score1, score2, score3; double average; bool found = false; while (file >> num >> name >> year >> month >> day >> gender >> score1 >> score2 >> score3 >> average) { if (num == findnum) { int newScore1, newScore2, newScore3; cout << "请输入新的三门课成绩: "; cin >> newScore1 >> newScore2 >> newScore3; double newAverage = (newScore1 + newScore2 + newScore3) / 3.0; int newMax = max({newScore1, newScore2, newScore3}); int newMin = min({newScore1, newScore2, newScore3}); temp << num << " " << name << " " << year << " " << month << " " << day << " " << gender << " " << newScore1 << " " << newScore2 << " " << newScore3 << " " << newAverage << " " << newMax << " " << newMin << endl; found = true; cout << "已成功修改学号为 " << num << " 的学生成绩!" << endl; } else { temp << num << " " << name << " " << year << " " << month << " " << day << " " << gender << " " << score1 << " " << score2 << " " << score3 << " " << average << endl; } } file.close(); temp.close(); remove("student.txt"); rename("temp.txt", "student.txt"); if (!found) { cout << "未找到学号为 " << findnum << " 的学生" << endl; } } private: int num; string name; string gender; int score1, score2, score3; };这个代码要怎么分析呢,编码的大概大概步骤是什么,然后进行结果分析,是否能实现题目所有功能

  • 写回答

1条回答 默认 最新

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

    "使用ofstream创建文件并赋值"

    以上回复有部分引自免费微信小程序:皆我百晓生。 如问题尚未解决,还请稍待,如有疑问请回复进一步沟通。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月28日
  • 已采纳回答 6月28日
  • 创建了问题 6月19日

悬赏问题

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