首页 > 其他分享 >实验六

实验六

时间:2022-12-01 18:01:13浏览次数:40  
标签:ch int char Vector 实验 template output

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

结果

文档

 

 

标签:ch,int,char,Vector,实验,template,output
From: https://www.cnblogs.com/oRIng/p/16939175.html

相关文章

  • 实验四 Web服务器1-socket编程
    任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:time服务器的客户端服务器,提交程序运行截图echo服务器的客户端服务器,提交程序运行截图,服务器把客......
  • 实验6-模板类和文件
    task3:task3.1.cpp1#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain()6{7usingnamespacestd;8array<int......
  • 网络渗透测试实验_1_网络扫描与网络侦察
    以下内容为课堂上的实验记录1. 实验目的和要求理解网络扫描、网络侦察的作用;通过搭建网络渗透测试平台,了解并熟悉常用搜索引擎、扫描工具的应用,通过信息收集为下一步渗......
  • 实验六
    1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;8array<char,N>x;91......
  • Python实验报告——第13章 Pygame游戏编程
    实验报告【实验目的】 1.掌握Pygame的基础知识。【实验条件】1.PC机或者远程编程环境。 【实验内容】1.完成第十三章  实例01:篮球自动弹跳。  实例01:创......
  • 实验6 模板类和文件I/O
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验四 Web服务器1-socket编程
    实验四Web服务器1-socket编程ipa查询网络状态echo服务器的客户端服务器,提交程序运行截图,服务器把客户端传进来的内容加入“服务器进程pid你的学号姓名echo:”......
  • 实验6 模板类和文件I/O
     实验任务3task3_1.cpp1#include<iostream>2#include<vector>3template<typenameT>4voidoutput(constT&obj){5for(auto&item:obj)6std::......
  • 实验六
    实验任务3task3_1.cpp源码:1#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain(){6usingnamespacestd;7......
  • 实验四 Web服务器2
    任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:Web服务器的客户端服务器,提交程序运行截图实现GET即可,请求,响应要符合HTTP协议规范服务器部署到华......