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

task3_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     //把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入data1.txt
19     out.write(reinterpret_cast<char *>(&x), sizeof(x));
20     out.close();   
21 } 

task3_2:

 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;
10     
11     ifstream in;
12     in.open("data1.dat", ios::binary);
13     if(!in.is_open()) {
14         cout << "fail to open data1.dat\n";
15         return 1;
16     }
17     
18     //从文件流对象in关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元
19     in.read(reinterpret_cast<char *>(&x), sizeof(x));
20     in.close();
21     
22     for(auto i = 0; i < N; ++i) 
23         cout << x[i] << ", ";
24     cout << "\b\b \n"; 
25 }

 

 修改后:

 

 分析:

char型占1个字节,而int型占4个字节,导致输出3个空格且b出现在第5个位置

 

task4.hpp:

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

task4.cpp:

 1 #include <iostream>
 2 #include "实验六task4_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 }

 

 

task5

 1 #include <iostream>
 2 #include <fstream>
 3 #include <iomanip>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 void put(std::ostream &out, int n, char a) {
 9     char A = 'A';
10     int i = 0;
11     cout << n;
12     out << n;
13     for(;a <= 'Z'; a++)
14     {
15        cout << setw(2) << a;
16        out << setw(2) << a;
17        i++;    
18     }
19     for(;i < 26; i++, A++)
20     {
21        cout << setw(2) << A;
22        out << setw(2) << A;
23     }
24 }
25 
26 void output(std::ostream &out) {
27     char k = 'a', K = 'B';
28     cout << "  ";
29     out << "  ";
30     for(;k <= 'z'; k++)
31     {
32        cout << setw(2) << k;
33        out << setw(2) << k;    
34     }
35     cout << endl;
36     out << endl;
37     for(int i = 1; i <= 26; i++, K++) {
38         if(i < 10)
39            cout << " ";
40            out << " ";
41         put(out,i,K);
42         cout << endl;
43         out << endl;
44     }
45     
46 }
47 
48 int main() {
49     ofstream out;
50     
51     out.open("cipher_key.txt", ios::out);
52     if(!out.is_open()) {
53         cout << "fail to open chipher_key\n";
54         return 1;
55     }
56     output(out);
57     out.close();
58 }

 

 

 

posted @ 2022-12-04 21:14  .木枝  阅读(32)  评论(0)    收藏  举报