首页 > 其他分享 >实验6

实验6

时间:2022-12-07 09:23:42浏览次数:46  
标签:cout int open 实验 include out size

task3:

3.1代码:

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

 

3.2代码:

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

运行截图 :

改为char 运行截图:

 

分析:int占4个空间,char只占1个空间。存入时97存入1号空间,2到4号空间存0;98存入5号空间;读取时对应1号和5号读取ASCII码为a,b;0为不显示字符。

 

task4:

 

Vector.hpp:

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

 

运行截图:

 

task5:

代码:

#include<iostream>
#include<fstream>
using namespace std;

void output(ofstream& out)
{
    cout<<"   ";
    out<<"   ";
    
    for(int i=0;i<26;i++)
    {
        char ch='a'+i;
        cout<<ch<<" ";
        out<<ch<<" ";
    }
    cout<<endl; out<<endl;
    
    for(int i=0;i<26;i++)
    {
        if(i+1<10){
        cout<<" "<<i+1<<" ";
        out<<" "<<i+1<<" ";}
        else{
            cout<<""<<i+1<<" ";
        out<<""<<i+1<<" ";
        }
        for(int j=i;j<i+26;j++)
        {
            char ch='A'+j%26;
            cout<<ch<<" ";
            out<<ch<<" ";
        }
        cout<<endl; out<<endl;
    }    
}

int main()
{
    ofstream out;
    out.open("cipher_key.txt", ios::out);
    output(out);
}

 

截图:

 

标签:cout,int,open,实验,include,out,size
From: https://www.cnblogs.com/202183290359gyk/p/16962100.html

相关文章

  • 实验六
    实验六task4vector.hpp#pragmaonce#include<iostream>usingnamespacestd;template<typenameT>classVector{public: Vector(){}; Vector(intn):size{n......
  • 实验六
    Task3:task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99......
  • 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  对样例区样例画的一些图:       题目的一些争议......