实验任务一:
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 = R"(C:\Users\zhang\Downloads\实验6部分代码及数据文件_gbk\实验6部分代码及数据文件_gbk\1\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 #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 = R"(C:\Users\zhang\Downloads\实验6部分代码及数据文件_gbk\实验6部分代码及数据文件_gbk\1\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 #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:
答:(1)根据类型兼容规则,ostream是ofstream的基类,所以可以接受std::ostream的实参,当然也可以接受本就是ostream类的对象std::cout。
(2)不需要了。
问题2:
答:(1)无法打开文件时抛出异常。
(2)异常被std::exception &e 捕捉了。
处理:输出了错误类型,并接结束了程序的运行。
问题3:
答:是可行的。功能性能结果一致。
问题4:
答:第二三行之间的空行对读取没有影响,cin会自动跳过,但是第七行数据的解题数和总罚时数据空缺,因此顺延读到了下一行数据的序列和学号,因此导致了后面数据的读取混乱。

实验任务二:
答:(类的实现直接写在了hpp文件里面)
1 #include <iostream> 2 #include <limits> 3 #include <string> 4 #include "stumgr.hpp" 5 6 const std::string in_file = "./data_bad.txt"; 7 const std::string out_file = "./ans_bad.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 #pragma once 2 #include <string> 3 #include<fstream> 4 #include <vector> 5 #include<algorithm> 6 #include "student.hpp" 7 class StuMgr { 8 public: 9 void load(const std::string& file); // 加载数据文件(空格分隔) 10 void sort(); // 排序: 按专业字典序升序、同专业分数降序 11 void print() const; // 打印到屏幕 12 void save(const std::string& file) const; // 保存到文件 13 14 private: 15 void write(std::ostream &os) const; // 把数据写到任意输出流 16 17 private: 18 std::vector<Student> students; 19 }; 20 21 void StuMgr::load(const std::string& file)// 加载数据文件(空格分隔) 22 { 23 std::ifstream inf; 24 std::string temp; 25 Student u; 26 inf.open(file); 27 if (!inf) 28 throw std::runtime_error("cannot open file: " + file); 29 std::getline(inf, temp, '\n'); 30 while (inf >> u) 31 { 32 students.push_back(u); 33 } 34 } 35 36 void StuMgr::sort() // 排序: 按专业字典序升序、同专业分数降序 37 { 38 std::sort(students.begin(), students.end(), [](Student& a, Student& b)->bool {if (a.get_grade() != b.get_grade())return a.get_grade() > b.get_grade(); else return a.get_name() > a.get_name(); }); 39 } 40 41 void StuMgr::print() const // 打印到屏幕 42 { 43 44 for (auto item : students) 45 cout << item ; 46 } 47 48 void StuMgr::write(std::ostream& os) const // 把数据写到任意输出流 49 { 50 for (auto item : students) 51 os << item; 52 } 53 54 55 void StuMgr::save(const std::string& file) const // 保存到文件 56 { 57 std::ofstream outf; 58 outf.open("./ans",std::ios::out); 59 if (!outf) 60 throw std::runtime_error("cannot open file: " + file); 61 62 write(outf); 63 }
1 #include <iostream> 2 #include <limits> 3 #include <string> 4 #include "stumgr.hpp" 5 6 const std::string in_file = "./data_bad.txt"; 7 const std::string out_file = "./ans_bad.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 学号 姓名 专业 成绩 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

