任务一
源代码task1.cpp
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <vector>
#include "contestant.hpp"
#include "utils.hpp"
const std::string in_file = "./data.txt";
const std::string out_file = "./ans.txt";
void app() {
std::vector<Contestant> contestants;
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;
}
}
int main() {
app();
}
utils.hpp
#pragma once
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "contestant.hpp"
// ACM 排序规则:先按解题数降序,再按罚时升序
inline bool cmp_by_solve(const Contestant& a, const Contestant& b) {
if(a.solved != b.solved)
return a.solved > b.solved;
return a.penalty < b.penalty;
}
// 将结果写至任意输出流
inline void write(std::ostream& os, const std::vector<Contestant>& v) {
for (const auto& x : v)
os << x << '\n';
}
// 将结果打印到屏幕
inline void print(const std::vector<Contestant>& v) {
write(std::cout, v);
}
// 将结果保存到文件
inline void save(const std::string& filename, const std::vector<Contestant>& v) {
std::ofstream os(filename);
if (!os)
throw std::runtime_error("fail to open " + filename);
write(os, v);
}
// 从文件读取信息(跳过标题行)
inline std::vector<Contestant> load(const std::string& filename) {
std::ifstream is(filename);
if (!is)
throw std::runtime_error("fail to open " + filename);
std::string line;
std::getline(is, line); // 跳过标题
std::vector<Contestant> v;
Contestant t;
int seq;
while (is >> seq >> t)
v.push_back(t);
return v;
}
contestant.hpp
#pragma once
#include <iomanip>
#include <iostream>
#include <string>
struct Contestant {
long id; // 学号
std::string name; // 姓名
std::string major; // 专业
int solved; // 解题数
int penalty; // 总罚时
};
// 重载<<
// 要求:姓名/专业里不含空白符
inline std::ostream& operator<<(std::ostream& out, const Contestant& c) {
out << std::left;
out << std::setw(15) << c.id
<< std::setw(15) << c.name
<< std::setw(15) << c.major
<< std::setw(10) << c.solved
<< std::setw(10) << c.penalty;
return out;
}
// 重载>>
inline std::istream& operator>>(std::istream& in, Contestant& c) {
in >> c.id >> c.name >> c.major >> c.solved >> c.penalty;
return in;
}
运行结果截图
![屏幕截图 2025-12-22 185933]()
问题一:(1) std::ostream是std::cout和std::ofstream的基类。C++ 中多态特性支持基类引用接收派生类对象,所以write()的std::ostream&参数可同时接收std::cout和std::ofstream。(2) 不需要改动:只要新设备的输出流类是std::ostream的派生类,write()可以直接复用。
问题二:(1) save()中:打开输出文件(std::ofstream os(filename))失败时,抛出std::runtime_error;load()中:打开输入文件(std::ifstream is(filename))失败时,抛出std::runtime_error。(2) 异常的捕获与处理:异常被app()中的try-catch块捕获,捕获后通过std::cerr输出异常信息(e.what()),并终止app()函数执行。
问题三:可以替换,功能、性能、结果完全一致。功能:lambda 表达式的逻辑与cmp_by_solve函数完全相同(先按解题数降序,再按罚时升序);性能:lambda 是匿名函数,与普通函数的排序性能无差异;结果:排序规则一致,最终结果完全相同。
问题四:(1) 运行问题与原因:若data_bad.txt含空白行:load()中is >> seq >> t会读取失败,导致部分数据未加载;若data_bad.txt含字段缺失:operator>>读取字段时会触发流的 “错误状态”,后续读取全部失败,最终vector<Contestant>数据不完整。
任务二:
源代码task2.cpp
#include <iostream>
#include <limits>
#include <string>
#include "stumgr.hpp"
const std::string in_file = "./data.txt";
const std::string out_file = "./ans.txt";
void menu() {
std::cout << "\n**********简易应用**********\n"
"1. 加载文件\n"
"2. 排序\n"
"3. 打印到屏幕\n"
"4. 保存到文件\n"
"5. 退出\n"
"请选择:";
}
void app() {
StuMgr mgr;
while(true) {
menu();
int choice;
std::cin >> choice;
try {
switch (choice) {
case 1: mgr.load(in_file);
std::cout << "加载成功\n"; break;
case 2: mgr.sort();
std::cout << "排序已完成\n"; break;
case 3: mgr.print();
std::cout << "打印已完成\n"; break;
case 4: mgr.save(out_file);
std::cout << "导出成功\n"; break;
case 5: return;
default: std::cout << "不合法输入\n";
}
}
catch (const std::exception& e) {
std::cout << "Error: " << e.what() << '\n';
}
}
}
int main() {
app();
}
student.hpp
#pragma once
#include <iostream>
#include <string>
class Student {
public:
Student() = default;
~Student() = default;
const std::string get_major() const;
int get_grade() const;
friend std::ostream& operator<<(std::ostream& os, const Student& s);
friend std::istream& operator>>(std::istream& is, Student& s);
private:
int id;
std::string name;
std::string major;
int grade; // 0-100
};
stumgr.hpp
#pragma once
#include <string>
#include <vector>
#include "student.hpp"
class StuMgr {
public:
void load(const std::string& file); // 加载数据文件(空格分隔)
void sort(); // 排序: 按专业字典序升序、同专业分数降序
void print() const; // 打印到屏幕
void save(const std::string& file) const; // 保存到文件
private:
void write(std::ostream &os) const; // 把数据写到任意输出流
private:
std::vector<Student> students;
};
student.cpp
#include "student.hpp"
#include <iostream>
#include <string>
// 获取专业
const std::string Student::get_major() const {
return major;
}
// 获取成绩
int Student::get_grade() const {
return grade;
}
// 重载输出运算符<<:格式化输出学生信息
std::ostream& operator<<(std::ostream& os, const Student& s) {
os << s.id << "\t" << s.name << "\t" << s.major << "\t" << s.grade;
return os;
}
// 重载输入运算符>>:从流中读取学生信息
std::istream& operator>>(std::istream& is, Student& s) {
is >> s.id >> s.name >> s.major >> s.grade;
return is;
}
stumgr.cpp
#include "stumgr.hpp"
#include "student.hpp"
#include <fstream>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <string>
// 加载文件:从指定文件读取学生数据(跳过标题行)
void StuMgr::load(const std::string& file) {
std::ifstream ifs(file);
if (!ifs.is_open()) {
throw std::runtime_error("cannot open file: " + file);
}
// 跳过标题行
std::string title;
std::getline(ifs, title);
// 读取学生数据
Student s;
students.clear();
while (ifs >> s) {
students.push_back(s);
}
ifs.close();
}
// 排序:按专业字典序升序,同专业按成绩降序
void StuMgr::sort() {
std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
if (a.get_major() != b.get_major()) {
return a.get_major() < b.get_major(); // 专业字典序升序
} else {
return a.get_grade() > b.get_grade(); // 同专业成绩降序
}
});
}
// 打印到屏幕:调用write输出到std::cout
void StuMgr::print() const {
write(std::cout);
}
// 保存到文件:调用write输出到指定文件
void StuMgr::save(const std::string& file) const {
std::ofstream ofs(file);
if (!ofs.is_open()) {
throw std::runtime_error("cannot open file: " + file);
}
write(ofs);
ofs.close();
}
// 通用写操作:将学生数据写入任意输出流(屏幕/文件)
void StuMgr::write(std::ostream& os) const {
for (const auto& s : students) {
os << s << "\n";
}
}
运行结果截图
![屏幕截图 2025-12-22 194543]()
![屏幕截图 2025-12-22 194555]()