首页 > 其他分享 >实验6

实验6

时间:2022-12-03 16:12:43浏览次数:33  
标签:int Vector 实验 output include open out

TASK3:

#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.dat", 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();
}
CPP1
#include <iostream>
#include <fstream>
#include <array>
#define N 5
int main() {
    using namespace std;
    array<int, N> x;
    ifstream in;
    in.open("data1.dat", ios::binary);
    if (!in.is_open()) {
        cout << "fail to open data1.dat\n";
        return 1;
    }
    // 从文件流对象in关联的文件data1.dat中读取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";
}
CPP2

改动之后结果如图所示,首先内存是连续储存的,int类型占4个字节,char类型占1个,储存就空的部分自动补零,所以一开始97,输出a,而a之后连续三个内存0,所以输出错误,第五个内存单元则储存98,输出b

TASk4:

#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 * 0.7;

    output(x1);

    Vector<int> x2(n, 42);
    Vector<int> x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;
    output(x2);

    x3[0] = 999;
    output(x3);
}

int main() {
    test();
}
cpp

 

#pragma once
#include <iostream>
using namespace std;

template <typename T>
class Vector
{
public:
    Vector(int n)
    {
        size = n;
        t = new T[n];
    }
    Vector(int n, T a)
    {
        size = n;
        t= new T[n];
        for (int i = 0; i < n; i++)
        {
            t[i] = a;
        }
    }
    T& at(int n)
    {
        return t[n];
    }
    T& operator[](int n)
    {
        return t[n];
    }
    int getsize()
    {
        return size;
    }

private:
    int size;
    T* t;
};
template <typename T>
void output(Vector<T>& t)
{
    for (int i = 0; i < t.getsize(); i++)
        cout << t[i] << ' ';
    cout << endl;
}
hpp

Task5:

#include<iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void output(std::ostream& out);
int main() {
    ofstream f;
    f.open("cipher_key.txt");
    if (!f.is_open()) {
        cout << "fail to open file " << "cipher_key" << endl;
    }
    output(f);
    f.close();
    output(cout);

}
void output(std::ostream& out) {
    char code[30];
    char Code[30];
    for (int i = 0; i < 26; i++) {
        code[i] = 'a' + i;
    }
    out << "    ";
    for (int i = 0; i < 26; i++) {
        out << code[i] << " ";
    }
    out << "\n";
    for (int i = 1; i <= 26; i++) {
        for (int t = 0; t < 26; t++) {

            if (t + i <= 25) {
                Code[t] = 'A' + t + i;
            }
            else if (t + i > 25) {
                Code[t] = 'A' + t + i - 25 - 1;
            }
        }
        out << setw(2) << i << " ";
        for (int t = 0; t < 26; t++) {
            out << Code[t] << " ";
        }
        out << "\n";

    }
}
View Code

 

标签:int,Vector,实验,output,include,open,out
From: https://www.cnblogs.com/Xl995/p/16948195.html

相关文章

  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};of......
  • oop 实验6 模板类和文件I/O
    task3程序源码task3_1.cpp1#define_CRT_SECURE_NO_WARNINGS12#include<iostream>3#include<fstream>4#include<array>5#defineN56usingnames......
  • python实验报告(第13章)
    一、实验目的1.掌握Pygame的基础知识。二、实验环境python版本:3.10(64-bit)三、实验内容1.实例1  实验结果:  四、实验分析:1.掌握了Pygame的基础知识。......
  • 2022/12/3 Python实验报告
      实验报告1、实验目的和要求了解并掌握Pygame的基本应用2、实验环境笔记本与Python书本3、实验过程实例01制作一个跳跃的小球游戏创建一个游戏......
  • 实验6
    实验6.3task3.11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89arr......
  • 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......