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文件一直大小写字母对不齐,于是我换了一个字体就成功了
标签:int,Vector,实验,oop,template,output,include,模板,size From: https://www.cnblogs.com/abcdefg-gaoyuan/p/16943037.html