首页 > 其他分享 >实验6

实验6

时间:2022-12-03 22:22:06浏览次数:27  
标签:int Vector 实验 output include out 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<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";
}

运行截图

 

 

 

int类型占四个字节,但97等只保存在一个地址空间里,其余三个字节保存0,char类型占一个字节,读入char类型数组时,第一次读到97,转化为a,接下来读到数据为空,直到第五个读到98,变为b,结束

vector.hpp

#pragma once
#include<iostream>
using namespace std;
template<class T>
class Vector {
public:
	Vector() {

	}
	Vector(int n) {
		p = new T[n];
		size = n;
	}
	Vector(int n,T value) {
		p = new T[n];
		for (int i = 0; i < n; i++) {
			p[i] = value;
		}
		size = n;
	}
	
	Vector(const Vector<T> &vec) {
		p = new T[vec.get_size()];
		for (int i = 0; i < vec.get_size(); i++) {
			p[i] = vec.p[i];
		}
		size = vec.size;
	}
	~Vector(){
		delete p;
	}

	int get_size() const;
	T& at(int index);
	T& operator[](int index);
	template<typename T>
	friend void output(Vector<T> vec);

private:
	T* p;
	int size;
};
template<class T>
int Vector<T>::get_size() const{
	return size;
}
template<class T>
T& Vector<T>::at(int index) {
	return p[index];
}
template<class T>
T& Vector<T>::operator[](int index) {
	return p[index];
}
template<class T>
void output(Vector<T> vec) {
	for (int i = 0; i < vec.get_size(); i++) {
		cout << vec.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

#include<iostream>
using namespace std;
#include<fstream>

void output(ofstream& out);

int main() {
	ofstream out;
	out.open("cipher_key.txt", ios::out);
	output(out);
}

void output(ofstream& out) {
	cout << " ";
	out << " ";

	for (int i = 0; i < 26; i++) {
		char c = i + 'a';
		cout <<" " << c;
		out <<" "<< c;
	}
	cout << endl;
	out << endl;
	for (int i = 1; i<=26; i++) {
		cout << i;
		out << i;
		for (int j = i; j < i+26; j++) {
			char c = 'A' + j%26;
			cout << " " << c;
			out << " " << c;
		}
		cout << endl;
		out << endl;
	}
}

  

 

 

 

 

 

标签:int,Vector,实验,output,include,out,size
From: https://www.cnblogs.com/Zhouzhou-/p/16948925.html

相关文章

  • 实验六
    #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')......
  • 实验三)
           实验目的1、熟练Wireshark的使用;2、分析TCP三次握手的工作原理;分析UDP协议;3、研究Ping操作与ARP协议的关系;4、分析DHCP与DNS协议的工作原......
  • 实验二
     实验目的1、了解和认识常见的网络设备及其功能;2、了解双绞线的制作过程;3、掌握简单局域网的搭建;4、掌握一些常见命令的使用;命令的含义和相关的操作。实验原......
  • 实验一
     实验目的1、了解和认识常见的网络设备及其功能;2、了解双绞线的制作过程;3、掌握简单局域网的搭建;4、掌握一些常见命令的使用;命令的含义和相关的操作。实验原......
  • 实验6 模板类和文件
    1.实验3task3_1.cpp1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89......
  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六 模板类和文件
    实验三task3.11#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain(){6usingnamespacestd;7array<int,N>x{97,......
  • 实验6
    TASK3:#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};of......