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

实验6 模板类和文件

时间:2022-12-03 18:11:16浏览次数:32  
标签:std 文件 int Vector 实验 output include 模板 out

1.实验3

task3_1.cpp

 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     out.write(reinterpret_cast<char *>(&x), sizeof(x));
19     out.close();
20 }

task_3.2.cpp

 

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 
 6 int main() {
 7     using namespace std;
 8     array<int, N> x;
 9 
10     ifstream in;
11     in.open("data1.dat", ios::binary);
12     if(!in.is_open()) {
13         cout << "fail to open data1.dat\n";
14         return 1;
15     }
16 
17     in.read(reinterpret_cast<char *>(&x), sizeof(x));
18     in.close();
19 
20     for(auto i = 0; i < N; ++i)
21         cout << x[i] << ", ";
22     cout << "\b\b \n";
23 }

运行结果:

 

 

 

 修改后的task3_1.cpp

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 
 6 int main() {
 7     using namespace std;
 8 
 9     array<char, 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     out.write(reinterpret_cast<char *>(&x), sizeof(x));
19     out.close();
20 }

运行结果

 

 

 

 问题:

 

 

2.实验4

VecotrT.hpp

 1 #include<iostream>
 2 using namespace std;
 3 template<typename T>
 4  class Vector{
 5       private:
 6            int size;
 7            T *data;
 8      public:
 9          Vector(int n){
10              size=n;
11              data=new T[n];
12              T m;
13              m=0;
14              for(int i=0,m;i<n;i++,m++)
15                  data[i]=m;
16          }
17          Vector(int n,T value)
18          {
19              size=n;
20              data=new T[n];
21              for(int i=0;i<n;i++)
22                  data[i]=value;
23          }
24          Vector(const Vector<T> &p):size{p.size}
25          {
26              data=new T[p.size];
27              for(int i=0;i<p.size;i++)
28              data[i]=p.data[i];
29          }
30          ~Vector()
31          {};
32          int get_size()
33          {
34               return size;
35          }
36          T &at(int i)
37          {
38               return data[i];
39          }
40          T &operator [](int i)
41          {
42               return data[i];
43           } 
44         template<typename T1>
45         friend void output(const Vector<T1> &p);
46  };
47   template<typename T1>
48   void output(const Vector<T1> &p)
49   {
50        for(int i=0;i<p.size;i++)
51        cout<<p.data[i]<<",";
52        cout<<endl;
53   }
54  

task4.cpp

 1 #include <iostream>
 2 #include "vectorT.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 }

运行结果

 

 3.实验5

 task5.cpp

 1 #include <iostream>
 2 #include <fstream>
 3 #include<istream>
 4 #include<iomanip>
 5 using namespace std;
 6  void output(std::ofstream &out)
 7  {       
 8          out.open("cipher_key.txt",ios::app);
 9          out<<setw(4);
10          cout<<setw(4);
11          for(char a='a';a<='z';a++)
12          {
13               out<<a<<" ";
14               cout<<a<<" ";
15          }
16          out<<endl;
17          cout<<endl;
18          for(int i=1;i<=26;i++)
19          {
20              out<<setw(2)<<i;
21              cout<<setw(2)<<i;
22              int c;
23              for(char b='A'+(i%26),c=1;c<=26;b++,c++)
24              {
25                   if(b>'Z')
26                   {
27                        b-=25;
28                   }
29                 out<<setw(2)<<b;
30                 cout<<setw(2)<<b;
31              }
32              out<<endl;
33              cout<<endl;
34          }
35          out.close();
36  }
37 int main() {
38     ofstream out;
39     output(out);
40 }

 运行结果

 

 

 

 

 

   

标签:std,文件,int,Vector,实验,output,include,模板,out
From: https://www.cnblogs.com/lc114514/p/16939177.html

相关文章

  • 另一个程序已锁定文件的一部分,进程无法访问 打不开磁盘,模块“Disk”启动失败。 未能
    问题描述另一个程序已锁定文件的一部分,进程无法访问产生原因在上网查阅资料得知,这是一种​​虚拟机​​的保护机制虚拟机在运行时,为防止数据被篡改,会将所运行的文件保护起来......
  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • @FileLimit – AOP最佳实践:上传文件大小限制
    @FileLimit结构分析   1、FileLimitUnit定义枚举:文件的单位publicenumFileLimitUnit{KB,MB,GB}2、定义注解importorg.springframework.core.a......
  • 阅读原文怎么加文件
    公众号阅读原文是唯一一处可以放外部链接的地方,有不少人习惯用它来链接到文件下载地址,引导粉丝点击阅读原文,打开文件内容。我们可以通过这个方式给公众号添加附件。阅读原......
  • 实验六 模板类和文件
    实验三task3.11#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain(){6usingnamespacestd;7array<int,N>x{97,......
  • 实验6
    TASK3:#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};of......
  • C++学习------csetjmp头文件的源码学习
    引言csetjmp是C++对setjmp.h头文件的封装,通过这个头文件提供的工具允许程序员通过提供执行跳转的方法来绕过正常的函数调用和返回规程,从而保留调用环境。其中定义了一些函数......
  • oop 实验6 模板类和文件I/O
    task3程序源码task3_1.cpp1#define_CRT_SECURE_NO_WARNINGS12#include<iostream>3#include<fstream>4#include<array>5#defineN56usingnames......
  • gps.conf文件解读
    https://www.cnblogs.com/zhuwei0901-yanwu/p/9485607.html 手机adbshell修改gps.conf文件后,导入记得更改权限。adbshell里面要有/system/framework里面的com.g......