实验任务4:
实验代码:
Vector.hpp:
1 #pragma once 2 3 #include <iostream> 4 #include <stdexcept> 5 6 using namespace std; 7 8 template <typename T> 9 class Vector { 10 public: 11 Vector(int n, int value = 0); 12 Vector(const Vector<T> &v); 13 14 ~Vector() { 15 delete[] ptr; 16 } 17 18 int get_size() const {return size; } 19 20 T& at(int index) const; 21 T& at(int index); 22 23 T& operator[](int index) const; 24 T& operator[](int index); 25 26 template<typename T1> 27 friend void output(const Vector<T1> &v); 28 private: 29 int size; 30 T *ptr; 31 }; 32 33 template <typename T> 34 Vector<T>::Vector(int n, int value): size{n} { 35 if(n < 0) 36 throw length_error("Vector constructor: negative size"); 37 ptr = new T[size]; 38 for(int i = 0; i < size; i++) 39 ptr[i] = value; 40 } 41 42 template <typename T> 43 Vector<T>::Vector(const Vector<T> &v): size{v.size}, ptr{new int[size]} { 44 for(int i = 0; i < size; i++) 45 ptr[i] = v.ptr[i]; 46 } 47 48 template <typename T> 49 T& Vector<T>::at(int index) const { 50 if(index < 0 || index >= size) 51 throw out_of_range("Vector::at(): index out of this->size"); 52 return ptr[index]; 53 } 54 55 template <typename T> 56 T& Vector<T>::at(int index) { 57 return static_cast<const Vector*>(this)->at(index); 58 } 59 60 template <typename T> 61 T& Vector<T>::operator[](int index) const { 62 if(index < 0 || index >= size) 63 throw out_of_range("Vector::opertor[](): index out of this->size"); 64 return ptr[index]; 65 } 66 67 template <typename T> 68 T& Vector<T>::operator[](int index) { 69 return static_cast<const Vector*>(this)->operator[](index); 70 } 71 72 template <typename T> 73 void output(const Vector<T> &v) { 74 for(int i = 0; i < v.get_size(); i++) 75 cout << v.at(i) << ", "; 76 cout << "\b\b \n"; 77 }
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:
实验代码:
Celebrity.hpp:
1 #pragma once 2 3 #include <iostream> 4 #include <fstream> 5 #include <iomanip> 6 #include <string> 7 #include <vector> 8 9 using namespace std; 10 11 class Celebrity { 12 public: 13 Celebrity() = default; 14 ~Celebrity() = default; 15 string get_major() const {return major; } 16 int get_score() const {return score; } 17 friend ostream& operator<<(ostream &out, const Celebrity &c); 18 friend istream& operator>>(istream &in, Celebrity &c); 19 private: 20 string no; 21 string name; 22 string major; 23 int score; 24 }; 25 26 ostream& operator<<(ostream &out, const Celebrity &c) { 27 out << setiosflags(ios_base::left); 28 out << setw(15) << c.no 29 << setw(15) << c.name 30 << setw(15) << c.major 31 << setw(5) << c.score; 32 return out; 33 } 34 35 istream& operator>>(istream &in, Celebrity &c) { 36 in >> c.no >> c.name >> c.major >> c.score; 37 return in; 38 } 39 40 bool compare(const Celebrity &c1, const Celebrity &c2) { 41 if(c1.get_major() != c2.get_major()) 42 return c1.get_major() < c2.get_major(); 43 else 44 return c1.get_score() > c2.get_score(); 45 return false; 46 } 47 48 void output(ostream &out, const vector<Celebrity> &v) { 49 for(auto &i: v) 50 out << i << endl; 51 } 52 53 void save(const string &filename, vector<Celebrity> &v) { 54 55 ofstream out(filename); 56 if(!out.is_open()) { 57 cout << "fail to open file to write\n"; 58 return; 59 } 60 61 output(out, v); 62 out.close(); 63 } 64 65 void load(const string &filename, vector<Celebrity> &v) { 66 67 ifstream in(filename); 68 if(!in.is_open()) { 69 cout << "fail to open file to read\n"; 70 return; 71 } 72 73 string title_line; 74 getline(in, title_line); // 跳过标题行 75 76 Celebrity t; 77 while(in >> t) 78 v.push_back(t); 79 80 in.close(); 81 }
task5.cpp:
1 #include "Celebrity.hpp" 2 #include <fstream> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 8 void test() { 9 using namespace std; 10 11 vector<Celebrity> v; 12 13 load("data5.txt", v); // 从文件加载选手信息到对象v 14 sort(v.begin(), v.end(), compare); // 按解题情况排序 15 output(cout, v); // 输出对象v中信息到屏幕 16 save("ans.txt", v); // 把对象v中选手信息保存到文件 17 } 18 19 int main() { 20 test(); 21 }
实验截图: