实验六
task4
Vector.hpp
#pragma once #ifndef VECTOR_HPP #define VECTOR_HPP #include <iostream> #include <stdexcept> #include <memory> 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]; } friend void output(const Vector<T>& vec) { for (int i = 0; i < vec.size; ++i) { std::cout << vec.data[i] << " "; } std::cout << std::endl; } };
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); 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() { using namespace std; cout << "测试1: 模板类接口测试\n"; test1(); cout << "\n测试2: 模板类异常处理测试\n"; test2(); return 0; }
task5
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); } 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; }