首页 > 其他分享 >实验六

实验六

时间:2022-12-04 21:45:46浏览次数:39  
标签:std int Vector 实验 output include size

实验三

task3_1.cpp

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 
 6 int main() {
 7     using namespace std;
 8 
 9     array<int, N> x {97, 98, 99, 100, 101};
10 
11     ofstream out;
12     out.open("data1.dat", ios::binary);
13     if(!out.is_open()) {
14         cout << "fail to open data1.dat\n";
15         return 1;
16     }
17 
18     out.write(reinterpret_cast<char *>(&x), sizeof(x));
19     out.close();
20 }

实验截图

 

task3_2.cpp

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

实验截图:

对task3_2.cpp的代码稍做改动,把line7的对象类型从 array<int, N> 改成 array<char, N> 其它不做任何改动,再次运行程序,观察运行结果是什么?尝试分析原因:为什么会是那样的结果? 截图:

 

在task3_1中,将数存入二进制文件中是以char*型写入的(write),故存入的内容为a   b   c   d   e;

在task3_2将字符读出时,int 读到的是ASCLL码值的形式的数字形式,故为97,98,99,100,101

修改后,以char 读取时,每次只读一个字符,故第一次读到的是a,在2~4次中,读到的是ab之间的空格,共有三个,所以读了三次,在第五次的时候,读到了之后的字符b;

实验四

Vector.hpp

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 template <class T>
 5 class Vector;
 6 
 7 template <class T>
 8 void output(Vector<T>& x)
 9 {
10     for (int i = 0; i < x.size; i++)
11     {
12         cout << x.p[i] << ", ";
13     }
14     cout << "\b\b \n";
15 }
16 
17 template <class T>
18 class Vector
19 {
20 private:
21     int size;
22     T* p;
23 
24 public:
25     Vector(int n);
26     Vector(int n, T);
27     ~Vector();
28     int get_size() const { return size; }
29     T& at(int index)
30     {
31         return p[index];
32     }
33     T& operator[](int index)
34     {
35         return p[index];
36     }
37     Vector(const Vector<T>& v);
38     friend void output<>(Vector<T>& x);
39 };
40 
41 
42 template <class T>
43 Vector<T>::Vector(int n) : size{ n }
44 {
45     p = new T[n];
46 }
47 
48 template <class T>
49 Vector<T>::Vector(int n, T x) : size{ n }
50 {
51     p = new T[n];
52     for (auto i = 0; i < n; i++)
53     {
54         p[i] = x;
55     }
56 }
57 
58 template <class T>
59 Vector<T>::~Vector()
60 {
61     delete[] p;
62 }
63 
64 template <class T>
65 Vector<T>::Vector(const Vector<T>& v) : size{ v.size }
66 {
67     p = new T[size];
68     for (auto i = 0; i < get_size(); i++)
69     {
70         p[i] = v.p[i];
71     }
72 }

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.cpp

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

实验截图:

 

 

 

 

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

相关文章

  • 实验六 模板类和文件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{......
  • 实验6 模板类和文件IO
    一、实验任务三1.源代码:task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,9......
  • 实验6 模板类和文件I/O
    实验任务3task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;8array<in......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};o......
  • 实验6
    task3.1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101}......
  • 实验6 模板类和文件IO
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,1......
  • 实验6 模板类和文件io
    task4Vector.cpp#pragmaonceusingnamespacestd;template<typenameT>classVector{public:Vector(intn);Vector(intn,Tv);~Vector();V......
  • 实验六
    #include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};ofstreamout;ou......
  • 实验6
    一、实验目的体验模板函数、模板类的编写,从多态角度理解模板函数和模板类(类型作为参数)体验标准I/O流类、文件I/O流类、字符串I/O流类的用法,能正确使用针对问题场景,能......