实验六
task4:
代码:
1 #pragma once 2 3 #include<iostream> 4 #include<stdexcept> 5 #include<cmath> 6 7 using namespace std; 8 9 template<typename T> 10 class Vector { 11 private: 12 int size; 13 T* ptr; 14 public: 15 Vector(int s ) :size { s } { 16 if (s < 0) 17 throw std::length_error("vector constructor:negative size"); 18 19 ptr = new T[size]; 20 } 21 22 Vector(int s , T value ) :size { s } { 23 if (s < 0) 24 throw std::length_error("vector constructor:negative size"); 25 26 ptr = new T[size]; 27 for (int i = 0; i < size; ++i) 28 ptr[i] = value; 29 } 30 31 Vector(const Vector<T>& v) :size { v.size }, ptr { new T[size] } { 32 for (int i = 0; i < size; ++i) 33 ptr[i] = v.ptr[i]; 34 } 35 36 ~Vector() { 37 delete[]ptr; 38 } 39 40 int get_size() { 41 return size; 42 } 43 44 T& at(int index)const { 45 if (index < 0 || index >= size) 46 throw std::out_of_range("vector::at():index out of this->size"); 47 48 return ptr[index]; 49 } 50 51 T& operator[](int index) { 52 if (index < 0 || index >= size) 53 throw std::out_of_range("vector::operator[]():index out of this->size"); 54 55 return ptr[index]; 56 } 57 58 friend void output(const Vector<T>v) { 59 for (int i = 0; i < v.size; i++) 60 cout << v.at(i) << ","; 61 cout << "\b\b\n"; 62 } 63 };
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 }
截图:
task5:
代码:
1 #include<iostream> 2 #include<fstream> 3 #include<string> 4 #include<vector> 5 #include<iomanip> 6 #include<algorithm> 7 8 using namespace std; 9 10 class student { 11 private: 12 string number; 13 string name; 14 string major; 15 int score; 16 public: 17 student() = default; 18 ~student() = default; 19 20 string get_major()const { 21 return major; 22 } 23 24 int get_score()const { 25 return score; 26 } 27 28 friend ostream& operator<<(ostream& out, const student& s) { 29 out << setiosflags(ios_base::left); 30 out << setw(10) << s.number 31 << setw(10) << s.name 32 << setw(10) << s.major 33 << setw(10) << s.score; 34 35 return out; 36 } 37 38 friend istream& operator>>(istream& in, student& s) { 39 in >>s.number >> s.name >> s.major >> s.score; 40 41 return in; 42 } 43 44 }; 45 46 bool compare_by_major(const student& s1, const student& s2) { 47 if (s1.get_major() < s2.get_major()) 48 return true; 49 50 if (s1.get_major() == s2.get_major()) 51 return s1.get_score() > s2.get_score(); 52 53 return false; 54 } 55 56 void output(ostream& out, const vector<student>& v) { 57 for (auto& i : v) 58 out << i << endl; 59 } 60 61 void save(const string& filename, vector<student>& v) { 62 ofstream out(filename); 63 if (!out.is_open()) { 64 cout << "fail to open file to write\n"; 65 return; 66 } 67 68 output(out, v); 69 out.close(); 70 } 71 72 void load(const string& filename, vector<student>& v) { 73 ifstream in(filename); 74 if (!in.is_open()) { 75 cout << "fail to open to read\n"; 76 return; 77 } 78 79 string title_line; 80 getline(in, title_line); 81 82 student t; 83 while (in >> t) 84 v.push_back(t); 85 86 in.close(); 87 } 88 89 int main() { 90 vector<student>v; 91 load("data5.txt", v); 92 sort(v.begin(), v.end(), compare_by_major); 93 output(cout, v); 94 save("ans5.txt", v); 95 }
截图: