1. 程式人生 > 其它 >實驗六 模板和檔案IO

實驗六 模板和檔案IO

task 3_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 }
#include <iostream>
#include <fstream>
#include <array>
#define N 5

int main() {
    using namespace
std; array<int, N> x; ifstream in; in.open("data1.dat", ios::binary); if(!in.is_open()) { cout << "fail to open data1.dat\n"; return 1; } in.read(reinterpret_cast<char *>(&x), sizeof(x)); in.close(); for(auto i = 0; i < N; ++i) cout
<< x[i] << ", "; cout << "\b\b \n"; }

測試截圖:

 改動後測試截圖:

 原因是97的ASCLL碼值是a,檔案裡存放的x是整型,在記憶體佔4個位元組的空間,把它轉換成char字元,a只佔1個位元組,多出來的3個位元組被後面的變數讀取,故生成了a,,,b.

task 4

#pragma once

#include<iostream>
using namespace std;
template <typename T>
class Vector{
    public:
    Vector(int n0);
    Vector(int n0,T value);
    Vector(const Vector<T> &v1);
    ~Vector();
    int get_size() const {return n ;}
    T& at(int i){return *(p+i);}
    T& operator[](int i){return *(p+i);}
    template<typename T1>
    friend void output( Vector<T1> &x);

    private:
        int n;
        T *p;
};
template <typename T>
Vector<T>::Vector(int n0)
{
    n=n0;
    p=new T[n];
    //cout<<"沒有初始值的建構函式被呼叫";

}
template <typename T>
Vector<T>::Vector(int n0,T value)
{
    n=n0;
    p=new T[n];
    for(int i=0;i<n;i++)
    *(p+i)=value;
    //cout<<"有初始值的建構函式被呼叫";
}
template <typename T>
Vector<T>::Vector(const Vector<T>& v1)
{n=v1.n;
p=new T[n];
for(int i=0;i<n;i++)
p[i]=v1.p[i];
}
template <typename T>
Vector<T>::~Vector()
{
    delete [] p;
}
    template<typename T1>
void output(Vector<T1>&x)
{for(int i=0;i<x.n;i++)
cout<<x.at(i)<<",";
cout<<endl;
}
 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, 42);
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] = 999;
26     output(x3);
27 }
28 
29 int main() {
30     test();
31 }

執行測試截圖

 task 5

#include<iomanip>
using namespace std;

void output(ostream &out){
    for(int i = 0;i <= 26; i++){
        if(i==0) out << "  ";
        else out << setw(2) << i;
        for(int j = 0; j < 26; j++){
            if(i==0){
                out << " " << (char)('a'+j);
            } 
            else{
                if('A'+i+j<='Z') out << " " << (char)('A'+i+j);
                else out << " " << (char)('A'+i+j-26);
            }
        }
        out << endl;
    }
}
int main(){
    ofstream out;
    output(cout);
    out.open("cipher_key.txt");
    if(!out.is_open()) cout << "error!\n";
    output(out);
    out.close();
    
    
    return 0;
}

執行測試截圖