实验6 模板类、文件IO和异常处理

发布时间 2023-12-17 22:58:21作者: 袁德聪

任务4

 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 }
task4.cpp
 1 #ifndef VECTOR_HPP
 2 #define VECTOR_HPP
 3 
 4 #include <iostream>
 5 #include <stdexcept>
 6 
 7 template <typename T>
 8 class Vector {
 9 private:
10     T* data;
11     size_t size;
12 
13 public:
14     Vector(size_t sz) : size(sz) {
15         data = new T[size];
16     }
17 
18     Vector(size_t sz, const T& value) : size(sz) {
19         data = new T[size];
20         for (size_t i = 0; i < size; ++i) {
21             data[i] = value;
22         }
23     }
24 
25     Vector(const Vector& other) : size(other.size) {
26         data = new T[size];
27         for (size_t i = 0; i < size; ++i) {
28             data[i] = other.data[i];
29         }
30     }
31 
32     ~Vector() {
33         delete[] data;
34     }
35 
36     size_t get_size() const {
37         return size;
38     }
39 
40     T& at(size_t index) {
41         if (index < size) {
42             return data[index];
43         }
44         else {
45             throw std::out_of_range("Index out of range");
46         }
47     }
48 
49     const T& at(size_t index) const {
50         if (index < size) {
51             return data[index];
52         }
53         else {
54             throw std::out_of_range("Index out of range");
55         }
56     }
57 
58     T& operator[](size_t index) {
59         return data[index];
60     }
61 
62     const T& operator[](size_t index) const {
63         return data[index];
64     }
65 
66     friend void output(const Vector& vec) {
67         for (size_t i = 0; i < vec.size; ++i) {
68             std::cout << vec.data[i] << ' ';
69         }
70         std::cout << std::endl;
71     }
72 };
73 
74 #endif
Vector.hpp

运行结果:

 

任务5

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <fstream>
 4 
 5 void output(std::ostream& out) {
 6     const int numLines = 26;
 7 
 8     for (int i = 0; i < numLines; ++i) {
 9         out << std::setw(2) << std::right << i + 1 << ' ';
10         for (int j = 0; j < 26; ++j) {
11             char ch = 'A' + (i + j) % 26;
12             out << ch << ' ';
13         }
14         out << std::endl;
15     }
16 }
17 
18 int main() {
19     std::cout << "屏幕上打印输出:\n"
20         << "   a b c d e f g h i j k l m n o p q r s t u v w x y z"
21         << std::endl;
22     output(std::cout);
23 
24     std::ofstream outFile("cipher_key.txt");
25     if (outFile.is_open()) {
26         std::cout << "写入文件 cipher_key.txt:" << std::endl;
27         output(outFile);
28         outFile.close();
29     }
30     else {
31         std::cerr << "无法打开文件 cipher_key.txt" << std::endl;
32         return 1;
33     }
34 
35     return 0;
36 }
task5.cpp

运行结果: