task4
Vector.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <stdexcept> 5 6 using std::cout; 7 using std::endl; 8 9 template<typename T> 10 class Vector { 11 public: 12 Vector(int size0 = 0); 13 Vector(int size0, T same_value); 14 Vector(const Vector<T>& v); 15 ~Vector(); 16 17 int get_size() const; 18 T& at(int index) const; 19 T& at(int index); 20 21 T& operator[](int index) const; 22 T& operator[](int index); 23 24 template<typename T1> 25 friend void output(const Vector<T1>& v); 26 27 private: 28 int size; 29 T* ptr; 30 }; 31 32 template<typename T> 33 Vector<T>::Vector(int size0) : size{ size0 } { 34 if (size < 0) 35 throw std::length_error("vectorInt constructor: negative size"); 36 37 ptr = new T[size]; 38 } 39 40 template<typename T> 41 Vector<T>::Vector(int size0, T same_value) : size{ size0 } { 42 if (size < 0) 43 throw std::length_error("vectorInt constructor: negative size"); 44 45 ptr = new T[size]; 46 for (int i = 0; i < size; ++i) 47 ptr[i] = same_value; 48 } 49 50 template<typename T> 51 Vector<T>::Vector(const Vector<T>& v) : size{ v.size }, ptr{ new T[size] } {//? get_size() 52 for (int i = 0; i < size; ++i) 53 ptr[i] = v.ptr[i]; 54 } 55 56 template<typename T> 57 Vector<T>::~Vector() { 58 delete[] ptr; 59 } 60 61 template<typename T> 62 int Vector<T>::get_size() const { 63 return size; 64 } 65 66 template<typename T> 67 T& Vector<T>::at(int index) const { 68 if (index < 0 || index >= size) 69 throw std::out_of_range("vectorInt::operator[](): index out of this->size"); 70 71 return ptr[index]; 72 } 73 74 template<typename T> 75 T& Vector<T>::at(int index) { 76 return static_cast<const Vector<T>*>(this)->at(index); 77 } 78 79 template<typename T> 80 T& Vector<T>::operator[](int index) const { 81 if (index < 0 || index >= size) 82 throw std::out_of_range("vectorInt::operator[](): index out of this-> size"); 83 84 return ptr[index]; 85 } 86 87 template<typename T> 88 T& Vector<T>::operator[](int index) { 89 return static_cast<const Vector<T>*>(this)->operator[](index); 90 } 91 92 template<typename T1> 93 void output(const Vector<T1>& v) { 94 for (int i = 0; i < v.size; ++i) { 95 cout << v.at(i) << ", "; 96 } 97 cout << "\b\b \n"; 98 }
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) { // 多组输入,按下Ctrl + Z终止 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 }
result4
task5
Student.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 #include <iomanip> 6 7 using std::ostream; 8 using std::istream; 9 using std::string; 10 using std::setiosflags; 11 using std::setw; 12 using std::ios_base; 13 14 15 16 class Student { 17 public: 18 Student() = default; 19 ~Student() = default; 20 21 string get_major() const; 22 int get_score() const; 23 24 friend ostream& operator<<(ostream& out, const Student& s); 25 friend istream& operator>>(istream& in, Student& s); 26 private: 27 string no; 28 string name; 29 string major; 30 int score; 31 }; 32 33 string Student::get_major() const { 34 return major; 35 } 36 37 int Student::get_score() const { 38 return score; 39 } 40 41 42 ostream& operator<<(ostream& out, const Student& s) { 43 out << setiosflags(ios_base::left); 44 out << setw(15) << s.no 45 << setw(15) << s.name 46 << setw(15) << s.major 47 << setw(15) << s.score; 48 49 return out; 50 } 51 52 istream& operator>>(istream& in, Student& s) { 53 in >> s.no >> s.name >> s.major >> s.score; 54 55 return in; 56 }
utils.hpp
1 #include "Student.hpp" 2 #include <fstream> 3 #include <iostream> 4 #include <vector> 5 #include <string> 6 7 bool compare_by_solutionInfo(const Student& s1, const Student& s2) { 8 if (s1.get_major() < s2.get_major()) 9 return true; 10 11 if (s1.get_major() == s2.get_major()) 12 return s1.get_score() > s2.get_score(); 13 14 return false; 15 } 16 17 // 把vector<Student>对象中的元素插入到输出流out 18 void output(std::ostream& out, const std::vector<Student>& v) { 19 for (auto& i : v) { 20 out << i << std::endl; 21 } 22 } 23 24 // 把vector<Student>对象中的元素写到filename文件中 25 void save(const std::string& filename, std::vector<Student>& v) { 26 using std::ofstream; 27 28 ofstream out(filename); 29 if (!out.is_open()) { 30 std::cout << "fail to open file to write\n"; 31 return; 32 } 33 34 output(out, v); 35 out.close(); 36 } 37 38 // 从文件filename读取参赛选手信息到vector<Student>对象 39 void load(const std::string& filename, std::vector<Student>& v) { 40 using std::ifstream; 41 42 ifstream in(filename); 43 if (!in.is_open()) { 44 std::cout << "fail to open file to read\n"; 45 return; 46 } 47 48 std::string title_line; 49 getline(in, title_line); // 跳过标题行 50 51 Student t; 52 while (in >> t) { 53 v.push_back(t); 54 } 55 56 in.close(); 57 }
task5.cpp
1 #include "Student.hpp" 2 #include "utils.hpp" 3 #include <iostream> 4 #include <vector> 5 #include <algorithm> 6 7 void test() { 8 using namespace std; 9 10 vector<Student> v; 11 12 load("data.txt", v); 13 sort(v.begin(), v.end(), compare_by_solutionInfo); 14 output(cout, v); 15 save("ans.txt", v); 16 } 17 18 int main() { 19 test(); 20 }
result5