实验6

实验任务4

Vector.hpp代码

点击查看代码
#include<iostream>
#include<stdexcept>

using namespace std;

template<typename T>
class Vector{
public:
	Vector(int n);
	Vector(int n,T value);
	Vector(const Vector<T> &v);
	~Vector();
	
	int get_size() const{
		return size;
	}
	
	T& at(int i){
		if(i<0||i>=size){
			throw out_of_range("Vector::at():index out of size");
		}
		
		return data[i];
	}
	
	T& operator[](int i);
	
	template<typename T1>
	friend void output(const Vector<T1> &v);
	
private:
	int size;
	T *data;
	
};

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

template<typename T>
Vector<T>::Vector(int n,T value){
	if(n<0){
		throw length_error("Vector constructor: negative size");
	}
	size=n;
	data=new T[n];
	for(int i=0;i<n;i++){
		data[i]=value;
	}
}

template<typename T>
Vector<T>::Vector(const Vector<T> &v){
	size=v.size;
	data=new T[size];
	for(int i=0;i<size;i++){
		data[i]=v.data[i];
	}
}

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

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

template<typename T1>
void output(const Vector<T1> &v){
	for(int i=0;i<v.size;i++){
		cout<<v.data[i];
		if(i!=v.size-1){
			cout<<",";
		}
	}
	cout<<endl;
}

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

运行结果:

实验任务5

task5.cpp代码

点击查看代码
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
 
using std::string;
using std::vector;
using std::ostream;
using std::istream;
using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::setiosflags;
using std::ios_base;

class Student {
public:
	Student() {}
    ~Student() {}
 
    string get_major() const { return major; }
    int get_score() const { return score; }

    friend ostream& operator<<(ostream &out, const Student &s);
    friend istream& operator>>(istream &in, Student &s);

private:
    string no;         
    string name;        
    string major;      
    int score;            
};

// 友元函数实现
// 重载流插入运算符<<
ostream& operator<<(ostream &out, const Student &c) {
    out << setiosflags(ios_base::left);
    out << setw(15) << c.no
        << setw(15) << c.name
        << setw(15) << c.major
        << setw(5) << c.score;
    
    return out;
}

// 重载流提取运算符>>
istream& operator>>(istream &in, Student &s) {
    in >> s.no >> s.name >> s.major >> s.score;

    return in;
}

// 排序函数

bool compare_by_solutionInfo(const Student &c1, const Student &c2) {
    if(c1.get_major() < c2.get_major())
        return true;
    
    if(c1.get_major() == c2.get_major())
        return c1.get_score() > c2.get_score();
    
    return false;
}

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



void save(const string &filename,vector<Student> &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 string &filename,vector<Student> &v) {
    using std::ifstream;

    ifstream in(filename);
    if(!in.is_open()) {
        std::cout << "fail to open file to read\n";
        return;
    }

    string title_line;
    getline(in, title_line);     // 跳过标题行

    int first_column;
    Student t;
    while(in >> t) 
        v.push_back(t);

    in.close();
}

void test() {
    using namespace std;

    vector<Student> v;

    load("data5.txt", v);   // 从文件加载选手信息到对象v
    sort(v.begin(), v.end(), compare_by_solutionInfo);  // 按解题情况排序
    output(cout, v);    // 输出对象v中信息到屏幕
    save("ans.txt", v); // 把对象v中选手信息保存到文件
}

int main() {
    test();
}

运行结果:

posted @ 2024-12-17 17:04  Z泽  阅读(3)  评论(0编辑  收藏  举报