当前位置: 首页 > news >正文

作业6

作业6

实验任务1

    #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;
}```cpp```
#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;
}```cpp```#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <vector>
#include "contestant.hpp"
#include "utils.hpp"const std::string in_file = "./data_bad.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();
}```cpp![0c750d25627d76590e2e739c6d0e44e8](https://img2024.cnblogs.com/blog/3713721/202512/3713721-20251223232104568-1032294922.png)![57d3cb2b99200305c9298f3d17b99e60](https://img2024.cnblogs.com/blog/3713721/202512/3713721-20251223232111062-659250288.png)问题1.1. 因为std::ostream 是他们的基类2. 不需要![06731f2550415d2e52adeb7194dc7f12](https://img2024.cnblogs.com/blog/3713721/202512/3713721-20251223231629582-1410769249.png)![23931618aa1106384641bc46b35b6c24](https://img2024.cnblogs.com/blog/3713721/202512/3713721-20251223231638705-611389031.png)问题2.1.在输入文件和输出文件无法打开的时候会被抛出2. 会被app里的catch捕获,然后他会打印抛出的内容问题3.可以,结果都是一致的问题4.![d4a672d0d4425bd302163d5b040ac827](https://img2024.cnblogs.com/blog/3713721/202512/3713721-20251223231832101-892864065.png)1.Thomas的数据出现异常,他之后的数据没有被处理, 因为Thomas有未给出的数据,程序把下一行的开头内容作为Thomas数据的一部分2.按行读取文件,先读取一行的数据,在处理单行时发现异常跳过该行并输出异常,无异常的话push_back;### 试验任务2```#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
};``````cpp#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;
};```cpp```#include <exception>
#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;int choice = 0;while(true) {menu();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();
}```cpp```#include "student.hpp"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 << " " << s.name << " " << s.major << " " << s.grade << std::endl;return os;}std::istream& operator>>(std::istream& is, Student& s) {if(!(is >> s.id >> s.name >> s.major >> s.grade)) {throw std::runtime_error("line is error");}return is;}``````cpp#include "stumgr.hpp"#include "student.hpp"#include <fstream>#include <istream>#include <sstream>#include <stdexcept>#include <string>#include <algorithm>void StuMgr::load(const std::string& file){std::ifstream fs(file);if(!fs) throw std::runtime_error("ERROR: input file can not to open");std::string line; int line_count = 0;Student s;  std::getline(fs,line);while(std::getline(fs,line)) {line_count++;std::istringstream is(line);try{is >> s;students.push_back(s);}catch(std::exception& e) {std::cerr << line_count << e.what() << std::endl;}is.clear();}}  // 加载数据文件(空格分隔)void StuMgr::sort() {std::sort(students.begin(),students.end(), [&](Student& a, Student& b) {if(a.get_major() < b.get_major()) {return true;}else if (a.get_major() == b.get_major()) {return a.get_grade() < b.get_grade();}return false;});}                     // 排序: 按专业字典序升序、同专业分数降序void StuMgr::print() const {for(auto& student: students) {std::cout << student << std::endl;}}                  // 打印到屏幕void StuMgr::save(const std::string& file) const {std::ofstream os(file);write(os);} // 保存到文件void StuMgr::write(std::ostream &os) const {for(auto& student: students) {os << student;}}  // 把数据写到任意输出流```cpp|      |      |      |
| ---- | ---- | ---- |
|      |      |      |![fcd4f63705909c2fbd74ba7bea76f24e](https://img2024.cnblogs.com/blog/3713721/202512/3713721-20251223232504309-634910279.png)![85aa51d9343c113a190f063fc9bee2ec](https://img2024.cnblogs.com/blog/3713721/202512/3713721-20251223232511295-1291731631.png)![57d3cb2b99200305c9298f3d17b99e60](https://img2024.cnblogs.com/blog/3713721/202512/3713721-20251223232111062-659250288.png)![0c750d25627d76590e2e739c6d0e44e8](https://img2024.cnblogs.com/blog/3713721/202512/3713721-20251223232104568-1032294922.png)
http://www.jsqmd.com/news/130796/

相关文章:

  • 微服务的同步异步
  • 我的第一篇随笔
  • 2025智能体(Agent)框架全景:构建自主智能的基石
  • GraniStudio:IO写入例程
  • 安川焊接机器人混合气节约方案
  • Harmony学习之图片处理与相机调用
  • 知识图谱构建
  • 2025最新沈阳堵漏公司top5推荐!专业堵漏企业及施工单位权威榜单发布,技术实力与服务品质双优助力建筑安全 - 全局中转站
  • 青少年学习困境干预的系统化路径:APCF整合咨询体系的十年技术演进与实践
  • BAS模拟入侵攻击系统:前置防控核心,守护企业网络安全
  • Harmony之路:网络请求——HTTP模块与数据交互
  • Harmony之路:分布式软总线与设备发现——构建跨设备协同的“神经网络“
  • 当下,官网为什么越来越重要了
  • GraniStudio:初始化例程
  • 自学嵌入式day37,网络编程
  • 大数据领域数据科学的气象数据分析技术
  • 注意!教你选出合肥市面上正规又靠谱的门头设计安装企业!
  • Harmony之路:优雅交互——手势处理与动画基础
  • Harmony之路:实战起航(一)——项目结构与模块化设计
  • AGV物流+机器视觉:解锁包装车间自动化升级的核心密码
  • Harmony之路:数据持久化——Preferences本地存储方案
  • 【Java】异常
  • 【Java】异常
  • Harmony之路:页面的舞台——Ability与页面路由的奥秘
  • 如何优化微信个人号的API二次开发流程?
  • 震惊!想找靠谱艺术漆品牌?联系方法竟藏在这!
  • 为什么偏偏是周二?一文了解微软“补丁星期二”的前世今生
  • Harmony之路:列表的艺术——List与ForEach高效渲染
  • python学习day05
  • 基于java的SpringBoot/SSM+Vue+uniapp的高校智能考试系统的详细设计和实现(源码+lw+部署文档+讲解等)