首页 > 其他分享 >实验6模板类和文件IO

实验6模板类和文件IO

时间:2022-12-05 23:00:43浏览次数:36  
标签:num int Vector 实验 IO array include 模板 size

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.txt", 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<char, N> x; ifstream in; in.open("data1.txt", ios::binary); if (!in.is_open()) { cout << "fail to open data1.dat\n"; return 1; } // 从文件流对象in关联的文件data1.txt中读取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"; }

产生该结果的原因是因为写文件的时候写入的是int类型,int类型占四个字节,读文件的时候采用的是一次读一个char,即一个字节,所以造成该结果


 

 
Vector.hpp
#pragma once #include<iostream> using namespace std; template<typename T> class Vector { template<typename T> friend void output(Vector<T> &v); public: Vector(int num); Vector(int num, T value); Vector(const Vector<T>& v); ~Vector(); int get_size(); T& at(int i); T& operator[](int i); private: T* array; int size; }; template<typename T> void output(Vector<T> &v) { for (auto i = 0; i < v.get_size(); i++) { cout << v.array[i]<<","; } cout << endl; } template<typename T> Vector<T>::Vector(int num) { this->size = num; array = new T[size]; } template<typename T> Vector<T>::Vector(int num, T value) { this->size = num; array = new T[size]; for (int i = 0; i < size; i++) { array[i] = value; } } template<typename T> Vector<T>::Vector(const Vector<T> &v) { this->size = v.size; this->array = new T[size]; for (int i = 0; i < size; i++) { this->array[i] = v.array[i]; } } template<typename T> Vector<T>::~Vector() { if (this->array != NULL) { delete[] array; } array = NULL; } template<typename T> int Vector<T>::get_size() { return size; } template<typename T> T& Vector<T>::at(int i) { return array[i]; } template<typename T> T& Vector<T>::operator[](int i) { return array[i]; }
#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 * 1.1;
    output(x1);
    Vector<int> x2(n, 66);
    Vector<int> x3(x2);
    output(x2);
    output(x3);
    x2.at(0) = 66;
    output(x2);
    x3[0] = 888;
    output(x3);
}
int main() {
    test();
}

 

 
task5.cpp
#include<iostream> #include<fstream> #include<iomanip> #include<iomanip> using namespace std; void output(ostream& out) { ifstream ifs; ifs.open("cipher_key.txt"); int num[27][27]; for (int i = 0; i < 27; i++) { for (int j = 0; j < 27; j++) { if (j == 0 && i == 0) { out<<" "; continue; } if (j == 0 && i != 0) { ifs >> num[i][j]; out << setw(2) << num[i][j]; continue; } ifs >> num[i][j]; out << setw(2) << (char)num[i][j]; } out << endl; } ifs.close(); } int main() { ofstream ofs; ofs.open("cipher_key.txt", ios::out); int a[27][27]; int cnt = 1; int num = 97; int num2 = 66; for (int j = 1; j < 27; j++) { a[0][j] = num++; a[j][0] = cnt++; } for (int i = 1; i < 27; i++) { for (int j = 1; j < 27; j++) { if (a[i][j - 1]== 90)a[i][j] = 65; else if (j == 1) { if (num2 == 91)a[i][j] = 65; else a[i][j] = num2++; } else a[i][j] = a[i][j - 1] + 1; } } for (int i = 0; i < 27; i++) { for (int j = 0; j < 27; j++) { if (i == 0 && j == 0)ofs << " "; else ofs << a[i][j] << " "; } ofs << endl; } ofs.close(); output(cout); }

 



 

标签:num,int,Vector,实验,IO,array,include,模板,size
From: https://www.cnblogs.com/1916wenle/p/16953827.html

相关文章