1. 程式人生 > 其它 >實驗6 模板類和檔案

實驗6 模板類和檔案

1.實驗3

task3_1.cpp

 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 }

task_3.2.cpp

 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 }

執行結果:

 修改後的task3_1.cpp

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

執行結果

 問題:

2.實驗4

VecotrT.hpp

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

task4.cpp

 1 #include <iostream>
 2 #include "vectorT.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 }

執行結果

 3.實驗5

 task5.cpp

 1 #include <iostream>
 2 #include <fstream>
 3 #include<istream>
 4 #include<iomanip>
 5 using namespace std;
 6  void output(std::ofstream &out)
 7  {       
 8          out.open("cipher_key.txt",ios::app);
 9          out<<setw(4);
10          cout<<setw(4);
11          for(char a='a';a<='z';a++)
12          {
13               out<<a<<" ";
14               cout<<a<<" ";
15          }
16          out<<endl;
17          cout<<endl;
18          for(int i=1;i<=26;i++)
19          {
20              out<<setw(2)<<i;
21              cout<<setw(2)<<i;
22              int c;
23              for(char b='A'+(i%26),c=1;c<=26;b++,c++)
24              {
25                   if(b>'Z')
26                   {
27                        b-=25;
28                   }
29                 out<<setw(2)<<b;
30                 cout<<setw(2)<<b;
31              }
32              out<<endl;
33              cout<<endl;
34          }
35          out.close();
36  }
37 int main() {
38     ofstream out;
39     output(out);
40 }

 執行結果