首页 > 其他分享 >实验6

实验6

时间:2023-12-17 21:44:23浏览次数:26  
标签:out Vector 实验 output include data size

1.实验任务4

 1 #include <iostream>
 2 #include <stdexcept>
 3 
 4 template <typename T>
 5 class Vector {
 6 private:
 7     T* data;
 8     size_t size;
 9 public:
10     Vector(size_t n) : size(n) {
11         data = new T[size];
12     }
13     Vector(size_t n, const T& value) : size(n) {
14         data = new T[size];
15         for (size_t i = 0; i < size; ++i) {
16             data[i] = value;
17         }
18     }
19     Vector(const Vector& other) : size(other.size) {
20         data = new T[size];
21         for (size_t i = 0; i < size; ++i) {
22             data[i] = other.data[i];
23         }
24     }  
25     ~Vector() {
26         delete[] data;
27     }
28 
29     size_t get_size() const {
30         return size;
31     }
32   
33     T& at(size_t i) {
34         if (i >= size) {
35             throw std::out_of_range("Index out of range");
36         }
37         return data[i];
38     }   
39     T& operator[](size_t i) {
40         if (i >= size) {
41             throw std::out_of_range("Index out of range");
42         }
43         return data[i];
44     }
45     friend void output(const Vector& v) {
46         for (size_t i = 0; i < v.size; ++i) {
47             std::cout << v.data[i] << " ";
48         }
49         std::cout << std::endl;
50     }
51 };
vector.hpp
 1 #include <iostream>
 2 #include "Vector.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6     int n;
 7     cin >> n;
 8     Vector<double> x1(n);
 9     for (auto i = 0; i < n; ++i)
10         x1.at(i) = i * 0.7;
11     output(x1);
12     Vector<int> x2(n, 42);
13     Vector<int> x3(x2);  
14     output(x2);
15     output(x3);
16     x2.at(0) = 77;
17     output(x2);
18     x3[0] = 999;
19     output(x3);
20 
21 int main() {
22     test();
23     return 0;
24 }
task4.cpp

 2.实验任务5

 1 #include <iostream>
 2 #include <fstream>
 3 #include <iomanip>
 4 
 5 using namespace std;
 6 
 7 void printCharRow(std::ostream& out, int row, char baseChar)
 8 {
 9     for (int i = 0; i < 26; i++) {
10         char n = baseChar + (row + i) % 26;
11         out << setw(2) << n << ' ';
12     }
13     out << endl;
14 }
15 
16 void output(std::ostream& out)
17 {
18     for (int i = 0; i < 26; i++) {
19         if (i == 0) {
20             out << ' ' << setw(2) << ' ';
21         }
22         else {
23             out << setw(2) << i << ' ';
24         }
25         printCharRow(out, i, (i == 0) ? 'a' : 'A');
26     }
27 }
28 
29 int main()
30 {
31     cout << "屏幕上打印出:\n" << endl;
32     output(cout);
33 
34     ofstream outfile("cipher_key.txt");
35     if (outfile.is_open()) {
36         output(outfile);
37         outfile.close();
38     }
39     else {
40         cout << "无法打开文件 cipher_key.txt" << endl;
41     }
42 
43     return 0;
44 }
task5.cpp
 1     a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z 
 2  1  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A 
 3  2  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B 
 4  3  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C 
 5  4  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D 
 6  5  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E 
 7  6  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F 
 8  7  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G 
 9  8  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H 
10  9  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I 
11 10  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J 
12 11  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K 
13 12  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L 
14 13  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M 
15 14  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N 
16 15  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O 
17 16  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P 
18 17  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q 
19 18  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R 
20 19  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S 
21 20  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T 
22 21  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U 
23 22  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V 
24 23  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W 
25 24  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X 
26 25  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y 
cipher_key.txt

 

标签:out,Vector,实验,output,include,data,size
From: https://www.cnblogs.com/ZJY1203/p/17909902.html

相关文章

  • 实验6 模板类、文件I/O和异常处理
    实验任务4Vector.hpp#pragmaonce#include<iostream>#include<stdexcept>#include<string>usingnamespacestd;template<typenameT>classVector{public:Vector(intx=0,constT&value=T());Vector(constVecto......
  • 实验6 模板类、文件I/O和异常处理
    Task4: vector.hpp:#include<iostream>#include<string>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:T*data;intsize;public:Vector():data(nullptr),size(0){}Vector......
  • 实验7
    实验任务4:\#include<stdio.h>intmain(){inti=0;chars;FILE*fp;fp=fopen("data4.txt","r");while(1){s=fgetc(fp);if(s==EOF){break;}elseif(s=='�......
  • 实验六
    实验代码实验任务4.hpp#include<iostream>#include<stdexcept>#include<cassert>usingnamespacestd;template<typenameT>classVector{private:T*ptr;intsize;public:Vector(intsize){if(size<0)throwstd:......
  • 实验六
    任务四1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • 实验六
    task4.hpp#include<iostream>#include<stdexcept>#include<cassert>usingnamespacestd;template<typenameT>classVector{private:T*ptr;intsize;public:Vector(intsize){if(size<0)......
  • 实验六
    4.hpp#include<iostream>#include<stdexcept>template<typenameT>classVector{private:intsize;T*p;public:Vector<T>(ints);Vector<T>(ints,Tvalue);Vector<T>(Vector<T>&x);......
  • 渗透测试实验报告一
    1. 实验目的和要求实验目的:理解网络扫描、网络侦察的作用;通过搭建网络渗透测试平台,了解并熟悉常用搜索引擎、扫描工具的应用,通过信息收集为下一步渗透工作打下基础。系统环境:KaliLinux2、Windows网络环境:交换网络结构2.实验步骤 1:利用Google语法搜索site:mit.eduinti......
  • 实验六
    实验任务4vector.hpp#include<iostream>#include<stdexcept>#include<cassert>usingnamespacestd;template<typenameT>classVector{private:T*ptr;intsize;public:Vector(intsize){if(size<0)......
  • 实验三-电子公文传输系统1-个人贡献
    实验三-电子公文传输系统1-个人贡献任务详情1简述你完成的工作2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?3你们小组总共的文档数?你贡献的文档数?相关链接?主要处理完成的工作1我完成了项目冲刺的5和6两天的实现情况的撰写,编写了部分后端代码和系统的使用指南2......