实验6

实验任务1

源代码:

点击查看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;
}
点击查看utils.hpp
#pragma once
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <algorithm>
#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;
}
点击查看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);
        // 按ACM规则排序
        std::sort(contestants.begin(), contestants.end(), cmp_by_solve);
        // 打印到屏幕
        std::cout << "排序后的选手信息:\n";
        print(contestants);
        // 保存到文件
        save(out_file, contestants);
        std::cout << "\n结果已保存到 " << out_file << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "错误:" << e.what() << '\n';
        return;
    }
}

int main() {
    app();
    return 0;
}
运行截图:

wps (5)

问题1:
(1)派生类对象可以隐式转换为基类引用传递给函数。
(2)不需要。
问题2:
(1)打开输出输入文件失败时,catch (const std::exception& e),打印异常信息,程序终止。
问题3:
(1)可行,一致。
问题4:
(1)程序不完整。
运行截图:

wps (6)

实验任务2

源代码:

点击查看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)
};
点击查看student.cpp
#include "student.hpp"
#include <iomanip>  // 修正多尖括号错误

// 获取专业
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 << std::left;  // 补全左对齐,保证setw生效
    os << std::setw(10) << s.id
       << std::setw(10) << s.name
       << std::setw(10) << s.major
       << std::setw(5) << s.grade;
    return os;
}

// 重载>>运算符:读取学员信息(格式:学号 姓名 专业 成绩)
std::istream& operator>>(std::istream& is, Student& s) {
    is >> s.id >> s.name >> s.major >> s.grade;
    return is;
}
点击查看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;          // 把数据写到任意输出流
    std::vector<Student> students;               // 存储学员数据
};
点击查看stumgr.cpp
#include "stumgr.hpp"
#include <fstream>
#include <stdexcept>
#include <algorithm>
#include <iomanip>
#include <iostream>  // 新增:用于打印读取数量

// 加载文件:修复跳过表头、打印读取数量,增强路径验证
void StuMgr::load(const std::string& file) {
    std::ifstream is(file);
    if (!is) {
        throw std::runtime_error("cannot open file: " + file);
    }

    // 清空原有数据
    students.clear();
    Student s;
    std::string header;

    // 跳过表头行(解决data.txt含表头导致读取失败的问题)
    std::getline(is, header);

    // 循环读取所有学员信息
    int count = 0;  // 统计读取的行数
    while (is >> s) {
        students.push_back(s);
        count++;
    }

    is.close();
    // 打印读取数量,方便验证
    std::cout << "成功读取 " << count << " 条学员数据" << std::endl;
}

// 排序:专业字典序升序,同专业成绩降序
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();
        }
        return a.get_grade() > b.get_grade();
    });
}

// 打印到屏幕(调用write函数,输出流为std::cout)
void StuMgr::print() const {
    // 输出表头
    std::cout << std::left
              << std::setw(10) << "学号"
              << std::setw(10) << "姓名"
              << std::setw(10) << "专业"
              << std::setw(5) << "成绩" << std::endl;
    std::cout << "----------------------------------------" << std::endl;
    
    write(std::cout);
}

// 保存到文件:处理文件打开异常
void StuMgr::save(const std::string& file) const {
    std::ofstream os(file);
    if (!os) {
        throw std::runtime_error("cannot open file: " + file);
    }

    // 输出表头到文件
    os << std::left
       << std::setw(10) << "学号"
       << std::setw(10) << "姓名"
       << std::setw(10) << "专业"
       << std::setw(5) << "成绩" << std::endl;
    os << "----------------------------------------" << std::endl;
    
    write(os);
    os.close();
}

// 写入任意输出流(复用代码,支持屏幕/文件输出)
void StuMgr::write(std::ostream& os) const {
    for (const auto& student : students) {
        os << student << std::endl;
    }
}
点击查看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();
    return 0;
}

运行截图:

屏幕截图 2025-12-21 225750

屏幕截图 2025-12-21 225821

posted @ 2025-12-21 23:02  王扁  阅读(6)  评论(0)    收藏  举报