task3:
task3.1
源码:
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{ 97,98,99,100,101 }; 8 ofstream out; 9 out.open("data1.dat", ios::binary); 10 if (!out.is_open()) { 11 cout << "fail to open data1.dat" << endl; 12 return 1; 13 } 14 out.write(reinterpret_cast<char*>(&x), sizeof(x));//将从地址&X开始连续sizeof(X)个字节的数据块以字节数据块的方式写入data1.txt 15 out.close(); 16 }
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.daat" << endl; 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" << endl; 19 }
运行截图:
task4:
vector.hpp
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 template<typename T> 5 class vector { 6 int size; 7 T *p; 8 public: 9 vector(){} 10 vector(int size0) { 11 size = size0; 12 p = new T[size]; 13 } 14 vector(int size0, T value) { 15 size = size0; 16 p = new T[size]; 17 for (auto i = 0; i < size; i++) 18 p[i] = value; 19 } 20 vector(const vector& obj) { 21 size = obj.size; 22 p = new int[size]; 23 for (int i = 0; i < size; i++) 24 p[i] = obj.p[i]; 25 } 26 ~vector() { delete[]p; } 27 T& at(int i) { return p[i]; } 28 int get_size() const { return size; } 29 T& operator[](int X) { 30 return p[X]; 31 } 32 friend void output(vector& V) { 33 for (int i = 0; i < V.size; i++) 34 cout << V.p[i] << " "; 35 cout << endl; 36 } 37 };
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 }
截图
task5
1 #include<iostream> 2 #include<iomanip> 3 #include<fstream> 4 using namespace std; 5 6 void output(ostream& out) { 7 char a[26][26], i; 8 cout << " "; 9 out << " "; 10 for (i = 'a'; i <= 'z'; i++) 11 { 12 cout << setw(2) << setfill(' ') << i; 13 out << setw(2) << setfill(' ') << i; 14 } 15 cout << endl; out << endl; 16 for (int j = 0; j < 26; j++) 17 { 18 cout << setw(2) << setfill(' ') << j + 1; 19 out << setw(2) << setfill(' ') << j + 1; 20 for (int n = 0; n < 26; n++) 21 { 22 a[j][n] = 'A' + char(j + n + 1) % 26; 23 cout << setw(2) << setfill(' ') << a[j][n]; 24 out << setw(2) << setfill(' ') << a[j][n]; 25 } 26 cout << endl; out << endl; 27 } 28 } 29 30 int main() { 31 ofstream out; 32 out.open("cipher_key.txt", ios::out); 33 if (!out.is_open()) 34 { 35 cout << "fail to open!" << endl; 36 return 0; 37 } 38 output(out); 39 out.close(); 40 }
标签:cout,int,vector,实验,output,include,size From: https://www.cnblogs.com/zzzys/p/16939209.html