实验六

TASK 3:

int:

char:

 char和int的的字节数不一样,int占一个字节数,插入占4个,改为char后,按4个数据写入,则写入了97 “ 空格” “空格 ” “ 空格” “空格 ”,然后写入98,在写入时已经不满足i<N的条件,因此写入最终结果为97 “ 空格” “空格 ” “ 空格” “空格 ” 98,97为阿斯克码值,对应字符a,98则对应字符b,因此输出结果为上图所示。

TASK 4:

Vector.hpp

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

task4

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

测试结果

TASK 6

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

结果

文档

 

 

posted @ 2022-12-01 17:54  zoRing  阅读(27)  评论(0编辑  收藏  举报