实验任务3
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\n"; 12 return 1; 13 } 14 // 把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入文件data1.txt 15 out.write(reinterpret_cast<char*>(&x), sizeof(x)); 16 out.close(); 17 }task3_1.cpp
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关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元 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 cout << "\b\b \n"; 20 }task3_2.cpp
测试截图:
修改成:array<char,N>x;后的截图:
原因分析:
由于char类型占一个字节,而int类型占四个字节。故当以int类型读取时,每次读取四个字节的内容,因而能正常输出97,98,99,100,101;当以char类型读取时,由于一次读取一个字节的内容,故第一此输出的是ASCII码97对应的字符'a',后三次输出为空,直至四个字节读取完,再读取ASCII98对应的字符'b',因而输出为a, , , , b。见下示意图。
实验任务4
1 #pragma once 2 #include<iostream> 3 template<typename T> 4 class Vector { 5 public: 6 Vector(int n, T v); 7 Vector(int n) :size{ n } { value = new T[size]; } 8 Vector(const Vector<T>& v0); 9 ~Vector() = default; 10 int get_size()const { return size; } 11 T& at(int i)const { return value[i]; } 12 T& operator[](int i) const { return value[i]; } 13 template<typename T1> 14 friend void output(const Vector<T1>& v0); 15 private: 16 int size; 17 T* value; 18 }; 19 template<typename T> 20 Vector<T>::Vector(int n, T v0) :size{ n } { 21 value = new T[size]; 22 for (int i = 0; i < size; i++) 23 value[i] = v0; 24 } 25 template<typename T> 26 Vector<T>::Vector(const Vector<T>& v0) : size{ v0.size } { 27 value = new T[size]; 28 for (int i = 0; i < size; i++) 29 value[i] = v0.value[i]; 30 } 31 template<typename T1> 32 void output(const Vector<T1>& v0) { 33 for (int i = 0; i < v0.size; i++) 34 std::cout << v0[i] << ", "; 35 std::cout << "\b\b \n"; 36 }Vector.hpp
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 }task4.cpp
测试截图:
实验任务5
1 #include<fstream> 2 #include<iostream> 3 #include<iomanip> 4 #define N 27 5 using namespace std; 6 char alph[N][N]; 7 void output(ostream& out) { 8 for (int i = 0; i < N; i++) { 9 if (i == 0) cout << " "; 10 else cout << setw(2) << alph[i][0] - '0'; 11 for (int j = 1; j < N; j++) { 12 cout << setw(2) << alph[i][j]; 13 } 14 cout << endl; 15 } 16 for (int i = 0; i < N; i++) { 17 if (i == 0) out << " "; 18 else out << setw(2) << alph[i][0] - '0'; 19 for (int j = 1; j < N; j++) { 20 out << setw(2) << alph[i][j]; 21 } 22 out << endl; 23 } 24 } 25 int main() { 26 ofstream out; 27 out.open("cipher_key.txt"); 28 if (!out.is_open()) { 29 cout << "fail to open the cipher_key.txt\n"; 30 return 1; 31 } 32 alph[0][0] = ' '; 33 for (int i = 1; i < N; i++) { 34 alph[0][i] = 'a' + i - 1; 35 alph[i][0] = i + '0'; 36 } 37 int j; 38 for (int i = 1; i < N; i++) { 39 alph[1][i] = alph[0][i] - 31; 40 alph[1][N - 1] = 'A'; 41 } 42 for(int i=2;i<N;i++){ 43 char c = alph[i - 1][1]; 44 for (int j = 1; j < N - 1; j++) { 45 46 alph[i][j] = alph[i - 1][j + 1]; 47 } 48 alph[i][N - 1] = c; 49 } 50 output(out); 51 }task5.cpp
测试截图:
标签:文件,int,value,v0,Vector,实验,include,模板,size From: https://www.cnblogs.com/Terrence0403/p/16950616.html