实验六
任务一:
1 #pragma once 2 #include <iomanip> 3 #include <iostream> 4 #include <string> 5 struct Contestant { 6 long id; // 学号 7 std::string name; // 姓名 8 std::string major; // 专业 9 int solved; // 解题数 10 int penalty; // 总罚时 11 }; 12 // 重载<< 13 // 要求:姓名/专业里不含空白符 14 inline std::ostream& operator<<(std::ostream& out, const Contestant& c) { 15 out << std::left; 16 out << std::setw(15) << c.id 17 << std::setw(15) << c.name 18 << std::setw(15) << c.major 19 << std::setw(10) << c.solved 20 << std::setw(10) << c.penalty; 21 return out; 22 } 23 // 重载>> 24 inline std::istream& operator>>(std::istream& in, Contestant& c) { 25 in >> c.id >> c.name >> c.major >> c.solved >> c.penalty; 26 return in; 27 }
1 #pragma once 2 #include <fstream> 3 #include <iostream> 4 #include <stdexcept> 5 #include <string> 6 #include <vector> 7 #include "contestant.hpp" 8 // ACM 排序规则:先按解题数降序,再按罚时升序 9 inline bool cmp_by_solve(const Contestant& a, const Contestant& b) { 10 if (a.solved != b.solved) 11 return a.solved > b.solved; 12 13 return a.penalty < b.penalty; 14 } 15 // 将结果写至任意输出流 16 inline void write(std::ostream& os, const std::vector<Contestant>& v) { 17 for (const auto& x : v) 18 os << x << '\n'; 19 } 20 // 将结果打印到屏幕 21 inline void print(const std::vector<Contestant>& v) { 22 write(std::cout, v); 23 } 24 // 将结果保存到文件 25 inline void save(const std::string& filename, const std::vector<Contestant>& v) { 26 std::ofstream os(filename); 27 if (!os) 28 throw std::runtime_error("fail to open " + filename); 29 write(os, v); 30 } 31 // 从文件读取信息(跳过标题行) 32 inline std::vector<Contestant> load(const std::string& filename) { 33 std::ifstream is(filename); 34 if (!is) 35 throw std::runtime_error("fail to open " + filename); 36 std::string line; 37 std::getline(is, line); // 跳过标题 38 std::vector<Contestant> v; 39 Contestant t; 40 int seq; 41 while (is >> seq >> t) 42 v.push_back(t); 43 44 return v; 45 }
1 #include <algorithm> 2 #include <iostream> 3 #include <stdexcept> 4 #include <vector> 5 #include "contestant.hpp" 6 #include "utils.hpp" 7 const std::string in_file = "./data1.txt"; 8 const std::string out_file = "./ans1.txt"; 9 void app() { 10 std::vector<Contestant> contestants; 11 try { 12 contestants = load(in_file); 13 std::sort(contestants.begin(), contestants.end(), cmp_by_solve); 14 print(contestants); 15 save(out_file, contestants); 16 } 17 catch (const std::exception& e) { 18 std::cerr << e.what() << '\n'; 19 return; 20 } 21 } 22 int main() { 23 app(); 24 }


问题一:
(1)std::ostream 是C++标准库中所有输出流类的基类,std::cout\std::ofstream都继承自 std::ostream
(2) 不需要
问题二:
(1)第一处是当无法打开文件时
(2)异常被 app() 函数中的 try-catch 块捕获,并提前返回,不再执行后续代码
(3) 可以,功能、性能、结果都一致。
(4)遇到空白行时,operator>> 会读取失败,流状态变为 fail,循环不会立即终止,会继续尝试读取,直到文件结束,读取失败的数据也可能被录入,导致数据错误

任务二:
1 #pragma once 2 #include <iostream> 3 #include <string> 4 class Student { 5 public: 6 Student() = default; 7 ~Student() = default; 8 9 const std::string get_major() const; 10 int get_grade() const; 11 friend std::ostream& operator<<(std::ostream& os, const Student& s); 12 friend std::istream& operator>>(std::istream& is, Student& s); 13 private: 14 int id; 15 std::string name; 16 std::string major; 17 int grade; // 0-100 18 };
1 #pragma once 2 #include <string> 3 #include <vector> 4 #include "student.hpp" 5 class StuMgr { 6 public: 7 void load(const std::string& file); // 加载数据文件(空格分隔) 8 void sort(); // 排序: 按专业字典序升序、同专业分数降序 9 void print() const; // 打印到屏幕 10 void save(const std::string& file) const; // 保存到文件 11 private: 12 void write(std::ostream &os) const; // 把数据写到任意输出流 13 private: 14 std::vector<Student> students; 15 };
1 #include<iostream> 2 #include<vector> 3 #include<fstream> 4 #include<algorithm> 5 #include<string> 6 #include"stumgr.hpp" 7 8 void StuMgr::load(const std::string& file) { 9 std::ifstream is(file); 10 if (!is) { 11 throw std::runtime_error("fail to open" + file); 12 } 13 std::string line; 14 std::getline(is, line); 15 Student s; 16 while (is >> s) { 17 students.push_back(s); 18 } 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 else { 28 return a.get_grade() > b.get_grade(); 29 } 30 }); 31 } 32 33 void StuMgr::print() const { 34 for (const auto& s : students) { 35 std::cout << s<< std::endl; 36 } 37 }; 38 39 void StuMgr::save(const std::string& file) const { 40 std::ofstream os(file); 41 if (!os) 42 throw std::runtime_error("fail to open " + file); 43 write(os); 44 } 45 46 void StuMgr::write(std::ostream& os) const { 47 for (const auto& s : students) 48 os << s << std::endl; 49 };
1 #include <iostream> 2 #include <limits> 3 #include <string> 4 #include "stumgr.hpp" 5 const std::string in_file = "./data.txt"; 6 const std::string out_file = "./ans.txt"; 7 void menu() { 8 std::cout << "\n**********简易应用**********\n" 9 "1. 加载文件\n" 10 "2. 排序\n" 11 "3. 打印到屏幕\n" 12 "4. 保存到文件\n" 13 "5. 退出\n" 14 "请选择:"; 15 } 16 void app() { 17 StuMgr mgr; 18 while (true) { 19 menu(); 20 int choice; 21 std::cin >> choice; 22 try { 23 switch (choice) { 24 case 1: mgr.load(in_file); 25 std::cout << "加载成功\n"; break; 26 case 2: mgr.sort(); 27 std::cout << "排序已完成\n"; break; 28 case 3: mgr.print(); 29 std::cout << "打印已完成\n"; break; 30 case 4: mgr.save(out_file); 31 std::cout << "导出成功\n"; break; 32 case 5: return; 33 default: std::cout << "不合法输入\n"; 34 } 35 } 36 catch (const std::exception& e) { 37 std::cout << "Error: " << e.what() << '\n'; 38 } 39 } 40 } 41 int main() { 42 app(); 43 }




浙公网安备 33010602011771号