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

oop 實驗6 模板類和檔案I/O

task3

程式原始碼

task3_1.cpp

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <iostream>
 3 #include <fstream>
 4 #include <array>
 5 #define N 5
 6 using namespace std;
 7 
 8 
 9 int main() {
10     array<int, N> x{ 97, 98, 99, 100, 101 };
11     ofstream out;
12     out.open("data1.txt
", ios::binary); 13 if (!out.is_open()) { 14 cout << "fail to open data" << endl; 15 return 1; 16 } 17 out.write(reinterpret_cast<char*>(&x), sizeof(x)); 18 out.close(); 19 }

task3_2.cpp

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <iostream>
 3
#include <fstream> 4 #include <array> 5 #define N 5 6 using namespace std; 7 int main() { 8 array<char, N> x; 9 ifstream in; 10 in.open("data1.txt", ios::binary); 11 if (!in.is_open()) { 12 cout << "fail to open data.dat\n"; 13 return 1;
14 } 15 in.read(reinterpret_cast<char*>(&x), sizeof(x)); 16 in.close(); 17 for (auto i = 0; i < N; i++) { 18 cout << x[i] << ","; 19 } 20 cout << "\b\b\n"; 21 return 0; 22 }

程式執行截圖:

因為char是一個位元組,而int為4個位元組,char相對少了3個位元組,空出來留給下一次讀取。

task4

程式原始碼:

Vector.hpp

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

task4.cpp

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <iostream>
 3 #include "Vector.hpp"
 4 using namespace std;
 5 
 6 void test() {
 7     int n;
 8     cin >> n;
 9     Vector<double> x1(n);
10     for (auto i = 0; i < n; i++) {
11         x1.at(i) = i * 0.7;
12     }
13     Vector<int> x2(n, 42);
14     Vector<int> x3(x2);
15     output(x2);
16     output(x3);
17     x2.at(0) = 77;
18     output(x2);
19     x3[0] = 999;
20     output(x3);
21 
22 }
23 
24 int main() {
25     test();
26     return 0;
27 }

程式執行截圖:

 task5

程式原始碼

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <iostream>
 3 #include <fstream>
 4 #include <iomanip>
 5 using namespace std;
 6 void output(ostream& out) {
 7     cout << "  ";
 8     for (int i = 97; i < 97 + 26; i++) {
 9         cout << char(i) << " ";
10     }
11     cout << endl;
12     for (int i = 1; i <= 26; i++) {
13         cout << setw(2) << i ; 
14         for (int j = 65 + i; j <= 65 + 25; j++) {
15             cout << char(j) << " ";
16         }
17         for (int k = 65; k < 65 + i; k++) {
18             cout << char(k) << " ";
19         }
20         cout << endl;
21     }
22     
23     
24 
25 }
26 int main() {
27     ofstream ofs;
28     ofs.open("cipher_key.txt");
29     if (!ofs.is_open()) {
30         cout << "faile to open" << endl;
31     }
32     else {
33         ofs << setiosflags(ios::left) << setw(2) << "";
34         for (int i = 97; i < 97 + 26; i++) {
35             ofs << setiosflags(ios::left) <<setw(2) << char(i) << " ";
36         }
37         ofs << endl;
38         for (int i = 1; i <= 26; i++) {
39             ofs << setiosflags(ios::left) << setw(2) << i;
40             for (int j = 65 + i; j <= 65 + 25; j++) {
41                 ofs << setiosflags(ios::left) << setw(2) <<char(j) << " ";
42             }
43             for (int k = 65; k < 65 + i; k++) {
44                 ofs << setiosflags(ios::left) << setw(2) << char(k) << " ";
45             }
46             ofs << endl;
47         }
48 
49     }
50     ofs.close();
51     output(ofs);
52     return 0;
53 }

執行截圖

 這裡在txt檔案一直大小寫字母對不齊,於是我換了一個字型就成功了