首页 > 其他分享 >实验六 模板类和文件I/O

实验六 模板类和文件I/O

时间:2022-12-05 13:25:23浏览次数:34  
标签:文件 int namespace Vector 实验 include open 模板 out

实验任务3

#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("data.txt",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();
}
#include<iostream>
#include<fstream>
#include<array>
#define N 5
int main(){
    using namespace std;
    array<int,N>x;
    ifstream in;
    in.open("data.txt",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("data.txt",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";
}

 

 实验任务4

Vector.h

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

task4.cpp

#include <iostream>
#include "Vector.h"

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.5;

    output(x1);

    Vector<int> x2(n, 66);
    Vector<int> x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 88;
    output(x2);

    x3[0] = 99;
    output(x3);
}

int main() {
    test();
}

实验任务5

#include <iostream>
#include<fstream>
#include<string>
using namespace std;
void output() {
    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++;
        }

    }


    ofstream out;
    out.open("cipher_ke.txt");
    if (!out.is_open()) {
        cout << "fail to open cipher_key.txt to write" << endl;
    }

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

 

标签:文件,int,namespace,Vector,实验,include,open,模板,out
From: https://www.cnblogs.com/DeviIMayCry/p/16952006.html

相关文章

  • 在asp.net 2.0中的web.config文件中调用外部文件
    在一个工作项目或者工作小组中,有可能经常要转换工作的调试环境,比如开发环境,测试环境,部署环境,这样有可能要对web.config文件进行修改或改动,比如......
  • JAX-RS之下载文件
    今天学习两个,分别是JAX-RS之下载文件  首先,看例子,下载服务器的文本文件  ​1.@Path("/file")2.publicclass3.4.privatestaticf......
  • extjs4,spring mvc3上传文件
    本文讲解下extjs4结合springmvc3的注解完成上传文件的例子。1页面文件  <!--ExtJSFiles--><linkrel="stylesheet"type="text/css......
  • TinyShell(CSAPP实验)
    简介CSAPP实验介绍学生实现他们自己的带有作业控制的UnixShell程序,包括Ctrl+C和Ctrl+Z按键,fg,bg,和jobs命令。这是学生第一次接触并发,并且让他们对Unix的进程控制、......
  • Selenium4+Python3系列(九) - 上传文件及滚动条操作
    一、上传文件操作上传文件是每个做自动化测试同学都会遇到,而且可以说是面试必考的问题,标准控件我们一般用​​send_keys()​​​就能完成上传,但是我们的测试网站的上传控件......
  • Linux文件系统
    1.Linux的分区创建后还不能直接访问,还需要挂载到某个目录下;2.分区创建好后,此分区会有个分区名,然后需要对该分区以特定文件系统进行格式化(比如ext4);3.通过命令将此格式化后......
  • win8激活时出现”错误代码:0×8007007B 错误描述:文件名、目录名或卷标语法不正确“解
    今天装了Win8Pro但是在激活的时候提示”错误代码:0×8007007B错误描述:文件名、目录名或卷标语法不正确“,效果图如下:解决方法:安装好后在桌面点击win+X再点A复制以下内容:......
  • 不懂业务不清楚指标?这40套可视化大屏模板,让你突破职场天花板
    报表可以说是中国职场的一大特色,不少职场人需要每天做各种报表给领导或者业务决策者看,为此甚至诞生了不少的“表哥表姐”。但很多人在做报表的时候其实并不懂业务,需要找业务......
  • mybatis之xml映射文件>、<=等特殊符号写法
    前言在使用mybatis框架进行开发时,编写sql少不了<=,>=,>,<,<>等比较符号,但是直接在mapper文件中,直接使用这些符号是不行的,此时就需要对类似于这种的符号进行转换。正文直接符......
  • 类项目中的配置文件app.config在打包安装后的信息获取的问题
    在一个项目中碰到这样的一个问题,做一个WORD插件,功能在类库项目中实现了,配置信息存在类库项目的配置文件app.config中,在进行打包后,获取的配置文件中的DocType节点信息时,使用......