实验6
Vector.hpp
#include <iostream> #include <stdexcept> using namespace std; template<typename T> class Vector { private: int length; T *space; public: Vector<T>(int l, T value=0): length{l} { if(l<0) throw length_error("Vector constructor: negative size"); space = new T[l]; for(int i=0; i<l; i++) space[i] = value; } Vector<T>(const Vector<T>& x){ length = x.length; space = new T[length]; for(int i=0; i<x.length; i++) space[i] = x.space[i]; } ~Vector<T>() { delete[] space; } int get_size(){ return length; } T& at(int i){ if(i>=length || i<0) throw out_of_range("Vector: index out of range"); return space[i]; } T& operator[](int i){ if(i>=length || i<0) throw out_of_range("Vector: index out of range"); return space[i]; } friend void output(const Vector<T> x){ for(int i=0; i<x.length; i++) { cout << x.space[i]; if(i!=x.length-1) cout <<", "; } cout<<endl; } };
test4.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(); }
Student.hpp
#include <iostream> #include <string> #include <fstream> #include <iomanip> #include <vector> using namespace std; class Student { private: string ID; string Name; string Major; int Score; public: Student() = default; ~Student() = default; string Get_Major() const { return Major; } int Get_Score() const { return Score; } friend ostream& operator<<( ostream &out, const Student &v ){ out << left << setw(15) << v.ID << setw(15) << v.Name << setw(15) << v.Major << setw(15) << v.Score; return out; } friend istream& operator>>( istream &in, Student &v ){ in >> v.ID >> v.Name >> v.Major >> v.Score; return in; } }; bool Stucmp(const Student a, const Student b){ if( a.Get_Major() < b.Get_Major() ) return true; if( a.Get_Major() == b.Get_Major() ) return a.Get_Score() > b.Get_Score(); return false; } void output( ostream &out, const vector<Student> &v ) { for(auto &i: v) out << i << endl; } void WriFil(const string &filename, vector<Student> &v){ ofstream out(filename); if(!out.is_open()) { cout<<"fail to open"<<endl; return; } output(out,v); out.close(); } void ReadFil(const string &filename, vector<Student> &v){ ifstream in(filename); if(!in.is_open()) { cout<<"fail to open"<<endl; return; } string title; getline(in, title); Student t; while( in >> t ) v.push_back(t); in.close(); }
tast5.cpp
#include <iostream> #include <vector> #include <algorithm> #include "Student.hpp" using namespace std; int main() { vector<Student> v; ReadFil("data5.txt",v); sort( v.begin(), v.end(), Stucmp); output(cout,v); WriFil("ans5.txt",v); return 0; }