实验六

任务四
task4.cpp
 1 #include <iostream>
 2 #include "vector.hpp"
 3 void test() 
 4 {
 5     using namespace std;
 6     int n;
 7     cin >> n;
 8     
 9     Vector<double> x1(n);
10     for (auto i = 0; i < n; ++i)
11         x1.at(i) = i * 0.7;
12 
13     output(x1);
14 
15     Vector<int> x2(n, 42);
16     Vector<int> x3(x2);
17 
18     output(x2);
19     output(x3);
20 
21     x2.at(0) = 77;
22     output(x2);
23 
24     x3[0] = 999;
25     output(x3);
26 }
27 
28 int main()
29 {
30     test();
31 }
View Code
Vector.hpp
 1 #include<iostream>
 2 #include<stdexcept>
 3 #include <cassert>
 4 using namespace std;
 5 template<typename T>
 6 class Vector
 7 {
 8 private:
 9     int size;
10     T* ptr;
11 public:
12     
13     Vector(int size)
14     {
15         if(size<0)
16             throw std::length_error("vector constructor:negative size"); 
17         this->size = size;
18         ptr = new T[this->size];
19     }
20     Vector(int size, T value)
21     {
22         if(size<0)
23             throw std::length_error("vector constructor:negative size");
24         this->size = size; ptr = new T[size];
25         for (auto i = 0; i < size; i++)
26         {
27             ptr[i] = value;
28         }
29 
30     }
31     Vector(const Vector<T>& x)
32     {
33         size = x.size;
34         ptr = new T[size];
35         for (auto i = 0; i < size; i++)
36         {
37             ptr[i] = x.ptr[i];
38         }
39     }
40     ~Vector()
41     {
42         delete[] ptr;
43     }
44     int get_size() { return size; }
45     T& at(int i)
46     {
47         assert(i >= 0 && i < size);
48         return(ptr[i]);
49     }
50     T& at(int i) const
51     {
52         assert(i >= 0 && i < size);
53         return(ptr[i]);
54     }
55      T& operator[](int i)
56     {
57         return ptr[i];
58     }
59      friend void output(Vector<T> x)
60      {
61          for (auto i = 0; i < x.get_size(); i++)
62              cout << x.at(i) << ",";
63          cout << "\b\b\n";
64      }
65 };
View Code

任务五

task5.cpp

 1 #include<fstream> 
 2 #include<iostream>
 3 #include<stdexcept>
 4 #include<string>
 5 #include <iomanip>
 6 using namespace std;
 7 void output(ostream &out)
 8 {
 9     int i,j;
10     string a[27][27];
11     for( i=0;i<27;i++)
12     {
13         for( j=0;j<27;j++)
14         {
15             if(i==0&&j==0)
16             {
17                 a[i][j]=' ';
18                 out<<a[i][j];
19             }
20             else if(i==0)
21             {
22                 a[i][j]='a'+j-1;
23                 out<<a[i][j];
24             }
25             else if(j==0)
26             {
27                 a[i][j]='1'+i-1;
28                 out<<i; 
29             }
30             else
31             {
32                 a[i][j]='A'+(i+j+26)%26;
33                 out<<a[i][j];
34             }
35         }
36     }
37 }
38 int main()
39 {
40     cout<<setiosflags(ios::left);
41     int i,j;
42     string a[27][27];
43     for( i=0;i<27;i++)
44     {
45         for( j=0;j<27;j++)
46         {
47             if(i==0&&j==0)
48             {
49                 a[i][j]=' ';
50                 cout<<a[i][j]<<"  ";
51             }
52             else if(i==0)
53             {
54                 a[i][j]='a'+j-1;
55                 cout<<a[i][j]<<"  ";
56             }
57             else if(j==0)
58             {
59                 a[i][j]='1'+i-1;
60                 cout<<i<<"\t\b\b\b\b\b"; 
61             }
62             else
63             {
64                 a[i][j]='A'+(i+j+26)%26;
65                 cout<<a[i][j]<<"  ";
66             }
67         }
68         cout<<endl;
69     }
70     cout<<endl;
71     ofstream out;
72     out.open("cipher_key.txt");
73     if(!out.is_open()) 
74     {
75         std::cout << "fail to open file " << std::endl;
76         return 0;
77     }
78     output(out);
79     out.close();
80 
81 }
View Code

 

 

posted @ 2023-12-13 20:23  dmsx  阅读(35)  评论(0)    收藏  举报