实验6 模板类、文件I/O和异常处理

1.实验任务4

Vector.hpp

 1 #pragma once
 2 #include <iostream>
 3 #include <stdexcept>
 4 
 5 using namespace std;
 6 
 7 template<typename T>
 8 class Vector {
 9 public:
10     Vector(int n); 
11     Vector(int n, T value);
12     Vector(const Vector<T>& vi);
13     ~Vector();  
14 
15     int get_size() const; 
16     T& at(int index) const; 
17     T& at(int index); 
18 
19     T& operator[](int index) const; 
20     T& operator[](int index);
21 
22     template<typename U>
23     friend void output(const Vector<U>& vi);
24 
25 private:
26     int size; 
27     T* ptr;     
28 };
29 
30 template<typename T>
31 void output(const Vector<T>& vi) {
32     for(int i=0;i<vi.size;i++) { 
33         cout<<vi.at(i)<<", "; 
34     }
35     cout<<endl;
36 }
37 
38 template<typename T>
39 Vector<T>::Vector(int n):size(n) {
40     if(n<0)
41         throw length_error("Vector constructor: negative size");
42 
43     ptr=new T[size];
44 }
45 
46 template<typename T>
47 Vector<T>::Vector(int n, T value) : size(n) {
48     if(n<0)
49         throw length_error("Vector constructor: negative size");
50 
51     ptr=new T[size]; 
52     for(int i=0;i<size;i++) { 
53         ptr[i]=value;
54     }
55 }
56 
57 template<typename T>
58 Vector<T>::Vector(const Vector<T>& vi):size(vi.size),ptr(new T[size]) { 
59     for(int i=0;i<size;i++) { 
60         ptr[i]=vi.ptr[i];
61     }
62 }
63 
64 template<typename T>
65 Vector<T>::~Vector() {
66     delete[] ptr;
67 }
68 
69 template<typename T>
70 int Vector<T>::get_size() const {
71     return size;
72 }
73 
74 template<typename T>
75 T& Vector<T>::at(int index) const {
76     if(index<0||index>=size) {
77         throw out_of_range("Vector::at(): index out of range");
78     }
79     return ptr[index]; 
80 }
81 
82 template<typename T>
83 T& Vector<T>::at(int index) {
84     return const_cast<T&>(static_cast<const Vector<T>*>(this)->at(index)); 
85 }
86 
87 template<typename T>
88 T& Vector<T>::operator[](int index) const {
89     if (index < 0 || index >= size) {
90         throw out_of_range("Vector::operator[](): index out of range");
91     }
92     return ptr[index]; 
93 }
94 
95 template<typename T>
96 T& Vector<T>::operator[](int index) {
97     return const_cast<T&>(static_cast<const Vector<T>*>(this)->operator[](index));
98 }

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 }

结果截图:

 

2.实验任务5

task5.cpp

 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 #include<vector>
 5 #include<iomanip>
 6 #include<algorithm>
 7 
 8 using namespace std;
 9 
10 class student{
11 public:
12     student()=default;
13     ~student()=default;
14 
15     string get_major()const {
16         return major;
17     }
18 
19     int get_score()const {
20         return score;
21     }
22 
23     friend ostream& operator<<(ostream& out,const student& s) {
24         out<<setiosflags(ios_base::left);
25         out<<setw(10)<<s.number
26             <<setw(10)<<s.name
27             <<setw(10)<<s.major
28             <<setw(10)<<s.score;
29 
30         return out;
31     }
32 
33     friend istream& operator>>(istream& in,student& s) {
34         in>>s.number>>s.name>>s.major>>s.score;
35 
36         return in;
37     }
38 
39 private:
40     string number;
41     string name;
42     string major;
43     int score;
44 
45 };
46 
47 bool compare(const student& s1,const student& s2) {
48     if (s1.get_major()<s2.get_major())
49         return true;
50 
51     if (s1.get_major()==s2.get_major())
52         return s1.get_score()>s2.get_score();
53 
54     return false;
55 }
56 
57 void output(ostream& out,const vector<student>& v) {
58     for (auto& i:v)
59         out<<i<<endl;
60 }
61 
62 void save(const string& filename,vector<student>& v) {
63     ofstream out(filename);
64     if (!out.is_open()) {
65         cout << "fail to open file to write\n";
66         return;
67     }
68 
69     output(out, v);
70     out.close();
71 }
72 
73 void load(const string& filename, vector<student>& v) {
74     ifstream in(filename);
75     if (!in.is_open()) {
76         cout<<"fail to open to read\n";
77         return;
78     }
79 
80     string title_line;
81     getline(in, title_line);
82 
83     student t;
84     while(in>>t)
85         v.push_back(t);
86 
87     in.close();
88 }
89 
90 int main() {
91     vector<student>v;
92     load("data5.txt", v);
93     sort(v.begin(),v.end(),compare);
94     output(cout, v);
95     save("ans5.txt", v);
96 }

结果截图:

 

posted @ 2024-12-17 18:48  lbldmx  阅读(8)  评论(0编辑  收藏  举报