实验六
task1:
1.源代码
contestant.hpp
#pragma once
#include <iomanip>
#include <iostream>
#include <string>
struct Contestant {
long id; // 学号
std::string name; // 姓名
std::string major; // 专业
int solved; // 解题数
int penalty; // 总罚时
};
// 重载<<
// 要求:姓名/专业里不含空白符
inline std::ostream& operator<<(std::ostream& out, const Contestant& c) {
out << std::left;
out << std::setw(15) << c.id
<< std::setw(15) << c.name
<< std::setw(15) << c.major
<< std::setw(10) << c.solved
<< std::setw(10) << c.penalty;
return out;
}
// 重载>>
inline std::istream& operator>>(std::istream& in, Contestant& c) {
in >> c.id >> c.name >> c.major >> c.solved >> c.penalty;
return in;
}
utils.hpp
#pragma once #include <fstream> #include <iostream> #include <stdexcept> #include <string> #include <vector> #include "contestant.hpp" // ACM 排序规则:先按解题数降序,再按罚时升序 inline bool cmp_by_solve(const Contestant& a, const Contestant& b) { if(a.solved != b.solved) return a.solved > b.solved; return a.penalty < b.penalty; } // 将结果写至任意输出流 inline void write(std::ostream& os, const std::vector<Contestant>& v) { for (const auto& x : v) os << x << '\n'; } // 将结果打印到屏幕 inline void print(const std::vector<Contestant>& v) { write(std::cout, v); } // 将结果保存到文件 inline void save(const std::string& filename, const std::vector<Contestant>& v) { std::ofstream os(filename); if (!os) throw std::runtime_error("fail to open " + filename); write(os, v); } // 从文件读取信息(跳过标题行) inline std::vector<Contestant> load(const std::string& filename) { std::ifstream is(filename); if (!is) throw std::runtime_error("fail to open " + filename); std::string line; std::getline(is, line); // 跳过标题 std::vector<Contestant> v; Contestant t; int seq; while (is >> seq >> t) v.push_back(t); return v; }
task1.cpp
#include <algorithm> #include <iostream> #include <stdexcept> #include <vector> #include "contestant.hpp" #include "utils.hpp" const std::string in_file = "./data.txt"; const std::string out_file = "./ans.txt"; void app() { std::vector<Contestant> contestants; try { contestants = load(in_file); std::sort(contestants.begin(), contestants.end(), cmp_by_solve); print(contestants); save(out_file, contestants); } catch (const std::exception& e) { std::cerr << e.what() << '\n'; return; } } int main() { app(); }
运行结果截图


问题1:流操作与代码复用
观察 print() 与 save() 的实现,均在内部调用 write() :
(1) write() 的参数类型是 std::ostream& ,它为什么能同时接受 std::cout 和 std::ofstream 对象作为实参?
std::cout 属于 std::ofstream 类 ,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
#pragma once #include <iostream> #include <string> class Student { public: Student() = default; ~Student() = default; const std::string get_major() const; int get_grade() const; friend std::ostream& operator<<(std::ostream& os, const Student& s); friend std::istream& operator>>(std::istream& is, Student& s); private: int id; std::string name; std::string major; int grade; // 0-100 };
student.cpp
#include<iostream> #include<string> #include<iomanip> #include "student.hpp" const std::string Student::get_major() const { return major; } int Student::get_grade() const { return grade; } std::ostream& operator<<(std::ostream& os, const Student& s) { os << std::left; os << std::setw(10) << s.id; os << std::setw(10) << s.name; os << std::setw(10) << s.major; os << std::setw(10) << s.grade; return os; } std::istream& operator>>(std::istream& is, Student& s) { is >> s.id >> s.name >> s.major >> s.grade; return is; }
stumgr.hpp
#pragma once #include <string> #include <vector> #include "student.hpp" class StuMgr { public: void load(const std::string& file); // 加载数据文件(空格分隔) void sort(); // 排序: 按专业字典序升序、同专业分数降序 void print() const; // 打印到屏幕 void save(const std::string& file) const; // 保存到文件 private: void write(std::ostream &os) const; // 把数据写到任意输出流 private: std::vector<Student> students; };
stumgr.cpp
#include<iostream> #include<string> #include<vector> #include<fstream> #include<algorithm> #include<stdexcept> #include "stumgr.hpp" bool cmp_by_major(Student &s1, Student &s2) { if (s1.get_major() != s2.get_major()) return s1.get_major() < s2.get_major(); return s1.get_grade() > s2.get_grade(); } void StuMgr::write(std::ostream& os) const { for (auto& x : students) os << x << "\n"; } void StuMgr::load(const std::string& file) { std::ifstream is(file); if (!is) throw std::runtime_error("fail to open" + file); std::string line; std::getline(is, line); Student t; while (is >> t) students.push_back(t); } void StuMgr::sort() { std::sort(students.begin(), students.end(), cmp_by_major); } void StuMgr::print() const { write(std::cout); } void StuMgr::save(const std::string& file) const { std::ofstream os(file); if (!os) throw std::runtime_error("fail to open" + file); write(os); }
运行结果截图



浙公网安备 33010602011771号