实验六

试验任务三

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     out.write(reinterpret_cast<char*>(&x), sizeof(x));
19     out.close();
20 }

 

task3_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 }

 

 试验任务四

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 }

Vector.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 template<typename T>
 8 
 9 class Vector {
10 public:
11     Vector(int a) :n{ a } { p = new T[n](); }
12     Vector(int a, T value) :n{ a } { p = new T[n](); for (int i = 0; i < n; i++)p[i] = value; }
13     Vector(const Vector<T>& obj) :n{ obj.n } { p = new T[n](); for (int i = 0; i < n; i++)p[i] = obj.p[i]; }
14     ~Vector() { delete p; }
15 
16     int get_size();
17     T& at(int i);
18 
19     template<typename T1>
20     friend void output(Vector<T1> t);
21 
22     T& operator[](int i) { return p[i]; }
23 private:
24     int n;
25     T* p;
26 };
27 
28 template<typename T>
29 int Vector<T>::get_size() {
30     return n;
31 }
32 template<typename T>
33 T& Vector<T>::at(int i) {
34     return p[i];
35 }
36 
37 template<typename T1>
38 void output(Vector<T1>t) {
39     for (int i = 0; i < t.n; i++)
40         cout << t.p[i] << ",";
41     cout << endl;
42 }

 

 试验任务五

task_6.cpp

 1 #include<iostream>
 2 #include<fstream>
 3 #include<iomanip>
 4 #define N 30
 5 using namespace std;
 6 
 7 void outline(int i) {
 8     char a[N] = "";
 9     cout << setw(2)<<i << " ";
10     for (int j = 1; j <= 26; j++) {
11         a[j] = (char)((i + j - 1) % 26 + 'A');
12         cout << a[j] << " ";
13     }
14     cout << endl;
15 }
16 void output(ostream &out) {
17     char a[N] = "";
18     for (int i = 1; i <= 26; i++) {
19         outline(i);
20     }
21     
22 }
23 
24 int main() {
25     ofstream out;
26     out.open("cipher_key.txt");
27     char a[N] = "";
28     cout << "   ";
29     for (int i = 1; i <= 26; i++) {
30         a[i] = (char)('a' + i - 1);
31         cout << a[i] << " ";
32     }
33     cout << endl;
34     output(out);
35     return 0;
36 }

 

posted @ 2022-12-06 22:40  Z`x  阅读(11)  评论(0编辑  收藏  举报