首页 > 其他分享 >实验六

实验六

时间:2022-12-05 14:34:37浏览次数:38  
标签:cout int vector 实验 output include size

task3:

task3.1

源码:

 1 #include<iostream>
 2 #include<fstream>
 3 #include<array>
 4 #define N 5
 5 int main() {
 6     using namespace std;
 7     array<int, N> x{ 97,98,99,100,101 };
 8     ofstream out;
 9     out.open("data1.dat", ios::binary);
10     if (!out.is_open()) {
11         cout << "fail to open data1.dat" << endl;
12         return 1;
13     }
14     out.write(reinterpret_cast<char*>(&x), sizeof(x));//将从地址&X开始连续sizeof(X)个字节的数据块以字节数据块的方式写入data1.txt
15     out.close();
16 }

task3.2

源码:

 1 #include<iostream>
 2 #include<fstream>
 3 #include<array>
 4 #define N 5
 5 int main() {
 6     using namespace std;
 7     array<int, N>x;
 8     ifstream in;
 9     in.open("data1.dat", ios::binary);
10     if (!in.is_open()) {
11         cout << "fail to open data1.daat" << endl;
12         return 1;
13     }
14     in.read(reinterpret_cast<char*>(&x), sizeof(x));
15     in.close();
16     for (auto i = 0; i < N; i++)
17         cout << x[i] << " ";
18     cout << "\b\b" << endl;
19 }

运行截图:

task4:

 vector.hpp

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 template<typename T>
 5 class vector {
 6     int size;
 7     T *p;
 8 public:
 9     vector(){}
10     vector(int size0) {
11         size = size0;
12         p = new T[size];
13     }
14     vector(int size0, T value) {
15         size = size0;
16         p = new T[size];
17         for (auto i = 0; i < size; i++)
18             p[i] = value;
19     }
20     vector(const vector& obj) {
21         size = obj.size;
22         p = new int[size];
23         for (int i = 0; i < size; i++)
24             p[i] = obj.p[i];
25     }
26     ~vector() { delete[]p; }
27     T& at(int i) { return p[i]; }
28     int get_size() const { return size; }
29     T& operator[](int X) {
30         return p[X];
31     }
32     friend void output(vector& V) {
33         for (int i = 0; i < V.size; i++)
34             cout << V.p[i] << " ";
35         cout << endl;
36     }
37 };

task4.cpp

 1 #include <iostream>
 2 #include "Vector.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     int n;
 8     cin >> n;
 9 
10     vector<double> x1(n);
11     for (auto i = 0; i < n; ++i)
12         x1.at(i) = i * 0.7;
13 
14     output(x1);
15 
16     vector<int> x2(n, 42);
17     vector<int> x3(x2);
18 
19     output(x2);
20     output(x3);
21 
22     x2.at(0) = 77;
23     output(x2);
24 
25     x3[0] = 999;
26     output(x3);
27 }
28 
29 int main() {
30     test();
31 }

截图

task5

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<fstream>
 4 using namespace std;
 5 
 6 void output(ostream& out) {
 7     char a[26][26], i;
 8     cout << "  ";
 9     out << "  ";
10     for (i = 'a'; i <= 'z'; i++)
11     {
12         cout << setw(2) << setfill(' ') << i;
13         out << setw(2) << setfill(' ') << i;
14     }
15     cout << endl; out << endl;
16     for (int j = 0; j < 26; j++)
17     {
18         cout << setw(2) << setfill(' ') << j + 1;
19         out << setw(2) << setfill(' ') << j + 1;
20         for (int n = 0; n < 26; n++)
21         {
22             a[j][n] = 'A' + char(j + n + 1) % 26;
23             cout << setw(2) << setfill(' ') << a[j][n];
24             out << setw(2) << setfill(' ') << a[j][n];
25         }
26         cout << endl; out << endl;
27     }
28 }
29 
30 int main() {
31     ofstream out;
32     out.open("cipher_key.txt", ios::out);
33     if (!out.is_open())
34     {
35         cout << "fail to open!" << endl;
36         return 0;
37     }
38     output(out);
39     out.close();
40 }

 

 

标签:cout,int,vector,实验,output,include,size
From: https://www.cnblogs.com/zzzys/p/16939209.html

相关文章

  • 实验六 模板类和文件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<......
  • 实验6 模板类和文件I/O
    实验任务31#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain(){6usingnamespacestd;7array<int,N>x{......