实验6

任务4:

源代码:

Vector.hpp

#ifndef VECTOR_HPP
#define VECTOR_HPP

#include <iostream>
#include <stdexcept>  // For exceptions like length_error and out_of_range

template <typename T>
class Vector {
private:
    T* data;  // Pointer to the dynamically allocated array
    size_t size;  // The size of the array

public:
    // Default constructor: throws exception if size is negative
    Vector(int n) {
        if (n < 0) {
            throw std::length_error("Size cannot be negative");
        }
        size = n;
        data = new T[size]();  // Initialize array with default value for type T
    }

    // Constructor with an initial value for all elements
    Vector(int n, const T& value) {
        if (n < 0) {
            throw std::length_error("Size cannot be negative");
        }
        size = n;
        data = new T[size];
        for (size_t i = 0; i < size; ++i) {
            data[i] = value;
        }
    }

    // Copy constructor: performs deep copy
    Vector(const Vector<T>& other) {
        size = other.size;
        data = new T[size];
        for (size_t i = 0; i < size; ++i) {
            data[i] = other.data[i];
        }
    }

    // Destructor: deallocates the dynamic array
    ~Vector() {
        delete[] data;
    }

    // Member function to get size
    size_t get_size() const {
        return size;
    }

    // Access element with bounds checking
    T& at(size_t index) {
        if (index >= size) {
            throw std::out_of_range("Index out of range");
        }
        return data[index];
    }

    // Access element without bounds checking (unsafe)
    T& operator[](size_t index) {
        return data[index];
    }

    // Friend function to output vector
    friend void output(const Vector<T>& v) {
        for (size_t i = 0; i < v.size; ++i) {
            std::cout << v.data[i] << " ";
        }
        std::cout << std::endl;
    }
};

#endif

task4.cpp

#include <iostream>
#include "Vector.hpp"
using namespace std;
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();
}

运行结果截图:

 任务5:

源代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// 定义学生结构体
struct Student {
    string name;
    string major;
    int score;

    // 构造函数
    Student(string n, string m, int s) : name(n), major(m), score(s) {}
};

// 排序规则:先按专业字典序排序,若专业相同,再按分数降序排序
bool compare(const Student& a, const Student& b) {
    if (a.major == b.major) {
        return a.score > b.score;  // 分数降序
    }
    return a.major < b.major;  // 专业按字典序升序
}

int main() {
    vector<Student> students;
    ifstream infile("data5.txt");
    string name, major;
    int score;

    // 读取文件
    if (!infile) {
        cerr << "无法打开文件 data5.txt" << endl;
        return 1;
    }

    while (infile >> name >> major >> score) {
        students.push_back(Student(name, major, score));
    }

    // 关闭文件
    infile.close();

    // 排序
    sort(students.begin(), students.end(), compare);

    // 打印结果到屏幕,并保存到 ans5.txt
    ofstream outfile("ans5.txt");

    if (!outfile) {
        cerr << "无法打开文件 ans5.txt" << endl;
        return 1;
    }

    // 打印输出并保存到文件
    for (const auto& student : students) {
        cout << student.name << " " << student.major << " " << student.score << endl;
        outfile << student.name << " " << student.major << " " << student.score << endl;
    }

    // 关闭输出文件
    outfile.close();

    return 0;
}

实验结果截图:

 

posted @ 2024-12-23 01:40  相梓文  阅读(19)  评论(0)    收藏  举报