实验6 模板类、文件I/O和异常处理
实验任务4:
vector.hpp
1 #pragma once 2 3 #include<iostream> 4 #include<stdexcept> 5 #include<string> 6 7 using namespace std; 8 9 template<typename T> 10 class Vector 11 { 12 public: 13 Vector(int s); 14 Vector(int s, T value); 15 Vector(const Vector<T>& v); 16 ~Vector(); 17 18 19 T get_size()const; 20 T& at(int index); 21 T& at(int index)const; 22 T& operator[](int index); 23 T& operator[](int index)const; 24 25 template<typename U> 26 friend void output(const Vector<U>& v); 27 28 29 private: 30 int size; 31 T* ptr; 32 }; 33 34 template<typename T> 35 Vector<T>::Vector(int s):size{s} 36 { 37 if (s < 0) { throw length_error("Vector constructor: negative size."); } 38 ptr = new T[size]; 39 } 40 41 template<typename T> 42 Vector<T>::Vector(int s, T value) :size{ s } 43 { 44 if (s < 0) { throw length_error("Vector constructor: negative size."); } 45 ptr = new T[size]; 46 for (int i = 0; i < size; i++) 47 { 48 ptr[i] = value; 49 } 50 } 51 52 template<typename T> 53 Vector<T>::Vector(const Vector<T>& v):size(v.size) 54 { 55 ptr = new T[size]; 56 for (int i = 0; i < size; i++) 57 { 58 ptr[i] = v.ptr[i]; 59 } 60 } 61 62 template<typename T> 63 Vector<T>::~Vector() 64 { 65 delete[] ptr; 66 } 67 68 template<typename T> 69 T Vector<T>::get_size()const 70 { 71 return size; 72 } 73 74 template<typename T> 75 T& Vector<T>::at(int index) 76 { 77 if (index < 0 || index >= size) { throw out_of_range("Vector: index out of range."); } 78 return ptr[index]; 79 } 80 81 template<typename T> 82 T& Vector<T>::at(int index)const 83 { 84 if (index < 0 || index >= size) { throw out_of_range("Vector: index out of range."); } 85 return ptr[index]; 86 } 87 88 template<typename T> 89 T& Vector<T>::operator[](int index) 90 { 91 if (index < 0 || index >= size) { throw out_of_range("Vector: index out of range."); } 92 return ptr[index]; 93 } 94 95 96 template<typename T> 97 T& Vector<T>::operator[](int index)const 98 { 99 if (index < 0 || index >= size) { throw out_of_range("Vector: index out of range."); } 100 return ptr[index]; 101 } 102 103 template<typename U> 104 void output(const Vector<U>& v) 105 { 106 for (int i = 0; i < v.get_size(); i++) 107 { 108 cout << v[i] << " "; 109 } 110 cout << endl; 111 }
task4.cpp
1 #include <iostream> 2 #include "Vector.hpp" 3 4 void test1() { 5 using namespace std; 6 7 int n; 8 cout << "Enter n: "; 9 cin >> n; 10 11 Vector<double> x1(n); 12 for (auto i = 0; i < n; ++i) 13 x1.at(i) = i * 0.7; 14 15 cout << "x1: "; output(x1); 16 17 Vector<int> x2(n, 42); 18 const Vector<int> x3(x2); 19 20 cout << "x2: "; output(x2); 21 cout << "x3: "; output(x3); 22 23 x2.at(0) = 77; 24 x2.at(1) = 777; 25 cout << "x2: "; output(x2); 26 cout << "x3: "; output(x3); 27 } 28 29 void test2() { 30 using namespace std; 31 32 int n, index; 33 while (cout << "Enter n and index: ", cin >> n >> index) { 34 try { 35 Vector<int> v(n, n); 36 v.at(index) = -999; 37 cout << "v: "; output(v); 38 } 39 catch (const exception& e) { 40 cout << e.what() << endl; 41 } 42 } 43 } 44 45 int main() { 46 cout << "测试1: 模板类接口测试\n"; 47 test1(); 48 49 cout << "\n测试2: 模板类异常处理测试\n"; 50 test2(); 51 }
运行结果截图:
实验任务5:
task5.cpp
1 #include"Student.h" 2 #include<iostream> 3 #include<fstream> 4 #include<vector> 5 #include<algorithm> 6 7 using namespace std; 8 9 bool compare(const students& s1, const students& s2) 10 { 11 if (s1.get_maj() < s2.get_maj()) { return true; } 12 if (s1.get_maj() == s2.get_maj()) 13 { 14 return s1.get_score() > s2.get_score(); 15 } 16 } 17 18 void output(ostream& out, const vector<students>& S) 19 { 20 for (auto &i : S) 21 { 22 out << i; 23 } 24 } 25 26 int main() 27 { 28 vector<students> S; 29 ifstream fin("data5.txt"); 30 if (!fin.is_open()) { cout << "fail to open data5.txt"; return -1; } 31 string line; 32 getline(fin, line); //输入提示 33 students s; 34 while (fin >> s) 35 { 36 S.push_back(s); 37 } 38 sort(S.begin(), S.end(), compare); 39 ofstream fout("ans5.txt"); 40 if (!fout.is_open()) { cout << "fail to open ans5.txt"; return -1; } 41 output(cout, S); 42 output(fout, S); 43 return 0; 44 }
运行结果截图:
原始数据:
ans5.txt