首页 > 其他分享 >实验6 模板类和文件IO

实验6 模板类和文件IO

时间:2022-12-07 09:44:16浏览次数:44  
标签:template int length Vector 实验 IO array include 模板

实验任务3

task3_1.cpp

#include <iostream>
#include <fstream>
#include <array>
#define N 5

int main()
{
    using namespace std;

    array<int, N> x {97, 98, 99, 100, 101};

    ofstream out;
    out.open("data1.dat", ios::binary);
    if (!out.is_open())
    {
        cout << "fail to open data1.dat" << endl;
        return 1;
    }

    out.write(reinterpret_cast<char *>(&x), sizeof(x));
    out.close();
}

task3_2.cpp

#include <iostream>
#include <fstream>
#include <array>
#define N 5

int main()
{
    using namespace std;

    array<int, N> x;
    //array<char, N> x;

    ifstream in;
    in.open("data1.dat", ios::binary);
    if(!in.is_open())
    {
        cout << "fail to open data1.dat" << endl;
        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";
}

测试:

改动后两词结果不同,类型发生变化,后面line21行,还有一个操作是强制类型转化为char*

实验任务4

Vector.hpp

#pragma once

#include <iostream>
using namespace std;

template<typename T>
class Vector {
private:
    int length;
    T *array;

public:
    Vector(int n);
    Vector(int n, T value);
    Vector(const Vector<T> &X);
    ~Vector();

    T & at(int i) const ;
    int get_size() const ;
    T & operator[](int i); // 运算符[]重载为模板类

    template<typename T1>
    friend void output(const Vector<T1> &Y);
};

template<typename T>
Vector<T>::Vector(int n) : length(n)
{
    array = new T[length];
}

template<typename T>
Vector<T>::Vector(int n, T value) : length(n)
{
    array = new T[length];
    for (int i = 0; i < length; i++)
         array[i] = value;
}

template<typename T>
Vector<T>::Vector(const Vector<T> &X) : length(X.length)
{
    length = X.length;
    array = new T[length];
    for (int i = 0; i < length; i++)
         array[i] = X.array[i];
}

template<typename T>
Vector<T>::~Vector()
{
    free(array);
}

template<typename T>
T & Vector<T>::at(int i) const
{
    return array[i];
}

template<typename T>
T & Vector<T>::operator[](int i)
{
    return array[i];
}

template<typename T>
int Vector<T>::get_size() const
{
    return length;
}

template<typename T1>
void output(const Vector<T1> &Y)
{
    for (int i = 0; i < Y.length; i++)
         cout << Y.array[i] << " ";
    cout << "\b " << endl;
}

task4.cpp

#include <iostream>
#include "vector.hpp"

void test()
{
    using namespace std;

    int n;
    cin >> n;

    Vector<double> x1(n);
    for(auto i = 0; i < n; ++i)
        x1.at(i) = i * 0.7;
    
    output(x1);
    
    Vector<int> x2(n, 42);
    Vector<int> x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;
    output(x2);

    x3[0] = 999;
    output(x3);
}

int main()
{
    test();
}

测试:

实验任务5

task5.cpp

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void output(ofstream &out){

    out.open("cipher_key.txt");
    
    if(!out.is_open()){
        cout << "open fail!!\n";
        return;
    }

    out << "  ";
    for(char c = 'a'; c <= 'z'; c ++)
        out << ' ' << c;
    out << endl;
    
    for(int i = 1; i <= 26; i ++){
        out << setw(2) << right << i;
        for(int drt = 1; drt <= 26; drt ++)
            out << ' ' << (char)((drt + i - 1) % 26 + 'A');
        out << endl;
    }

    out.close();
}

int main(){
    
    ofstream out;
    
    output(out);
}

测试:

 

标签:template,int,length,Vector,实验,IO,array,include,模板
From: https://www.cnblogs.com/Sr-duel/p/16962180.html

相关文章