实验三
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\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.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关联的文件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 }
array<int, n=""> x; ----> 修改成: array<char, n=""> x
读取97的ascii码为97,int型占四个字节,<int,5>一共占20个字节,<char,5>占五个字节,所以先输出97对应的a,然后空出三个字节的空再读取下一个98ascii码对应的字母b.
实验四
Vector.hpp
1 #include<cassert> 2 using namespace std; 3 template<typename T> 4 class Vector{ 5 public: 6 Vector(int n) { 7 p=new T[n]; 8 size = n; 9 } 10 Vector(int n,T value):size{n}{ 11 p=new T[size]; 12 for(int i=0;i<size;i++){ 13 p[i]=value; 14 } 15 } 16 Vector(const Vector<T> &a):size{a.size}{ 17 p=new T[size]; 18 for(int i=0;i<size;i++){ 19 p[i]=a.p[i]; 20 } 21 } 22 ~Vector(){ 23 delete p; 24 } 25 T &at(int index){ 26 assert(index>=0&&index<size); 27 return p[index]; 28 } 29 int get_size(){ 30 return size; 31 } 32 friend void output(Vector &v){ 33 int i; 34 for(i=0;i<v.size-1;i++){ 35 cout << v.p[i] << ","; 36 } 37 cout << v.p[i] << endl; 38 } 39 T& operator[](int i){ //运算符重载 40 return p[i]; 41 } 42 private: 43 T size; 44 T *p; 45 46 };
task4.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 }
实验五
1 #include<iostream> 2 #include<iomanip> 3 #include<string> 4 #include<fstream> 5 using namespace std; 6 void output(std::ostream &out) 7 { 8 char a[26][26]; 9 cout<<" "; 10 out<<" "; 11 for(char i='a';i<='z';i++) 12 { 13 cout<<setw(2)<<setfill(' ')<<i; 14 out<<setw(2)<<setfill(' ')<<i; 15 } 16 cout<<endl; 17 out<<endl; 18 for(int j=0;j<26;j++) 19 { 20 cout<<setw(2)<<setfill(' ')<<j+1; 21 out<<setw(2)<<setfill(' ')<<j+1; 22 for(int k=0;k<26;k++) 23 { 24 a[j][k]='A'+char((j+k+1)%26); 25 cout<<setw(2)<<setfill(' ')<<a[j][k]; 26 out<<setw(2)<<setfill(' ')<<a[j][k]; 27 } 28 out<<endl; 29 cout<<endl; 30 } 31 } 32 int main() 33 { 34 ofstream out; 35 out.open("cipher_key.txt",ios::out); 36 if(!out.is_open()) 37 { 38 cout<<"fail to open cipher_ke.text"<<endl; 39 return 0; 40 } 41 output(out); 42 out.close(); 43 }
标签:std,文件,int,Vector,实验,output,include,模板,size From: https://www.cnblogs.com/ljhakuna/p/16948223.html