实验6 模板类、文件I/O和异常处理
4. 实验任务4
Vector.hpp
View Code
View Code
1 #pragma once 2 #include<string> 3 #include<iostream> 4 5 6 using namespace std; 7 8 template<typename T> 9 class Vector { 10 public: 11 Vector(int n);//构造函数 12 Vector(int n, T vlaue); 13 Vector(const Vector<T>& x); 14 ~Vector();//析构函数 15 16 T get_size()const; 17 T& at(int index)const; 18 T& at(int index); 19 T& operator[](int index)const; 20 T& operator[](int index); 21 22 template<typename T1> 23 friend void output(const Vector<T1>& x); 24 25 private: 26 int size; 27 T* ptr; 28 int value; 29 }; 30 template<typename T> 31 Vector<T>::Vector(int n) :size{ n } { 32 if (n < 0) 33 throw std::length_error("Vector constructor: negative size"); 34 ptr = new T[size]; 35 } 36 template<typename T> 37 Vector<T>::Vector(int n, T vlaue) :size{n} { 38 if (n < 0) 39 throw std::length_error("Vector constructor: negative size"); 40 ptr = new T[size]; 41 for (int i = 0; i < size; ++i) 42 ptr[i] = value; 43 } 44 template<typename T> 45 Vector<T>::Vector(const Vector<T>& x) :size{ x.size }, ptr{new int[size]} { 46 for (int i = 0; i < size; ++i) 47 ptr[i] = x.value; 48 } 49 template<typename T> 50 Vector<T>::~Vector() { 51 delete[]ptr; 52 } 53 template<typename T> 54 T Vector<T>::get_size()const { 55 return size; 56 } 57 template<typename T> 58 T& Vector<T>::at(int index)const { 59 if (index < 0 || index >= size) 60 throw std::out_of_range("Vector::at(): index out of this->size"); 61 return ptr[index]; 62 } 63 template<typename T> 64 T& Vector<T>::at(int index) { 65 return static_cast<const Vector*>(this)->at(index); 66 } 67 template<typename T> 68 T& Vector<T>::operator[](int index)const { 69 if (index < 0 || index >= size) 70 throw std::out_of_range("Vector::operator[]():index out of this->size"); 71 return ptr[index]; 72 } 73 template<typename T> 74 T& Vector<T>::operator[](int index) { 75 return static_cast<const Vector*>(this)->operator[](index); 76 } 77 78 template<typename T1> 79 void output(const Vector<T1>& x) { 80 for (int i=0;i<x.size;++i) 81 cout << x.ptr[i] << " "; 82 cout << endl; 83 }
task4.cpp
1 #include <iostream> 2 #include "Vector.hpp" 3 void test1() { 4 using namespace std; 5 int n; 6 cout << "Enter n: "; 7 cin >> n; 8 Vector<double> x1(n); 9 for (auto i = 0; i < n; ++i) 10 x1.at(i) = i * 0.7; 11 cout << "x1: "; output(x1); 12 Vector<int> x2(n, 42); 13 const Vector<int> x3(x2); 14 cout << "x2: "; output(x2); 15 cout << "x3: "; output(x3); 16 x2.at(0) = 77; 17 x2.at(1) = 777; 18 cout << "x2: "; output(x2); 19 cout << "x3: "; output(x3); 20 } 21 void test2() { 22 using namespace std; 23 int n, index; 24 while (cout << "Enter n and index: ", cin >> n >> index) { // 多组输入,按下Ctrl + Z终止 25 try { 26 Vector<int> v(n, n); 27 v.at(index) = -999; 28 cout << "v: "; output(v); 29 } 30 catch (const exception& e) { 31 cout << e.what() << endl; 32 } 33 } 34 } 35 int main() { 36 cout << "测试1: 模板类接口测试\n"; 37 test1(); 38 cout << "\n测试2: 模板类异常处理测试\n"; 39 test2(); 40 }

5. 实验任务5
task5.cpp
View Code
View Code
View Code
1 #include"students.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<students>v; 11 12 load("data5.txt", v); 13 sort(v.begin(), v.end(), compare); 14 output(cout, v); 15 save("ans.txt", v); 16 17 } 18 19 int main() { 20 test(); 21 }
students.hpp
1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 #include<iomanip> 6 7 using namespace std; 8 using std::ostream; 9 using std::istream; 10 using std::setw; 11 using std::setprecision; 12 using std::setiosflags; 13 using std::ios_base; 14 15 //students 16 class students { 17 public: 18 students() = default; 19 ~students() = default; 20 21 string get_major()const { return major; } 22 float get_score()const { return score; } 23 24 friend ostream& operator<<(ostream & out, const students &c); 25 friend istream& operator>>(istream& in, students& c); 26 27 private: 28 int no;//学号 29 string name; 30 string major; 31 float score; 32 }; 33 34 ostream& operator<<(ostream& out, const students& c) { 35 out << setiosflags(ios_base::left); 36 out << setw(15) << c.no 37 << setw(15) << c.name 38 << setw(15) << c.major 39 << setw(15) << c.score; 40 return out; 41 42 } 43 44 istream& operator>>(istream& in, students& c) { 45 in >> c.no >> c.name >> c.major >> c.score; 46 return in; 47 }
utils.hpp
1 #include"students.hpp" 2 #include<fstream> 3 #include<iostream> 4 #include<string> 5 #include<vector> 6 7 //排序函数 8 bool compare(const students& c1, const students& c2) { 9 if (c1.get_major() != c2.get_major()) 10 return c1.get_major() < c2.get_major(); 11 return c1.get_score() > c2.get_score(); 12 } 13 14 //把vector<students>对象中元素插入到输出流out 15 void output(std::ostream& out, const std::vector<students>& v) { 16 for (auto& i : v) 17 out << i << std::endl; 18 } 19 20 //把vector<students>对象中元素写入filename文件 21 void save(const std::string& filename, std::vector<students>& v) { 22 using std::ofstream; 23 24 ofstream out(filename); 25 if (!out.is_open()) { 26 std::cout << "fail to open file to write\n"; 27 return; 28 } 29 30 output(out, v); 31 out.close(); 32 } 33 34 //从文件filename读取信息到vector<students>对象 35 void load(const std::string& filename, std::vector<students>& v) { 36 using std::ifstream; 37 38 ifstream in(filename); 39 if (!in.is_open()) { 40 std::cout << "fail to open file to read\n"; 41 return; 42 } 43 44 std::string title_line; 45 getline(in, title_line); 46 47 students t; 48 while (in >> t) 49 v.push_back(t); 50 51 in.close(); 52 }


浙公网安备 33010602011771号