首页 > 其他分享 >实验六

实验六

时间:2022-12-06 23:23:21浏览次数:40  
标签:int ++ Vector 实验 x2 output include

实验三

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;
    }
    // 把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入文件data1.txt
    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<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";
}

结果:

修改后

结果:

原因:

录入记事本中的数据中,ab之间有三个空格字符,使用int类型时会将其空格字符自动忽略,而char类型会将空格字符一同录入。

实验四

Vector.hpp

#pragma once
#include<iostream>
using namespace std;
template<typename T>
class Vector 
{
public:
    Vector(int n) :size(n) { data = new T[n]; }
    Vector(int n,T obj) :size(n)
    {
        int i;
        data = new T[n];
        for (i = 0; i < n; i++)
            data[i] = obj;
    }
    Vector(const Vector<T>& a) :size{ a.size } {
        data = new T[size];
        for (int i = 0; i < size; i++)
            data[i] = a.data[i];
    }
    T& operator[](int i)
    {
        return data[i];
    }
    T& at(int i)
    {
        return data[i];
    }
    int get_size()const
    {
        return size;
    }
    friend void output(const Vector<T>& obj)
    {
        int i;
        for (i = 0; i < obj.size; i++)
            cout << obj.data[i] << ",";
        cout << endl;
    }
private:
    int size;
    T* data;
};

task.cpp

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

结果

 

修改数据后

#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, 16);
    Vector<int> x3(x2);
    output(x2);
    output(x3);
    x2.at(0) = 66;
    output(x2);
    x3[0] = 99;
    output(x3);
}
int main() {
    test();
}

结果

实验五

task_5.cpp

#include <iostream>
#include<fstream>
#include<string>
using namespace std;
void output(std::ostream& out) {
    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++;
        }
    }
    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() 
{
    ofstream out;
    out.open("cipher_ke.txt");
    if (!out.is_open()) {
        cout << "fail to open cipher_key.txt to write" << endl;
    }
    output(out);
    out.close();
}

结果:

 

标签:int,++,Vector,实验,x2,output,include
From: https://www.cnblogs.com/qiumusu/p/16961742.html

相关文章

  • 程序设计基础实验课 单元六的题-UVA10410 TreeReconstruction 树重建
    入门指南题面:  洛谷题面:   观看的题解:https://www.cnblogs.com/jerryRey/p/4622927.html  对样例区样例画的一些图:       题目的一些争议......
  • 实验六
    task3-1cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六
    试验任务三task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验六
    #pragmaonce#include<iostream>usingstd::cout;usingstd::endl;template<typenameT>classVector{public:Vector(Tn):size(n){p=newT[n];}V......
  • 实验六
    实验任务3#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六 模板类和文件IO
    任务四代码:Vector.hpp:#pragmaonce#include<iostream>usingnamespacestd;template<classT>classVector{public:Vector(intsize0):size{size......
  • 实验六 模板类和文件IO
    Task4//VEctor.hpp#include<bits/stdc++.h>usingnamespacestd;template<typenameT>classVector{private:intlength;T*base;publ......
  • 包层次的时间同步实验
    一、实验目的理解掌握时间同步的重要意义了解时间同步的基本方法,理解TPSN协议机理掌握TinyOS定时器相关接口和组件的使用4.能够在TinyOS中实现多节点间的时间......
  • SRP实验
    一、实验目的学习源路由协议的机制理解节点在源路由协议中的几种角色掌握源路由协议相关接口SourceRouteSend、SourceRoutePacket的使用 二、实验要求功能:root......