实验6.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 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 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 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.read(reinterpret_cast<char *>(&x), sizeof(x)); 18 in.close(); 19 20 for(auto i = 0; i < N; ++i) 21 cout << x[i] << ", "; 22 cout << "\b\b \n"; 23 }
用int整型存储每个数字占4个字节;存在文本文件中4个字节为一个单位,而给的数字仅需一个字节存储,剩下的三个字节为空,task2以字节形式读入,读入5个字节。
task4
Vector.hpp
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 template<typename T> 5 class Vector { 6 public: 7 Vector(int n) ; 8 Vector(int n, T value); 9 Vector(const Vector<T>& v); 10 ~Vector(){delete []p;} 11 int get_size() { return size; } 12 T &at(int i); 13 T &operator[](int i); 14 template<typename T1> 15 friend void output(Vector<T1> &C); 16 private: 17 int size; 18 T* p; 19 }; 20 template<typename T> 21 Vector<T>::Vector(int n){ 22 size=n; 23 p = new T[size]; 24 } 25 template<typename T> 26 Vector<T>::Vector(int n, T value) :size{n} { 27 p = new T[size]; 28 for (int i = 0; i < size; i++) 29 p[i] = value; 30 } 31 template<typename T> 32 Vector<T>::Vector(const Vector<T>& C) : size{ C.size } { 33 p= new T[size]; 34 for (int i = 0; i < size; i++) 35 p[i] = C.p[i]; 36 } 37 template<typename T> 38 T &Vector<T>::at(int i){ 39 return p[i]; 40 } 41 template<typename T> 42 T &Vector<T>::operator[](int i){ 43 return p[i]; 44 } 45 template<typename T1> 46 void output( Vector<T1> &C) { 47 for (int i = 0; i < C.size; i++) 48 cout << C[i] << ", "; 49 cout << "\b\b \n"; 50 }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, 6); 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] = 909; 26 output(x3); 27 } 28 29 int main() { 30 test(); 31 }
task5.cpp
1 #include<iostream> 2 #include <fstream> 3 #include <iomanip> 4 using namespace std; 5 void output(std::ostream &out); 6 int main(){ 7 ofstream fout; 8 fout.open("cipher_key.txt"); 9 if(!fout.is_open()){ 10 cout<<"fail to open file "<<"cipher_key"<<endl; 11 } 12 output(fout); 13 fout.close(); 14 output(cout); 15 16 } 17 void output(std::ostream &out){ 18 char code[26]; 19 char Code[26]; 20 for(int i=0;i<26;i++){ 21 code[i]='a'+i; 22 } 23 out<<" "; 24 for(int i=0;i<26;i++){ 25 out<<code[i]<<" "; 26 } 27 out<<"\n"; 28 for(int i=1;i<=26;i++){ 29 for(int t=0;t<26;t++){ 30 31 if(t+i<=25){ 32 Code[t]='A'+t+i; 33 } 34 else if(t+i>25){ 35 Code[t]='A'+t+i-25-1; 36 } 37 } 38 out<<setw(2)<<i<<" "; 39 for(int t=0;t<26;t++){ 40 out<<Code[t]<<" "; 41 } 42 out<<"\n"; 43 44 } 45 }
标签:int,Vector,实验,template,output,include,size From: https://www.cnblogs.com/gzj035/p/16947219.html