首页 > 其他分享 >实验6

实验6

时间:2022-12-06 20:00:27浏览次数:41  
标签:int Vector 实验 template output include out

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


//array<int, 5> 共有5*4=20个字节,in.read(reinterpret_cast<char *>(&x), sizeof(x)); 可以读出20个字节

//array<char, 5>共有5个字节, in.read(reinterpret_cast<char *>(&x), sizeof(x)); 可以读出5个字节

//但out.write(reinterpret_cast<char *>(&x), sizeof(x)); 都是写入20个字节,其中修改后为'97',0,0,0,'98',0,0,0,等

//因此在读的时候只能读出前五个字节 '97'对应a,0对应空,结果为 a, , , , b
 

task4:

Vector.hpp

#include<iostream>

using namespace std;

template<typename T>
class Vector
{
public:
    Vector(int n1);
    Vector(int n1, T x);
    Vector(const Vector<T> &y);
    ~Vector();
    int get_size() const { return n; }
    T& at(int i);
    T& operator[](int i);
    template<typename T1> 
    friend void output(const Vector<T1> &x);
private:
    int  n;
    T   *p;
};

template<typename T>
T& Vector<T>::operator[](int i)
{
    return p[i];    
}

template<typename T>
T& Vector<T>::at(int i)
{
    return p[i];
}

template <typename T>
Vector<T>::Vector(int n1)
{
    n = n1;
    p = new T[n];
}

template <typename T>
Vector<T>::Vector(int n1, T x)
{
    n = n1;
    p = new int[n];
    for(int i = 0; i < n1; i++)
    {
        p[i] = x;
    }
}

template <typename T>
Vector<T>::Vector(const Vector<T> &y)
{
    n = y.n;
    p = new int[n];
    for(int i = 0; i < n; i++)
    {
        p[i] = y.p[i];
    }
}

template <typename T>
Vector<T>::~Vector()
{
    delete[] p;
    cout << "destructor called" << endl;
}

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

task4.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>
using namespace std;
void output(ostream& out) 
{
    out << "  ";
    for (int i = 97; i < 123; i++) 
    {
        out << setw(2) << char(i);
    }
    out << endl;
    for (int i = 1; i <= 26; i++) 
    {
        out << setw(2) << i;
        for (int j = i; j < i + 26; j++) 
        {
            out << setw(2) << char(65 + j % 26);
        }
        out << endl;
    }
}
int main() {
    ofstream out;
    output(cout);
    out.open("cipher.txt");
    if (!out.is_open())
    {
        cout << "fail to open" << endl;
        return 0;
    }
    output(out);
    out.close();
    return 0;
}

 

 

 

标签:int,Vector,实验,template,output,include,out
From: https://www.cnblogs.com/miantiaooooo/p/16960345.html

相关文章

  • 无线传感器网络技术-实验一-TINYOS/6LOWPAN 编程基础
    【实验目的】1.掌握TinyOS/6LoWPAN开发环境的搭建方法2.掌握VisualStudioCode中TinyOS编程的相关设置3.掌握VSCode中CC2530节点(平台名称cc2530zn)和CC......
  • 实验六
    #pragmaonce#include<iostream>usingstd::cout;usingstd::endl;template<typenameT>classVector{public:Vector(Tn):size(n){p=newT[n];}......
  • 实验6 模板类和文件IO
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){ usingnamespacestd; array<int,N>x{97,98,99,100,101}; ofstr......
  • 实验六
    3.1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101}......
  • 实验六 模板类和文件IO
    1.1Vector.hpp:#include<iostream>#include<string>#include<iomanip>usingnamespacestd;template<classT>classVector{public:Vector(intn):s......
  • 实验6
    实验四vector.hpp#pragmaonce#include<iostream>usingnamespacestd;template<typenameT>classVector{public:Vector(intn):size{n}{p=newT[n......
  • 实验六 模板和文件IO
    task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89array<int,......
  • 云计算——实验3:AWS实验-EC2操作
    本次实验属于验证型实验,通过本次实验学生将掌握以下内容:1、EC2免费实例创建方法;2、EC2实例SSH连接以及命令使用。我使用阿里云进行注册和远程连接,aws需要使用信用卡,国......
  • 一点关于深度学习实验的思考:重复实验
    前言最近做深度学习实验,时常会感叹深度学习就像炼丹一样,效果好坏似乎就像上帝在掷骰子。后面反思了一下自己的实验方法,再反思了一下做实验的目的。什么时候我们可以自信......
  • 51单片机之继电器实验
    一、继电器(relay)的工作原理和特性   当输入量(如电压、电流、温度等)达到规定值时,使被控制的输出电路导通或断开的电器。可分为电气量(如电流、电压、频率、功率等)继......