首页 > 其他分享 >实验六

实验六

时间:2022-12-05 18:45:03浏览次数:33  
标签:std int namespace 实验 include out size

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

vector.hpp

#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

#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.3;
    
    output(x1);
    
    Vector<int> x2(n,33);
    Vector<int> x3(x2);
    
    output(x2);
    output(x3);
    
    x2.at(0)=22;
    output(x2);
    
    x3[0]=222;
    output(x3);
}
int main(){
    test();
}

task5

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

string stand = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void output(std::ostream& out)
{
    int row = 1;
    out << "   ";
    for (int i = 0; i < 26; i++)
        out << (char)(stand[i] + 32) << " ";
    out << endl;
    while (row <= 26)
    {
        out << setw(2) << row << " ";
        for (int i = 0; i < 26; i++)
            out << stand[(i + row) % 26] << " ";
        out << endl;
        row++;
    }
}
int main()
{
    ofstream out;
    out.open("ciper.txt");
    if (!out.is_open())
    {
        std::cout << "fail to open ciper.txt" << endl;
        return 1;
    }
    output(out);
    output(std::cout);
    return 0;
}

 

标签:std,int,namespace,实验,include,out,size
From: https://www.cnblogs.com/wyh-205113/p/16953148.html

相关文章

  • 实验六
    task3:task3.1源码:1#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain(){6usingnamespacestd;7array<int,......
  • 实验六 模板类和文件I/O
    实验任务3#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};ofst......
  • TinyShell(CSAPP实验)
    简介CSAPP实验介绍学生实现他们自己的带有作业控制的UnixShell程序,包括Ctrl+C和Ctrl+Z按键,fg,bg,和jobs命令。这是学生第一次接触并发,并且让他们对Unix的进程控制、......
  • 有来实验室|第一篇:Seata1.5.2版本部署和开源全栈商城订单支付业务实战
    在线体验:Seata实验室一.前言相信youlai-mall的实验室大家有曾在项目中见到过,但应该都还处于陌生的阶段,毕竟在此之前实验室多是以概念般的形式存在,所以我想借着此次......
  • 二十四. vlan综合实验
    1.实验图2.配置流程第1个里程:配置SW11接口配置<Huawei>sys<Huawei>sysnameS1[Huawei]vlanbatch1020[Huawei]inte0/0/1[Huawei]portlink-typeaccess[H......
  • 2022-2023-1学期 20221417 《计算机基础与程序设计》实验七-缓冲区溢出
    1.实验指导书内容2.缓冲区溢出的原理3.缓冲区溢出的防范实验指导书内容https://blog.csdn.net/weixin_43771137/article/details/128063046缓冲区溢出原理缓冲......
  • 实验5
    #pragmaonce#include<iostream>#include<iomanip>#include<string>usingnamespacestd;classMachinePets{public: MachinePets(){}; MachinePets(conststri......
  • 实验6
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};o......
  • 实验六
    实验三task3_1.cpp1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89......
  • 实验六 模板类和文件I/O
    task3_1:1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89array<......