220011wan

导航

实验六

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 
 6 int main() {
 7     using namespace std;
 8     array<char, 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 }

 

 int占4个字节,char占1个字节

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 template<typename T>
 5 class Vector
 6 {
 7 public:
 8     Vector(T n):size(n)
 9     {
10         p = new T[n];
11     }
12     Vector(T n, T value):size(n)
13     {
14         p = new T[n];
15         for (int i = 0; i < size; i++)
16         {
17             p[i] = value;
18         }
19     }
20     Vector(const Vector<T>& r):size(r.size)
21     {
22         p = new int[size];
23         for (int i = 0; i < size; i++)
24             p[i] = r.p[i];
25     }
26     ~Vector()
27     {
28         delete[] p;
29     }
30     T& at(int i)
31     {
32         return p[i];
33     }
34     T get_size()
35     {
36         return size;
37     }
38     friend void output(Vector<T>& x)
39     {
40         for (int i = 0; i < x.size; i++)
41             cout << x.p[i] << " ";
42         cout << endl;
43     }
44     T& operator[](int i)
45     {
46         return p[i];
47     }
48 private:
49     T size;
50     T* p;
51 };
 1 #include <iostream>
 2 #include "Vector.h"
 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.5;
13 
14     output(x1);
15 
16     Vector<int> x2(n, 66);
17     Vector<int> x3(x2);
18 
19     output(x2);
20     output(x3);
21 
22     x2.at(0) = 88;
23     output(x2);
24 
25     x3[0] = 99;
26     output(x3);
27 }
28 
29 int main() {
30     test();
31 }

 

 

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<fstream>
 4 using namespace std;
 5 void output(std::ostream& out)
 6 {
 7    
 8     for (int i = 0; i < 27; i++)
 9     {
10         if (!i)
11         {
12             out << "   ";         
13             for (int j = 97; j < 123; j++)
14             {
15                 out << char(j) << " ";              
16             }
17             out << endl;
18             continue;
19         }
20         out << std::left << setw(2) << i << " ";      
21         for (int j = i; j < i + 26; j++)
22         {
23             out << std::left << setw(2) << char(65 + j % 26);
24         }
25         out << endl;        
26     }   
27 }
28 int main()
29 {
30     output(cout);
31     ofstream out;
32     out.open("cipher_key.txt");
33     if (!out.is_open()) {
34         cout << "fail to open file cipher_key.txt to write" << endl;
35         return 0;
36     }
37     output(out);
38     out.close();
39 }

 

 

 

posted on 2022-12-01 15:45  或者明天  阅读(16)  评论(0编辑  收藏  举报