首页 > 其他分享 >实验六

实验六

时间:2022-12-07 00:44:35浏览次数:32  
标签:int Vector 实验 template output include size

Task3:

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<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.read(reinterpret_cast<char*>(&x), sizeof(x));
    in.close();

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

 

 运行结果:

 

 改动后:

 

 原因:

in.write(reinterpret_cast<char*>(&x), sizeof(x)),输入时,x是int类型,读取时也是int类型,int占四个字节,而char每次只读一个字符,因此修改后读取a后,又读取了三个空格,之后接着读b。

Task4:

Vector.hpp

#pragma once
#include<iostream>
#include<cassert>
using namespace std;
template<typename T>
class Vector {
public:
    Vector(int n);
    Vector(int n, int v);
    Vector(const Vector<T>& x);
    ~Vector() = default;

    int get_size() { return size; }
    T& at(int i);
    T& operator[](int i);
    template<typename T1>
    friend void output(const Vector<T1>& x);
private:
    T* p;
    int size;
};
template<typename T>
Vector<T>::Vector(int n) :size{ n } {
    p = new T[size];
}
template<typename T>
Vector<T>::Vector(int n, int v) : size{ n } {
    p = new T[size];
    for (int i = 0; i < size; i++)
        p[i] = v;
}
template<typename T>
Vector<T>::Vector(const Vector<T>& x) {
    size = x.size;
    p = new T[size];
    for (int i = 0; i < size; i++)
        p[i] = x.p[i];
}
template<typename T>
T& Vector<T>::at(int i) {
    assert(i >= 0 && i < size);
    return p[i];
}
template<typename T>
T& Vector<T>::operator[](int i) {
    assert(i >= 0 && i < size);
    return p[i];
}
template<typename T1>
void output(const Vector<T1>&x) {
    for (int i = 0; i < x.size; i++) {
        cout << x.p[i] << ",";
    }
    cout << "\b " << endl;
}

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 * 1.3;
    output(x1);
    Vector<int> x2(n, 42);
    Vector<int> x3(x2);
    output(x2);
    output(x3);
    x2.at(0) = 25;
    output(x2);
    x3[0] = 897;
    output(x3);
}
int main() {
    test();
}

运行结果:

 

 Task5:

 task5.cpp

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void output(std::ostream& out) {
    char a[26][26];
    cout << " ";
    out << " ";
    for (char i = 'a'; i <= 'z'; i++) {
        cout << setw(2) << i;
        out << setw(2) << i;
    }
    cout << endl;
    out << endl;
    for (int i = 0; i < 26; i++) {
        cout << setw(2) << i + 1;
        out << setw(2) << i + 1;
        for (int j = 0; j < 26; j++) {
            a[i][j] = (char)(65 + (i + j + 1) % 26);
            cout << setw(2) << a[i][j];
            out << setw(2) << a[i][j];
        }
        cout << endl;
        out << endl;
    }
}
int main() {
    ofstream out;
    out.open("cipher_key.txt");
    if (!out.is_open()) {
        cout<<"fail to open cipher_key.txt."<<endl;
        return 1;
    }
    output(out);
    out.close();
}

运行结果:

 

 

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

相关文章

  • Python实验报告——第6章 函数
    实例01:输出每日一帖(共享版) 在IDLE中创建一个名称为function_tips.py的文件,然后在该文件中创建一个名称为function_tips的函数,在该函数中,从励志文字列表中获取一条......
  • 实验六
    task3测试结果:      源代码:1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingname......
  • 实验六 模板类与文件I/O
     task3task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89a......
  • 实验6 模板类和文件IO
    2022.11.30OOP实验实验6模板类和文件IO任务3task36_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespac......
  • Python实验报告——第5章 字符串及正则表达式
    实例01:使用字符串拼接输出一个关于程序员的笑话 在IDLE中创建一个名称为programmer_splice.py的文件,然后在该文件中定义两个字符串变量,分别记录两名程序说的话,再将......
  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};of......
  • 实验六
    实验三task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,......
  • 程序设计基础实验课 单元六的题-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......