实验六
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};
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<int, N> x;
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类型,在内存中占4个字节,读入文件时读入char类型,在内存中占一个字节。
所以读入后会有三个字节的空缺空间。
Vector.hpp
#include<bits/stdc++.h>
using namespace std;
template <class T>
class Vector{
public:
Vector(int n);
Vector(int n,T v);
~Vector();
Vector(Vector<T>& v);
int get_size()const;
T &at(int index);
T &operator[](int index);
template<class A> friend void output(Vector<A> &xx);
private:
int size;
T *a;
};
template <class T>
Vector<T>::Vector(int n):size{n}{
// cout<<"constructor 1 called."<<endl;
a = new T[n];
}
template <class T>
Vector<T>::Vector(int n,T v):size{n}{
// cout<<"constructor 2 called."<<endl;
a = new T[n];
for(int i=0;i<n;i++)
a[i] = v;
}
template <class T>
Vector<T>::Vector(Vector<T>& v){
// cout<<"copy constructor called."<<endl;
size = v.size;
a = new T[size];
for(int i=0;i<size;i++)
a[i] = v[i];
}
template <class T>
Vector<T>::~Vector(){
delete[] a;
// cout<<"destructor called."<<endl;
}
template <class T>
int Vector< T>::get_size()const{
return size;
}
template <class T>
T &Vector<T>::at(int index){
return a[index];
}
template <class T>
T &Vector<T>::operator[](int index){
assert(index >= 0 && index < size);
return a[index];
}
template<class A>
void output(Vector<A> &xx){
for(int i=0;i<xx.get_size();i++)
cout<<xx[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.cpp
#include<bits/stdc++.h>
using namespace std;
void output(std::ofstream &out)
{
int i,j;
out.open("cipher_key.txt");
if(!out.is_open()) {
cout << "fail to open cipher_key.txt\n";
return;
}
for(i=0;i<27;i++){
//
if(i==0){
cout<<setw(2)<<" ";
out<<setw(2)<<" ";
for(j=97;j<123;j++){
cout<<setw(2)<<char(j);
out<<setw(2)<<char(j);
}
cout << endl;
out<< endl;
}
else{
for(j=0;j<27;j++){
if(j==0){
cout<<setw(2)<<i;
out<<setw(2)<<i;
continue;
}
cout<<setw(2)<<char(65+(i+j-1)%26);
out<<setw(2)<<char(65+(i+j-1)%26);
}
cout<<endl;
out<<endl;
}
}
out.close();
}
int main(){
ofstream out;
output(out);
}

小结
任务4中,对于友元函数的模板类声明必须换一个模板类字符。对每个成员函数类外定义前也要声明模板类。
任务五中,按行写入文件,基本思路是输出一行写一行。对于这种矩阵的基本循环控制也要熟悉。

浙公网安备 33010602011771号