首页 > 其他分享 >实验6

实验6

时间:2022-12-04 16:46:08浏览次数:35  
标签:int value Vector 实验 output include size

task3.1

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

task3.2

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

 

原因:int类型占用四个字节,char类型占用一个字节,强制转换后读取a的ASCII码后读取三个空格后才会读取b的ASCII码98.

task4

vector.hpp

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

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

task5

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
void output(std::ostream& out)
{
    char a[26][26];
    cout << "  ";
    out << "  ";
    for (char i = 'a'; i <= 'z'; i++)
    {
        cout << setw(2) << setfill(' ') << i;
        out << setw(2) << setfill(' ') << i;
    }
    cout << endl;
    out << endl;
    for (int j = 0; j < 26; j++)
    {
        cout << setw(2) << setfill(' ') << j + 1;
        out << setw(2) << setfill(' ') << j + 1;
        for (int k = 0; k < 26; k++)
        {
            a[j][k] = 'A' + char((j + k + 1) % 26);
            cout << setw(2) << setfill(' ') << a[j][k];
            out << setw(2) << setfill(' ') << a[j][k];
        }
        out << endl;
        cout << endl;
    }
}
int main()
{
    ofstream out;
    out.open("cipher_key.txt", ios::out);
    if (!out.is_open())
    {
        cout << "fail to open cipher_ke.text" << endl;
        return 0;
    }
    output(out);
    out.close();
}

 

 

标签:int,value,Vector,实验,output,include,size
From: https://www.cnblogs.com/zqpqaz151034/p/16950090.html

相关文章

  • 实验6 模板类和文件IO
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,1......
  • 实验6 模板类和文件io
    task4Vector.cpp#pragmaonceusingnamespacestd;template<typenameT>classVector{public:Vector(intn);Vector(intn,Tv);~Vector();V......
  • 实验六
    #include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};ofstreamout;ou......
  • 实验6
    一、实验目的体验模板函数、模板类的编写,从多态角度理解模板函数和模板类(类型作为参数)体验标准I/O流类、文件I/O流类、字符串I/O流类的用法,能正确使用针对问题场景,能......
  • 二十三. 单臂路由实验和三层交换实验
    1.单臂路由实验图配置流程第1个里程:配置交换机2接口配置<Huawei>sys[Huawei]sysnameS1[S1]vlanbatch1020[S1]intg0/0/2[S1-GigabitEthernet0/0/2]portl......
  • java实验四
    一、实验目的掌握Java语言中final关键字的含义及使用方法;掌握Java语言中抽象类的基本概念及使用方法;掌握Java语言中接口的基本概念及使用方法;理解程序设计中常用数据......
  • 实验6 模板类和文件OL
    task3-1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101......
  • 实验6
    task3_1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};ofstreamo......
  • Python 第11章 上机实验
    说明:导入pymysql包,关于使用mysql的代码,只能在我的电脑使用,同时我抹去了使用mysql的账号秘密importsqlite3#连接到SQLite数据库conn=sqlite3.connect('mrsoft.db')......