实验六
任务四:
代码:
vector.hpp:
1 #pragma once 2 #include<iostream> 3 #include<stdexcept> 4 using namespace std; 5 template<typename T> 6 class Vector{ 7 public: 8 Vector(int n,const T& value=T()):size(n){ 9 if(n<0) 10 { 11 throw length_error("大小是负数!"); 12 } 13 else 14 { 15 x=new T[n]; 16 for(int i=0;i<n;i++) 17 { 18 x[i]=value; 19 } 20 } 21 } 22 Vector(const Vector<T>& other):size(other.size){ 23 x=new T[size]; 24 for(int i=0;i<size;i++) 25 { 26 x[i]=other.x[i]; 27 } 28 } 29 ~Vector(){ 30 delete[] x; 31 } 32 33 34 int get_size() const{ 35 return size; 36 } 37 38 39 T& at(int index){ 40 if(index>=size) 41 { 42 throw out_of_range("下标访问越界!"); 43 } 44 else 45 return x[index]; 46 } 47 const T& at(int index) const 48 { 49 if(index>=size) 50 { 51 throw out_of_range("下标访问越界!"); 52 } 53 else 54 return x[index]; 55 } 56 57 58 T& operator[](int i){ 59 return at(i); 60 } 61 const T& operator[](int i)const{ 62 return at(i); 63 } 64 65 66 friend void output(const Vector<T>& a) 67 { 68 for(int i=0;i<a.size;i++) 69 { 70 cout<<a[i]<<" "; 71 } 72 cout<<endl; 73 } 74 private: 75 int size; 76 T* x; 77 };
vector.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 }
运行截图:
任务五:
代码:
task5.cpp:
1 #include<iostream> 2 #include<fstream> 3 #include<string> 4 #include<vector> 5 #include<algorithm> 6 #include<iomanip> 7 using namespace std; 8 class information{ 9 private: 10 int id; 11 string name; 12 string major;//专业 13 int score; 14 public: 15 information()=default; 16 ~information()=default; 17 friend ostream& operator<<(ostream& out,const information& a) 18 { 19 out<<setiosflags(ios_base::left); 20 out<<setw(10)<<a.id 21 <<setw(10)<<a.name 22 <<setw(10)<<a.major 23 <<setw(10)<<a.score; 24 25 return out; 26 } 27 friend istream& operator>>(istream& in,information& a) 28 { 29 in>>a.id >>a.name>>a.major>>a.score; 30 return in; 31 } 32 string get_major()const {return major;} 33 int get_score() const{return score;} 34 35 }; 36 bool compare(const information& a,const information& b) 37 { 38 if(a.get_major()<b.get_major()) 39 return true; 40 if(a.get_major()==b.get_major()){ 41 return a.get_score()>b.get_score(); 42 } 43 return false; 44 } 45 void output(ostream& out,const vector<information>& v) 46 { 47 for(const auto &i:v) 48 { 49 out<<i<<endl; 50 } 51 } 52 void save(const string &filename,vector<information>& v) 53 { 54 ofstream outfile(filename); 55 if(!outfile.is_open()) 56 { 57 cout<<"文件打开失败"; 58 return; 59 } 60 output(outfile,v); 61 outfile.close(); 62 } 63 void load(const string& filename,vector<information>& v) 64 { 65 ifstream infile(filename); 66 if(!infile.is_open()) 67 { 68 cout<<"文件打开失败"; 69 return; 70 } 71 string line; 72 getline(infile,line);//跳过标题行 73 information a; 74 while(infile>>a) 75 v.push_back(a); 76 77 infile.close(); 78 79 } 80 81 82 int main() 83 { 84 vector<information> v; 85 load("data.txt",v); 86 sort(v.begin(),v.end(),compare); 87 output(cout,v); 88 save("data1.txt",v); 89 }
运行截图: