首页 > 其他分享 >实验六

实验六

时间:2022-11-30 23:22:34浏览次数:35  
标签:int ++ Vector 实验 output include size

实验任务3

task3_1.cpp源码:
 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\n";
12         return 1;
13     }
14     // 把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入文件data1.txt
15     out.write(reinterpret_cast<char*>(&x), sizeof(x));
16     out.close();
17 }
task3_2.cpp源码:
 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.dat\n";
12         return 1;
13     }
14     // 从文件流对象in关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元
15     in.read(reinterpret_cast<char*>(&x), sizeof(x));
16     in.close();
17     for (auto i = 0; i < N; ++i)
18         cout << x[i] << ", ";
19     cout << "\b\b \n";
20 }

运行截图为:

把line7的数组类型从int类型修改成char类型,即:array<int, N> x; ----> 修改成: array<char, N> x;其它不做任何改动,再次运行程序,观察运行结果是:

task3_1中是把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入文件data1.txt,data1.txt存入a   b   c   d   e   ,因为int 类型占四个字符的长度,当task3_2中int改为char后,每一个字符的长度只读一次,读到的是a,空格,空格,空格,b,所以输出a, , ,b。


Vector.hpp源代码:
 1 #include <iostream>
 2 #include <cassert>
 3 using namespace std;
 4 template<typename T>
 5 class Vector {
 6 public:
 7     Vector(int n){
 8             size = n;
 9             p = new T[n];
10         }
11     Vector(int n, int x0){
12         size = n;
13         p = new T[size];
14         for (auto i = 0; i < size; ++i)
15             p[i] = x0;
16     }
17     Vector( Vector<T>& vp){
18         size = vp.size;
19             p = new T[size];
20             for (auto i = 0; i < size; ++i)
21                 p[i] = vp.p[i];
22         }
23     ~Vector(){
24         delete[] p;
25     }
26     T& at(int index){
27         assert(index >= 0 && index < size);
28         return p[index];
29     }
30     int get_size() { return size; }
31     T &operator [](int i) {
32         return p[i];
33     }
34     template<typename T1>
35     friend void output(Vector<T1>& x);
36 private:
37     int size;
38     T * p;
39 };
40 template<typename T1>
41 void output(Vector<T1>& x) {
42     for (int i = 0; i < x.get_size(); i++)
43         cout << x.at(i) << " ";
44     cout << endl;
45 }
task4.cpp源代码:
 1 #include <iostream>
 2 #include "Vector.hpp"
 3 void test() {
 4     using namespace std;
 5     int n;
 6     cin >> n;
 7     Vector<double> x1(n);
 8     for (auto i = 0; i < n; ++i)
 9         x1.at(i) = i * 0.7;
10     output(x1);
11     Vector<int> x2(n, 42);
12     Vector<int> x3(x2);
13     output(x2);
14     output(x3);
15     x2.at(0) = 77;
16     output(x2);
17     x3[0] = 999;
18     output(x3);
19 }
20 int main() {
21     test();
22 }

运行截图:

实验任务5

task5.cpp源码:
 1 #include <iostream>
 2 #include<fstream>
 3 #include<iomanip>
 4 using namespace std;
 5 void output(std::ostream& out) {
 6     char a[30][30];
 7     int j = 0;
 8     a[0][0] = ' ';
 9     for (int i = 1; i < 27; i++) {
10         a[0][i] = 'a' + i - 1;
11         a[i][0] = i + '0';
12     }
13     for (int i = 1; i < 27; i++) {
14         for (j = 1; j + i < 27; j++)
15             a[i][j] = a[0][j] - 32 + i;
16         for (; j + i >= 26 && j <= 26; j++)
17             a[i][j] = a[0][j] - 58 + i;
18     }    
19     for (int i = 0; i < 27; i++) {
20         if (i != 0)
21             cout << setw(2) << a[i][0] - '0';
22         else
23             cout << "  ";
24         for (j = 1; j < 27; j++) {
25             cout <<setw(2)<< a[i][j];
26         }
27         cout << endl;
28     }    
29     
30     for (int i = 0; i < 27; i++) {
31         if (i != 0)
32             out << setw(2) << a[i][0] - '0';
33         else
34             out << "  ";
35         for (j = 1; j < 27; j++) {
36             out << setw(2) << a[i][j] ;
37         }
38         out << endl;
39     }
40 }
41 int main() {
42     ofstream out;
43     out.open("cipher_key.txt");
44     if (!out.is_open()) {
45         cout << "Fail to open the cipher_key.txt" << endl;
46         return 1;
47     }
48     output(out);
49     return 0;
50 }

运行截图:

 

标签:int,++,Vector,实验,output,include,size
From: https://www.cnblogs.com/dyb20030328/p/16939003.html

相关文章

  • 实验四 Web服务器2
    任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:Web服务器的客户端服务器,提交程序运行截图实现GET即可,请求,响应要符合HTTP协议规范服务器部署到华......
  • 实验四 Web服务器1-socket编程
    任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:time服务器的客户端服务器,提交程序运行截图echo服务器的客户端服务器,提交程序运行截图,服务器把客......
  • 实验四-Web服务器2
    一、任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.Web服务器的客户端服务器,提交程序运行截图2.实现GET即可,请求,响应要符合HTTP协议规范3.服务......
  • 实验6
    实验内容3task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x={97,98,99......
  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六:模板类和文件
    w实验任务三task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六
    6.3#include<iostream>#include<fstream>#include<array>#defineN5voidtest1(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验6 模板类和文件I/O
    1.程序源码task1#include<iostream>#include<fstream>#include"Complex.hpp"voidtest1(){usingnamespacestd;Complex<double>c1{1,2},c2;......
  • 实验四 Web服务器2
    实验四Web服务器2基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:Web服务器的客户端服务器,提交程序运行截图实现GET即可,请求,响应要符合HTTP协议规范服务......
  • 实验六
    task3_1#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;//array<int,N>x{97,98,99,100,10......