实验六 模板类与文件I/O

 task 3

task 3_1

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 
 6 int main() {
 7     using namespace std;
 8 
 9     array<int, N> x {97, 98, 99, 100, 101};
10 
11     ofstream out;
12     out.open("data1.dat", ios::binary);
13     if(!out.is_open()) {
14         cout << "fail to open data1.dat\n";
15         return 1;
16     }
17 
18     out.write(reinterpret_cast<char *>(&x), sizeof(x));
19     out.close();
20 }

 

 

task 3_2

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 
 6 int main() {
 7     using namespace std;
 8     array<int, N> x;
 9 
10     ifstream in;
11     in.open("data1.dat", ios::binary);
12     if(!in.is_open()) {
13         cout << "fail to open data1.dat\n";
14         return 1;
15     }
16 
17     in.read(reinterpret_cast<char *>(&x), sizeof(x));
18     in.close();
19 
20     for(auto i = 0; i < N; ++i)
21         cout << x[i] << ", ";
22     cout << "\b\b \n";
23 }

task 3_2 测试截图

 

 改动后

 

 原因分析:

 char类型一次只读取一个字节长度,int 类型占四个字符的长度,所以改为char后b会在第五个出现。

 

 

 

 

 

task4

Vector.hpp

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

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 }

运行截图

 

 

 

 

 

task 5

task 5.cpp

 1 #include <iostream> 
 2 #include <fstream>
 3 #include <iomanip> 
 4 
 5 using namespace std;
 6 
 7 void  output(ostream &out) {
 8     char a[26];
 9     //利用字符数组保存26个字母,方便循环时通过直接下标找到相应字母 
10     for(int i = 0; i < 26; i++) {
11         a[i] = i + 65;
12     }
13     
14     out << "  ";
15     for(char c = 'a'; c <= 'z'; c++) {
16         out << " " << c;
17     }
18     out << endl;
19     
20     int x;
21     for(int i = 1; i <= 26; i++) {
22         x = 0;
23         out << setw(2) << i;
24         int j = i % 26;
25         for(; x < 26; ) {
26             out << " " << a[j];
27             if(j == 25) j = 0;
28             else j++;
29             x++;
30         }
31         out << endl;
32     }
33 }
34 int main() {
35     output(cout);
36     ofstream out("cipher_key.txt"); 
37     
38     if(!out.is_open()) {
39         cout << "fail to open the file\n";
40         return 1;
41     } 
42     output(out);
43     
44     out.close();
45     return 0;
46 }

运行截图

 

 

 

posted @ 2022-12-07 00:06  尤夏  阅读(14)  评论(0编辑  收藏  举报