实验任务四
源代码
Vector.hpp
1 #include<iostream> 2 #include<stdexcept> 3 using namespace std; 4 5 template<typename T> 6 class Vector { 7 public: 8 Vector(int n); 9 Vector(int n, T a); 10 Vector(const Vector<T>& c); 11 ~Vector(); 12 13 int get_size()const { return n; } 14 T& at(int i); 15 T operator[](int i)const; 16 17 template<typename T> 18 friend void output(Vector<T> t); 19 private: 20 T* ptr; 21 int n; 22 }; 23 template<typename T> 24 Vector<T>::Vector(int n):n(n) { 25 if (n < 0) 26 throw length_error("Vector constructor: negative size"); 27 else 28 ptr = new T[n]; 29 } 30 31 template<typename T> 32 Vector<T>::Vector(int n, T a):n(n) { 33 ptr = new T[n]; 34 for (int i = 0; i < n; i++) { 35 ptr[i] = a; 36 } 37 } 38 template<typename T> 39 Vector<T>::Vector(const Vector<T>& c) { 40 ptr = new T[c.n]; 41 n = c.n; 42 for (int i = 0; i < n; i++) { 43 ptr[i] = c.ptr[i]; 44 } 45 } 46 47 template<typename T> 48 Vector<T>::~Vector() { 49 delete[] ptr; 50 } 51 52 template<typename T> 53 T& Vector<T>::at(int i) { 54 if (i < 0 || i >= n) 55 throw out_of_range("Vector:index out of range"); 56 else 57 return ptr[i]; 58 } 59 60 template<typename T> 61 T Vector<T>::operator[](int i)const { 62 if (i < 0 || i >= n) 63 throw out_of_range("Vector:index out of range"); 64 else 65 return ptr[i]; 66 } 67 68 template<typename T> 69 void output(Vector<T> t) { 70 for (int i = 0; i < t.n;i++) { 71 cout << t.ptr[i] << ", "; 72 } 73 cout <<"\b\b "<< endl; 74 }
task.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 }
运行结果截图
实验任务五
源代码
data5.hpp
1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 #include<string> 5 6 using namespace std; 7 8 class Student { 9 public: 10 Student() = default; 11 ~Student() = default; 12 string getcourse()const { return course; } 13 int getso()const { return score; } 14 15 friend ostream& operator<<(ostream& out, const Student& s); 16 friend istream& operator>>(istream& in, Student& s); 17 private: 18 int num; 19 string name; 20 string course; 21 int score; 22 23 }; 24 25 ostream& operator<<(ostream& out, const Student& s) { 26 out << left; 27 out << setw(15) << s.num 28 << setw(15) << s.name 29 << setw(15) << s.course 30 << setw(15) << s.score; 31 return out; 32 } 33 istream& operator>>(istream& in, Student& s) { 34 in >> s.num >> s.name >> s.course >> s.score; 35 return in; 36 }
sort.cpp
1 #include<iostream> 2 #include<algorithm> 3 #include<fstream> 4 #include<string> 5 #include"data5.hpp" 6 #include<vector> 7 8 using namespace std; 9 bool compare_by_so(const Student& s1, const Student& s2) { 10 if (s1.getcourse() > s2.getcourse()) 11 return true; 12 if (s1.getcourse() == s2.getcourse()) 13 return s1.getso() > s2.getso(); 14 15 return false; 16 } 17 18 void output(ostream& out, const vector<Student>& v) { 19 for (auto& i : v) 20 out << i << endl; 21 } 22 23 void save(const string& filename, vector<Student>& v) { 24 ofstream out(filename); 25 if (!out.is_open()) { 26 cout << "fail to open file to write\n"; 27 return; 28 } 29 30 output(out, v); 31 out.close(); 32 } 33 34 void load(const string& filename, vector<Student>& v) { 35 ifstream in(filename); 36 if (!in.is_open()) { 37 cout << "fail to open file to write\n"; 38 return; 39 } 40 41 string title; 42 getline(in, title); 43 44 Student s; 45 while (in >> s) 46 v.push_back(s); 47 in.close(); 48 } 49 void test() { 50 using namespace std; 51 52 vector<Student> v; 53 54 load("data5.txt", v); 55 sort(v.begin(), v.end(), compare_by_so); 56 output(cout, v); 57 save("ans5.txt", v); 58 } 59 60 int main() { 61 test(); 62 }
运行结果截图