首页 > 其他分享 >实验6

实验6

时间:2022-12-03 11:33:06浏览次数:33  
标签:int Vector 实验 template output include size

实验6.3

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

task3.2

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 
 6 int main() {
 7     using namespace std;
 8     array<char, 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 }

用int整型存储每个数字占4个字节;存在文本文件中4个字节为一个单位,而给的数字仅需一个字节存储,剩下的三个字节为空,task2以字节形式读入,读入5个字节。

task4

Vector.hpp

 

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

task5.cpp

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

 

 

标签:int,Vector,实验,template,output,include,size
From: https://www.cnblogs.com/gzj035/p/16947219.html

相关文章

  • Python实验报告
    实验13:Pygame游戏编程一、实验目的和要求学会Pygame的基本应用二、Pygame的优点及应用  使用Python进行游戏开发的首选模块就是Pygame,专为电子游戏设计(包括图像、......
  • 基于yolo进行目标检测的实验和研究【BLOG】
          根据我接触到的项目经验来看,需要我们进行检测的不是自然场景下的任意物体,而是特定场景下一类物体。典型的就是钢管识别,这些照片一般都是在厂区里面拍的、......
  • Python实验报告——第13章 Pygame游戏编程
    实验报告实例01:制作一个跳跃的小球游戏代码如下:importsysimportpygamepygame.init()size=width,height=640,480screem=pygame.display.set_mode(size)c......
  • 3.2动态分析基础实验(2)--《恶意代码分析实战》
    实验三Lab03-03.exe1.当使用ProcessExplorer工具进行监视的时候,注意到了什么?2.可以找出内存修改的行为吗?3.这个恶意代码在主机上的感染迹象特征是什么?4.这个恶意代码......
  • Python实验报告(第13章)
    实验13:Pygame游戏编程一、实验目的和要求学会Pygame的基本应用二、实验环境软件版本:Python3.1064_bit三、实验过程1、实例1:制作一个跳跃的小游戏(1)代码如下:1......
  • 2.1 实验:反病毒引擎扫描、编译时间、查壳、导入表查看、字符串查看--《恶意代码分析
    实验内容:1、将文件上传到http://www.VirusTotal.com进行分析并查看报告。文件匹配到了已有的反病毒软件特征吗?(国内用https://www.virscan.org/替代)2、这些文件是什么时候......
  • 2.2 实验:UPX脱壳--《恶意代码分析实战》
    Lab01-02.exe实验内容:1、将文件上传到http://www.VirusTotal.com进行分析并查看报告。文件匹配到了已有的反病毒软件特征吗?2、是否有这个文件被加壳或混淆的任何迹象?如......
  • 2.3 实验:用linxerUnpack进行通用脱壳--《恶意代码分析实战》
    Lab01-03.exe  实验内容: 1、将文件上传到http://www.VirusTotal.com进行分析并查看报告。文件匹配到了已有的反病毒软件特征吗? 2、是否有这个文件被加壳或混淆......
  • Linux-iscsi存储服务配置实验
    1)给一台centos7虚拟主机添加一个硬盘50G硬盘,同时由一台centos7虚拟机克隆后修改克隆主机IP地址。Crt连上两台虚拟机,改主机名:#先添加硬盘在开机省略掉扫描总线步骤#ifcon......
  • 实验6 模板类和文件I/O
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......