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

task4:
Vector.hpp:

点击查看代码
#pragma once

#include<iostream>
#include<stdexcept>
using namespace std;
template<typename T>
class Vector{
    private:
        int size;
        T *ptr;
    public:
        Vector(int s):size(s){
            if(s<0)
                throw length_error("Vector constructor:negative size"); 
            else
                ptr=new T[size];
        }
        Vector(int s,T value):size(s){
            if(s<0)
                throw length_error("Vector constructor:negative size"); 
            else
            {
                ptr=new T[size];
                for(int i=0;i<s;i++)
                    *(ptr+i)=value;
            }
        }
        
        Vector(const Vector<T> &v){
            size=v.size;
            ptr=new T[size];
            for(int i=0;i<size;i++)
                *(ptr+i)=v.ptr[i];
        }
        
        ~Vector(){delete []ptr;}
        int get_size();
        T& at(int index);
        T& operator[](int index);
        friend void output(Vector<T> v){
            for(int i=0;i<v.size;i++)
                cout<<v.ptr[i]<<",";
            cout<<endl;
        }
};
template<typename T>

int Vector<T>::get_size(){return size;} 

template<typename T>
T& Vector<T>::at(int index){
    if (index<0||index>=size)
        throw out_of_range("Vector:index out of range");
    return *(ptr+index);
}

template<typename T>
T& Vector<T>::operator[](int index){                                                                                                                         
    if (index<0||index>=size)
        throw out_of_range("Vector:index out of range");
    return *(ptr+index);
}

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() {
    cout << "测试1: 模板类接口测试\n";
    test1();

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

task5:

点击查看代码
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <string>

struct Student {
    std::string id;
    std::string name;
    std::string major;
    int score;

    // 用于排序的比较函数
    bool operator<(const Student& other) const {
        if (major != other.major) {
            return major < other.major; 
        } else {
            return score > other.score; 
        }
    }
};

std::vector<Student> readStudentsFromFile(const std::string& filename) {
    std::vector<Student> students;
    std::ifstream file(filename);
    std::string line;

    std::getline(file, line);

    while (std::getline(file, line)) {
        std::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 std::vector<Student>& students, const std::string& filename) {
    std::ofstream file(filename);
    for (const auto& student : students) {
        file << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << std::endl;
    }
    file.close();
}

int main() {
    std::vector<Student> students = readStudentsFromFile("data5.txt");

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

    std::cout << "学号\t姓名\t专业\t分数" << std::endl;
    for (const auto& student : students) {
        std::cout << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << std::endl;
    }

    writeStudentsToFile(students, "ans5.txt");

    return 0;
}

posted @ 2024-12-22 10:54  一口八个小笼包  阅读(0)  评论(0编辑  收藏  举报