OOP实验六

news/2025/3/9 10:55:24/文章来源:https://www.cnblogs.com/0448wyz/p/18613308
实验任务一:
#pragma once#include <iostream>
#include <stdexcept>// 声明
////////////////////////////////////////////////////
// 复数模板类声明
template<typename T>
class Complex {
public:Complex(T r = 0, T i = 0);Complex(const Complex<T> &c);T get_real() const;T get_imag() const;// 重载+=为成员函数Complex<T>& operator+=(const Complex<T> &c);// 重载<<、>>为友元函数template<typename T1>friend std::ostream& operator<<(std::ostream &out, const Complex<T1> &c);template<typename T1>friend std::istream& operator>>(std::istream &in, Complex<T1> &c);private:T real, imag;
};// 普通函数声明
// 重载+用于Complex类型
template<typename T>
Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2);// 重载==用于Complex类型
template<typename T>
bool operator==(const Complex<T> &c1, const Complex<T> &c2);// 实现
////////////////////////////////////////////////////
// 成员函数模板实现
template<typename T>
Complex<T>::Complex(T r, T i): real{r}, imag{i} {
}template<typename T>
Complex<T>::Complex(const Complex<T> &c): real{c.real}, imag{c.imag} {
}template<typename T>
T Complex<T>::get_real() const {return real;
}template<typename T>
T Complex<T>::get_imag() const {return imag;
}// 重载+=为成员函数
template<typename T>
Complex<T>& Complex<T>::operator+=(const Complex<T> &c) {real += c.real;imag += c.imag;return *this;
}///////////////////////////////////////
// 友元函数模板实现
template<typename T1>
std::ostream& operator<<(std::ostream &out, const Complex<T1> &c) {if(c.imag >= 0)out << c.real << " + " << c.imag << "i";elseout << c.real << " - " << -c.imag << "i";return out;
}template<typename T1>
std::istream& operator>>(std::istream &in, Complex<T1> &c) {in >> c.real >> c.imag;return in;
}///////////////////////////////////////
// 普通函数模板实现
// 重载+用于Complex类型
template<typename T>
Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2) {return Complex<T>(c1.get_real()+c2.get_real(), c1.get_imag()+c2.get_imag());
}// 重载==用于Complex类型
template<typename T>
bool operator==(const Complex<T> &c1, const Complex<T> &c2) {return c1.get_real() == c2.get_real() && c1.get_imag() && c2.get_imag();
}#include "Complex.hpp"
#include <iostream>
#include <fstream>
#include <stdexcept>void test1();
void test2();int main() {using namespace std;cout << "测试1: 复数模板类测试" << endl;test1();cout << "\n测试2: 文件I/O测试" << endl;test2();
}void test1() {using namespace std;Complex<double> c1{3.5, 2}, c2;cout << "Enter c2: ";cin >> c2;cout << "c1 = " << c1 << endl;cout << "c2 = " << c2 << endl;cout << "c1 == c2: " << boolalpha << (c1 == c2) << endl;cout << "c1 + c2 = " << c1 + c2 << endl;c1 += c2;cout << "c1.real = " << c1.get_real() << endl;cout << "c1.imag = " << c1.get_imag() << endl;cout << "c1 == c2: " << boolalpha << (c1 == c2) << endl;
}void test2() {using namespace std;Complex<int> c1{1, 2}, c2{9, -7};ofstream out("ans.txt");if(!out.is_open()) {cout << "fail to open file ans.txt to write\n";return;}out << "c1 = " << c1 << endl;out << "c2 = " << c2 << endl;out << "c1 + c2 = " << c1 + c2 << endl;out << "(c1 == c2) = " << boolalpha << (c1 == c2) << endl;out.close();cout << "测试ok!" << endl;
}

实验任务二:

