实验6 模板类、文件I/O和异常处理

任务四:

#ifndef VECTOR_HPP
#define VECTOR_HPP

#include <iostream>
#include <stdexcept>

template <typename T>
class Vector {
private:
    T* elements;
    size_t numElements;
public:
    Vector(size_t n = 0) : numElements(n) {
        if (n < 0) {
            throw std::length_error("Size cannot be negative");
        }
        elements = new T[n];
    }
    Vector(size_t n, const T& value) : numElements(n) {
        if (n < 0) {
            throw std::length_error("Size cannot be negative");
        }
        elements = new T[n];
        for (size_t i = 0; i < n; ++i) {
            elements[i] = value;
        }
    }
    Vector(const Vector<T>& other) : numElements(other.numElements) {
        elements = new T[numElements];
        for (size_t i = 0; i < numElements; ++i) {
            elements[i] = other.elements[i];
        }
    }
    ~Vector() {
        delete[] elements;
    }
    size_t get_size() const {
        return numElements;
    }
    T& at(size_t i) {
        if (i >= numElements) {
            throw std::out_of_range("Index out of range");
        }
        return elements[i];
    }
    const T& at(size_t i) const {
        if (i >= numElements) {
            throw std::out_of_range("Index out of range");
        }
        return elements[i];
    }
    T& operator[](size_t i) {
        if (i >= numElements) {
            throw std::out_of_range("Index out of range");
        }
        return elements[i];
    }
    const T& operator[](size_t i) const {
        if (i >= numElements) {
            throw std::out_of_range("Index out of range");
        }
        return elements[i];
    }
    friend std::ostream& output(std::ostream& os, const Vector<T>& vec) {
        for (size_t i = 0; i < vec.get_size(); ++i) {
            os << vec[i] << " ";
        }
        return os;
    }
};
#endif
Vector.hpp
#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) { // 多组输入,按下Ctrl+Z终止
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();
}
task4.cpp

运行结果:

 

 

任务五:

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

class Student {
public:
    std::string no;          
    std::string name;        
    std::string major;       
    int score;              

    friend std::ostream& operator<<(std::ostream& out, const Student& s);

    friend std::istream& operator>>(std::istream& in, Student& s);
};

std::ostream& operator<<(std::ostream& out, const Student& s) {
    out << std::left;
    out << std::setw(15) << s.no
        << std::setw(15) << s.name
        << std::setw(15) << s.major
        << std::setw(5) << s.score;
    return out;
}

std::istream& operator>>(std::istream& in, Student& s) {
    in >> s.no >> s.name >> s.major >> s.score;
    return in;
}

bool compareStudents(const Student& a, const Student& b) {
    if (a.major < b.major) return true;
    else if (a.major == b.major && a.score > b.score) return true;
    return false;
}

int main() {
    std::vector<Student> students;
    std::ifstream inFile("data5.txt");
    if (!inFile.is_open()) {
        std::cout << "fail to open file to write\n";
        return 0;
    }
    if (inFile) {
        std::string title_line;
        std::getline(inFile, title_line);     

        Student s;
        while (inFile >> s)
            students.push_back(s);

        inFile.close();
    }

    std::sort(students.begin(), students.end(), compareStudents);

    std::ofstream outFile("ans5.txt");
    if (outFile) {
        for (auto& i : students) {
            std::cout << i << std::endl;
            outFile << i << std::endl;
        }
        outFile.close();
    }

    return 0;
}
task5.cpp

运行结果:

 

posted @ 2024-12-22 21:35  枫羽秋茜  阅读(1)  评论(0编辑  收藏  举报