实验六
任务1:
代码:
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 }
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 = "./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.
(1)std::cout是std::ostream类的实例,std::ofstream是std::ostream的派生类,基类引用可以绑定任意派生类对象,所以write()能统一处理std::cout和std::ofsream
(2) 不需要修改,只要新设备的输出流类继承自std::ostream,就能直接传入write()
2.
第一处:
std::ofstream os(filename);
if (!os)
throw std::runtime_error("fail to open " + filename);
抛出异常情况:打开输出文件失败
第二处:
std::ifstream is(filename);
if (!is)
throw std::runtime_error("fail to open " + filename);
抛出异常情况:打开输入文件失败
被try-catch捕获
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;
}
处理:打印异常描述信息到标准错误流std::cerr,执行return终止app()函数
3.
可以替代
功能一致:内部逻辑和排序规则不变
结果一致,逻辑一致对结果无影响
性能一致:两者均为值传递比较,无额外开销
4.

问题:读取不完整·出现乱码
原因:load()函数中while(is>>seq>>t)依赖流的正常状态,一旦某行读取失败,流状态变成fail,循环直接终止
修改:
逐行读取并且记录行号,用stringstream对每行单独解析检测读取是否完整,打印错误
1 inline std::vector<Contestant> load(const std::string& filename) { 2 std::ifstream is(filename); 3 if (!is) 4 throw std::runtime_error("fail to open " + filename); 5 6 std::string line; 7 std::getline(is, line); // 跳过标题 8 int line_num=1; 9 std::vector<Contestant> v; 10 11 while(std::getline(is,line)) 12 { 13 line_num++; 14 if(line.empty()) 15 { 16 std::cerr<<"第"<<line_num<<"空白,已跳过\n"; 17 continue; 18 } 19 20 std::istringstream line_is(line); 21 Contestant t; 22 int seq; 23 if (!(line_is >> seq >> t)) { 24 std::cerr << "第" << line_num << "行数据格式错误,已跳过\n"; 25 continue; 26 } 27 28 // 检测是否有多余字段(可选,确保每行格式严格) 29 std::string extra; 30 if (line_is >> extra) { 31 std::cerr << "第" << line_num << "行存在多余字段,已忽略多余部分\n"; 32 } 33 34 v.push_back(t); 35 } 36 return v; 37 }

任务2:
代码:
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<stdexcept> 3 #include<iomanip> 4 #include<iostream> 5 6 const std::string Student::get_major() const 7 { 8 return major; 9 } 10 11 int Student::get_grade() const 12 { 13 return grade; 14 } 15 //重载运算符<< 16 std::ostream& operator<<(std::ostream& os,const Student& s) 17 { 18 os<<std::left 19 <<std::setw(10)<<s.id 20 <<std::setw(15)<<s.name 21 <<std::setw(15)<<s.major 22 <<std::setw(5)<<s.grade; 23 return os; 24 } 25 26 //重载运算符>> 27 std::istream& operator>>(std::istream& is, Student& s) { 28 int id; 29 std::string name, major; 30 int grade; 31 32 33 if (!(is >> id >> name >> major >> grade)) { 34 throw std::runtime_error("字段缺失"); // 字段缺失抛异常 35 } 36 37 38 if (grade < 0 || grade > 100) { 39 throw std::runtime_error("成绩值非法(应在0-100之间)"); 40 } 41 42 s.id = id; 43 s.name = name; 44 s.major = major; 45 s.grade = grade; 46 47 return is; 48 }
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 #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 <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 }
截图:



改动:
1 void StuMgr::load(const std::string& file) 2 { 3 std::ifstream is(file); 4 if(!is) 5 { 6 throw std::runtime_error("文件打开失败:"+file); 7 } 8 9 std::string line; 10 int line_num = 0; 11 students.clear(); // 加载前清空原有数据 12 13 // 逐行读取文件 14 while (std::getline(is, line)) { 15 line_num++; 16 // 跳过空白行 17 if (line.empty()) { 18 std::cerr << "第" << line_num << "行:空白行,已跳过\n"; 19 continue; 20 } 21 22 23 std::istringstream line_is(line); 24 Student s; 25 try { 26 line_is >> s; 27 // 检测是否有多余字段 28 std::string extra; 29 if (line_is >> extra) { 30 std::cerr << "第" << line_num << "行:存在多余字段,已忽略多余部分\n"; 31 } 32 // 校验通过,加入容器 33 students.push_back(s); 34 } catch (const std::runtime_error& e) { 35 // 捕获单行解析异常,提示错误行+原因,跳过该行 36 std::cerr << "第" << line_num << "行:" << e.what() << ",已跳过\n"; 37 continue; 38 } 39 } 40 41 if (students.empty()) { 42 std::cerr << "警告:文件中无有效学员数据\n"; 43 } else { 44 std::cout << "加载成功!共读取" << students.size() << "条有效学员数据\n"; 45 } 46 }
截图


浙公网安备 33010602011771号