实验六
任务4:
源码:
1 #include<iostream> 2 3 using namespace std; 4 5 template<typename T> 6 class Vector { 7 private: 8 T* head; 9 int size; 10 int length; 11 public: 12 Vector(int num); 13 Vector(int num, T data); 14 Vector(Vector<T>& v); 15 int get_size(); 16 int get_len(); 17 T& at(int index) const; 18 T& operator[](int index); 19 template<typename U> 20 friend void output(const Vector<U>& v); 21 }; 22 23 template<typename T> 24 Vector<T>::Vector(int num) { 25 if (num < 0) { 26 throw length_error("Vector constructor: negative size"); 27 } 28 else { 29 head = (T*)malloc(sizeof(T) * num); 30 size = num; 31 length = size; 32 } 33 } 34 35 template<typename T> 36 Vector<T>::Vector(int num, T data) { 37 if (num < 0) { 38 throw length_error("Vector constructor: negative size"); 39 } 40 else { 41 head = (T*)malloc(sizeof(T) * num); 42 size = num; 43 length = size; 44 for (int i = 0; i < size; i++) { 45 *(head + i) = data; 46 } 47 } 48 } 49 50 template<typename T> 51 Vector<T>::Vector(Vector<T>& v) { 52 size = v.get_size(); 53 length = v.get_len(); 54 head = (T*)malloc(sizeof(T) * size); 55 for (int i = 0; i < size; i++) { 56 head[i] = v.at(i); 57 } 58 } 59 60 template<typename T> 61 inline int Vector<T>::get_size() { 62 return size; 63 } 64 65 template<typename T> 66 inline int Vector<T>::get_len() { 67 return length; 68 } 69 70 template<typename T> 71 T& Vector<T>::at(int index) const { 72 if (index > size - 1) { 73 throw overflow_error("Vector: index out of range"); 74 } 75 return head[index]; 76 } 77 78 template<typename T> 79 T& Vector<T>::operator[](int index) { 80 if (index > size - 1) { 81 throw overflow_error("Vector: index out of range"); 82 } 83 return head[index]; 84 } 85 86 template<typename T> 87 void output(const Vector<T>& v) { 88 for (int i = 0; i < v.length; i++) { 89 cout << v.at(i) << ' '; 90 } 91 cout << endl; 92 }
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:
源码:
1 #include<iostream> 2 3 using namespace std; 4 5 class Student { 6 private: 7 string id; 8 string name; 9 string major; 10 int score; 11 public: 12 Student(string id, string name, string major, int score); 13 string getId(); 14 string getName(); 15 string getMajor(); 16 int getScore(); 17 friend bool cmp(Student& s1, Student& s2); 18 }; 19 20 Student::Student(string id, string name, string major, int score) : 21 id{ id }, name{ name }, major{ major }, score{ score } {} 22 23 inline string Student::getId() { 24 return id; 25 } 26 27 inline string Student::getName() { 28 return name; 29 } 30 31 inline string Student::getMajor() { 32 return major; 33 } 34 35 inline int Student::getScore() { 36 return score; 37 } 38 39 bool cmp(Student& s1, Student& s2) { 40 if (s1.major.compare(s2.major) > 0) { 41 return false; 42 } 43 else if (s1.major.compare(s2.major) == 0) { 44 if (s1.score < s2.score) { 45 return false; 46 } 47 } 48 return true; 49 }
1 #include<iostream> 2 #include<fstream> 3 #include<vector> 4 #include<algorithm> 5 #include<iomanip> 6 #include"student.hpp" 7 8 int main() { 9 ifstream fin; 10 fin.open("D://data5.txt"); 11 string title; 12 for (int i = 0; i < 4; i++) { 13 fin >> title; 14 } 15 vector<Student> list; 16 string id, name, major; 17 int score; 18 int cnt = 0; 19 while (fin >> id >> name >> major >> score) { 20 list.push_back(Student(id, name, major, score)); 21 } 22 fin.close(); 23 sort(list.begin(), list.end(), cmp); 24 ofstream fout; 25 fout.open("D://ans.txt"); 26 for (auto stu : list) { 27 cout << stu.getId() << '\t' << stu.getName() << '\t' << stu.getMajor() << '\t' << stu.getScore() << endl; 28 fout << stu.getId() << '\t' << setw(8) << stu.getName() << '\t' << stu.getMajor() << '\t' << stu.getScore() << endl; 29 } 30 }
结果: