实验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 public: 9 Vector(int n){ 10 if(n<0)throw length_error("vector constructor: negative size"); 11 x = new T[n]; 12 size = n; 13 }; 14 Vector(int n,T value){ 15 if(n<0)throw length_error("vector constructor: negative size"); 16 x = new T[n]; 17 size = n; 18 for(int i=0;i<size;i++)x[i]=value; 19 } 20 Vector(const Vector<T> &c):size{c.size},x{new T{size}}{ 21 for(int i=0;i<size;i++) 22 x[i] = c.x[i]; 23 }; 24 ~Vector(){ 25 delete []x; 26 }; 27 int get_size()const{ 28 return size; 29 }; 30 T& at(int i){ 31 if(i<0||i>=size)throw out_of_range("vector: index out of range"); 32 return x[i]; 33 }; 34 T& at(int i)const{ 35 if(i<0||i>=size)throw out_of_range("vector: index out of range"); 36 return x[i]; 37 }; 38 private: 39 T *x; 40 int size; 41 template<typename T1> 42 friend void output(const Vector<T1> &c); 43 }; 44 template<typename T1> 45 void output(const Vector<T1> &c){ 46 for(int i=0;i<c.size;i++) 47 cout << c.at(i) << "," << " "; 48 cout << "\b\b \n"; 49 }
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) { 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
源代码
Staff.hpp
1 #include<iostream> 2 #include<iomanip> 3 #include<string> 4 #include<vector> 5 #include<fstream> 6 using namespace std; 7 8 class Staff{ 9 private: 10 string no; 11 string name; 12 string major; 13 int score; 14 public: 15 Staff() = default; 16 ~Staff() = default; 17 string get_major()const; 18 int get_score()const; 19 friend ostream& operator<<(ostream &out,const Staff &s); 20 friend istream& operator>>(istream &in,Staff &s); 21 }; 22 string Staff::get_major()const{ 23 return major; 24 } 25 int Staff::get_score()const{ 26 return score; 27 } 28 ostream& operator<<(ostream &out,const Staff &s){ 29 out << setiosflags(ios_base::left); 30 out << setw(15) << s.no 31 << setw(15) << s.name 32 << setw(15) << s.major 33 << setw(2) << s.score; 34 return out; 35 } 36 istream& operator>>(istream &in,Staff &s){ 37 in >> s.no >> s.name >> s.major >> s.score; 38 return in; 39 } 40 bool compare_by_solutionInfo(const Staff &s1,const Staff &s2){ 41 if(s1.get_major() < s2.get_major())return true; 42 if(s1.get_major() == s2.get_major()) 43 return s1.get_score()>s2.get_score(); 44 return false; 45 } 46 void load(const string &filename,vector<Staff> &v){ 47 ifstream in(filename); 48 if(!in.is_open()){ 49 cout << "fail to open file to read\n"; 50 return ; 51 } 52 string title_line; 53 getline(in,title_line); 54 Staff s; 55 while(in>>s) 56 v.push_back(s); 57 in.close(); 58 } 59 void output(ostream &out,const vector<Staff> &v){ 60 for(auto &i: v) 61 out << i << endl; 62 } 63 void save(const string &filename,vector<Staff> &v){ 64 ofstream out(filename); 65 if(!out.is_open()){ 66 cout << "fail to open fele to write\n"; 67 return ; 68 } 69 output(out,v); 70 out.close(); 71 }
task5.cpp
1 #include "Staff.hpp" 2 #include<iostream> 3 #include<vector> 4 #include<algorithm> 5 6 int main(){ 7 vector<Staff> v; 8 9 load("data5.txt",v); 10 sort(v.begin(),v.end(),compare_by_solutionInfo); 11 output(cout,v); 12 save("ans5.txt",v); 13 }
运行结果截图:
ans5.txt