实验6 模板类、文件I/O和异常处理
实验任务1-3:
验证性实验。无需写入实验博客文档。
已亲自动手实践。
实验任务4:
1 #pragma once 2 #include<iostream> 3 #include<stdexcept> 4 using namespace std; 5 template <typename T> 6 class Vector{ 7 private: 8 int size; 9 T *data; 10 template<typename T1> 11 friend void output(const Vector<T1> &v) ; 12 13 public: 14 Vector(int n){ 15 if(n<0) 16 throw length_error("Vector constructor:negative size"); 17 data =new T[n]; 18 size=n; 19 } 20 Vector(int n,T value){ 21 if(n<0) 22 throw length_error("Vector constructor:negative size"); 23 data =new T[n]; 24 size=n; 25 for(int i=0;i<n;i++) 26 data[i]=value; 27 } 28 Vector(const Vector<T>& other) { 29 size = other.size; 30 data = new T[size]; 31 for (int i = 0; i < size; i++) { 32 data[i] = other.data[i]; 33 } 34 } 35 ~Vector() { 36 delete[] data; 37 } 38 int get_size()const{ 39 return size; }; 40 T& at(int i) const { 41 if(i<0||i>=size)throw out_of_range("Vector:index out of range"); 42 return data[i]; 43 }; 44 T& operator[](int i){ 45 if(i>size)throw out_of_range("Vector:index out of range"); 46 return data[i]; 47 48 } 49 50 }; 51 52 template<typename T1> 53 void output(const Vector<T1> &v) 54 { 55 for(int i=0;i<v.size;i++){ 56 cout<<v.at(i)<<", "; 57 } 58 cout<<"\b\b "<<endl; 59 }
1 #include <iostream> 2 #include "Vector.hpp" 3 4 using namespace std; 5 6 void test1() { 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 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 #include<string> 5 #include<vector> 6 #include<fstream> 7 using namespace std; 8 9 class Count{ 10 private: 11 string id; 12 string name; 13 string major; 14 int score; 15 public: 16 Count()=default; 17 ~Count()=default; 18 int get_score()const; 19 string get_major()const; 20 friend ostream& operator<<(ostream &out,const Count &c); 21 friend istream& operator>>(istream &in,Count &c); 22 23 24 }; 25 int Count::get_score()const{ 26 return score; 27 } 28 string Count::get_major() const{ 29 return major; 30 } 31 ostream& operator<<(ostream &out,const Count &c) 32 { 33 out << setiosflags(ios_base::left); 34 out<<setw(15)<<c.id 35 <<setw(15)<<c.name 36 <<setw(15)<<c.major 37 <<setw(5)<<c.score; 38 return out; 39 40 } 41 istream& operator>>(istream &in, Count &c) 42 { 43 in>>c.id>>c.name>>c.major>>c.score; 44 return in; 45 46 }
1 #pragma once 2 #include "Student.hpp" 3 #include <vector> 4 #include<iostream> 5 #include<fstream> 6 #include <algorithm> 7 8 bool compare_by_major(const Count &c1,const Count &c2){ 9 if(c1.get_major() < c2.get_major())return true; 10 if(c1.get_major() == c2.get_major()) 11 return c1.get_score()>c2.get_score(); 12 return false; 13 } 14 void output(ostream &out,const vector<Count> &v){ 15 for(auto &i: v) 16 out << i << endl; 17 } 18 void load(const string &filename,vector<Count> &v){ 19 ifstream in(filename); 20 if(!in.is_open()){ 21 cout << "fail to open file to read\n"; 22 return ; 23 } 24 string title_line; 25 getline(in,title_line); 26 Count c; 27 while(in>>c) 28 v.push_back(c); 29 in.close(); 30 } 31 void save(const string &filename,vector<Count> &v){ 32 ofstream out(filename); 33 if(!out.is_open()){ 34 cout << "fail to open fele to write\n"; 35 return ; 36 } 37 output(out,v); 38 out.close(); 39 }
1 #include "Student.hpp" 2 #include"utils.hpp" 3 #include<iostream> 4 #include<vector> 5 #include<algorithm> 6 7 int main(){ 8 vector<Count> v; 9 load("data5.txt",v); 10 sort(v.begin(),v.end(),compare_by_major); 11 output(cout,v); 12 save("ans5.txt",v); 13 return 0; 14 }



浙公网安备 33010602011771号