实验六

task1:

源代码:

 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 }
task1.cpp
 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 }
utils.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 }
contestant.hpp

 

运行结果截图:

屏幕截图 2025-12-17 081437

问题一:

(1)因为 std::cout 和 std::ofstream 都是 std::ostream 的派生类。std::ostream 是输出流类的基类,std::cout 是标准输出流(控制台),std::ofstream 是文件输出流。通过基类引用可以接受任何派生类对象,这是C++多态性的体现。

(2)不需要改动 write() 函数。因为 write() 函数接收的是 std::ostream& 类型参数,任何提供了 std::ostream 接口的设备流对象都可以作为参数传入,这体现了代码的复用性和扩展性。

问题二:

(1)代码中有两处 throw 语句:

if (!os)
throw std::runtime_error("fail to open " + filename);

if (!is)
throw std::runtime_error("fail to open " + filename);

(2)

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;
}

异常在 task1.cpp的app() 函数中被捕获。使用 try-catch 块捕获 std::exception 类型异常,通过 e.what() 获取异常信息并输出到标准错误流(std::cerr)。

问题三:

可以。一致。

问题四:

(1)

屏幕截图 2025-12-17 091451

里面少了成员数据,其中一个人罚时甚至变成了学号。

问题原因:load() 函数使用 while (is >> seq >> t) 读取,当遇到格式错误时,流状态会失败,循环提前终止。

 

 

 

task2:

源代码:

 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 }
task2.cpp
 1 #include "student.hpp"
 2 #include <iomanip>
 3 #include <sstream>
 4 
 5 // 获取专业
 6 const std::string Student::get_major() const {
 7     return major;
 8 }
 9 
10 // 获取成绩
11 int Student::get_grade() const {
12     return grade;
13 }
14 
15 // 输出运算符重载
16 std::ostream& operator<<(std::ostream& os, const Student& s) {
17     os << std::left;
18     os << std::setw(10) << s.id
19        << std::setw(15) << s.name
20        << std::setw(15) << s.major
21        << std::setw(8) << s.grade;
22     return os;
23 }
24 
25 // 输入运算符重载
26 std::istream& operator>>(std::istream& is, Student& s) {
27     is >> s.id >> s.name >> s.major >> s.grade;
28     return is;
29 }
student.cpp
 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.hpp
 1 #include "stumgr.hpp"
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <stdexcept>
 5 #include <iostream>
 6 #include <iomanip>
 7 
 8 // 加载数据文件
 9 void StuMgr::load(const std::string& file) {
10     std::ifstream in(file);
11     if (!in) {
12         throw std::runtime_error("cannot open file: " + file);
13     }
14     
15     students.clear();
16     std::string line;
17     std::getline(in, line);  // 跳过标题行
18     
19     Student s;
20     while (in >> s) {
21         // 验证成绩范围(0-100)
22         if (s.get_grade() < 0 || s.get_grade() > 100) {
23             throw std::runtime_error("成绩值不合法,应在0-100范围内");
24         }
25         students.push_back(s);
26     }
27     
28     if (students.empty()) {
29         std::cout << "(empty)\n";
30     }
31 }
32 
33 // 排序:按专业字典序升序,同专业按成绩降序
34 void StuMgr::sort() {
35     std::sort(students.begin(), students.end(),
36         [](const Student& a, const Student& b) {
37             if (a.get_major() != b.get_major()) {
38                 return a.get_major() < b.get_major();
39             }
40             return a.get_grade() > b.get_grade();
41         });
42 }
43 
44 // 打印到屏幕
45 void StuMgr::print() const {
46     if (students.empty()) {
47         std::cout << "(empty)\n";
48         return;
49     }
50     
51     // 打印标题
52     std::cout << std::left;
53     std::cout << std::setw(10) << "学号"
54               << std::setw(15) << "姓名"
55               << std::setw(15) << "专业"
56               << std::setw(8) << "成绩" << std::endl;
57     std::cout << std::string(48, '-') << std::endl;
58     
59     // 打印数据
60     for (const auto& s : students) {
61         std::cout << s << std::endl;
62     }
63 }
64 
65 // 保存到文件
66 void StuMgr::save(const std::string& file) const {
67     std::ofstream out(file);
68     if (!out) {
69         throw std::runtime_error("cannot create file: " + file);
70     }
71     
72     write(out);
73 }
74 
75 // 内部写函数
76 void StuMgr::write(std::ostream& os) const {
77     if (students.empty()) {
78         os << "(empty)\n";
79         return;
80     }
81     
82     // 写入标题
83     os << std::left;
84     os << std::setw(10) << "学号"
85        << std::setw(15) << "姓名"
86        << std::setw(15) << "专业"
87        << std::setw(8) << "成绩" << std::endl;
88     
89     // 写入数据
90     for (const auto& s : students) {
91         os << s << std::endl;
92     }
93 }
stumgr.cpp
 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.hpp

 

运行结果截图:

屏幕截图 2025-12-17 155226

 

屏幕截图 2025-12-17 160732

 

posted @ 2025-12-17 16:08  yyzbh  阅读(4)  评论(0)    收藏  举报