实验6 文件I/O与异常处理

task1:

 1 #include <iostream>
 2 #include <string>
 3 
 4 struct Contestant {
 5     long   id;              // 学号
 6     std::string name;       // 姓名
 7     std::string major;      // 专业
 8     int    solved;          // 解题数
 9     int    penalty;         // 总罚时
10 };
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 
22     return out;
23 }
24 
25 // 重载>>
26 inline std::istream& operator>>(std::istream& in, Contestant& c) {
27     in >> c.id >> c.name >> c.major >> c.solved >> c.penalty;
28 
29     return in;
30 }
 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 #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 = "D://data.txt";
 9 const std::string out_file = "D://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 }

屏幕截图 2025-12-17 081522

问题1:

(1)std::cout是标准输出流,std::ofstream是文件输出流,都是输出流对象,即ostream类的子类

(2)不需要

问题2:

(1)第32行:当创建的文件输出流os无法打开filename的文件时

 第41行:当创建的文件输入流is无法打开filename的文件时

(2)被catch模块捕获,捕获后就会输出fail to open的错误信息

问题3:

可以替换,功能,性能和结果也大致相同

问题4:

(1)Thomas之后的三位学员信息没有输出,同时Thomas的解题数和罚时变成了第8位学员的序号和学号

屏幕截图 2025-12-17 091706

原因是:>>会跳过空白字符 如换行和空格 

 

task2:

 

 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 };
 1 #include"student.hpp"
 2 #include<string>
 3 #include<iostream>
 4 #include<iomanip>
 5 
 6 const std::string Student::get_major() const{
 7     return major;
 8 }
 9 
10 int Student::get_grade() const{
11     return grade;
12 }
13 
14 std::ostream& operator<<(std::ostream& out,const Student& s){
15     out<<std::left;
16     out<<std::setw(15)<<s.id
17       <<std::setw(15)<<s.name
18       <<std::setw(15)<<s.major
19       <<std::setw(15)<<s.grade;
20       
21     return out;
22 }
23 
24 std::istream& operator>>(std::istream& in,Student& s){
25     in>>s.id>>s.name>>s.major>>s.grade;
26     
27     return in;
28 }
 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 };
 1 #include"student.hpp"
 2 #include"stumgr.hpp"
 3 #include<algorithm>
 4 #include<fstream>
 5 #include<iostream>
 6 #include<stdexcept>
 7 #include<string>
 8 #include<vector>
 9 
10 void StuMgr::write(std::ostream &os)const{
11     for(const auto&s:students)
12     os<<s<<'\n';
13 }
14 
15 void StuMgr::load(const std::string& file){
16     Student s;
17     
18     std::ifstream is(file);
19     if(!is)
20     throw std::runtime_error("fail to open"+file);
21     
22     std::string line;
23     std::getline(is,line);
24     
25     while(is>>s){
26         students.push_back(s);
27     }
28 
29 }
30 
31 void StuMgr::sort(){
32     std::sort(students.begin(),students.end(),
33     [](const Student& a,const Student& b){return (a.get_major()!=b.get_major())?(a.get_major()<b.get_major()):(a.get_grade()>b.get_grade());});
34     
35 }
36 
37 void StuMgr::print()const{
38     write(std::cout);
39 }
40 
41 void StuMgr::save(const std::string& file)const{
42     std::ofstream os(file);
43     
44     if(!os)
45     throw std::runtime_error("fail to open"+file);
46     
47     write(os);
48     
49 } 

屏幕截图 2025-12-17 165455

屏幕截图 2025-12-17 165505

屏幕截图 2025-12-17 165511

 

posted @ 2025-12-17 16:58  sunishope  阅读(3)  评论(0)    收藏  举报