实验六
1 #pragma once 2 #include<iostream> 3 #include<stdexcept> 4 using namespace std; 5 6 template<typename T> 7 class Vector 8 { 9 private: 10 int size; 11 T* ptr; 12 public: 13 //拷贝构造析构 14 Vector(int n); 15 Vector(int n, T value); 16 Vector(const Vector<T>& v); 17 ~Vector(); 18 19 int get_size(){return size;} 20 21 //at 22 T& at(int index)const; 23 T& at(int index); 24 25 //重载运算符[] 26 T& operator[](int index)const; 27 T& operator[](int index); 28 29 //友元output 30 template<typename T1> 31 friend void output(const Vector<T1>& v); 32 }; 33 34 template<typename T> 35 Vector<T>::Vector(int n) :size(n) 36 { 37 if (n < 0) 38 { 39 throw length_error("Vector constructor:negative size"); 40 } 41 else 42 { 43 ptr = new T[size]; 44 } 45 } 46 47 48 template<typename T> 49 Vector<T>::Vector(int n, T value) :size(n) 50 { 51 52 if (n < 0) 53 { 54 throw length_error("Vector constructor:negative size"); 55 } 56 else 57 { 58 ptr = new T[size]; 59 for (int i = 0; i < size; i++) 60 { 61 ptr[i] = value; 62 63 } 64 } 65 66 67 } 68 69 template<typename T> 70 Vector<T>::Vector(const Vector<T>& v) :size(v.size), ptr(new T[size]) 71 { 72 for (int i = 0; i < size; i++) 73 { 74 ptr[i] = v.ptr[i]; 75 } 76 77 } 78 79 template<typename T> 80 Vector<T>::~Vector(){delete[]ptr;} 81 82 template<typename T> 83 T& Vector<T>::at(int index)const 84 { 85 if (index < 0 || index >= size) 86 { 87 throw out_of_range("Vector:index out of range"); 88 } 89 else 90 { 91 return ptr[index]; 92 } 93 94 } 95 96 template<typename T> 97 T& Vector<T>::at(int index) 98 { 99 return static_cast<const Vector*>(this)->at(index); 100 } 101 102 template<typename T> 103 T& Vector<T>::operator[](int index)const 104 { 105 if (index < 0 || index >= size) 106 { 107 throw out_of_range("Vector:index out of range"); 108 } 109 else 110 { 111 return ptr[index]; 112 } 113 114 } 115 template<typename T> 116 T& Vector<T>::operator[](int index) 117 { 118 return static_cast<const Vector*>(this)->operator[](index); 119 } 120 121 template<typename T> 122 void output(const Vector<T>& v) 123 { 124 for (int i = 0; i < v.size; i++) 125 cout << v.ptr[i] << ", "; 126 cout << endl; 127 128 }
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 }
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 6 using namespace std; 7 8 class stu 9 { 10 private: 11 int No; 12 string name; 13 string major; 14 int score; 15 16 public: 17 stu() = default; 18 ~stu() = default; 19 20 string get_major()const 21 { 22 return major; 23 } 24 int get_score()const 25 { 26 return score; 27 } 28 29 friend ostream& operator<<(ostream& out, stu& p); 30 friend istream& operator>>(istream& in, stu& p); 31 32 33 }; 34 35 ostream& operator<<(ostream& out, stu& p) 36 { 37 out << setiosflags(ios_base::left); 38 out << setw(15) << p.No 39 << setw(15) << p.name 40 << setw(15) << p.major 41 << setw(15) << p.score << endl; 42 return out; 43 44 } 45 46 istream& operator>>(istream& in, stu& p) 47 { 48 in >> p.No >> p.name >> p.major >> p.score; 49 return in; 50 }
1 #pragma once 2 #include"stu.hpp" 3 #include<iostream> 4 #include<string> 5 #include<fstream> 6 #include<vector> 7 8 bool compare(const stu& p1, const stu& p2) 9 { 10 if (p1.get_major() < p2.get_major()) 11 { 12 return true; 13 } 14 if (p1.get_major() == p2.get_major()) 15 { 16 return p1.get_score() > p2.get_score(); 17 18 } 19 return false; 20 21 } 22 23 void output(ostream& out, vector<stu>& v) 24 { 25 for (auto& i : v) 26 { 27 out << i; 28 } 29 30 } 31 32 void save(const string& filename, vector<stu>& v) 33 { 34 ofstream out(filename); 35 if (!out.is_open()) 36 { 37 cout << "fail to open the file to write\n"; 38 return; 39 } 40 41 output(out, v); 42 out.close(); 43 44 } 45 46 void load(const string& filename, vector<stu>& v) 47 { 48 ifstream in(filename); 49 50 if (!in.is_open()) 51 { 52 cout << "fail to open the file to read\n"; 53 return; 54 } 55 56 string firstline; 57 getline(in, firstline); 58 stu p; 59 while (in >> p) 60 { 61 v.push_back(p); 62 } 63 64 in.close(); 65 }
1 #include"stu.hpp" 2 #include"process.hpp" 3 #include<iostream> 4 #include<string> 5 #include<vector> 6 #include<algorithm> 7 8 int main() 9 { 10 11 vector<stu> v; 12 load("data5.txt", v); 13 sort(v.begin(), v.end(), compare); 14 15 output(cout, v); 16 save("ans.txt", v); 17 18 system("pause"); 19 return 0; 20 }