首页 > 其他分享 >实验六

实验六

时间:2022-12-01 15:57:43浏览次数:29  
标签:int ++ Vector 实验 output include size

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 
 6 int main() {
 7     using namespace std;
 8     array<char, N> x;
 9 
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 }

 

 int占4个字节,char占1个字节

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 template<typename T>
 5 class Vector
 6 {
 7 public:
 8     Vector(T n):size(n)
 9     {
10         p = new T[n];
11     }
12     Vector(T n, T value):size(n)
13     {
14         p = new T[n];
15         for (int i = 0; i < size; i++)
16         {
17             p[i] = value;
18         }
19     }
20     Vector(const Vector<T>& r):size(r.size)
21     {
22         p = new int[size];
23         for (int i = 0; i < size; i++)
24             p[i] = r.p[i];
25     }
26     ~Vector()
27     {
28         delete[] p;
29     }
30     T& at(int i)
31     {
32         return p[i];
33     }
34     T get_size()
35     {
36         return size;
37     }
38     friend void output(Vector<T>& x)
39     {
40         for (int i = 0; i < x.size; i++)
41             cout << x.p[i] << " ";
42         cout << endl;
43     }
44     T& operator[](int i)
45     {
46         return p[i];
47     }
48 private:
49     T size;
50     T* p;
51 };
 1 #include <iostream>
 2 #include "Vector.h"
 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.5;
13 
14     output(x1);
15 
16     Vector<int> x2(n, 66);
17     Vector<int> x3(x2);
18 
19     output(x2);
20     output(x3);
21 
22     x2.at(0) = 88;
23     output(x2);
24 
25     x3[0] = 99;
26     output(x3);
27 }
28 
29 int main() {
30     test();
31 }

 

 

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

 

 

 

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

相关文章

  • Python实验报告——第13章 Pygame游戏编程
    实验报告【实验目的】 1.掌握Pygame的基础知识。【实验条件】1.PC机或者远程编程环境。 【实验内容】1.完成第十三章  实例01:篮球自动弹跳。  实例01:创......
  • 实验6 模板类和文件I/O
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验四 Web服务器1-socket编程
    实验四Web服务器1-socket编程ipa查询网络状态echo服务器的客户端服务器,提交程序运行截图,服务器把客户端传进来的内容加入“服务器进程pid你的学号姓名echo:”......
  • 实验6 模板类和文件I/O
     实验任务3task3_1.cpp1#include<iostream>2#include<vector>3template<typenameT>4voidoutput(constT&obj){5for(auto&item:obj)6std::......
  • 实验六
    实验任务3task3_1.cpp源码:1#include<iostream>2#include<fstream>3#include<array>4#defineN55intmain(){6usingnamespacestd;7......
  • 实验四 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,......