yy-L886

导航

实验六 模板和文件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;
}

运行测试截图

 

 

 

posted on 2022-12-06 16:47  小猪葛屁了  阅读(12)  评论(0编辑  收藏  举报