实验6
任务一
代码
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 return out; 24 } 25 26 // 重载>> 27 inline std::istream& operator>>(std::istream& in, Contestant& c) { 28 in >> c.id >> c.name >> c.major >> c.solved >> c.penalty; 29 return in; 30 }
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 try { 14 contestants = load(in_file); // 原文缺失右括号,补充修正 15 std::sort(contestants.begin(), contestants.end(), cmp_by_solve); 16 print(contestants); // 原文缺失右括号,补充修正 17 save(out_file, contestants); // 原文缺失右括号,补充修正 18 } catch (const std::exception& e) { 19 std::cerr << e.what() << '\n'; 20 return; 21 } 22 } 23 24 int main() { 25 app(); 26 return 0; // 补充主函数返回值,符合C++标准 27 }
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 return a.penalty < b.penalty; 14 } 15 16 // 将结果写至任意输出流 17 inline void write(std::ostream& os, const std::vector<Contestant>& v) { 18 for (const auto& x : v) 19 os << x << '\n'; 20 } 21 22 // 将结果打印到屏幕 23 inline void print(const std::vector<Contestant>& v) { 24 write(std::cout, v); 25 } 26 27 // 将结果保存到文件 28 inline void save(const std::string& filename, const std::vector<Contestant>& v) { 29 std::ofstream os(filename); 30 if (!os) 31 throw std::runtime_error("fail to open " + filename); 32 write(os, v); 33 } 34 35 // 从文件读取信息(跳过标题行) 36 inline std::vector<Contestant> load(const std::string& filename) { 37 std::ifstream is(filename); 38 if (!is) 39 throw std::runtime_error("fail to open " + filename); 40 41 std::string line; 42 std::getline(is, line); // 跳过标题(原文缺失右括号,补充修正) 43 44 std::vector<Contestant> v; 45 Contestant t; 46 int seq; 47 while (is >> seq >> t) 48 v.push_back(t); 49 return v; 50 }
效果

问题
问题1
1. 兼容原因:`std::cout`是`std::ostream`的直接对象,`std::ofstream`是`std::ostream`的派生类。C++中派生类对象可隐式转换为基类引用,因此`write`函数的`std::ostream&`参数能同时接收这两种输出流对象。
2. 无需修改`write`函数。其基于`std::ostream&`的实现已兼容所有标准输出流类型,无需额外调整即可适配不同输出场景。
问题2
1. 文件失败处理:`load`函数中,若`std::ifstream`打开文件失败,会抛出`std::runtime_error`异常;`save`函数中,若`std::ofstream`打开文件失败,同样抛出该类型异常。
2. `app`函数的异常处理:用`try`块包裹`load`、`sort`、`print`、`save`的核心操作。捕获到`std::exception`异常后,将错误信息输出至`std::cerr`,随后终止`app`函数执行,避免程序异常崩溃。
问题3
用lambda表达式替代`cmp_by_solve`后,效果如下:
功能一致:排序逻辑与原函数完全相同,先按` solved`降序排列,`solved`相同时按`penalty`升序排列。
性能一致:编译器会对lambda进行内联优化,执行效率与普通函数无差异。
结果一致:排序后的`contestants`容器序列与原代码输出完全一致。
问题4
1. 原代码缺陷:直接通过`operator>>`循环读取时,若遇到格式不匹配的行,会设置流的`failbit`,导致后续所有读取操作失效,程序无法继续获取有效数据。
2.
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 #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_bad.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 #pragma once 2 #include <fstream> 3 #include <iostream> 4 #include <sstream> 5 #include <stdexcept> 6 #include <string> 7 #include <vector> 8 #include "contestant.hpp" 9 10 // ACM 排序规则:先按解题数降序,再按罚时升序 11 inline bool cmp_by_solve(const Contestant& a, const Contestant& b) { 12 if(a.solved != b.solved) 13 return a.solved > b.solved; 14 15 return a.penalty < b.penalty; 16 } 17 18 // 将结果写至任意输出流 19 inline void write(std::ostream& os, const std::vector<Contestant>& v) { 20 for (const auto& x : v) 21 os << x << '\n'; 22 } 23 24 // 将结果打印到屏幕 25 inline void print(const std::vector<Contestant>& v) { 26 write(std::cout, v); 27 } 28 29 // 将结果保存到文件 30 inline void save(const std::string& filename, const std::vector<Contestant>& v) { 31 std::ofstream os(filename); 32 if (!os) 33 throw std::runtime_error("fail to open " + filename); 34 35 write(os, v); 36 } 37 38 // 从文件读取信息 39 inline std::vector<Contestant> load(const std::string& filename) { 40 std::ifstream is(filename); 41 if (!is) 42 throw std::runtime_error("fail to open " + filename); 43 44 std::string line; 45 std::getline(is, line); 46 std::vector<Contestant> v; 47 int lineNum = 2; 48 49 while (std::getline(is, line)) { 50 std::istringstream iss(line); 51 int seq; 52 Contestant t; 53 if (iss >> seq >> t) { 54 v.push_back(t); 55 } else { 56 std::cerr << "警告:第 " << lineNum << " 行格式错误,已跳过\n"; 57 } 58 lineNum++; 59 } 60 return v; 61 }

