实验6 模板类、文件I/O异常处理
实验任务4:
Vector.hpp代码:
1 #pragma once 2 #include<iostream> 3 #include<stdexcept> 4 using namespace std; 5 6 template<typename T> 7 class Vector 8 { 9 private: 10 int size; 11 T* ptr; 12 public: 13 Vector(int n) 14 { 15 if (n < 0) 16 { 17 throw length_error("Vector constructor:negative size"); 18 } 19 else 20 { 21 ptr = new T[size]; 22 } 23 } 24 25 26 Vector(int n, T value) 27 { 28 29 if (n < 0) 30 { 31 throw length_error("Vector constructor:negative size"); 32 } 33 else 34 { 35 ptr = new T[size]; 36 for (int i = 0; i < size; i++) 37 { 38 ptr[i] = value; 39 40 } 41 } 42 } 43 44 45 Vector(const Vector<T>& v) 46 { 47 for (int i = 0; i < size; i++) 48 { 49 ptr[i] = v.ptr[i]; 50 } 51 } 52 ~Vector() 53 { 54 55 delete[]ptr; 56 } 57 58 int get_size() 59 { 60 return size; 61 } 62 63 T& at(int index)const; 64 T& at(int index); 65 66 T& operator[](int index)const; 67 T& operator[](int index); 68 69 friend void output(const Vector<T>& v); 70 }; 71 72 template<typename T> 73 const T& Vector<T>::at(int index) const 74 { 75 if (index < 0 || index >= size) 76 { 77 throw std::out_of_range("Vector: index out of range"); 78 } 79 return ptr[index]; 80 } 81 82 template<typename T> 83 T& Vector<T>::at(int index) 84 { 85 if (index < 0 || index >= size) 86 { 87 throw std::out_of_range("Vector: index out of range"); 88 } 89 return ptr[index]; 90 } 91 92 template<typename T> 93 const T& Vector<T>::operator[](int index) const 94 { 95 return at(index); 96 } 97 98 template<typename T> 99 T& Vector<T>::operator[](int index) 100 { 101 return at(index); 102 } 103 104 template<typename T> 105 void output(const Vector<T>& v) 106 { 107 for (int i = 0; i < v.size; i++) 108 { 109 cout << v.ptr[i] << ", "; 110 } 111 cout <<"\b\b " << endl; 112 113 }
task4.cpp代码:
1 #include <iostream> 2 #include "Vector1.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:
information.hpp:
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 6 using namespace std; 7 8 class information 9 { 10 private: 11 int xuehao; 12 string name; 13 string major; 14 int score; 15 public: 16 information() = default; 17 ~information() = default; 18 19 string get_major()const{ 20 return major; 21 } 22 int get_score()const{ 23 return score; 24 } 25 26 friend ostream& operator<<(ostream& out, information& p); 27 friend istream& operator>>(istream& in, information& p); 28 }; 29 30 ostream& operator<<(ostream& out, information& p){ 31 out << setiosflags(ios_base::left); 32 out << setw(10) << p.xuehao 33 << setw(10) << p.name 34 << setw(10) << p.major 35 << setw(10) << p.score << endl; 36 return out; 37 38 } 39 40 istream& operator>>(istream& in, information& p){ 41 in >> p.xuehao >> p.name >> p.major >> p.score; 42 return in; 43 }
tools.hpp:
1 #pragma once 2 #include"information.hpp" 3 #include<iostream> 4 #include<string> 5 #include<fstream> 6 #include<vector> 7 8 bool paixu(const information& p1, const information& p2) 9 { 10 if (p1.get_major() == p2.get_major()){ 11 return p1.get_score() > p2.get_score(); 12 } 13 if (p1.get_major() < p2.get_major()){ 14 return true; 15 } 16 return false; 17 } 18 19 void output(ostream& out, const vector<information>& v) 20 { 21 for (auto& i : v){ 22 out << i; 23 } 24 } 25 26 void save(const string& filename, vector<information>& v) 27 { 28 ofstream out(filename); 29 if (!out.is_open()) 30 { 31 cout << "文件写入失败" << endl; 32 exit(0); 33 } 34 output(out, v); 35 out.close(); 36 } 37 38 void load(const string& filename, vector<information>& v) 39 { 40 string firstline; 41 information p; 42 ifstream in(filename); 43 if (!in.is_open()){ 44 cout << "文件读出失败" << endl; 45 exit(0); 46 } 47 48 getline(in, firstline); 49 while (in >> p){ 50 v.push_back(p); 51 } 52 53 in.close(); 54 }
task5:
1 #include"information.hpp" 2 #include"tools.hpp" 3 #include<string> 4 #include<vector> 5 #include<algorithm> 6 #include<iostream> 7 8 int main() 9 { 10 vector<information> v; 11 load("data5.txt", v); 12 sort(v.begin(), v.end(), paixu); 13 output(cout, v); 14 save("ans.txt", v); 15 return 0; 16 }
代码截图:


总结:
在文件的读写操作中,我利用ifstream和ofstream类,分别实现了从文件中读取对象向量和将向量保存到文件的功能。
重载<<和>>运算符,使得可以方便地通过标准输入输出流来读取和写入对象。
在重载输入运算符时,我需要注意到输入流中可能存在的换行符和空格等干扰因素,采取措施来避免它们对输入操作的影响。
浙公网安备 33010602011771号