实验六 模板类和文件IO

实验任务3
task3-1.cpp

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

int main1() {
    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();
}

image
task3-2.cpp

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

int main() {
    using namespace std;
    array<char, 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";
}

image


实验任务4
Vector,h

#pragma once
#include<iostream>
using namespace std;
template<typename T>
class Vector
{
private:
    int n;
    T* p;
public:
    Vector(int number) : n{ number } { p = new T[n]; };
    Vector(int number, T value) : n{ number } { p = new T[n]; for (int i = 0; i < n; i++) { p[i] = value; } };
    ~Vector() { delete[] p; };
    Vector(const Vector<T>& c);
    int get_size();
    T& at(int index);
    T& operator[](int n);
    template<typename T1>
    friend void output(const Vector<T1>& c);
};
template<typename T>
Vector<T>::Vector(const Vector<T>& c) :n{ c.n }
{
    p = new T[n];
    for (int i = 0; i < c.n; i++)
    {
        p[i] = c.p[i];
    }
}
template<typename T>
int Vector<T>::get_size()
{
    return n;
}
template<typename T>
T& Vector<T>::at(int index)
{
    return p[index];
}
template<typename T>
T& Vector<T>::operator[](int n)
{
    return p[n];
}
template<typename T1>
void output(const Vector<T1>& c)
{
    for (int i = 0; i < c.n; i++)
    {
        cout << c.p[i] << " ";
    }    cout << endl;
}

task4.cpp

#include <iostream>
#include "Vector.h"

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();
}

image


实验任务5
task5.cpp

#include<iostream>
#include<fstream>
#define N 26
using namespace std;
void output(std::ostream& out);
void output(std::ostream& out)
{
    char a[N][N];
    char ch;
    int i, j;
    ch = 'a';
    ofstream ot;
    ot.open("cipher_key.txt");
    if (!ot.is_open())
    {
        cout << "false" << endl;
    }
    out << "   ";
    ot << "    ";
    for (i = 0; i < N; i++)
    {
        out << ch << " ";
        ot << ch << " ";
        ch = ch + 1;
    }
    out << endl;
    ot << endl;
    ch = 'B';
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            a[i][j] = ch;
            ch++;
            if (ch > 'Z')
            {
                ch = ch - 26;
            }
        }
        ch = ch + 1;
        if (ch > 'Z')
        {
            ch = ch - 26;
        }
    }
    for (i = 0; i < N; i++)
    {
        int k = i + 1;
        if (k < 10)
        {
            out << k << "  ";
            ot << k << "  ";
        }
        else
        {
            out << k << " ";
            ot << k << " ";
        }
        for (j = 0; j < N; j++)
        {
            out << a[i][j] << " ";
            ot << a[i][j] << " ";
        }
        out << endl;
        ot << endl;
    }
    ot.close();
}
int main()
{
    ostream& out = cout;
    output(out);
}

image


posted @ 2022-12-06 20:55  Cali-AKA  阅读(10)  评论(0编辑  收藏  举报