改善后处理逻辑:`load`函数通过`std::getline`逐行读取文件内容,对每一行用`std::istringstream`单独解析。解析成功则将`Contestant`对象加入容器,解析失败时输出警告信息并跳过该行,确保后续有效数据的读取不受影响,提升了程序的健壮性。
任务二
代码
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 #include "student.hpp" 2 #include <stdexcept> 3 4 // 获取专业 5 const std::string Student::get_major() const { 6 return major; 7 } 8 9 // 获取成绩 10 int Student::get_grade() const { 11 return grade; 12 } 13 14 std::ostream& operator<<(std::ostream& os, const Student& s) { 15 os << s.id << "\t" << s.name << "\t" << s.major << "\t" << s.grade; 16 return os; 17 } 18 19 std::istream& operator>>(std::istream& is, Student& s) { 20 if (!(is >> s.id >> s.name >> s.major >> s.grade)) { 21 is.setstate(std::ios::failbit); 22 return is; 23 } 24 25 if (s.grade < 0 || s.grade > 100) { 26 is.setstate(std::ios::failbit); 27 } 28 29 return is; 30 }
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 "stumgr.hpp" 2 #include <fstream> 3 #include <algorithm> 4 #include <sstream> 5 #include <stdexcept> 6 #include <iostream> 7 8 // 加载数据文件 9 void StuMgr::load(const std::string& file) { 10 std::ifstream is(file); 11 if (!is) { 12 throw std::runtime_error("cannot open file: " + file); 13 } 14 15 students.clear(); 16 17 std::string line; 18 std::getline(is, line); 19 20 int line_num = 2; 21 int error_count = 0; 22 23 while (std::getline(is, line)) { 24 if (line.empty()) { 25 line_num++; 26 continue; 27 } 28 29 std::istringstream iss(line); 30 Student s; 31 32 if (iss >> s) { 33 students.push_back(s); 34 } else { 35 std::cerr << "警告:第" << line_num << "行数据格式错误或成绩无效(0-100),已跳过\n"; 36 error_count++; 37 } 38 39 line_num++; 40 } 41 42 if (error_count > 0) { 43 std::cout << "加载完成,共跳过" << error_count << "行无效数据\n"; 44 } 45 } 46 47 // 排序 48 void StuMgr::sort() { 49 if (students.empty()) { 50 std::cout << "请先加载数据或数据为空!\n"; 51 return; 52 } 53 54 std::sort(students.begin(), students.end(), 55 [](const Student& a, const Student& b) { 56 if (a.get_major() != b.get_major()) { 57 return a.get_major() < b.get_major(); 58 } 59 return a.get_grade() > b.get_grade(); 60 } 61 ); 62 } 63 64 void StuMgr::print() const { 65 if (students.empty()) { 66 std::cout << "请先加载数据或数据为空!\n"; 67 return; 68 } 69 70 std::cout << "学号\t姓名\t专业\t成绩\n"; 71 std::cout << "----------------------------\n"; 72 write(std::cout); 73 } 74 75 // 保存到文件 76 void StuMgr::save(const std::string& file) const { 77 if (students.empty()) { 78 std::cout << "请先加载数据或数据为空!\n"; 79 return; 80 } 81 82 std::ofstream os(file); 83 if (!os) { 84 throw std::runtime_error("cannot open file: " + file); 85 } 86 87 os << "学号\t姓名\t专业\t成绩\n"; 88 write(os); 89 } 90 91 void StuMgr::write(std::ostream &os) const { 92 for (const auto& s : students) { 93 os << s << '\n'; 94 } 95 }
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 }
效果


浙公网安备 33010602011771号