首页 > 其他分享 >实验六

实验六

时间:2022-12-06 22:56:58浏览次数:40  
标签:index int Vector 实验 output include size

task3-1

cpp

#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

cpp

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

运行结果

修改后代码

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

运行结果

 

分析:

97对应的ACSⅡ为a,<int,5>占20个字节,<char,5>占5个字节,,所以int型转成char型时多余的三个字节转成空格,在读取98对应的b。

 

task4

 hpp

#pragma once
#include<iostream>
#include<cassert>

using namespace std;

// delaration of template class
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 (int i = 0; i < size; i++) {
            p[i] = value;
        }
    }

    Vector(const Vector& v) :size{ v.size } {
        p = new T[size];
        for (int i = 0; i < size; i++) {
            p[i] = v.p[i];
        }
    }

    ~Vector() {
        delete[] p;
    }

    int get_size() { 
        return size; 
    }

    T& at(int index) {
        assert(index >= 0 && index < size);
        return p[index];
    }

    T& operator[](int index) {
        assert(index >= 0 && index < size);
        return p[index];
    }

    template<typename T1>
    friend void output(const Vector<T1>& v);
private:
    int size;
    T* p;
};

template<typename T1>
void output(const Vector<T1>& v) {
    for (int i = 0; i < v.size; i++) {
        cout << v.p[i] << " ,";
    }
    cout << endl;
}

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

cpp

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace std;

void output(std::ostream& out) {
    char a[26][26];
    cout << "   ";
    out << "   ";
    for (char i = 'a'; i <= 'z'; i++) {
        cout << i << " ";
        out << i << " ";
    }
    cout << endl;
    out << endl;

    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            a[j][i] = 'A' + char((j + i ) % 26);
        }
    }
    for (int i = 0; i < 26; i++) {
        cout << setw(2) << i + 1;
        out << setw(2) << i + 1;
        for (int j = 0; j < 26; j++) {
            cout  << setw(2)  << setfill(' ') << a[i][j];
            out  << setw(2)  << setfill(' ') << a[i][j];
        }
        cout << endl;
        out << endl;
    }
}
int main() {
    ofstream out;
    out.open("cipher_key.txt");
    if (!out.is_open()) {
        cout << "fail to open file ans.txt to write" << endl;
        return 1;
    }
    output(out);
    out.close();
}

运行结果

 

标签:index,int,Vector,实验,output,include,size
From: https://www.cnblogs.com/lsoo/p/16960506.html

相关文章

  • 实验六
    试验任务三task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验六
    #pragmaonce#include<iostream>usingstd::cout;usingstd::endl;template<typenameT>classVector{public:Vector(Tn):size(n){p=newT[n];}V......
  • 实验六
    实验任务3#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六 模板类和文件IO
    任务四代码:Vector.hpp:#pragmaonce#include<iostream>usingnamespacestd;template<classT>classVector{public:Vector(intsize0):size{size......
  • 实验六 模板类和文件IO
    Task4//VEctor.hpp#include<bits/stdc++.h>usingnamespacestd;template<typenameT>classVector{private:intlength;T*base;publ......
  • 包层次的时间同步实验
    一、实验目的理解掌握时间同步的重要意义了解时间同步的基本方法,理解TPSN协议机理掌握TinyOS定时器相关接口和组件的使用4.能够在TinyOS中实现多节点间的时间......
  • SRP实验
    一、实验目的学习源路由协议的机制理解节点在源路由协议中的几种角色掌握源路由协议相关接口SourceRouteSend、SourceRoutePacket的使用 二、实验要求功能:root......
  • Dissemination实验
    一、实验目的学习少量数据在传感网中可靠传递的方法掌握TinyOS中DisseminationValue、DisseminationUpdate接口和组件的使用理解Dissemination的机制 二、实......
  • LEEP节点无线链路质量评估实验
    一、实验目的理解节点通信链路质量的影响因素理解提升链路质量的一般方法学习TinyOS系统中CC2530/CC2538节点发送功率的设置体验不同发送功率、不同通信距离、不......