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

实验6 模板类和文件I/O

时间:2022-12-02 00:22:05浏览次数:38  
标签:std 文件 int Vector 实验 using include 模板 size

实验任务三:

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\n";
        return 1;
    }

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

tsak3_2.cpp:

#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";
}

测试结果:

对task3_2.cpp的代码稍做改动,把line7的对象类型从 array<int, N> 改成 array<char, N> ,则测试结果为:

分析原因:

在task3_2.cpp原代码中,line18中in.read(reinterpret_cast<char *>(&x), sizeof(x))的注释为从文件流对象in关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元。

原代码中,x的类型是int,字节为4,而在更改后的代码中x的类型是char,字节为1。在输出中,原数据中,97占据4个字节,输出时97输出为a一个字节,后面3个字节为空,则

连续输出三个空格,接着继续输出98为b。

实验任务四:

Vector.hpp:

#pragma once
#include<iostream>
#include<cassert>
using std::cout;
using std::endl;
using std::ostream;
using std::istream;

template<typename T>
class Vector {
public:
    Vector(int n) :size{ n } { p = new T[n]; }
    Vector(int n, T value) :size{ n }
    {
        p = new T[n];
        for (auto i = 0;i < size;++i)
            p[i] = value;
    }
    Vector(const Vector<T>& v) :size{ v.size }
    {
        p = new T[size];
        for (auto k = 0;k < size;++k)
            p[k] = v.p[k];
    }
    ~Vector() { delete[] p; }

    int get_size() { return size; }
    T& at(int index);
    T& operator[]( int i);

    template<typename T1>
    friend void  output(const Vector<T1>& v);
private:
    int size;
    T* p;
};

template<typename T>
T& Vector<T>::at(int index)
{
    assert(index >= 0 && index < size);
    return p[index];
}

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

template<typename T1>
void output(const Vector<T1>& v)
{
    for (auto i = 0;i < v.size - 1;++i)
    {
        cout << v.p[i]<< ", ";
    }
    cout << v.p[v.size - 1] << 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.9;

    output(x1);

    Vector<int> x2(n, 9);
    Vector<int> x3(x2);

    output(x2);
    output(x3);

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

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

int main() {
    test();
}

测试结果:

实验任务五:

task6.cpp:

 

#include<iostream>
#include<fstream>
#include<iomanip>
#define N 26
using std::cout;
using std::endl;
using std::setw;
void output(std::ostream& out)
{
    char ch[N][N];
    cout << "   ";
    out << "   ";
    for (char i = 'a';i <= 'z';i++)
    {
        cout << i << " ";
        out << i << " ";
    }
    cout << endl;
    out << endl;
    for (int i = 0;i < 26;i++)
    {
        cout <<setw(2)<< i + 1;
        out << setw(2) << i + 1;
        for (int j = 0;j < 26;j++)
        {
            if (N -1- i > j)
                ch[i][j] = 'B' + i + j;
            else
                ch[i][j] = 'A' + i + j - 25;
            cout << setw(2) << ch[i][j];
            out << setw(2) << ch[i][j];
        }
        cout << endl;
        out << endl;
    }
}
int main()
{
    using namespace std;
    ofstream out;
    out.open("cipher_key.txt");
    if (!out.is_open()) {
        cout << "fail to open file ans.txt to write\n";
        return 1;
    }
    output(out);
    out.close();
}

测试结果:

 

标签:std,文件,int,Vector,实验,using,include,模板,size
From: https://www.cnblogs.com/z-zy/p/16943222.html

相关文章