实验六
实验任务1:
源代码:
1 #pragma once 2 #include <iomanip> 3 #include <iostream> 4 #include <string> 5 6 struct Contestant { 7 long id; // 学号 8 std::string name; // 姓名 9 std::string major; // 专业 10 int solved; // 解题数 11 int penalty; // 总罚时 12 }; 13 14 // 重载<< 15 // 要求:姓名/专业里不含空白符 16 inline std::ostream& operator<<(std::ostream& out, const Contestant& c) { 17 out << std::left; 18 out << std::setw(15) << c.id 19 << std::setw(15) << c.name 20 << std::setw(15) << c.major 21 << std::setw(10) << c.solved 22 << std::setw(10) << c.penalty; 23 24 return out; 25 } 26 27 // 重载>> 28 inline std::istream& operator>>(std::istream& in, Contestant& c) { 29 in >> c.id >> c.name >> c.major >> c.solved >> c.penalty; 30 31 return in; 32 }
1 #pragma once 2 #include <fstream> 3 #include <iostream> 4 #include <stdexcept> 5 #include <string> 6 #include <vector> 7 #include "contestant.hpp" 8 9 // ACM 排序规则:先按解题数降序,再按罚时升序 10 inline bool cmp_by_solve(const Contestant& a, const Contestant& b) { 11 if(a.solved != b.solved) 12 return a.solved > b.solved; 13 14 return a.penalty < b.penalty; 15 } 16 17 // 将结果写至任意输出流 18 inline void write(std::ostream& os, const std::vector<Contestant>& v) { 19 for (const auto& x : v) 20 os << x << '\n'; 21 } 22 23 // 将结果打印到屏幕 24 inline void print(const std::vector<Contestant>& v) { 25 write(std::cout, v); 26 } 27 28 // 将结果保存到文件 29 inline void save(const std::string& filename, const std::vector<Contestant>& v) { 30 std::ofstream os(filename); 31 if (!os) 32 throw std::runtime_error("fail to open " + filename); 33 34 write(os, v); 35 } 36 37 // 从文件读取信息(跳过标题行) 38 inline std::vector<Contestant> load(const std::string& filename) { 39 std::ifstream is(filename); 40 if (!is) 41 throw std::runtime_error("fail to open " + filename); 42 43 std::string line; 44 std::getline(is, line); // 跳过标题 45 46 std::vector<Contestant> v; 47 Contestant t; 48 int seq; 49 while (is >> seq >> t) 50 v.push_back(t); 51 52 return v; 53 }
1 #include <algorithm> 2 #include <iostream> 3 #include <stdexcept> 4 #include <vector> 5 #include "contestant.hpp" 6 #include "utils.hpp" 7 8 const std::string in_file = "./data.txt"; 9 const std::string out_file = "./ans.txt"; 10 11 void app() { 12 std::vector<Contestant> contestants; 13 14 try { 15 contestants = load(in_file); 16 std::sort(contestants.begin(), contestants.end(), cmp_by_solve); 17 print(contestants); 18 save(out_file, contestants); 19 } catch (const std::exception& e) { 20 std::cerr << e.what() << '\n'; 21 return; 22 } 23 } 24 25 int main() { 26 app(); 27 }
1 序号 学号 姓名 专业 解题数(道) 总罚时(分钟) 2 1 204942005 Jeny 未来专业1 4 210 3 2 204942302 Alex 未来专业2 3 180 4 3 204942059 Bob 未来专业2 5 350 5 4 204942111 Hellen 未来专业3 2 90 6 5 204942017 chappie 未来专业4 4 260 7 6 204942075 Shaw 未来专业5 3 150 8 7 204942076 Thomas 未来专业6 6 420 9 8 204942078 Jennie 未来专业7 1 30 10 9 204942079 Tibby 未来专业7 3 200 11 10 204942080 Vermont 未来专业8 5 310
1 序号 学号 姓名 专业 解题数(道) 总罚时(分钟) 2 1 204942005 Jeny 未来专业1 4 210 3 2 204942302 Alex 未来专业2 3 180 4 5 3 204942059 Bob 未来专业2 5 350 6 4 204942111 Hellen 未来专业3 2 90 7 5 204942017 chappie 未来专业4 4 260 8 6 204942075 Shaw 未来专业5 3 150 9 7 204942076 Thomas 未来专业6 10 8 204942078 Jennie 未来专业7 1 30 11 9 204942079 Tibby 未来专业7 3 200 12 10 204942080 Vermont 未来专业8 5 310
运行截图:

问题1:
std::ostream是std::cout和std::ofstream的基类
不需要,设备提供std::ostream的派生类的输出流可以直接复用write()
问题2:
if (!os)
throw std::runtime_error("fail to open " + filename);
if (!is)
throw std::runtime_error("fail to open " + filename);
当无法打开filename指定的文件时,抛出异常;
异常被app()中的try-catch块捕获,执行打印异常信息到标准错误流std::cerr,执行return,提前结束函数
问题3:可以,一致
问题4:

