实验六 模板类,文件I/O和异常处理

任务4

代码:

Vector.hpp:

 1 #pragma once
 2 #include<iostream>
 3 #include<stdexcept>
 4 #include<cmath>
 5 
 6 using namespace std;
 7 
 8 template<typename T>
 9 class Vector {
10 public:
11     Vector(int s ) :size{ s } {
12         if (s < 0)
13             throw std::length_error("vector constructor:negative size");
14 
15         ptr = new T[size];
16     }
17 
18     Vector(int s , T value ) :size{ s } {
19         if (s < 0)
20             throw std::length_error("vector constructor:negative size");
21 
22         ptr = new T[size];
23         for (int i = 0; i < size; ++i)
24             ptr[i] = value;
25     }
26 
27     Vector(const Vector<T>& v) :size{ v.size }, ptr{ new T[size] } {
28         for (int i = 0; i < size; ++i)
29             ptr[i] = v.ptr[i];
30     }
31 
32     ~Vector() { delete[]ptr; }
33 
34     int get_size() { return size; }
35      
36     T& at(int index)const { 
37         if (index < 0 || index >= size)
38             throw std::out_of_range("vector::at():index out of this->size");
39 
40         return ptr[index];
41     }
42 
43     T& operator[](int index) {
44         if (index < 0 || index >= size)
45             throw std::out_of_range("vector::operator[]():index out of this->size");
46 
47         return ptr[index];
48     }
49 
50     friend void output(const Vector<T>v) {
51         for (int i = 0; i < v.size; i++)
52             cout << v.at(i) << ",";
53         cout << "\b\b\n";
54     }
55 
56 private:
57     int size;
58     T* ptr;
59 };

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     x2.at(0) = 77;
21     x2.at(1) = 777;
22     cout << "x2:"; output(x2);
23     cout << "x3:"; output(x3);
24 }
25 
26 void test2() {
27     using namespace std;
28 
29     int n, index;
30     while (cout << "Enter n and index:", cin >> n >> index) {
31         try {
32             Vector<int>v(n, n);
33             v.at(index) = -999;
34             cout << "v:"; output(v);
35         }
36         catch (const exception& e) {
37             cout << e.what() << endl;
38         }
39     }
40 }
41 
42 int main() {
43     std::cout << "测试1:模板类接口测试\n";
44     test1();
45 
46     std::cout << "\n测试2:模板类异常处理测试\n";
47     test2();
48 }

运行结果:

任务5

代码:

 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 
93     load("data5.txt", v);
94     sort(v.begin(), v.end(), compare);
95     output(cout, v);
96     save("ans5.txt", v);
97 }

运行结果:

 

posted @ 2024-12-16 17:15  霍学金  阅读(14)  评论(0编辑  收藏  举报