首页 > 其他分享 >实验6 模板类、文件I/O和异常处理

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

时间:2024-12-22 10:52:25浏览次数:6  
标签:std 文件 index int Vector 实验 include 模板 size

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;
}

标签:std,文件,index,int,Vector,实验,include,模板,size
From: https://www.cnblogs.com/ykbgxlb/p/18621913

相关文章

  • 文件流(小山)
    #include<iostream>#include<cstdio>usingnamespacestd;structno{stringname;intage;stringblc;intshengao;inttizhong;};intmain(intargc,char**argv){intc;cin>>c;noasd[5];if(c==1......
  • 实验6
    任务1源代码编译 任务2源代码编译 任务3源代码编译 任务4源代码 编译 任务5源代码编译 任务6源代码 编译 任务7源代码 编译 ......
  • 实验6 模板类、文件I/O和异常处理
    任务4:Vector.hpp1#include<bits/stdc++.h>2usingnamespacestd;3template<typenameT>4classVector{5public:6Vector(intsize,intvalue=0):size{size}{7if(size<0)throwlength_error("negativesi......
  • 任意文件下载漏洞分析
    一、漏洞简介​app/adminapi/controller/v1/setting/SystemConfig.php​路由中存在任意文件下载漏洞二、影响版本<=v5.4.0三、环境搭建配置phpstudy,将网站的运行目录,设置在public​目录下设置伪静态四、漏洞原理分析该系统采用前后端分离技术,基于ThinkPHP6+eleme......
  • 嵌入式Linux,proc文件系统讲解,介绍以及读取使用
    1.简介         proc文件系统是一个虚拟文件系统,它以文件系统的方式为应用层访问系统内核数据提供了接口,用户和应用程序可以通过proc文件系统得到系统信息和进程相关信息,对proc文件系统的读写作为与内核进行通信的一种手段。但是与普通文件不同的是,proc文......
  • pta 7-363 sdut-C语言实验-简单字符串排序
    题解:#include<iostream>#include<string>usingnamespacestd;//定义学生结构体structstudent{stringname;intscore;};//快速排序实现单词字典序排序voidQuickSort(studentstu[],intleft,intright){if(left>=right)return;inti=left,j=r......
  • Pyqt6在lineEdit中输入文件名称并创建或删除JSON文件
    1、创建JSON文件代码importosdefaddModulekeyWordFile(self):if""!=self.lineEdit_module.text():moduleFile=self.lineEdit_module.text()+'.json'else:self.toolLogPrinting('请输入模块名称')returnfile......
  • 实验6
    task4代码:#pragmaonce#include<iostream>#include<stdexcept>#include<iomanip>//模板类Vector的定义template<typenameT>classVector{public://构造函数Vector(intsize);Vector(intsize,Tvalue);Vector(const......
  • 实验六
    任务四 源代码:#include<stdio.h>#defineN10typedefstruct{charisbn[20];charname[80];charauthor[80];doublesales_price;intsales_count;}Book;voidoutput(Bookx[],intn);voidsort(B......
  • 操作系统(20)文件共享
    前言    操作系统文件共享是指在不同设备或用户之间共享文件的功能,它使得多个用户或设备能够方便地访问、编辑和共享文件。一、文件共享的作用提高协作效率:文件共享允许团队成员之间方便地共享和编辑文件,从而提高协作效率。节省存储空间:通过文件共享,多个用户或设......