首页 > 其他分享 >实验6

实验6

时间:2022-12-04 15:13:53浏览次数:38  
标签:num int Vector 实验 template include size

一、实验目的

  1. 体验模板函数、模板类的编写,从多态角度理解模板函数和模板类(类型作为参数)
  2. 体验标准I/O流类、文件I/O流类、字符串I/O流类的用法,能正确使用
  3. 针对问题场景,能正确、合理使用流类库对I/O数据进行格式化和读、写操作
  4. 训练综合应用类的封装、继承、多态特性及现代C++标准库编写正确、高效、安全代码

二、实验内容

实验任务4

程序代码

Vector.hpp

#pragma

#include<iostream>

using namespace std;

template<typename T>
class Vector{
public:
    Vector(int s0);
    Vector(int s0, T val);
    Vector(const Vector<T>& t);
    ~Vector();

    int get_size();
    T &at(int i);

    T& operator[](int i); 

    template<typename T1>
    friend void output(Vector<T1> &m);

private:
    int size;
    T *num;
};

template<typename T>
Vector<T>::Vector(int s0): size{s0}{
    num = new T[s0];
}

template<typename T>
Vector<T>::Vector(int s0, T val): size{s0}{
    num = new T[s0];
    for(int i =0; i < s0; i++){
        num[i]=val;
    }
}

template<typename T>
Vector<T>::Vector(const Vector<T>& t): size{t.size}{
    num = new T[size];
    for (int i = 0; i < size; i++)
    {
        num[i] = t.num[i];
    }
    
}

template<typename T>
Vector<T>::~Vector(){delete num;}

template<typename T>
int Vector<T>::get_size(){
    return size;
} 

template<typename T>
T &Vector<T>::at(int i){
    return num[i];
}
template<typename T>
T& Vector<T>::operator[](int i){
    return num[i];
}

template<typename T1>
void output(Vector<T1> &m){
    for (int i = 0; i < m.size; i++)
    {
        cout << m.num[i] << " ";
    }
    cout << 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 * 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

程序代码

task5.cpp

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void output(ostream &out){
    char a[27][27];
    cout << "  ";
    out << "  ";
    for(int i = 1; i <= 26; i++)
    {
        cout << setw(2) << setfill(' ') << right << char(i + 'a'-1);
        out << setw(2) << setfill(' ') << right << char(i + 'a'-1); 
    }
    cout << endl; 
    out << endl; 
    int m;
    for(int i = 1; i < 27; i++)
    {
        m = 65 + i;
        for (int j = 1; j < 27; j++)
        {  
            if(m > 90) m = 65;
            a[i][j]= char(m);
            m++;
            
        }        
    }
    for(int i = 1; i < 27; i++)
    {
        cout << setw(2) << setfill(' ') << right << i;
        out << setw(2) << setfill(' ') << right << i;
        for (int j = 1; j < 27; j++)
        {
            cout << setw(2) << setfill(' ') << right << a[i][j];
            out << setw(2) << setfill(' ') << right << 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\n";
        return 1;
    }
    output(out);
    out.close();
}

结果截图

实验任务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<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";
}

结果截图

三、实验总结

通过本次实验,我了解了模板类的应用和c++文件操作函数的使用方法。并且能够自主写出相关的模板类,利用文件操作函数实现相关功能。

标签:num,int,Vector,实验,template,include,size
From: https://www.cnblogs.com/civilwen/p/16949359.html

相关文章

  • 二十三. 单臂路由实验和三层交换实验
    1.单臂路由实验图配置流程第1个里程:配置交换机2接口配置<Huawei>sys[Huawei]sysnameS1[S1]vlanbatch1020[S1]intg0/0/2[S1-GigabitEthernet0/0/2]portl......
  • java实验四
    一、实验目的掌握Java语言中final关键字的含义及使用方法;掌握Java语言中抽象类的基本概念及使用方法;掌握Java语言中接口的基本概念及使用方法;理解程序设计中常用数据......
  • 实验6 模板类和文件OL
    task3-1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101......
  • 实验6
    task3_1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};ofstreamo......
  • Python 第11章 上机实验
    说明:导入pymysql包,关于使用mysql的代码,只能在我的电脑使用,同时我抹去了使用mysql的账号秘密importsqlite3#连接到SQLite数据库conn=sqlite3.connect('mrsoft.db')......
  • 实验三)
           实验目的1、熟练Wireshark的使用;2、分析TCP三次握手的工作原理;分析UDP协议;3、研究Ping操作与ARP协议的关系;4、分析DHCP与DNS协议的工作原......
  • 实验二
     实验目的1、了解和认识常见的网络设备及其功能;2、了解双绞线的制作过程;3、掌握简单局域网的搭建;4、掌握一些常见命令的使用;命令的含义和相关的操作。实验原......
  • 实验一
     实验目的1、了解和认识常见的网络设备及其功能;2、了解双绞线的制作过程;3、掌握简单局域网的搭建;4、掌握一些常见命令的使用;命令的含义和相关的操作。实验原......
  • 实验6 模板类和文件
    1.实验3task3_1.cpp1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89......