首页 > 其他分享 >实验6

实验6

时间:2022-12-05 22:44:42浏览次数:41  
标签:std int Vector 实验 output include size

 1 3.1 cpp:
 2 
 3 #include <iostream>
 4 #include <fstream>
 5 #include <array>
 6 #define N 5
 7 int main() {
 8     using namespace std;
 9     array<int, N> x{ 97, 98, 99, 100, 101 };
10     ofstream out;
11     out.open("data1.dat", ios::binary);
12     if (!out.is_open()) {
13         cout << "fail to open data1.dat\n";
14         return 1;
15     }
16      //把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入文件data1.txt
17     out.write(reinterpret_cast<char*>(&x), sizeof(x));
18     out.close();
19 }
20 
21 3.2 cpp:
22 
23 #include <iostream>
24 #include <fstream>
25 #include <array>
26 #define N 5
27 int main() {
28     using namespace std;
29         array<int, N> x;
30     ifstream in;
31     in.open("data1.dat", ios::binary);
32     if (!in.is_open()) {
33         cout << "fail to open data1.dat\n";
34         return 1;
35     }
36     // 从文件流对象in关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元
37     in.read(reinterpret_cast<char*>(&x), sizeof(x));
38     in.close();
39     for (auto i = 0; i < N; ++i)
40         cout << x[i] << ", ";
41     cout << "\b\b \n";
42 }

更改为char之后:

int类型的数据占用四个字节,而char型的数据只占用一个字节,array<char, N> x中的x只占用5个字节,而array<int, N> x占用20个字节。
  所以在用array<char, N> x中x的地址来读取文件流中的数据,第一个数据97会占用四个字节,97对应的char型数据为a,98为b,只能读取两个数据,而且内存越界了。
 1 vector.hpp:
 2 
 3 #include<iostream>
 4 using namespace std;
 5 
 6 template<typename T>
 7 class Vector {
 8 private:
 9     int size;  //储存数据项个数
10     T* p;
11 public:
12     Vector(int n):size{n}{ p = new T[size]; }
13     Vector(int n, T value): size{ n } 
14     {
15     p = new T[size];
16     for (int i = 0; i < size; i++)
17         p[i] = value;
18     }
19     Vector(const Vector<T>& obj):size{obj.size} 
20     {
21     p = new T[size];
22     for (auto i = 0; i < size; i++)
23         p[i] = obj.p[i];
24     }
25     ~Vector() { delete []p; }
26     int get_size() { return size; }
27     T &at(int index);
28     T &operator[](int i);
29     template<typename T1>
30     friend void output(const Vector<T1>&p1);
31 };
32 
33 template<typename T>
34 T& Vector<T>::at(int index) {
35         if (index >= 0 && index < size)
36         return p[index];
37 }
38 
39 template<typename T>
40 T& Vector<T>::operator[](int i) {
41     return p[i];
42 }
43 
44 template<typename T1>
45 void output(const Vector<T1>& p1) {
46     for (int i = 0; i < p1.size; i++)
47         cout << p1.p[i] << ", ";
48     cout << "\b\b\n";
49 }
50 
51 task4:
52 #include <iostream>
53 #include "vector.hpp"
54 
55 void test() {
56     using namespace std;
57 
58     int n;
59     cin >> n;
60 
61     Vector<double> x1(n);
62     for (auto i = 0; i < n; ++i)
63         x1.at(i) = i * 0.7;
64 
65     output(x1);
66 
67     Vector<int> x2(n, 42);
68     Vector<int> x3(x2);
69 
70     output(x2);
71     output(x3);
72 
73     x2.at(0) = 77;
74     output(x2);
75 
76     x3[0] = 999;
77     output(x3);
78 }
79 
80 int main() {
81     test();
82 }

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

 1 task5:
 2 
 3 #include<fstream>
 4 #include<iostream>
 5 #include<iomanip>
 6 using std::ofstream;
 7 using std::ios_base;
 8 using std::setw;
 9 using namespace std;
10 void output(std::ostream& out)
11 {
12     char a[26];
13     char change1;
14     cout << "  ";
15     out << "  ";
16     for (int i = 0; i < 26; i++)
17         a[i] = char(i+65);
18     for (int i = 97; i <= 122; i++)
19     {
20         cout << setw(2) << char(i);
21         out << setw(2) << char(i);
22     }
23     cout << endl;
24     out << endl;
25     for (int i = 0; i < 26; i++)
26     {
27         cout << setw(2)<<i + 1;
28         out << setw(2) << i + 1;        
29         for (int k = 0; k < 25; k++)
30         {
31             change1 = a[k];
32             a[k] = a[k + 1];
33             a[k + 1] = change1;
34         }
35         for (int j = 0; j < 26; j++)
36         {
37             cout <<setw(2)<< a[j];
38             out  << setw(2)<<a[j];
39         }
40         cout << endl;
41         out << endl;
42     }
43 }
44 
45 
46 int main()
47 {
48     ofstream out;
49     out.open("cipher_key.txt", ios::out);
50     if (!out.is_open()) {
51         std::cout << "fail to open file cipher_key.txt" << std::endl;
52         return 1;
53     }
54     output(out);
55     out.close();
56 }

 

标签:std,int,Vector,实验,output,include,size
From: https://www.cnblogs.com/xtc111/p/16953787.html

相关文章

  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100......
  • 实验六
    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......