首页 > 其他分享 >实验6

实验6

时间:2022-11-30 18:55:42浏览次数:41  
标签:++ int Vector 实验 include open out

实验任务3:

task3_1.cpp
#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;
    }

    out.write(reinterpret_cast<char *>(&x), sizeof(x));
    out.close();
}
task3_2.cpp
#include <iostream>
#include <fstream>
#include <array>
#define N 5

int main() {
    using namespace std;
    array<char, N> x;

    ifstream in;
    in.open("data1.dat", ios::binary);
    if(!in.is_open()) {
        cout << "fail to open data1.dat\n";
        return 1;
    }

    in.read(reinterpret_cast<char *>(&x), sizeof(x));
    in.close();

    for(auto i = 0; i < N; ++i)
        cout << x[i] << ", ";
    cout << "\b\b \n";
}

 

 当把

array<int, N> x; ----> 修改成: array<char, N> x;

 

     原因: 当array数组储存字符型时,读入文件内容”a b c d e f“将把空格也读成字符。

 实验任务4:

Vector.hpp:

#include <iostream>
using namespace std;
template<typename T>
class Vector {
public:
	Vector(int n) :size{ n } {
		p = new T[n];

	}
	Vector(T n, T value) :size{ n } {
		p = new T[n];
		
		for (auto i = 0; i < size; i++) {
			p[i] = value;
		}
	}
	Vector(Vector& vi) :size{ vi.size } {
		cout << "copy constructor called." << endl;
		p = new T[size];
		for (auto i = 0; i < size; ++i)
		{
			p[i] = vi.p[i];
		}

	}
	~Vector() = default;

	T get_size() { return size; }
	
	T& at(int index) { return p[index]; }
	
	friend void output(Vector& x)
	{
		int i;
		for (i = 0; i < x.size - 1; i++)
		{
			cout << x.p[i] << ", ";
		}
		cout << x.p[i] << endl;
	}
	T & operator [](int n) { return p[n]; }
private:
	T size;
	T* p;
};

  

 

task:

#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();
}

  

 

 

 

 实验任务5:

 

#include <iostream>
#include<fstream>
#include<string>
using namespace std;
void output() {
    string a[27][27] = { " " };
    int i, j, n = 0;
    for (i = 1; i < 27; i++) {
        a[i][0] = to_string(i);
        a[0][i] = 97 + n;
        n++;
    }
    for (i = 1; i < 27; i++) {
        n = i;
        for (j = 1; j < 27; j++) {
            if (65 + n > 90)
            {
                a[i][j] = 65 + n - 26;
            }
            else {
                a[i][j] = 65 + n;
            }
            n++;
        }

    }


    ofstream out;
    out.open("cipher_ke.txt");
    if (!out.is_open()) {
        cout << "fail to open cipher_key.txt to write" << endl;
    }

    for (i = 0; i < 27; i++) {

        for (j = 0; j < 27; j++) {
            cout << a[i][j] << " ";
            out << a[i][j] << " ";
        }
        cout << endl;
        out << endl;
    }



}

int main() {
    
    output();

}

 

 

 

 

标签:++,int,Vector,实验,include,open,out
From: https://www.cnblogs.com/lovelexington/p/16938186.html

相关文章

  • 数据结构实验之栈与队列二:一般算术表达式转换成后缀式 sdut-oj
    #include<stdio.h>#include<stdlib.h>#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT100typedefstruct{  char*base;  ......
  • 数据结构实验之栈与队列四:括号匹配 sdut-oj
    #include<stdio.h>#include<stdlib.h>#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10typedefstruct{  char*base;  ......
  • 实验六
    task4Vector.hpp:1#pragmaonce2#include<bits/stdc++.h>3usingnamespacestd;4template<typenameT>5classVector6{7private:8intsize;......
  • 数字逻辑实验 9 FPGA数字钟(Verilog)
    目录实验9FPGA数字钟实验分析:实现思路:硬件支持:硬件描述语言代码编写:1顶层模块2时钟分频,(正/倒)计时器模块3输入处理模块in_out.v5......
  • 实验6 模板类和文件I/O
    实验目的体验模板函数、模板类的编写,从多态角度理解模板函数和模板类(类型作为参数)体验标准I/O流类、文件I/O流类、字符串I/O流类的用法,能正确使用针对问题场景,能正确、......
  • web实验四(二)
    web实验四(二)基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.Web服务器的客户端服务器,提交程序运行截图2.实现GET即可,请求,响应要符合HTTP协议规范3.服......
  • 实验四 Web服务器2
    基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.Web服务器的客户端服务器,提交程序运行截图2.实现GET即可,请求,响应要符合HTTP协议规范3.服务器部署到......
  • 焱融科技为国家重点实验室打造海量高性能存储
    中国科学院大气物理研究所大气科学和地球流体力学数值模拟国家重点实验室(英文缩写LASG)是国家级重点实验室。LASG主要研究方向为地球气候系统模式的研发与应用;天气气候动力......
  • 实验六
    task3task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,......
  • 实验四 Web服务器2
    基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.Web服务器的客户端服务器,提交程序运行截图2.实现GET即可,请求,响应要符合HTTP协议规范3.服务器部署到华......