任务4:
Vector.hpp
1 #include<bits/stdc++.h> 2 using namespace std; 3 template <typename T> 4 class Vector{ 5 public: 6 Vector(int size,int value=0):size{size}{ 7 if(size<0) throw length_error("negative size"); 8 else{ 9 ptr=new T[size]; 10 for(int i=0;i<size;i++){ 11 ptr[i]=value; 12 } 13 } 14 } 15 int get_size() const{ 16 return size; 17 } 18 T& at(int val){ 19 if(val>=size) throw out_of_range("index out of range"); 20 else return ptr[val]; 21 } 22 T& operator[] (int val){ 23 if(val<0||val>=size) throw out_of_range("index out of range"); 24 else return ptr[val]; 25 } 26 friend void output(const Vector<T> &x){ 27 for(int i=0;i<x.get_size();i++){ 28 cout<<x.ptr[i]<<", "; 29 } 30 cout<<endl; 31 } 32 Vector(const Vector<T>& x):size(x.size),ptr(new T[size]){ 33 for(int i=0;i<size;i++){ 34 ptr[i]=x.ptr[i]; 35 } 36 } 37 private: 38 int size; 39 T* ptr; 40 };
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:
task5.cpp
1 #include<bits/stdc++.h> 2 using namespace std; 3 class student{ 4 private: 5 string name; 6 string number; 7 int grade; 8 string major; 9 public: 10 student()=default; 11 ~student()=default; 12 string get_major() const{ 13 return major; 14 } 15 int get_grade() const{ 16 return grade; 17 18 } 19 friend ostream& operator<<(ostream &out,const student &s){ 20 out<<setiosflags(ios_base::left); 21 out<<setw(15)<<s.number<<setw(15)<<s.name<<setw(15)<<s.major<<setw(15)<<s.grade; 22 return out; 23 } 24 friend istream &operator>>(istream &in,student &s){ 25 in>>s.number>>s.name>>s.major>>s.grade; 26 return in; 27 } 28 }; 29 30 void ld(const string &filename,vector<student> &s){ 31 ifstream in(filename); 32 if(!in.is_open()) 33 { 34 cout<<"file open read\n"; 35 return ; 36 } 37 string title_line; 38 getline(in,title_line); 39 student t; 40 while(in>>t) 41 s.push_back(t); 42 in.close(); 43 } 44 45 void output(ostream &out,const vector<student> &s){ 46 for(auto& i:s) 47 out<<i<<endl; 48 } 49 50 void ew(const string &filename,vector<student> &s){ 51 ofstream out(filename); 52 if(!out.is_open()){ 53 cout<<"file open write\n"; 54 return ; 55 56 } 57 output(out,s); 58 out.close(); 59 } 60 61 62 bool compare(student &s1,student &s2){ 63 if(s1.get_major()<s2.get_major()) return true; 64 if(s1.get_major()==s2.get_major()) return s1.get_grade()>s2.get_grade(); 65 return false; 66 } 67 int main(){ 68 vector<student> s; 69 ld("data5.txt",s); 70 sort(s.begin(),s.end(),compare); 71 output(cout,s); 72 ew("data5_2.txt",s); 73 } 74
运行结果截图:
实验总结:通过这次实验,加深了对模板类使用的理解。