#pragma once#include <iostream>
#include <iomanip>
#include <string>using std::string;
using std::ostream;
using std::istream;
using std::setw;
using std::setprecision;
using std::setiosflags;
using std::ios_base;// Contestant类声明
class Contestant {
public:Contestant() = default;~Contestant() = default;int get_num() const { return num; }float get_time_usage() const { return time_usage; }friend ostream& operator<<(ostream &out, const Contestant &c);friend istream& operator>>(istream &in, Contestant &c);private:string no;          // 学号string name;        // 姓名string major;       // 专业int num;            // 解题数float time_usage;   // 总用时
};// 友元函数实现
// 重载流插入运算符<<
ostream& operator<<(ostream &out, const Contestant &c) {out << setiosflags(ios_base::left);out << setw(15) << c.no<< setw(15) << c.name<< setw(15) << c.major<< setw(5) << c.num<< setprecision(2) << c.time_usage;return out;
}// 重载流提取运算符>>
istream& operator>>(istream &in, Contestant &c) {in >> c.no >> c.name >> c.major >> c.num >> c.time_usage;return in;
}#include "Contestant.hpp"
#include "utils.hpp"
#include <iostream>
#include <vector>
#include <algorithm>void test() {using namespace std;vector<Contestant> v;load("data2.txt", v);   // 从文件加载选手信息到对象vsort(v.begin(), v.end(), compare_by_solutionInfo);  // 按解题情况排序output(cout, v);    // 输出对象v中信息到屏幕save("ans.txt", v); // 把对象v中选手信息保存到文件
}int main() {test();
}#include "Contestant.hpp"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>// 排序函数
// 按解题数比较,解题数相同的情况下,按总用时比较,总用时越少,排名越靠前
bool compare_by_solutionInfo(const Contestant &c1, const Contestant &c2) {if(c1.get_num() > c2.get_num())return true;if(c1.get_num() == c2.get_num())return c1.get_time_usage() < c2.get_time_usage();return false;
}// 把vector<Constestant>对象中的元素插入到输出流out
void output(std::ostream &out, const std::vector<Contestant> &v) {for(auto &i: v)out << i << std::endl;
}// 把vector<Contestant>对象中的元素写到filename文件中
void save(const std::string &filename, std::vector<Contestant> &v) {using std::ofstream;ofstream out(filename);if(!out.is_open()) {std::cout << "fail to open file to write\n";return;}output(out, v);out.close();
}// 从文件filename读取参赛选手信息到vector<Contestant>对象
void load(const std::string &filename, std::vector<Contestant> &v) {using std::ifstream;ifstream in(filename);if(!in.is_open()) {std::cout << "fail to open file to read\n";return;}std::string title_line;getline(in, title_line);     // 跳过标题行int first_column;Contestant t;while(in >> first_column >> t) v.push_back(t);in.close();
}

实验任务三:

#include <iostream>
#include <stdexcept>
#include <cmath>using namespace std;class Triangle {
public:Triangle(double s1, double s2, double s3);~Triangle() = default;double area() const;private:double a, b, c;
};Triangle::Triangle(double s1, double s2, double s3): a{s1}, b{s2}, c{s3} {if(a <= 0 || b <= 0 || c <= 0)throw invalid_argument("边长出现负值");if(a+b <= c || b+c <= a || a+c <= b) throw invalid_argument("不满足任意两边之和大于第三边");
}double Triangle::area() const {double s = (a + b + c)/2;return sqrt(s*(s-a)*(s-b)*(s-c));
}#include "Triangle.hpp"
#include <iostream>
#include <fstream>void test() {using namespace std;cout << "从文件读入三角形三边边长,计算面积" << endl;ifstream in("data3.txt");if(!in.is_open()) {cout << "fail to open file to read\n";return;}double a,b,c;do {cout << "三角形边长: ";in >> a >> b >> c;cout << a << " " << b << " " << c << endl;try {Triangle t(a, b, c);cout << "三角形面积: " << t.area() << endl << endl;}catch(const exception &e) {cout << "error: " << e.what() << endl << endl;}if(in.peek() == EOF)break;} while(1);in.close();
}int main() {test();
}

