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

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

时间:2022-12-04 21:22:30浏览次数:44  
标签:文件 int Vector 实验 template n0 output include 模板

task3_1:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 
 6 int main() {
 7     using namespace std;
 8     
 9     array<int, N> x {97, 98, 99, 100, 101};
10     
11     ofstream out;
12     out.open("data1.dat", ios::binary);
13     if(!out.is_open()) {
14         cout << "fail to open data1.dat\n";
15         return 1;
16     }
17     
18     //把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入data1.txt
19     out.write(reinterpret_cast<char *>(&x), sizeof(x));
20     out.close();   
21 } 

task3_2:

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

 

 修改后:

 

 分析:

char型占1个字节,而int型占4个字节,导致输出3个空格且b出现在第5个位置

 

task4.hpp:

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <cassert>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 template<typename T>
10 class Vector {
11     public:
12     Vector(int n0);
13     Vector(int n0, T value0);
14     Vector(const Vector<T> &v);
15     ~Vector();
16     
17     T &at(int i);
18     int get_size() { return n; };
19     T& operator[](int i);
20     
21     template<typename T1>
22     friend void output(Vector<T1> &v);    
23     
24     private:
25     int n;
26     T *p;
27 };
28 
29 template<typename T>
30 Vector<T>::Vector(int n0) : n{n0} {
31     p = new T[n0];
32 }
33 
34 template<typename T>
35 Vector<T>::Vector(int n0, T value0) : n{n0} {
36     p = new T[n];
37     for(auto i = 0;i <= n; i++)
38         p[i] = value0;
39 }
40 
41 template<typename T>
42 Vector<T>::Vector(const Vector<T> &v) {
43     n = v.n;
44     p = new T[n];
45     for(auto i = 0; i < n; i++)
46         p[i] = v.p[i];
47 }  
48 
49 template<typename T>
50 Vector<T>::~Vector() {
51     delete[] p;
52 }
53 
54 template<typename T>
55 T &Vector<T>::at(int i) {
56     assert(i >= 0 && i < n);
57     return p[i];
58 } 
59 
60 template<typename T1>
61 void output(Vector<T1> &v){
62     for(auto i = 0; i < v.n; i++)
63     cout << v.p[i] << " ";
64     cout << endl;
65 }
66 
67 template<typename T>
68 T& Vector<T>::operator[](int i) {
69     return p[i];
70 }

task4.cpp:

 1 #include <iostream>
 2 #include "实验六task4_vector.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     int n;
 8     cin >> n;
 9     
10     Vector<double> x1(n);
11     for(auto i = 0; i < n; ++i)
12         x1.at(i) = i * 0.7;
13     
14     output(x1);
15     
16     Vector<int> x2(n, 42);
17     Vector<int> x3(x2);
18     
19     output(x2);
20     output(x3);
21     
22     x2.at(0) = 77;
23     output(x2);
24     
25     x3[0] = 999;
26     output(x3);    
27 }
28 
29 int main() {
30     test();
31 }

 

 

task5

 1 #include <iostream>
 2 #include <fstream>
 3 #include <iomanip>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 void put(std::ostream &out, int n, char a) {
 9     char A = 'A';
10     int i = 0;
11     cout << n;
12     out << n;
13     for(;a <= 'Z'; a++)
14     {
15        cout << setw(2) << a;
16        out << setw(2) << a;
17        i++;    
18     }
19     for(;i < 26; i++, A++)
20     {
21        cout << setw(2) << A;
22        out << setw(2) << A;
23     }
24 }
25 
26 void output(std::ostream &out) {
27     char k = 'a', K = 'B';
28     cout << "  ";
29     out << "  ";
30     for(;k <= 'z'; k++)
31     {
32        cout << setw(2) << k;
33        out << setw(2) << k;    
34     }
35     cout << endl;
36     out << endl;
37     for(int i = 1; i <= 26; i++, K++) {
38         if(i < 10)
39            cout << " ";
40            out << " ";
41         put(out,i,K);
42         cout << endl;
43         out << endl;
44     }
45     
46 }
47 
48 int main() {
49     ofstream out;
50     
51     out.open("cipher_key.txt", ios::out);
52     if(!out.is_open()) {
53         cout << "fail to open chipher_key\n";
54         return 1;
55     }
56     output(out);
57     out.close();
58 }

 

 

 

标签:文件,int,Vector,实验,template,n0,output,include,模板
From: https://www.cnblogs.com/djynb/p/16950838.html

相关文章

  • csv文件保存
    #爬取起点中文网的书名作者并用csv文件保存importrequestsimportcsvfromlxmlimportetreeua={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/......
  • 实验6 模板类和文件I/O
    实验任务31#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain(){6usingnamespacestd;7array<int,N>x{......
  • 实验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最被人熟知的作用就是反向代理。反向代理:因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据......