信息错乱,读取到空白行时,输入流状态失效,后续读取的内容顺序错乱
文件中有数据缺失导致读取contestant对象时字段匹配错误
实验任务2:
源代码:
1 学号 姓名 专业 成绩 2 1001 抖森 Acting 80 3 1002 宝爷 Music 97 4 1003 大眼仔 Acting 75 5 1004 本喵 Acting 99 6 1005 裘花 Acting 89 7 1006 小李子 Acting 92 8 1007 无脸男 Acting 85 9 1008 甜茶 Acting 91 10 1009 囧瑟夫 Acting 88 11 1010 霉霉 Music 96
1 学号 姓名 专业 成绩 2 1001 抖森 Acting 80 3 1002 宝爷 Music 97 4 1003 大眼仔 Acting 5 1004 本喵 Acting 99 6 1005 裘花 Acting 89 7 1006 小李子 Acting 92 8 1007 无脸男 Acting 105 9 1008 甜茶 Acting 91 10 1009 囧瑟夫 Acting 88 11 1010 霉霉 Music 96
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 class Student { 7 public: 8 Student() = default; 9 ~Student() = default; 10 11 const std::string get_major() const; 12 int get_grade() const; 13 14 friend std::ostream& operator<<(std::ostream& os, const Student& s); 15 friend std::istream& operator>>(std::istream& is, Student& s); 16 17 private: 18 int id; 19 std::string name; 20 std::string major; 21 int grade; // 0-100 22 };
1 #pragma once 2 #include <string> 3 #include <vector> 4 #include "student.hpp" 5 6 class StuMgr { 7 public: 8 void load(const std::string& file); // 加载数据文件(空格分隔) 9 void sort(); // 排序: 按专业字典序升序、同专业分数降序 10 void print() const; // 打印到屏幕 11 void save(const std::string& file) const; // 保存到文件 12 13 private: 14 void write(std::ostream &os) const; // 把数据写到任意输出流 15 16 private: 17 std::vector<Student> students; 18 };
1 #include <iostream> 2 #include <limits> 3 #include <string> 4 #include "stumgr.hpp" 5 6 const std::string in_file = "./data.txt"; 7 const std::string out_file = "./ans.txt"; 8 9 void menu() { 10 std::cout << "\n**********简易应用**********\n" 11 "1. 加载文件\n" 12 "2. 排序\n" 13 "3. 打印到屏幕\n" 14 "4. 保存到文件\n" 15 "5. 退出\n" 16 "请选择:"; 17 } 18 19 void app() { 20 StuMgr mgr; 21 22 while(true) { 23 menu(); 24 int choice; 25 std::cin >> choice; 26 27 try { 28 switch (choice) { 29 case 1: mgr.load(in_file); 30 std::cout << "加载成功\n"; break; 31 case 2: mgr.sort(); 32 std::cout << "排序已完成\n"; break; 33 case 3: mgr.print(); 34 std::cout << "打印已完成\n"; break; 35 case 4: mgr.save(out_file); 36 std::cout << "导出成功\n"; break; 37 case 5: return; 38 default: std::cout << "不合法输入\n"; 39 } 40 } 41 catch (const std::exception& e) { 42 std::cout << "Error: " << e.what() << '\n'; 43 } 44 } 45 } 46 47 int main() { 48 app(); 49 }
1 #include "student.hpp" 2 #include <iomanip> 3 4 const std::string Student::get_major() const { 5 return major; 6 } 7 int Student::get_grade() const { 8 return grade; 9 } 10 11 std::ostream& operator<<(std::ostream& os, const Student& s) { 12 os << std::setw(6) << s.id 13 << std::setw(8) << s.name 14 << std::setw(10) << s.major 15 << std::setw(4) << s.grade; 16 return os; 17 } 18 19 std::istream& operator>>(std::istream& is, Student& s) { 20 is >> s.id >> s.name >> s.major >> s.grade; 21 return is; 22 }
1 #include "stumgr.hpp" 2 #include <fstream> 3 #include <stdexcept> 4 #include <algorithm> 5 #include <iostream> 6 // 加载文件 7 void StuMgr::load(const std::string& file) { 8 std::ifstream is(file); 9 if (!is.is_open()) { 10 throw std::runtime_error("cannot open file: " + file); 11 } 12 13 students.clear(); // 清空原有数据 14 Student s; 15 std::string line; 16 std::getline(is, line); // 跳过标题行 17 while (is >> s) { 18 students.push_back(s); 19 } 20 } 21 // 排序 22 void StuMgr::sort() { 23 std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) { 24 if (a.get_major() != b.get_major()) { 25 return a.get_major() < b.get_major(); 26 } 27 return a.get_grade() > b.get_grade(); 28 }); 29 } 30 //打印 31 void StuMgr::print() const { 32 write(std::cout); 33 } 34 35 // 保存 36 void StuMgr::save(const std::string& file) const { 37 std::ofstream os(file); 38 if (!os.is_open()) { 39 throw std::runtime_error("cannot open file: " + file); 40 } 41 write(os); 42 } 43 44 void StuMgr::write(std::ostream& os) const { 45 for (const auto& s : students) { 46 os << s << std::endl; 47 } 48 }
运行截图:



浙公网安备 33010602011771号