实验任务四:

#include<iostream>
using namespace std;
#include<vector>
template<typename T>
class Vector {
public:Vector(int  n) {if (n < 0) throw length_error("数组大小不能为负数");arr_length = n;arr = new T[n];}Vector() : arr(nullptr), arr_length(0) {}Vector(int n, T init) {if (n < 0)throw length_error("数组大小不能为负数");arr_length = n;arr = new T[n];if (init < 0)throw length_error("数组元素不能为负数");for (int i = 0; i < n; ++i) {arr[i] = init;}}Vector(const Vector<T>& a) {arr_length = a.arr_length;arr = new T[a.arr_length];for (int i = 0; i < a.arr_length; ++i) {arr[i] = a.arr[i];}}~Vector() {delete []arr;}int get_size() {return arr_length;}T& at(int n) {if (n < 0 || n >= arr_length)throw out_of_range("下标越界");return arr[n];}T& operator[](int n) {if (n < 0 || n >= arr_length)throw out_of_range("下标越界");return arr[n];}template<typename T1>friend void output(const Vector<T1> &a) ;
private:T* arr;int arr_length;
};
template<typename T1>
void output(const Vector<T1>& a) {for (int i = 0; i < a.arr_length; ++i)cout << a.arr[i]<<" ";cout << endl;
}
#include <iostream>
#include "C:\Users\29114\source\repos\MAJORCPP\Vector.hpp"void test1() {using namespace std;int n;cout << "Enter n: ";cin >> n;Vector<double> x1(n);for(auto i = 0; i < n; ++i)x1.at(i) = i * 0.7;cout << "x1: "; output(x1);Vector<int> x2(n, 42);const Vector<int> x3(x2);cout << "x2: "; output(x2);cout << "x3: "; output(x3);x2.at(0) = 77;x2.at(1) = 777;cout << "x2: "; output(x2);cout << "x3: "; output(x3);
}void test2() {using namespace std;int n, index;while(cout << "Enter n and index: ", cin >> n >> index) {try {Vector<int> v(n, n);v.at(index) = -999;cout << "v: "; output(v);}catch (const exception &e) {cout << e.what() << endl;}}
}int main() {cout << "测试1: 模板类接口测试\n";test1();cout << "\n测试2: 模板类异常处理测试\n";test2();
}

 

实验任务五:

