实验任务3
task3_1
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{ 97,98,99,100,101 }; 9 10 ofstream out; 11 out.open("data1.dat", ios::binary); 12 if (!out.is_open()) { 13 cout << "fail to open data1.dat\n"; 14 return 1; 15 } 16 17 //把从地址&x开始连续sizeof(x)个字节数据块写入文件data1.txt 18 out.write(reinterpret_cast<char*>(&x), sizeof(x)); 19 out.close(); 20 }
task3_2
1 #include<iostream> 2 #include<fstream> 3 #include<array> 4 #define N 5 5 int main() { 6 using namespace std; 7 array<int, N> x; 8 ifstream in; 9 in.open("data1.dat", ios::binary); 10 if (!in.is_open()) { 11 cout << "fail to open data1.dat\n"; 12 return 1; 13 } 14 in.read(reinterpret_cast<char* >(&x), sizeof(x)); 15 in.close(); 16 for (auto i = 0; i < N; i++) 17 cout << x[i] << ", "; 18 cout << "\b\b \n"; 19 }
运行测试截图:
将array<int,N> x;修改成array<char,N> x;运行结果为:
实验任务4
Vector.hpp
1 #pragma once 2 #include<iostream> 3 4 using namespace std; 5 6 template<typename T> 7 class Vector { 8 public: 9 Vector(int n) :size{ n } { p = new T[n]; } 10 Vector(int n, T value); 11 Vector(const Vector<T>& v); 12 ~Vector() { delete p; } 13 T get_size() { return size; } 14 T& at(int index) const {return p[index]; } 15 T& operator[](int i)const { return p[i]; } 16 template<typename T1> 17 friend void output(const Vector<T1>& v); 18 private: 19 T* p; 20 int size; 21 }; 22 template<typename T> 23 Vector<T>::Vector(int n, T value):size(n) { 24 p = new T[n]; 25 for (auto i = 0; i < n; i++) 26 p[i] = value; 27 } 28 template<typename T> 29 Vector<T>::Vector(const Vector<T>& v):size(v.size) { 30 p = new T[size]; 31 for (auto i = 0; i < size; i++) { 32 p[i] = v.p[i]; 33 } 34 } 35 template<typename T1> 36 void output(const Vector<T1>& v) { 37 for (auto i = 0; i < v.size; i++) 38 cout << v.p[i] << ", "; 39 cout << "\b\b \n"; 40 }
task4.cpp
1 #include <iostream> 2 #include "Vector.hpp" 3 void test() { 4 using namespace std; 5 int n; 6 cin >> n; 7 Vector<double> x1(n); 8 for (auto i = 0; i < n; ++i) 9 x1.at(i) = i * 0.7; 10 output(x1); 11 Vector<int> x2(n, 42); 12 Vector<int> x3(x2); 13 output(x2); 14 output(x3); 15 x2.at(0) = 77; 16 output(x2); 17 x3[0] = 999; 18 output(x3); 19 } 20 int main() { 21 test(); 22 }
运行测试结果:
改变测试数据,运行结果:
实验任务5
task5.cpp
1 #include<iostream> 2 #include<fstream> 3 #include<iomanip> 4 #include<string> 5 6 using namespace std; 7 8 void output(ostream& out) { 9 10 string a[27][27]= {" "}; 11 12 for (int i = 1; i < 27; i++) { 13 a[0][i] = 96 + i; 14 a[i][0] = to_string(i); 15 } 16 int k = 1; 17 for (int i = 1; i < 27; i++) { 18 19 for (int j = 1; j < 27; j++) { 20 a[i][j] = 65 + ((65 + k) % 65) % 26; 21 k++; 22 } 23 k = i + 1; 24 } 25 for (int i = 0; i < 27; i++) { 26 for (int j = 0; j < 27; j++) { 27 cout << right << setw(2) << a[i][j]; 28 out << right << setw(2) << a[i][j]; 29 } 30 cout << endl; 31 out << endl; 32 } 33 } 34 int main() { 35 ofstream outfile; 36 outfile.open("cipher_key.txt"); 37 if (!outfile.is_open()) { 38 cout << "fail to open cipher_key.txt\n"; 39 return 0; 40 } 41 output(outfile); 42 outfile.close(); 43 }
运行测试结果:
标签:文件,27,int,++,Vector,实验,include,模板,size From: https://www.cnblogs.com/zxy0324/p/16950478.html