实验六 C++
任务四:
Vector.hpp:
#pragma once
#ifndef VECTOR_HPP
#define VECTOR_HPP
#include <iostream>
#include <stdexcept> // 为异常类提供支持
#include <memory> // 为 std::unique_ptr 提供支持
template <typename T>
class Vector {
private:
std::unique_ptr<T[]> data; // 使用智能指针管理动态数组的内存
int size; // 数组的大小
public:
// 构造函数
Vector(int n) : size(n) {
if (n < 0) {
throw std::length_error("Size cannot be negative.");
}
data = std::make_unique<T[]>(n); // 使用智能指针分配内存
}
// 重载构造函数,初始化所有项为指定值
Vector(int n, T value) : size(n) {
if (n < 0) {
throw std::length_error("Size cannot be negative.");
}
data = std::make_unique<T[]>(n);
for (int i = 0; i < n; ++i) {
data[i] = value; // 初始化数组元素
}
}
// 深复制构造函数
Vector(const Vector<T>& other) : size(other.size) {
data = std::make_unique<T[]>(size);
for (int i = 0; i < size; ++i) {
data[i] = other.data[i]; // 复制数据项
}
}
// 获取数组的大小
int get_size() const {
return size;
}
// 通过索引访问元素
T& at(int index) {
if (index < 0 || index >= size) {
throw std::out_of_range("Index is out of range.");
}
return data[index];
}
// 运算符重载 []
T& operator[](int index) {
if (index < 0 || index >= size) {
throw std::out_of_range("Index is out of range.");
}
return data[index];
}
// 友元函数,用于输出 Vector 中的数据项
friend void output(const Vector<T>& vec) {
for (int i = 0; i < vec.size; ++i) {
std::cout << vec.data[i] << " ";
}
std::cout << std::endl;
}
// 析构函数(智能指针会自动释放内存)
};
#endif // VECTOR_HPP
task4.cpp:
#include <iostream>
#include "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); // 创建并初始化所有项为 42 的数组
const Vector<int> x3(x2); // 深复制 x2 到 x3
cout << "x2: "; output(x2);
cout << "x3: "; output(x3);
x2.at(0) = 77; // 修改 x2 中的值
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() {
using namespace std;
cout << "测试1: 模板类接口测试\n";
test1();
cout << "\n测试2: 模板类异常处理测试\n";
test2();
return 0;
}
实验结果截图:

任务五:
task5.cpp:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
// 学生信息结构体
struct Student {
int id; // 学号
string name; // 姓名
string major; // 专业
int score; // 分数
// 构造函数
Student(int _id, string _name, string _major, int _score)
: id(_id), name(_name), major(_major), score(_score) {}
};
// 比较函数,用于按专业和分数排序
bool compare(const Student& a, const Student& b) {
if (a.major == b.major) {
return a.score > b.score; // 专业相同按分数降序
}
return a.major < b.major; // 按专业字典序升序
}
// 读取数据并处理排序
void processStudents(const string& inputFile, const string& outputFile) {
vector<Student> students; // 存储学生信息
// 读取文件
ifstream inFile(inputFile);
if (!inFile) {
cerr << "无法打开文件: " << inputFile << endl;
return;
}
int id, score;
string name, major;
// 读取数据
while (inFile >> id >> name >> major >> score) {
students.emplace_back(id, name, major, score); // 将数据存入 vector
}
inFile.close();
// 排序
sort(students.begin(), students.end(), compare);
// 输出到屏幕和文件
ofstream outFile(outputFile);
if (!outFile) {
cerr << "无法打开文件: " << outputFile << endl;
return;
}
cout << "学号\t姓名\t专业\t分数" << endl; // 打印表头
outFile << "学号\t姓名\t专业\t分数" << endl; // 写入文件表头
for (const auto& student : students) {
cout << student.id << "\t" << student.name << "\t"
<< student.major << "\t" << student.score << endl;
outFile << student.id << "\t" << student.name << "\t"
<< student.major << "\t" << student.score << endl; // 写入文件
}
outFile.close();
}
int main() {
string inputFile = "data5.txt"; // 输入文件名
string outputFile = "ans5.txt"; // 输出文件名
processStudents(inputFile, outputFile); // 处理学生信息
return 0;
}
实验结果截图:


浙公网安备 33010602011771号