实验6 模板类、文件I\O和异常处理
实验任务4
#pragma once #include <iostream> #include <stdexcept> using namespace std; template <typename T> class Vector { public: Vector(int n, int value=0); Vector(Vector<T> &v); ~Vector(); int get_size() const; T& at(int i); T& operator[](int i); private: int size; T *a; template <typename T1> friend void output(const Vector<T1> &v); }; template <typename T> Vector<T>::Vector(int n, int value) : size{n} { if(size<0) throw length_error("Vector constructor: negative size"); else { a=new T [size]; for(int i = 0; i < n; i++) a[i] =value; } } template <typename T> Vector<T>::Vector(Vector<T> &v) { if(v.size<0) throw length_error("Vector constructor: negative size"); else{ size = v.size; a = new T [size]; for(int i = 0; i < size; i++) a[i] = v.a[i]; } } template <typename T> Vector<T>::~Vector() { delete [] a; } template <typename T> int Vector<T>::get_size() const { return size; } template < typename T> T& Vector<T>::at(int i) { if(i >= size || i < 0) throw out_of_range("Vector: index out of range"); else return a[i]; } template <typename T> T& Vector<T>::operator[](int i) { if(i >= size||i<0) throw out_of_range("Vctor: index out of range"); else return a[i]; } template <typename T1> void output(const Vector<T1> &v) { for(int i = 0; i < v.size - 1; i++) cout << v.a[i] << ", "; cout << v.a[v.size-1] << endl; }
#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
#include <iostream> #include <iomanip> #include <string> #include <vector> #include <fstream> #include <algorithm> using namespace std; class Stu { public: Stu()=default; ~Stu()=default; string get_major() const {return major;} int get_score() const {return score;} friend ostream& operator<<(ostream &out, const Stu &s); friend istream& operator>>(istream &in, Stu &s); private: string no; string name; string major; int score; }; ostream& operator<<(ostream &out, const Stu &s) { out << setiosflags(ios_base::left); out << setw(15) << s.no << setw(15) << s.name << setw(15) << s.major << setw(15) << s.score << endl; return out; } istream& operator>>(istream &in, Stu &s) { in>>s.no >>s.name >>s.major >>s.score; return in; } bool cmp(const Stu &s1, const Stu &s2) { if(s1.get_major()<s2.get_major()) return true; else if(s1.get_major()==s2.get_major()) return s1.get_score()>s2.get_score(); else return false; } void output(ostream &out, const vector<Stu> &v) { for(auto &i : v) out <<i; } void open_txt(const string &filename, vector<Stu> &v) { ifstream in(filename); if(!in.is_open()) { cout << "fail to open file to read" << endl; return; } string datas; getline(in,datas); Stu s; while(in>>s) v.push_back(s); in.close(); } void write_txt(const string &filename, vector<Stu> &v) { ofstream out(filename); if(!out.is_open()) { cout << "fail to open file to write" << endl; return; } output(out, v); out.close(); } int main() { vector<Stu> v; open_txt("data5.txt", v); sort(v.begin(), v.end(), cmp); output(cout, v); write_txt("ans5.txt", v); }