实验六

task3

测试结果:

 

 

 

 

 

 

源代码:

 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 }
 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     array<char,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.read(reinterpret_cast<char *>(&x), sizeof(x));
19     in.close();
20 
21     for(auto i = 0; i < N; ++i)
22         cout << x[i] << ", ";
23     cout << "\b\b \n";
24     return 0;
25 }

 原因: 当array数组储存字符型时,读入文件内容”a b c d e f“将把空格也读成字符。

task4

测试结果:

 

源代码:

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

 

task5

测试结果:

 

 

源代码:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <iomanip>
 4 using namespace std;
 5 
 6 int main(int argc, char const *argv[])
 7 {
 8     ofstream outFile;
 9     outFile.open("cipher_key.txt");
10     if (!outFile.is_open())
11     {
12         cout << "fail to open data1.dat\n";
13         return 1;
14     }
15     for (int i = 0; i < 27; i++)
16     {
17         if (!i)
18         {
19             cout << "   ";
20             outFile << "   ";
21             for (int j = 97; j < 123; j++)
22             {
23                 cout << char(j) << " ";
24                 outFile << char(j) << " ";
25             }
26             cout << endl;
27             outFile << endl;
28             continue;
29         }
30         cout << setiosflags(ios_base::left) << setw(2) << i << " ";
31         outFile << setiosflags(ios_base::left) << setw(2) << i << " ";
32         for (int j = i; j < i + 26; j++)
33         {
34             cout << setiosflags(ios_base::left) << setw(2) << char(65 + j % 26);
35             outFile << setiosflags(ios_base::left) << setw(2) << char(65 + j % 26);
36         }
37         cout << endl;
38         outFile << endl;
39     }
40     outFile.close();
41     return 0;
42 }

 

posted @ 2022-12-06 23:56  【VC】熬夜不吃饭的兔子  阅读(17)  评论(0编辑  收藏  举报