首页 > 其他分享 >实验六

实验六

时间:2022-12-01 22:24:00浏览次数:36  
标签:size int Vector 实验 output include out

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

将代码中的array<int,N> x 改为array<char,N> x 后,运行结果为:

将int改为char后,输入a b c d e,会将空格也读进去,又因为int类型的数据占四个字符,对编译器来说输入的其实是a,空格,空格,空格,b

所以输出结果才会改变。

#include <iostream>
using namespace std;
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>&a):size{a.size}{
            p=new T[size];
            for(auto i=0;i<size;i++)
            p[i]=a.p[i];
        }
        
        template<typename T1>
        friend void output(const Vector<T1>&a);
        
        T get_size()const{
            return size;
        }
            
        T& at(int index){
            return p[index];
        }
        
        T& operator[](int i){
            return p[i];
        }
                
    private:
        int size;
        T *p;    
};

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

#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
char password[30][30];

void output(std::ostream &out);

int main(){
    ofstream out;
    
    password[0][0]=' ';
    for(int i=0,j=1;j<=26;j++)
        password[i][j]='a'+j-1;
    for(int i=1,j=0;i<=26;i++)
        password[i][j]=i+'0';
    for(int i=1;i<=26;i++){
        for(int j=1;i+j<=26;j++)
            password[i][j]='A'+i+j-1;
    }
    for(int i=26;i>=1;i--)
        for(int j=26;i+j>26&&j>=1;j--)
            password[i][j]='A'+i+j-27;
    out.open("cipher_key.txt", ios::binary);
    if(!out.is_open()) {
        cout << "fail to open cipher_key.txt\n";
        return 1;
    }
    output(out);
    system("pause");
}

void output(std::ostream &out){
    for(int i=0;i<=26;i++){
        for(int j=0;j<=26;j++){
            if(j==0&&i!=0)
                cout<<left<<setw(2)<<password[i][j]-'0'<<" ";
            else
                cout<<left<<setw(2)<<password[i][j]<<" ";
        }
            cout<<endl;
    }
    for(int i=0;i<=26;i++){
        for(int j=0;j<=26;j++){
            if(j==0&&i!=0)
                out<<left<<setw(4)<<password[i][j]-'0'<<" ";
            else
                out<<left<<setw(4)<<password[i][j]<<"  ";
        }
            out<<endl;
    }
}

 

标签:size,int,Vector,实验,output,include,out
From: https://www.cnblogs.com/qjj12-30/p/16942978.html

相关文章

  • 实验六
    任务三: 任务四:#include<iostream>#include"Vector.hpp"voidtest(){usingnamespacestd;intn;cin>>n;Vector<double>x1(n);fo......
  • 实验六
     任务四、vector.hpp#pragmaonce#include<iostream>usingnamespacestd;template<typenameT>classVector{public:Vector(intn):size{n}{p=ne......
  • 实验6 模板类和文件I/O
    实验任务3#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101......
  • Android实验——使用Intent在Activity间传输数据
    一、实验要求和目的理解Activity组件的功能与作用;掌握使用Intent在多个Activity组件间传输数据的方法;掌握在AndroidManifest.xml中配置Activity组件的方法。二、实验......
  • Android实验——事件处理:显示持续触摸时间
    一、实验要求和目的掌握基于监听的事件处理机制,根据需求能够编写相应的事件处理程序。能够熟练应用各种布局管理器和控件进行界面设计。二、实验环境部署有Android......
  • web实验2
    基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.Web服务器的客户端服务器,提交程序运行截图2.实现GET即可,请求,响应要符合HTTP协议规范ubuntu运行截图1.......
  • web实验1-socket
    基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.time服务器的客户端服务器,提交程序运行截图timeserver代码#include<stdio.h>#include<unistd.h>#inc......
  • 实验六
    实验四#include<iostream>#include<cassert>usingstd::cout;usingstd::endl;template<classT>classVector{public:Vector(intn,intm);Vector(i......
  • 实验六
    TASK3:int:char: char和int的的字节数不一样,int占一个字节数,插入占4个,改为char后,按4个数据写入,则写入了97“ 空格”“空格”“ 空格”“空格”,然后写入98,在写......
  • 实验四 Web服务器1-socket编程
    任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:time服务器的客户端服务器,提交程序运行截图echo服务器的客户端服务器,提交程序运行截图,服务器把客......