首页 > 其他分享 >实验6

实验6

时间:2022-12-04 22:26:25浏览次数:42  
标签:capacity int length Vector 实验 include data

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

  

#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.app

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

  

#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,实验,include,data
From: https://www.cnblogs.com/wuyihaonb/p/16950967.html

相关文章

  • 实验六
    实验三task3_1.cpp1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89......
  • 实验六 模板类和文件I/O
    task3_1:1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89array<......
  • 实验6 模板类和文件I/O
    实验任务31#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain(){6usingnamespacestd;7array<int,N>x{......
  • 实验6 模板类和文件IO
    一、实验任务三1.源代码:task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,9......
  • 实验6 模板类和文件I/O
    实验任务3task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;8array<in......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};o......
  • 实验6
    task3.1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101}......
  • 实验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......