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

实验6 模板类和文件I/O

时间:2022-12-04 20:34:50浏览次数:42  
标签:文件 int value v0 Vector 实验 include 模板 size

实验任务3

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 int main() {
 6     using namespace std;
 7     array<int, N> x{ 97, 98, 99, 100, 101 };
 8     ofstream out;
 9     out.open("data1.dat", ios::binary);
10     if (!out.is_open()) {
11         cout << "fail to open data1.dat\n";
12         return 1;
13     }
14     // 把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入文件data1.txt
15     out.write(reinterpret_cast<char*>(&x), sizeof(x));
16     out.close();
17 }
task3_1.cpp
 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 int main() {
 6     using namespace std;
 7     array<int, N> x;
 8     ifstream in;
 9     in.open("data1.dat", ios::binary);
10     if (!in.is_open()) {
11         cout << "fail to open data1.dat\n";
12         return 1;
13     }
14     // 从文件流对象in关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元
15     in.read(reinterpret_cast<char*>(&x), sizeof(x));
16     in.close();
17     for (auto i = 0; i < N; ++i)
18         cout << x[i] << ", ";
19     cout << "\b\b \n";
20 }
task3_2.cpp

测试截图:

 

 修改成:array<char,N>x;后的截图:

 

 原因分析:

由于char类型占一个字节,而int类型占四个字节。故当以int类型读取时,每次读取四个字节的内容,因而能正常输出97,98,99,100,101;当以char类型读取时,由于一次读取一个字节的内容,故第一此输出的是ASCII码97对应的字符'a',后三次输出为空,直至四个字节读取完,再读取ASCII98对应的字符'b',因而输出为a, , , , b。见下示意图。

 

 

实验任务4

 1 #pragma once
 2 #include<iostream>
 3 template<typename T>
 4 class Vector {
 5 public:
 6     Vector(int n, T v);
 7     Vector(int n) :size{ n } { value = new T[size]; }
 8     Vector(const Vector<T>& v0);
 9     ~Vector() = default;
10     int get_size()const { return size; }
11     T& at(int i)const { return value[i]; }
12     T& operator[](int i) const { return value[i]; }
13     template<typename T1>
14     friend void output(const Vector<T1>& v0);
15 private:
16     int size;
17     T* value;
18 };
19 template<typename T>
20 Vector<T>::Vector(int n, T v0) :size{ n } {
21     value = new T[size];
22     for (int i = 0; i < size; i++)
23         value[i] = v0;
24 }
25 template<typename T>
26 Vector<T>::Vector(const Vector<T>& v0) : size{ v0.size } {
27     value = new T[size];
28     for (int i = 0; i < size; i++)
29         value[i] = v0.value[i];
30 }
31 template<typename T1>
32 void output(const Vector<T1>& v0) {
33     for (int i = 0; i < v0.size; i++)
34         std::cout << v0[i] << ", ";
35     std::cout << "\b\b \n";
36 }
Vector.hpp
 1 #include <iostream>
 2 #include "Vector.hpp"
 3 void test() {
 4     using namespace std;
 5     int n;
 6     cin >> n;
 7     Vector<double> x1(n);
 8     for (auto i = 0; i < n; ++i)
 9         x1.at(i) = i * 0.7;
10     output(x1);
11     Vector<int> x2(n, 42);
12     Vector<int> x3(x2);
13     output(x2);
14     output(x3);
15     x2.at(0) = 77;
16     output(x2);
17     x3[0] = 999;
18     output(x3);
19 }
20 int main() {
21     test();
22 }
task4.cpp

测试截图:

 

 

实验任务5

 1 #include<fstream>
 2 #include<iostream>
 3 #include<iomanip>
 4 #define N 27
 5 using namespace std;
 6 char alph[N][N];
 7 void output(ostream& out) {
 8     for (int i = 0; i < N; i++) {
 9         if (i == 0) cout << "  ";
10         else cout << setw(2) << alph[i][0] - '0';
11         for (int j = 1; j < N; j++) {
12             cout << setw(2) << alph[i][j];
13         }
14         cout << endl;
15     }
16     for (int i = 0; i < N; i++) {
17         if (i == 0) out << "  ";
18         else out << setw(2) << alph[i][0] - '0';
19         for (int j = 1; j < N; j++) {
20             out << setw(2) << alph[i][j];
21         }
22         out << endl;
23     }
24 }
25 int main() {
26     ofstream out;
27     out.open("cipher_key.txt");
28     if (!out.is_open()) {
29         cout << "fail to open the cipher_key.txt\n";
30         return 1;
31     }
32     alph[0][0] = ' ';
33     for (int i = 1; i < N; i++) {
34         alph[0][i] = 'a' + i - 1;
35         alph[i][0] = i + '0';
36     }
37     int j;
38     for (int i = 1; i < N; i++) {
39         alph[1][i] = alph[0][i] - 31;
40         alph[1][N - 1] = 'A';
41     }
42     for(int i=2;i<N;i++){
43         char c = alph[i - 1][1];
44         for (int j = 1; j < N - 1; j++) {
45             
46             alph[i][j] = alph[i - 1][j + 1];
47         }
48         alph[i][N - 1] = c;
49     }
50     output(out);
51 }
task5.cpp

测试截图:

 

标签:文件,int,value,v0,Vector,实验,include,模板,size
From: https://www.cnblogs.com/Terrence0403/p/16950616.html

相关文章

  • 实验6 模板类和文件IO
    一、实验任务三1.源代码:task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,9......
  • 13.C++模板初阶
    泛型编程如何实现一个通用的交换函数呢?voidSwap(int&left,int&right){ inttemp=left; left=right; right=temp;}voidSwap(double&left,double&ri......
  • 实验6 模板类和文件I/O
    实验任务3task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;8array<in......
  • split-切割文件
    1、描述作用:切割文件,将文件以行为单位或以字节为单位进行切割语法:split[-bl]file[name]2、参数参数描述-b后面可接欲分割成的档案大小,可加单位,例如b、k、m等-l......
  • 把本地项目文件夹改为git仓库并关联到远端分支(非主分支)
    问题本地已有代码目录am_flow,而且不是git仓库远端git仓库am_flow,有main主分支和branch分支dev_hq,有些许文件,基本为空目录目标:把本地代码目录关联上远端git仓库am_flow......
  • 压缩指定文件夹下所有文件夹,并输出压缩后的文件列表
    1,赋值下面代码另存为:compressionSpecifiedPath.bat 注意文件后缀为(.bat)echooff&color0A@echooff&setlocalenabledelayedexpansionrem压缩指定目录下所有......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};o......
  • Nginx之两个前端项目用同一个后台项目,如何配置配置文件?
    Nginx最被人熟知的作用就是反向代理。反向代理:因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据......
  • js:Vue2.js通过CDN方式引入模板
    文档https://v2.cn.vuejs.org/v2/guide/installation.html示例index.html<!--引入Vue2.7.14--><scriptsrc="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.......
  • 实验6
    task3.1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101}......