首页 > 其他分享 >实验6 模板类和文件IO

实验6 模板类和文件IO

时间:2022-12-07 00:01:08浏览次数:39  
标签:index int Vector 实验 IO output include 模板 size

2022.11.30 OOP实验

实验6 模板类和文件IO

任务3

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

运行结果:

task36_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";
}

运行结果:

修改过后:

#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";
}

运行结果:

分析:
a的ASCII码为97,所以97->a
<int,5>占20个字节,<char,5>占5个字节,int型转成char型时多余的三个字节转成空格
b的ASCII码为98,所以98->a

任务4


Vector46.hpp

#pragma once
#include<iostream>
#include<cassert>

using namespace std;

// delaration of template class
template<typename T>
class Vector {
public:
    Vector(int n) :size{ n } {
        p = new T[n];
    }

    Vector(int n, T value) :size{ n } {
        p = new T[n];
        for (int i = 0; i < size; i++) {
            p[i] = value;
        }
    }

    Vector(const Vector& v) :size{ v.size } {
        p = new T[size];
        for (int i = 0; i < size; i++) {
            p[i] = v.p[i];
        }
    }

    ~Vector() {
        delete[] p;
    }

    int get_size() { 
        return size; 
    }

    T& at(int index) {
        assert(index >= 0 && index < size);
        return p[index];
    }

    T& operator[](int index) {
        assert(index >= 0 && index < size);
        return p[index];
    }

    template<typename T1>
    friend void output(const Vector<T1>& v);
private:
    int size;
    T* p;
};

template<typename T1>
void output(const Vector<T1>& v) {
    for (int i = 0; i < v.size; i++) {
        cout << v.p[i] << " ,";
    }
    cout << endl;
}

tesk46.cpp

#include <iostream>
#include "Vector46.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



task56.cpp

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace std;

void output(std::ostream& out) {
    char a[26][26];
    cout << "   ";
    out << "   ";
    for (char i = 'a'; i <= 'z'; i++) {
        cout << i << " ";
        out << i << " ";
    }
    cout << endl;
    out << endl;

    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            a[j][i] = 'A' + char((j + i ) % 26);
        }
    }
    for (int i = 0; i < 26; i++) {
        cout << setw(2) << i + 1;
        out << setw(2) << i + 1;
        for (int j = 0; j < 26; j++) {
            cout  << setw(2)  << setfill(' ') << a[i][j];
            out  << setw(2)  << setfill(' ') << a[i][j];
        }
        cout << endl;
        out << endl;
    }
}
int main() {
    ofstream out;
    out.open("cipher_key.txt");
    if (!out.is_open()) {
        cout << "fail to open file ans.txt to write" << endl;
        return 1;
    }
    output(out);
    out.close();
}

运行结果:

标签:index,int,Vector,实验,IO,output,include,模板,size
From: https://www.cnblogs.com/Aquarius-CHEqijin/p/16961861.html

相关文章

  • 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  对样例区样例画的一些图:       题目的一些争议......
  • 【原创】Databricks 更改hive metastore version
    在DatabricksRuntime7.0及更高版本上,Hive1.2.0和1.2.1不是内置的元存储。如果要将Hive1.2.0或1.2.1与DatabricksRuntime7.0及更高版本一起使用,请按照[下......
  • 实验六
    task3-1cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to curr
    报错:Uncaught(inpromise)NavigationDuplicated:Avoidedredundantnavigationtocurrentlocation:"/xxx".atcreateRouterError这个错误是说着在promise里捕获了......
  • 实验六
    试验任务三task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89......
  • MAUI新生3.4-深入理解XAML:数据模板DataTemplate
    数据模板主要作用是定义集合类控件的数据显示外观,和前面几个章节自定义控件的关系不大。数据模板本质上是定义集合的每一个迭代对象的UI,和Vue的v-for或Blazor的foreach类似......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......