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

发布时间 2023-12-16 23:42:05作者: oacuiaka

实验任务4

Vector.hpp:

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

task4.cpp:

 1 #include <iostream>
 2 #include "Vector.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     int n;
 8     cin >> n;
 9     
10     Vector<double> x1(n);
11     for(auto i = 0; i < n; ++i)
12         x1.at(i) = i * 0.7;
13 
14     output(x1);
15 
16     Vector<int> x2(n, 42);
17     Vector<int> x3(x2);
18 
19     output(x2);
20     output(x3);
21 
22     x2.at(0) = 77;
23     output(x2);
24 
25     x3[0] = 999;
26     output(x3);
27 }
28 
29 int main() {
30     test();
31 }
View Code

运行测试截图:

实验任务5

task5.cpp:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <iomanip>
 4 
 5 using namespace std;
 6 
 7 void output(std::ostream& out) 
 8 {
 9     for (int i = 0; i <26; i++){
10         if (i == 0) {
11             out << ' ' << setw(2) << ' ';
12         }
13         else {
14             out << setw(2) << i << ' ';
15         }
16         for (int m = 0; m <26; m++) {
17             if (i == 0) {
18                 char n = 'a' + (i + m) % 26;
19                 out << n << ' ';
20             }
21             else {
22                 char n = 'A' + (i + m) % 26;
23                 out << n << ' ';
24             }
25         }
26         out << endl;
27     }
28 }
29 
30 int main() 
31 {
32     cout << "屏幕上打印出:\n" << endl;
33     output(cout);
34 
35     ofstream outfile("cipher_key.txt");
36         if (outfile.is_open()){
37             output(outfile);
38             outfile.close();
39         }
40         else {
41             cout << "无法打开文件 cipher_key.txt" << endl;
42         }
43 }
View Code

运行测试截图: