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

实验四

vector.hpp

  #pragma once
  #include<iostream>
  #include<stdexcept>
  using namespace std;
 template<typename T>
 class Vector {
  private:
     int size;
     T* ptr;
 public:
     Vector(int size, int value = 0) :size{ size } {
         if (size < 0) {
             throw length_error("negative size");
         }
         ptr = new T[size];
         for (int i = 0; i < size; i++) {
             ptr[i] = value;
         }
    }
     int get_size()const { return size; }
     T& at(int index)const {
         if (index < 0 || index >= size) throw out_of_range("index out of range");
         return ptr[index];
     }
     T& at(int index) {
         return static_cast<const Vector<T>*>(this)->at(index);
  }
     T& operator[](int index)const {
         if (index < 0 || index >= size) throw out_of_range("index out of range");
         return ptr[index];
    }
    T& operator[](int index) {
        return static_cast<const Vector<T>*>(this)->operator[](index);
     }
 };
 template<typename T1>
 void output(const Vector<T1>& v) {
     for (int i = 0; i < v.get_size();i++) {
         cout << v[i] << ", ";
    }
     cout << endl;
 }

test.cpp

#include <iostream>
#include "C:\Users\Administrator\Documents\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();
}

实验结果

 实验5

test.cpp

 

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

using std::string;
using std::ostream;
using std::istream;
using std::setw;
using std::setprecision;
using std::setiosflags;
using std::ios_base;

class People{
    private:
        string no;
        string name;
        string major;
        int score;
         
    public:
    People()=default;
    ~People()=default;
    int get_score() const{return score;} 
    string get_string() const{return major;}
    
    friend ostream& operator<<(ostream &out, const People &c);
    friend istream& operator>>(istream &in, People &c);
};


ostream& operator<<(ostream &out, const People &c) {
    out << setiosflags(ios_base::left);
    out << setw(15) << c.no
        << setw(15) << c.name
        << setw(15) << c.major
        << setw(15) << c.score; 
    return out;
}


istream& operator>>(istream &in, People &c) {
    in >> c.no >> c.name >> c.major >> c.score;
    return in;
}


bool compare_by_order(const People &p1,const People &p2) {
    if(p1.get_string()<p2.get_string())
    return true;
    
    if(p1.get_string()==p2.get_string())
    return p1.get_score()>p2.get_score();
    
    return false;
}

void output(std::ostream &out, const std::vector<People> &v) {
    for(auto &i: v)
    out << i << std::endl;
}


void save(const std::string &filename, std::vector<People> &v) {
    using std::ofstream;
    ofstream out(filename);
    if(!out.is_open()) {
    std::cout << "fail to open file to write\n";
    return;
    }
    output(out, v);
    out.close();
}

void load(const std::string &filename, std::vector<People> &v) {
    using std::ifstream;
    ifstream in(filename);
    if(!in.is_open()) {
        std::cout << "fail to open file to read\n";
    return;
}
std::string title_line;
getline(in, title_line); 
People t;
while(in >> t)
    v.push_back(t);
    in.close();
}

void test(){
    using namespace std;
    
    vector<People> v;
    
    load("data5.txt",v);
    sort(v.begin(),v.end(),compare_by_order);
    output(cout,v);
    save("ans5.txt",v); 
}

int main(){
    test();
}

实验结果

 

posted @ 2024-12-18 21:26  hinaou  阅读(14)  评论(0)    收藏  举报