首页 > 其他分享 >实验6

实验6

时间:2022-11-30 22:11:59浏览次数:45  
标签:arr int length Vector 实验 output include

实验内容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";
}

运行截图:

修改后运行截图:

 原因分析:

task3_2中以字节数据块方式写入文件data1.txt, char类型一次只读取一个字节长度,int 类型占四个字符的长度,x[0]读a,x[1]--x[3]读" ",x[4]读b,所以输出如此。

实验内容4:

 Vector.hpp

#include<iostream>
using namespace std;
template<typename T>
class Vector {
public:
    Vector(int n) {
        length = n;
        arr = new T [n];
    }
   Vector(int  n, T m) {
        length = n;
        arr = new T[n];
        for (int i = 0; i < n; i++)arr[i] = m;
    }
    Vector(Vector<T>& x) {
        length = x.get_size();
        arr = new T[length];
        for (int i = 0; i < length; i++)arr[i] = x.arr[i];
    }
    T& operator [](int n) {
        return arr[n];
    }
    T& at(int n) {
        return arr[n];
    }
    
    T get_size() {
        return length;
    }
    ~Vector(){}


private:
    
      int length;T *arr;
      
 template<typename T1>
 friend void output(Vector<T1>& x);
};
template<typename T1>
void output (Vector<T1>& x) {
    for (int i = 0; i < x.get_size(); i++)
        cout << x.at(i) << " ";
    cout << '\n';
}

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

运行测试截图:

 

实验内容五:

实验代码:

task6.cpp

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void output(std::ostream &out){

        cout << " ";
    out<<" ";
    char a[26];
    for (int i = 0; i < 26; i++)
    {
        a[i] = 97 + i;
        cout << " " << a[i];
        out << " " << a[i];
    }
    cout << endl;
    out << endl;
    int k = 0;
    char b[100];
    for (int i = 1; i <= 26; i++) {
        cout << i << " ";
        out << i << " ";
        for (int j = 0; j < 26; j++)
        {
            b[k] = (a[j] - 97 + i % 26) % 26 + 65;
            cout << b[k] << " ";
            out << b[k] << " ";
        }
        cout << endl;
        out << endl;
    } 

}
int main() {

    ofstream outFile("cipher_key.txt", ios::binary);
    output(outFile);
    return 0;
  
}

运行结果:

 

标签:arr,int,length,Vector,实验,output,include
From: https://www.cnblogs.com/akumanpower/p/16938885.html

相关文章

  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六:模板类和文件
    w实验任务三task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六
    6.3#include<iostream>#include<fstream>#include<array>#defineN5voidtest1(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验6 模板类和文件I/O
    1.程序源码task1#include<iostream>#include<fstream>#include"Complex.hpp"voidtest1(){usingnamespacestd;Complex<double>c1{1,2},c2;......
  • 实验四 Web服务器2
    实验四Web服务器2基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:Web服务器的客户端服务器,提交程序运行截图实现GET即可,请求,响应要符合HTTP协议规范服务......
  • 实验六
    task3_1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;//array<int,N>x{97,98,99,100,10......
  • 实验6
    实验任务3:task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98......
  • 数据结构实验之栈与队列二:一般算术表达式转换成后缀式 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;......