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

实验任务4:

Vector.hpp以及task4.cpp的源码,运行测试结果如下

#pragma once
#include<iostream>
#include<stdexcept>
using std::cout;
using std::endl;
template <typename T>
class Vector{
    public:
        Vector(int s);
        Vector(int s,T v);
        Vector(const Vector<T> &v);
        ~Vector();
        int get_size()const;
        T& at(int index){
            return static_cast<const Vector*>(this)->at(index);
        }
         T& at(int index)const;
        T& operator[](int index)const;
        T& operator[](int index){
            return  static_cast<const Vector*>(this)->operator[](index);
        }
        template <typename T1>
        friend void output(const Vector<T1>&);
        
    private: 
        int size;//数组大小 
        T *ptr;//数组类型 
};


template <typename T>
Vector<T>::Vector(int s):size(s)
{
    if(s<0)
    {
        throw std::length_error("vector constructor: negative size"); 
    }
    ptr = new T[size];
}

template <typename T>
Vector<T>::Vector(int s,T v):size(s)
{
    if(s<0)
    {
        throw std::length_error("vector constructor: negative size"); 
    }
    ptr = new T[size];
    for(int i=0;i<size;++i)
    {
        ptr[i] = v;
    }

}

template <typename T>
Vector<T>::Vector(    const Vector &v):size(v.size),ptr(new T[size])
{
    if(v.size<0)
    {
        throw std::length_error("vector constructor: negative size"); 
    }
    for(int i=0;i<size;++i)
    {
        ptr[i] = v.ptr[i];
    }
}

template <typename T>
Vector<T>::~Vector()
{
    delete[] ptr;
}

template <typename T>
int Vector<T>::get_size()const{
    return this->size;
}

template <typename T>
    T& Vector<T>::at(int index)const{
        if(index<0 || index >=size)
        {
            throw std::out_of_range("vector::at():index out of this->size");
        }
        return ptr[index];
    }
    
template<typename T>
   T& Vector<T>::operator[](int index)const
    {
            if(index<0 || index >=size)
    {
            throw std::out_of_range("vector::operator[]:index out of this->size");
    }
    return ptr[index];
    }
    
    template<typename T1>
     void output(const Vector<T1>& v){
         for (int i = 0; i < v.get_size(); ++i) {
        std::cout << v.at(i);
        if (i < v.get_size() - 1) {
            std::cout << ",";
        }
    }
    std::cout << std::endl;
    }
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) {
//        try {
//            Vector<int> v(n, n);
//            v.at(index) = -999;
//            cout << "v: "; output(v);
//        }
//        catch (const exception &e) {
//            cout << e.what() << endl;
//        }
//    }
test1();
}

int main() {
    cout << "测试1: 模板类接口测试\n";
    test1();

    cout << "\n测试2: 模板类异常处理测试\n";
    test2();
}
task4.cpp

 

实验任务5:

student.hpp以及task5.cpp的源码,运行测试结果如下

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
// 学生结构体
struct Student {
    string id;
    string name;
    string major;
    int score;

    // 用于排序的比较函数(降序比较分数)
    bool operator<(const Student& other) const {
        if (major != other.major) {
            return major < other.major; // 先按专业字典序排序
        } else {
            return score > other.score; // 专业相同则按分数降序排序
        }
    }
};

// 从文件中读取学生信息
vector<Student> readStudentsFromFile(const string& filename) {
    vector<Student> students;
    ifstream file(filename);
    string line;

    while (getline(file, line)) {
        istringstream iss(line);
        Student student;
        iss >> student.id >> student.name >> student.major >> student.score;
        students.push_back(student);
    }

    file.close();
    return students;
}

// 将学生信息写入文件
void writeStudentsToFile(const vector<Student>& students, const string& filename) {
    ofstream file(filename);
    for (const auto& student : students) {
        file << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << "\n";
    }
    file.close();
}
student.hpp
#include "student.hpp"
#include<iostream>
#include<fstream>
int main() {
    vector<Student> students = readStudentsFromFile("data5.txt");

    // 使用STL的sort函数进行排序,这里需要提供一个比较函数或操作符重载
    sort(students.begin(), students.end());

    // 打印到屏幕
    for (const auto& student : students) {
        cout << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << "\n";
    }

    // 保存到文件
    writeStudentsToFile(students, "ans5.txt");

    return 0;
}
task5.cpp

 

posted @ 2024-12-21 16:14  DownJackring  阅读(0)  评论(0编辑  收藏  举报