实验六
实验任务5:
(1)代码部分:
1 #pragma once 2 #include<iostream> 3 #include<cstring> 4 5 using namespace std; 6 7 template<class T> 8 class Vector { 9 public: 10 Vector(int n, int value=0); 11 Vector(const Vector& v) :data{ v.data }, size{v.size} {}; 12 ~Vector(); 13 int get_size() const { 14 return size; 15 } 16 T& at(int index) const { 17 if (index<0||index >= size) 18 throw out_of_range("index out of range"); 19 return data[index]; 20 } 21 T& at(int index) { 22 return static_cast<const Vector<T>*>(this)->at(index); 23 } 24 25 T& operator[](int index) const { 26 if (index < 0 || index >= size) 27 throw out_of_range("index out of range"); 28 return data[index]; 29 } 30 31 private: 32 T* data; 33 int size; 34 }; 35 36 template<class T> 37 Vector<T>::Vector(int n, int value) { 38 if (n < 0) 39 throw length_error("negative size"); 40 this->size = n; 41 this->data = new T[size]; 42 for (int i = 0;i < size;i++) 43 data[i] = value; 44 } 45 46 template<class T> 47 Vector<T>::~Vector() { 48 if (data != NULL) { 49 delete[] data; 50 data = NULL; 51 } 52 } 53 54 template < typename T1> 55 void output(const Vector<T1>&v) { 56 for (int i = 0; i < v.get_size()-1;i++) { 57 cout << v[i] << ", "; 58 } 59 cout << v[v.get_size() - 1]; 60 cout << endl; 61 } 62 #include <iostream> 63 #include "Vector.hpp" 64 65 void test1() { 66 using namespace std; 67 68 int n; 69 cout << "Enter n: "; 70 cin >> n; 71 72 Vector<double> x1(n); 73 for (auto i = 0; i < n; ++i) 74 x1.at(i) = i * 0.7; 75 76 cout << "x1: "; output(x1); 77 78 Vector<int> x2(n, 42); 79 const Vector<int> x3(x2); 80 81 cout << "x2: "; output(x2); 82 cout << "x3: "; output(x3); 83 84 x2.at(0) = 77; 85 x2.at(1) = 777; 86 cout << "x2: "; output(x2); 87 cout << "x3: "; output(x3); 88 } 89 90 void test2() { 91 using namespace std; 92 93 int n, index; 94 while (cout << "Enter n and index: ", cin >> n >> index) { 95 try { 96 Vector<int> v(n, n); 97 v.at(index) = -999; 98 cout << "v: "; output(v); 99 } 100 catch (const exception& e) { 101 cout << e.what() << endl; 102 } 103 } 104 } 105 106 int main() { 107 cout << "测试1: 模板类接口测试\n"; 108 test1(); 109 110 cout << "\n测试2: 模板类异常处理测试\n"; 111 test2(); 112 }
(2)运行结果:

