2022.11.30 OOP实验
实验6 模板类和文件IO
任务3
task36_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();
}
运行结果:
task36_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";
}
运行结果:
修改过后:
#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";
}
运行结果:
分析:
a的ASCII码为97,所以97->a
<int,5>占20个字节,<char,5>占5个字节,int型转成char型时多余的三个字节转成空格
b的ASCII码为98,所以98->a
任务4
Vector46.hpp
#pragma once
#include<iostream>
#include<cassert>
using namespace std;
// delaration of template class
template<typename T>
class Vector {
public:
Vector(int n) :size{ n } {
p = new T[n];
}
Vector(int n, T value) :size{ n } {
p = new T[n];
for (int i = 0; i < size; i++) {
p[i] = value;
}
}
Vector(const Vector& v) :size{ v.size } {
p = new T[size];
for (int i = 0; i < size; i++) {
p[i] = v.p[i];
}
}
~Vector() {
delete[] p;
}
int get_size() {
return size;
}
T& at(int index) {
assert(index >= 0 && index < size);
return p[index];
}
T& operator[](int index) {
assert(index >= 0 && index < size);
return p[index];
}
template<typename T1>
friend void output(const Vector<T1>& v);
private:
int size;
T* p;
};
template<typename T1>
void output(const Vector<T1>& v) {
for (int i = 0; i < v.size; i++) {
cout << v.p[i] << " ,";
}
cout << endl;
}
tesk46.cpp
#include <iostream>
#include "Vector46.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();
}
运行结果:
任务5
task56.cpp
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
void output(std::ostream& out) {
char a[26][26];
cout << " ";
out << " ";
for (char i = 'a'; i <= 'z'; i++) {
cout << i << " ";
out << i << " ";
}
cout << endl;
out << endl;
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
a[j][i] = 'A' + char((j + i ) % 26);
}
}
for (int i = 0; i < 26; i++) {
cout << setw(2) << i + 1;
out << setw(2) << i + 1;
for (int j = 0; j < 26; j++) {
cout << setw(2) << setfill(' ') << a[i][j];
out << setw(2) << setfill(' ') << a[i][j];
}
cout << endl;
out << endl;
}
}
int main() {
ofstream out;
out.open("cipher_key.txt");
if (!out.is_open()) {
cout << "fail to open file ans.txt to write" << endl;
return 1;
}
output(out);
out.close();
}
运行结果: