首页 > 其他分享 >实验六

实验六

时间:2022-11-30 21:47:45浏览次数:42  
标签:capacity int length Vector 实验 obj include

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

 如果将array模板参数改为char结果如下:

原因:因为写入文件时array的数据类型是int,所以数组中的每一个元素占四个字节,sizeof(array)就是4*N byte,那么将他们写入到文件中后共占4*N byte。但是如果读入时将array的数据类型改为char,每一个元素占一个字节,sizeof(array)就是1*N byte,读入时仅仅会从文件开始读入1*N byte,输出时将每一个byte译为char类型数据输出。

 

task4

Vector.h

#pragma once
#include<iostream>
using namespace std;

template<typename T>
class Vector
{
public:
    Vector(int capacity);
    Vector(int length, T value);
    Vector(const Vector& obj);
    ~Vector();
    int get_size()const;
    T& at(int i);
    T& operator[](int i);
    friend void output(const Vector<T>& obj);
private:
    T* _data;
    size_t _capacity;
    size_t _length;
};

template<typename T>
Vector<T>::Vector(int capacity) :_data(new T[capacity]), _capacity(capacity), _length(capacity)
{}

template<typename T>
Vector<T>::Vector(int length, T value) :_data(new T[length]), _capacity(length), _length(length)
{
    for (int i = 0; i < length; i++)
        _data[i] = value;
}

template<typename T>
Vector<T>::Vector(const Vector& obj)
{
    _data = new T[obj._capacity];
    _capacity = obj._capacity;
    _length = obj._length;
    for (int i = 0; i < _length; i++)
        _data[i] = obj._data[i];
}

template<typename T>
Vector<T>::~Vector()
{
    delete []_data;
    _length = 0;
    _capacity = 0;
    _data = nullptr;
}

template<typename T>
int Vector<T>:: get_size() const
{
    return _length;
}

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

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

void output(const Vector<int>& obj)
{
    for (int i = 0; i < obj._length; i++)
        cout << obj._data[i] << ",";
    cout << endl;
}

void output(const Vector<double>& obj)
{
    for (int i = 0; i < obj._length; i++)
        cout << obj._data[i] << ",";
    cout << endl;
}

main.cpp

#include <iostream>
#include "Vector.h"

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 5

main.cpp

#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using std::ofstream;
using std::endl;
using std::setw;
std::string stand = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void output(std::ostream& out)
{
    int row = 1;
    out << "   ";
    for (int i = 0; i < 26; i++)
        out << (char)(stand[i] + 32) << " ";
    out << endl;
    while (row <= 26)
    {
        out << setw(2) << row << " ";
        for (int i = 0; i < 26; i++)
            out << stand[(i + row) % 26] << " ";
        out << endl;
        row++;
    }
}
int main()
{
    ofstream out;
    out.open("ciper.txt");
    if (!out.is_open())
    {
        std::cout << "fail to open ciper.txt" << endl;
        return 1;
    }
    output(out);
    output(std::cout);
    return 0;
}

 

 

 

标签:capacity,int,length,Vector,实验,obj,include
From: https://www.cnblogs.com/xelfin/p/16939465.html

相关文章

  • 实验六:模板类和文件
    w实验任务三task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六
    6.3#include<iostream>#include<fstream>#include<array>#defineN5voidtest1(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验6 模板类和文件I/O
    1.程序源码task1#include<iostream>#include<fstream>#include"Complex.hpp"voidtest1(){usingnamespacestd;Complex<double>c1{1,2},c2;......
  • 实验四 Web服务器2
    实验四Web服务器2基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:Web服务器的客户端服务器,提交程序运行截图实现GET即可,请求,响应要符合HTTP协议规范服务......
  • 实验六
    task3_1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;//array<int,N>x{97,98,99,100,10......
  • 实验6
    实验任务3:task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98......
  • 数据结构实验之栈与队列二:一般算术表达式转换成后缀式 sdut-oj
    #include<stdio.h>#include<stdlib.h>#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT100typedefstruct{  char*base;  ......
  • 数据结构实验之栈与队列四:括号匹配 sdut-oj
    #include<stdio.h>#include<stdlib.h>#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10typedefstruct{  char*base;  ......
  • 实验六
    task4Vector.hpp:1#pragmaonce2#include<bits/stdc++.h>3usingnamespacestd;4template<typenameT>5classVector6{7private:8intsize;......
  • 数字逻辑实验 9 FPGA数字钟(Verilog)
    目录实验9FPGA数字钟实验分析:实现思路:硬件支持:硬件描述语言代码编写:1顶层模块2时钟分频,(正/倒)计时器模块3输入处理模块in_out.v5......