首页 > 其他分享 >实验六

实验六

时间:2022-12-06 22:44:20浏览次数:37  
标签:cout int namespace Vector 实验 output include

试验任务三

task3_1

 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

 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 
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 }

 

 试验任务四

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 }

Vector.hpp

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

 

 试验任务五

task_6.cpp

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

 

标签:cout,int,namespace,Vector,实验,output,include
From: https://www.cnblogs.com/zx1229/p/16954259.html

相关文章

  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验六
    #pragmaonce#include<iostream>usingstd::cout;usingstd::endl;template<typenameT>classVector{public:Vector(Tn):size(n){p=newT[n];}V......
  • 实验六
    实验任务3#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验六 模板类和文件IO
    任务四代码:Vector.hpp:#pragmaonce#include<iostream>usingnamespacestd;template<classT>classVector{public:Vector(intsize0):size{size......
  • 实验六 模板类和文件IO
    Task4//VEctor.hpp#include<bits/stdc++.h>usingnamespacestd;template<typenameT>classVector{private:intlength;T*base;publ......
  • 包层次的时间同步实验
    一、实验目的理解掌握时间同步的重要意义了解时间同步的基本方法,理解TPSN协议机理掌握TinyOS定时器相关接口和组件的使用4.能够在TinyOS中实现多节点间的时间......
  • SRP实验
    一、实验目的学习源路由协议的机制理解节点在源路由协议中的几种角色掌握源路由协议相关接口SourceRouteSend、SourceRoutePacket的使用 二、实验要求功能:root......
  • Dissemination实验
    一、实验目的学习少量数据在传感网中可靠传递的方法掌握TinyOS中DisseminationValue、DisseminationUpdate接口和组件的使用理解Dissemination的机制 二、实......
  • LEEP节点无线链路质量评估实验
    一、实验目的理解节点通信链路质量的影响因素理解提升链路质量的一般方法学习TinyOS系统中CC2530/CC2538节点发送功率的设置体验不同发送功率、不同通信距离、不......
  • 实验6
    task3_1:#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101......