#include<iostream>
using namespace std;#include<string>
#include<iomanip>
class stu {
public:string num;string name;string major;string score;
public ://顺序stu(string a, string b, string c, string d) :num{ a }, name{ b }, major{ c }, score{ d } {}void display() {cout << left << setw(18) << num;cout << left << setw(18) << name;cout << left << setw(18) << major;cout << left << setw(18) << score<<endl;}
};
#include<iostream>
using namespace std;
#include"task5sef.hpp"
#include<vector>
#include <algorithm>
#include<fstream>
#include <sstream>
bool compare(const stu &s1, const stu &s2) {if (!(s1.major == s2.major))return s1.major < s2.major;else{    return s1.score >s2.score;}
}
int main() {ifstream in("data5.txt");vector<stu> arr;if (in.is_open()){string line;getline(in, line);while (getline(in, line)) {stringstream ss(line);string num;string name;string major;string score;ss >> num >> name >> major >> score;arr.push_back(stu(num, name, major, score));}in.close();}elsecerr << "无法打开文件" << endl;sort(arr.begin(), arr.end(), compare);for (auto i : arr) {i.display();}ofstream out("data5_1.txt");if (out.is_open()) {for (auto i : arr) {out << i.num << "    " << i.name << "     " << i.major << "     " << i.score<<endl;}out.close();}elsecerr << "无法写入文件" << endl;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/854461.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MeteoInfo-Java解析与绘图教程(十)_JAVA绘制雷达PPI图

天气雷达的基本要素有很多,特别是双偏振雷达更多,但业务场景经常使用的一般为基本反射率,基本速度这两种要素 接下来我们以基本反射率为例,其他的要素也是一样的,一通百通 首先我们做基本反射率的图需要确定做哪一个仰角层,因为雷达体扫模式的扫描是不同仰角进行扫描的,常规的雷…

友情链接

风筝上的猫IT分享--肯定放我自己了访问博客风筝上的猫IT分享--肯定放我自己了访问博客

画8.0

因为期末周忙着做汇报和复习 (其实是因为自己比较懒) ,所以这幅画完成的相当迟了。 头发画起来真实相当费劲啊。

都在使用的《模版模式》

模版符合开闭原则,到处用于钩子方法。有的钩子方法不会改变行为,有的钩子会改变运行轨迹。框架中到处使用。Channel中定义了需要的操作,AbstractChannel实现了核心功能,然后不同的Channel实现只需要告知具体channel的描述信息即可。 Channel 接口⬇︎⬇︎⬇︎点击查看代码 …

ifconfig命令

ifconfig是linux中用于显示或配置网络设备(网络接口卡)的命令,英文全称是network interfaces configuring。配置网卡的IP地址语法例:ifconfig eth0 192.168.0.1 netmask 255.255.255.0 在 实体机上 ifconfig 命令用于 显示或配置网络设备(网络接口卡) 或修改。(以CentOS…

数据智能,融合创新|12月中国数据库行业分析报告已发布, 持续为产业助力

本期报告以数据智能与融合创新为主题,除详解国产数据库市场动向外,探究了在云计算的推动下数据库自治、智能运维能力的产品创新实践,欢迎免费下载。为了帮助大家及时了解中国数据库行业发展现状、梳理当前数据库市场环境和产品生态等情况,从2022年4月起,墨天轮社区行业分析…

maven仓库

https://mvnrepository.com/

【教程】第八章:知识库——学海无涯

通过循序渐进的功能升级,你将打造一个强大的管理系统,让团队协作更高效、流程更智能。8.1 欢迎来到新的一章 在本章中,我们将深入学习如何构建一个知识库。这将是一个综合性的模块,帮助我们管理和组织文档、任务和信息。通过设计和创建一个树形结构的文档表,我们将实现对文…

【YashanDB知识库】如何处理yasql输入交互模式下单行字符总量超过限制4000字节

现象 在yasql执行sql语句后报错:YASQL-00021 input line overflow (>4000 byte at line 4)原因 yasql在交互模式模式下单行字符总量限制4000字节,超出该限制即报错。 交互式模式下,yasql会显示一个提示符,通常是 SQL>,等待用户输入命令, 用户执行的每个命令都会立即…

Fiddler连接mumu模拟器抓包

主要介绍mumu模拟器如何设置 一、模拟器下载fiddler证书 保持本机 fiddler 运行状态 模拟器打开 localhost:8888(端口号),点击下载证书二、安装证书信任 路径:模拟器-网络和互联网-互联网-网络偏好设置 安装证书,选择下载好的 fiddler 证书即可三、开启手动代理 修改网络设…

编写 Java 单元测试最佳实践

在软件开发中,单元测试是保障代码质量的重要环节。对于程序员而言,它不仅提高了代码的稳定性和可维护性,还能帮助企业快速响应市场变化。然而,很多开发团队对单元测试的理解和实践并不深入。而腾讯云 AI 代码助手能够基于代码逻辑自动生成单元测试,减少手动编写测试代码的…

OCR数据集生成项目TextRecognitionDataGenerator

1、开源OCR数据集生成项目TextRecognitionDataGenerator 该项目通过 Python实现,可以通过 pip 安装: 终端: pip install trdg 然后在终端中输入以下命令: 终端:trdg -c 1000 -w 5 就可以生成如下图片,其中 -c 参数表示生成图片的数量, -w 表示图片中单词的个数。参考: …