实验6
task1:
1.源代码
contestant.hpp
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 }
utils.hpp
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 }
task1.cpp
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 }
2.运行结果截图


3.回答问题
问题1:流操作与代码复用
观察 print() 与 save() 的实现,均在内部调用 write() :
(1) write() 的参数类型是 std::ostream& ,它为什么能同时接受 std::cout 和 std::ofstream 对象作为实参?
std::ostream是所有输出类的基类,是一个抽象类
(2)如果要把结果写到其他设备,只要该设备也提供 std::ostream 接口,还需改动 write() 吗?
不需要改动
问题2:异常处理与捕获
在代码中找到两处 throw 语句,说明:
(1)什么情况下会抛出异常;
在打开文件时,如果找不到文件,就会抛出异常
(2)异常被谁捕获、做了哪些处理。
异常被try{}catch捕获,将错误信息输出,并不再执行后续代码
问题3:替代写法
函数 app 中 std::sort(contestants.begin(), contestants.end(), cmp_by_solve); 参数 cmp_by_solve 换成下面的lambda表达式是否可以?功能、性能、结果是否一致?
可以,功能性能结果均一致
问题4:数据完整性与代码健壮性
把 in_file 改成 "./data_bad.txt" (内含空白行或字段缺失),重新编译运行:
(1)观察运行结果有什么问题?给出测试截图并分析原因。

data_bad.txt中存在空白行,在读取数据时会出现问题
task2:
1.源代码
student.hpp
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 };
student.cpp
1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 5 #include "student.hpp" 6 7 const std::string Student::get_major() const { 8 return major; 9 } 10 11 int Student::get_grade() const { 12 return grade; 13 } 14 15 std::ostream& operator<<(std::ostream& os, const Student& s) { 16 os << std::left; 17 os << std::setw(10) << s.id; 18 os << std::setw(10) << s.name; 19 os << std::setw(10) << s.major; 20 os << std::setw(10) << s.grade; 21 return os; 22 } 23 24 std::istream& operator>>(std::istream& is, Student& s) { 25 is >> s.id >> s.name >> s.major >> s.grade; 26 return is; 27 }
stumgr.hpp
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 };
stumgr.cpp
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<fstream> 5 #include<algorithm> 6 #include<stdexcept> 7 8 #include "stumgr.hpp" 9 10 bool cmp_by_major(Student &s1, Student &s2) { 11 if (s1.get_major() != s2.get_major()) 12 return s1.get_major() < s2.get_major(); 13 return s1.get_grade() > s2.get_grade(); 14 } 15 16 void StuMgr::write(std::ostream& os) const { 17 for (auto& x : students) 18 os << x << "\n"; 19 } 20 21 void StuMgr::load(const std::string& file) { 22 std::ifstream is(file); 23 if (!is) 24 throw std::runtime_error("fail to open" + file); 25 26 std::string line; 27 std::getline(is, line); 28 29 Student t; 30 while (is >> t) 31 students.push_back(t); 32 } 33 34 void StuMgr::sort() { 35 std::sort(students.begin(), students.end(), cmp_by_major); 36 } 37 38 void StuMgr::print() const { 39 write(std::cout); 40 } 41 42 void StuMgr::save(const std::string& file) const { 43 std::ofstream os(file); 44 if (!os) 45 throw std::runtime_error("fail to open" + file); 46 write(os); 47 }
2.运行结果截图


浙公网安备 33010602011771号