实验6

任务四

Vector.hpp

#pragma once

#include <iostream>
#include<stdexcept>
using namespace std;

template<typename T>
class Vector{
public:
    Vector(int n,int value=0):size{n}{
    if(size<0)
    throw length_error("negative size");
    else
    {
        p=new T[size];
        for(int i=0;i<size;i++)
        p[i]=value;
    }
    }
    Vector(const Vector<T> &c){
        size=c.size;
        p=new T[size];
        for(int i=0;i<size;i++)
        p[i]=c.p[i];
    }
    ~Vector(){
        delete [] p;
    }

    int get_size() const{return size;}
    T& at(int i){
        if(i<0||i>=size)
        throw out_of_range("index out of range");
        return p[i];
    }
    T& operator [](int i) const{
        if(i<0||i>=size)
        throw out_of_range("index out of range");
        return p[i];
    }
    template<typename T1>
    friend void output(const Vector<T1> &c);
private:
    int size;
    T *p;
};
template<typename T1>
void output(const Vector<T1>&c){
    for(int i=0;i<c.get_size();i++)
    cout<< c[i] << ",";
    cout<<endl;
}
View Code

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();
}
View Code

 任务五

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


namespace StudentInfoUtil {
   
    struct StudentData {
        int studentId;
        std::string studentName;
        std::string majorSubject;
        double academicScore;
    };

  
    std::istream& readStudentData(std::istream& inputStream, StudentData& student) {
        inputStream >> student.studentId >> student.studentName >> student.majorSubject >> student.academicScore;
        return inputStream;
    }

    std::ostream& writeStudentData(std::ostream& outputStream, const StudentData& student) {
        outputStream << std::left << std::setw(10) << student.studentId
                     << std::setw(10) << student.studentName
                     << std::setw(10) << student.majorSubject
                     << std::fixed << std::setprecision(2) << student.academicScore;
        return outputStream;
    }
}

int main() {
    std::vector<StudentInfoUtil::StudentData> allStudents;

    const std::string inputFileName = "data5.txt";
    const std::string outputFileName = "ans5.txt";

    
    std::ifstream inputFile(inputFileName);
    std::ofstream outputFile(outputFileName);
    if (!inputFile.is_open()) {
        std::cerr << "无法打开输入文件: " << inputFileName << std::endl;
        return 1;
    }
    if (!outputFile.is_open()) {
        std::cerr << "无法打开输出文件: " << outputFileName << std::endl;
        return 1;
    }

    
    std::string headerLine;
    std::getline(inputFile, headerLine);

    
    StudentInfoUtil::StudentData tempStudent;
    while (StudentInfoUtil::readStudentData(inputFile, tempStudent)) {
        allStudents.push_back(tempStudent);
    }
    inputFile.close();

   
    auto compareStudents = [](const StudentInfoUtil::StudentData& a, const StudentInfoUtil::StudentData& b) {
        if (a.majorSubject == b.majorSubject) {
            return a.academicScore > b.academicScore;
        }
        return a.majorSubject < b.majorSubject;
    };
    std::sort(allStudents.begin(), allStudents.end(), compareStudents);


    for (const auto& student : allStudents) {
        StudentInfoUtil::writeStudentData(std::cout, student) << std::endl;
    }


    for (const auto& student : allStudents) {
        StudentInfoUtil::writeStudentData(outputFile, student) << std::endl;
    }
    outputFile.close();

    return 0;
}

 

posted @ 2024-12-18 14:56  孟潇辉  阅读(8)  评论(0)    收藏  举报