实验六 模板类和文件IO
Task 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 // 把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块的方式写入文件data1.dat 19 out.write(reinterpret_cast<char*>(&x), sizeof(x)); 20 out.close(); 21 }
-
task3_2.cpp
1. 源码
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关联的文件data.dat中读取sizeof(x)字节数据写入&x开始的地质单元 18 in.read(reinterpret_cast<char*>(&x), sizeof(x)); 19 in.close(); 20 21 for (auto i = 0; i < N; ++i) 22 cout << x[i] << ", "; 23 cout << "\b\b \n"; 24 }
2. 运行结果截图
3. 修改数组类型从int类型修改成char类型后运行结果如下:
分析如下:
(1)read函数原型为:
istream& read (char* s, streamsize n); //用来暂存内容的数组(必须是char*型),以及流的长度
(2)reinterpret_cast:是四种强制转换中功能最为强大的,它可以暴力完成两个完全无关类型的指针之间或指针和数之间的互转,比如用char类型指针指向double值。它对原始对象的位模式提供较低层次上的重新解释(即reinterpret),完全复制二进制比特位到目标对象,转换后的值与原始对象无关但比特位一致,前后无精度损失。int型是4个字节,char型式1个字节,所以int型转成char型时多余的三个字节转成空格。
(3)data1.dat里面的内容有空格,将文件内容读入时,会将空格读入,导致上面的运行结果。
(4)验证:将data1.dat里的空格删掉,得到运行结果如下:
Task 4
-
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 #include<iostream> 3 #include<cassert> 4 5 using std::cout; 6 using std::cin; 7 8 template<typename T> 9 class Vector { 10 public: 11 Vector(int n) :size{ n } { 12 p = new T[n]; 13 } 14 Vector(int n, T value) { 15 size = n; 16 p = new T[n](); 17 for (int i = 0; i < size; ++i) { 18 p[i] = value; 19 } 20 } 21 Vector(const Vector& v) { // 深复制 22 size = v.size; 23 p = new T[v.size]; 24 for (int i = 0; i < size; ++i) { 25 p[i] = v.p[i]; 26 } 27 } 28 ~Vector() { delete[]p; } 29 30 int get_size()const { return size; } 31 32 T& at(int index) { 33 assert(index >= 0 && index < size); 34 return p[index]; 35 } 36 37 T& operator[](int index) { 38 assert(index >= 0 && index < size); 39 return p[index]; 40 } 41 template<typename T1> 42 friend void output(const Vector<T1>& v); 43 private: 44 int size; 45 T* p; 46 }; 47 48 // friend fuction 友元函数 49 template<typename T1> 50 void output(const Vector<T1>& v) { 51 for (int i = 0; i < v.size; ++i) 52 cout << v.p[i] << ", "; 53 cout << "\b\b \n"; 54 }
-
测试截图
1. 原测试数据
2.更改测试代码如下:
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<char> x1(n); 11 for (auto i = 0; i < n; ++i) 12 x1.at(i) = i + 'a'; 13 14 output(x1); 15 16 Vector<int> x2(n, 88); 17 Vector<int> x3(x2); 18 19 output(x2); 20 output(x3); 21 22 x2.at(2) = 99; 23 cout << "x2: "; 24 output(x2); 25 cout << "x3: "; 26 output(x3); 27 28 x3[0] = 999; 29 cout << "x3: "; 30 output(x3); 31 cout << "x2: "; 32 output(x2); 33 } 34 35 int main() { 36 test(); 37 }
3. 测试结果:
Task 5
-
task5.cpp
1 #include<fstream> 2 #include<iostream> 3 #include<iomanip> 4 5 using std::endl; 6 using std::setw; 7 using std::ofstream; 8 9 void output(std::ostream& out) { 10 out << " "; 11 char n = 'a'; 12 13 // 输入第一行 14 for (int i = 1; i <= 26; ++i, ++n) { 15 out << setw(2) << n; 16 } 17 out << endl; 18 19 // 输入后面的密码信息 20 for (int i = 1; i <= 26; ++i) { 21 out << setw(2) << i; 22 if (n == 'Z') 23 n = 'A'; 24 else 25 n = i + 'A'; 26 for (int j = 1; j <= 26; ++j, ++n) { 27 out << setw(2) << n; 28 if (n == 'Z') 29 n = 'A' - 1; 30 } 31 out << endl; 32 } 33 } 34 35 int main() { 36 output(std::cout); 37 ofstream out; 38 out.open("cipher_key.txt"); 39 if (!out.is_open()) { 40 std::cout << "fail to open file cipher_key.txt.\n"; 41 return 1; 42 } 43 output(out); 44 out.close(); 45 }
-
运行结果截图