实验6 模板类、文件I/O和异常处理
实验任务4:
#pragma once #include <iostream> #include <stdexcept> using namespace std; template<typename T> class Vector { public: Vector(int n,T value=0); ~Vector(); Vector(const Vector<T> &v); int get_size() const; T& at(int i) ; T& operator[](int i); template<typename T1> friend void output(Vector<T1> &v); template<typename T2> friend void output(const Vector<T2> &v) ; private: T *arr; int length; }; template<typename T> Vector<T>::Vector(int n,T value): length{n} { if(n<0) throw length_error("Negative"); arr = new T[n]; for(int i=0;i<n;i++) { arr[i]=value; } } template<typename T> Vector<T>::~Vector(){ delete[] arr; } template<typename T> Vector<T>::Vector(const Vector<T> &v){ length=v.length; arr = new T[length]; for(int i=0;i<length;i++) { arr[i]=v.arr[i]; } } template<typename T> int Vector<T>::get_size() const{ return length; } template<typename T> T& Vector<T>::at(int i) { if(i>=length) throw overflow_error("index out of range!"); return arr[i]; } template<typename T> T& Vector<T>::operator[](int i){ return arr[i]; } template<typename T1> void output( Vector<T1> &v){ for(int i=0;i<v.length;i++) { cout<<v.arr[i]<<" "; } cout<<endl; } template<typename T2> void output(const Vector<T2> &v) { for(int i=0;i<v.length;i++) { cout<<v.arr[i]<<" "; } cout<<endl; }
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 <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <string> 5 #include <vector> 6 #include <fstream> 7 8 using std::string; 9 using std::vector; 10 using std::ostream; 11 using std::istream; 12 using std::setw; 13 using std::setprecision; 14 using std::setiosflags; 15 using std::ios_base; 16 17 class Score { 18 public: 19 Score() = default; 20 ~Score() = default; 21 22 int get_score() const { return score; } 23 string get_major() const { return major; } 24 25 friend ostream& operator<<(ostream &out, const Score &c); 26 friend istream& operator>>(istream &in, Score &c); 27 28 private: 29 string no; // 学号 30 string name; // 姓名 31 string major; // 专业 32 int score; // 分数 33 }; 34 35 ostream& operator<<(ostream &out, const Score &c) { 36 out << setiosflags(ios_base::left); 37 out << setw(15) << c.no 38 << setw(15) << c.name 39 << setw(15) << c.major 40 << setw(5) << c.score; 41 42 return out; 43 } 44 45 istream& operator>>(istream &in, Score &c) { 46 in >> c.no >> c.name >> c.major >> c.score; 47 48 return in; 49 } 50 51 bool compare_by_score(const Score &c1, const Score &c2) { 52 if(c1.get_major()<c2.get_major()) 53 return true; 54 55 if(c1.get_major()==c2.get_major()) 56 return c1.get_score() > c2.get_score(); 57 58 return false; 59 } 60 61 // 把对象中的元素插入到输出流out 62 void output(ostream &out, const vector<Score> &v) { 63 for(auto &i: v) 64 out << i << std::endl; 65 } 66 67 68 // 把对象中的元素写到filename文件中 69 void save(const string &filename, vector<Score> &v) { 70 using std::ofstream; 71 72 ofstream out(filename); 73 if(!out.is_open()) { 74 std::cout << "fail to open file to write\n"; 75 return; 76 } 77 78 output(out, v); 79 out.close(); 80 } 81 82 // 从文件filename读取对象 83 void load(const string &filename, vector<Score> &v) { 84 using std::ifstream; 85 86 ifstream in(filename); 87 if(!in.is_open()) { 88 std::cout << "fail to open file to read\n"; 89 return; 90 } 91 92 std::string title_line; 93 getline(in, title_line); // 跳过标题行 94 95 Score t; 96 while(in >> t) 97 v.push_back(t); 98 99 in.close(); 100 } 101 102 void test() { 103 104 vector<Score> v; 105 106 load("data5.txt", v); 107 sort(v.begin(), v.end(),compare_by_score); 108 output(std::cout, v); // 输出对象v中信息到屏幕 109 save("ans5.txt", v); 110 } 111 112 int main() { 113 test(); 114 }