实验任务5:
(1)代码部分:
1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 #include<string> 5 6 using std::string; 7 using std::ostream; 8 using std::istream; 9 using std::setw; 10 using std::ios_base; 11 using std::setiosflags; 12 13 class member { 14 public: 15 member() = default; 16 ~member() = default; 17 string get_course()const { 18 return course; 19 } 20 21 int get_score()const { 22 return score; 23 } 24 25 friend ostream& operator << (ostream & out, const member & m); 26 friend istream& operator >> (istream& in, member& m); 27 private: 28 string number; 29 string name; 30 string course; 31 int score; 32 }; 33 34 ostream& operator << (ostream& out, const member& m) { 35 out << setiosflags(ios_base::left); 36 out << setw(15) << m.number 37 << setw(15) << m.name 38 << setw(15) << m.course 39 << setw(5) << m.score; 40 41 return out; 42 } 43 44 istream& operator >> (istream& in, member& m) { 45 in >> m.number >> m.name >> m.course >> m.score; 46 return in; 47 } 48 49 50 #include"member.hpp" 51 #include<vector> 52 #include<iostream> 53 #include<fstream> 54 #include<string> 55 #include<vector> 56 57 bool compare(const member& m1, const member& m2) { 58 if (m1.get_course() < m2.get_course()) 59 return true; 60 if (m1.get_course() == m2.get_course()) 61 return m1.get_score()>m2.get_score(); 62 return false; 63 } 64 65 void output(ostream& out,const std::vector<member>& v) { 66 for (auto& i : v) { 67 out << i << std::endl; 68 } 69 } 70 71 void save(const std::string& filename, std::vector<member>& v) { 72 using std::ofstream; 73 74 ofstream out(filename); 75 if (!out.is_open()) { 76 std::cout << "fail to open file to write\n"; 77 return; 78 } 79 80 output(out, v); 81 out.close(); 82 } 83 84 void load(const std::string& filename, std::vector<member>& v) { 85 using std::ifstream; 86 87 ifstream in(filename); 88 if (!in.is_open()) { 89 std::cout << "fail to open file to read\n"; 90 return; 91 } 92 93 std::string title_line; 94 getline(in, title_line); // 跳过标题行 95 96 member t; 97 while (in >> t) 98 v.push_back(t); 99 100 in.close(); 101 } 102 103 104 #include"member.hpp" 105 #include"tool.hpp" 106 #include <iostream> 107 #include <vector> 108 #include <algorithm> 109 110 void test() { 111 using namespace std; 112 vector<member> v; 113 load("data5.txt", v); 114 sort(v.begin(), v.end(), compare); 115 output(cout, v); 116 save("ans2.txt", v); 117 } 118 int main() { 119 test(); 120 }
(2)运行结果:


1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 #include<string> 5 6 using std::string; 7 using std::ostream; 8 using std::istream; 9 using std::setw; 10 using std::ios_base; 11 using std::setiosflags; 12 13 class member { 14 public: 15 member() = default; 16 ~member() = default; 17 string get_course()const { 18 return course; 19 } 20 21 int get_score()const { 22 return score; 23 } 24 25 friend ostream& operator << (ostream & out, const member & m); 26 friend istream& operator >> (istream& in, member& m); 27 private: 28 string number; 29 string name; 30 string course; 31 int score; 32 }; 33 34 ostream& operator << (ostream& out, const member& m) { 35 out << setiosflags(ios_base::left); 36 out << setw(15) << m.number 37 << setw(15) << m.name 38 << setw(15) << m.course 39 << setw(5) << m.score; 40 41 return out; 42 } 43 44 istream& operator >> (istream& in, member& m) { 45 in >> m.number >> m.name >> m.course >> m.score; 46 return in; 47 } 48 #include"member.hpp" 49 #include<vector> 50 #include<iostream> 51 #include<fstream> 52 #include<string> 53 #include<vector> 54 55 bool compare(const member& m1, const member& m2) { 56 if (m1.get_course() < m2.get_course()) 57 return true; 58 if (m1.get_course() == m2.get_course()) 59 return m1.get_score()>m2.get_score(); 60 return false; 61 } 62 63 void output(ostream& out,const std::vector<member>& v) { 64 for (auto& i : v) { 65 out << i << std::endl; 66 } 67 } 68 69 void save(const std::string& filename, std::vector<member>& v) { 70 using std::ofstream; 71 72 ofstream out(filename); 73 if (!out.is_open()) { 74 std::cout << "fail to open file to write\n"; 75 return; 76 } 77 78 output(out, v); 79 out.close(); 80 } 81 82 void load(const std::string& filename, std::vector<member>& v) { 83 using std::ifstream; 84 85 ifstream in(filename); 86 if (!in.is_open()) { 87 std::cout << "fail to open file to read\n"; 88 return; 89 } 90 91 std::string title_line; 92 getline(in, title_line); // 跳过标题行 93 94 member t; 95 while (in >> t) 96 v.push_back(t); 97 98 in.close(); 99 } 100 101 102 #include"member.hpp" 103 #include"tool.hpp" 104 #include <iostream> 105 #include <vector> 106 #include <algorithm> 107 108 void test() { 109 using namespace std; 110 vector<member> v; 111 load("data5.txt", v); 112 sort(v.begin(), v.end(), compare); 113 output(cout, v); 114 save("ans2.txt", v); 115 } 116 int main() { 117 test(); 118 }

浙公网安备 33010602011771号