實驗六 模板類和檔案
阿新 • • 發佈:2022-12-03
實驗三
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 namespacestd; 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 }