实验六

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;
	}
	// 把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入文件data1.txt
	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关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元
	in.read(reinterpret_cast<char*>(&x), sizeof(x));
	in.close();
	for (auto i = 0; i < N; ++i)
		cout << x[i] << ", ";
	cout << "\b\b \n";
}

  运行结果:

 

 把line7的数组类型从int类型修改成char类型,即:array<int, N> x; ----> 修改成: array<char, N> x;

运行结果:

 

原因分析:char类型的数据占据一个内存单元,储存时会自动在后面补零,所以97对应字符a的ASCII码值,则输出a,而a之后的三个内存单元均为0,为不可输出型字符,第五个内存单元则储存98,对应输出b

 

task4.hpp:

#pragma once
#pragma once
#include<iostream>
#define MAXSIZE 10000
using namespace std;

template<typename T>

class Vector{
public:
	//构造函数与析构函数
	Vector(int n) {
		data = new T[n];
		size = n;
		cout << "constructor 1 called" << endl;
	}

	Vector(int n, T value){
		data = new int[n];
		size = n;
		for (int i = 0; i < size; i++) {
			data[i] = value;
		}
		cout << "constructor 2 called" << endl;
	}

	Vector(const Vector<T>&x) {
		cout << "copy constructor called" << endl;
		size = x.size;
		data = new int[size];

		for (int i = 0; i < size; i++) {
			data[i] = x.data[i];
		}

	}

	~Vector<T>() {
		cout << "destructor called" << endl;
		delete[] data;
	}

	//成员函数
	T& at(int i) { return data[i]; }
	T& operator[](int i) { return data[i]; }
	int get_size() { return size; }

private:
	T* data;
	int size;

	friend void output(const Vector& x) {
		for (int i = 0; i < x.size; i++) {
			cout << x.data[i] << " ";
		}
		cout << endl;
	}
};

  task4.hpp:

#include<iostream>
#include "Vector.h"
using namespace std;

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.hpp:

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

void test() {
	ofstream out;
	out.open("cipher_ke.txt");
	if (!out.is_open()) {
		cout << "fail to open cipher_key.txt to write" << endl;
		return;
	}
	//首行
	out << "  ";
	cout << "  ";
	for (int i = 0, j = 97; i < 26; i++ ,j++) {
		char x = j;
		cout << setw(2) << setfill(' ') << x;
		out << setw(2) << setfill(' ') << x;
	}
	out << endl;
	cout << endl;

	//密码表
	for (int i = 1; i <= 26; i++) {
		cout << setw(2) << setfill(' ') << i;
		out << setw(2) << setfill(' ') << i;

		static int temp = 66;
		int  x;
		int j;
		for (x = temp; x <= 90; x++) {
			char x1 = x;
			cout << setw(2) << setfill(' ') << x1;
			out << setw(2) << setfill(' ') << x1;
		}
		for (j = 65; j < temp; j++) {
			char x2 = j;
			cout << setw(2) << setfill(' ') << x2;
			out << setw(2) << setfill(' ') << x2;
		}
		temp++;
		cout << endl;
		out << endl;
	}
}

int main() {
	test();
}

  运行测试结果:

 

 文件写入cipher_key.txt:

 

posted @ 2022-12-02 14:02  彭鑫宇  阅读(29)  评论(0编辑  收藏  举报