首页 > 其他分享 >实验6-模板类、文件I/O和异常处理

实验6-模板类、文件I/O和异常处理

时间:2023-12-17 12:45:09浏览次数:22  
标签:文件 arr int Vector 实验 output include 模板 size

Vector.hpp

 1 #ifndef VECTOR_HPP
 2 #define VECTOR_HPP
 3 
 4 #include <iostream>
 5 #include <stdexcept>
 6 
 7 template<class T>
 8 class Vector {
 9 private:
10     T* arr; 
11     int size; 
12 
13 public:
14     Vector() : arr(nullptr), size(0) {} 
15 
16     Vector(int n) : size(n) { 
17         arr = new T[size];
18     }
19 
20     Vector(int n, const T& value) : size(n) { 
21         arr = new T[size];
22         for (int i = 0; i < size; i++) {
23             arr[i] = value;
24         }
25     }
26 
27     Vector(const Vector<T>& other) { 
28         size = other.size;
29         arr = new T[size];
30         for (int i = 0; i < size; i++) {
31             arr[i] = other.arr[i];
32         }
33     }
34 
35     ~Vector() { 
36         delete[] arr;
37     }
38 
39     int get_size() const { 
40         return size;
41     }
42 
43     T& at(int index) { 
44         if (index < 0 || index >= size) {
45             throw std::out_of_range("Index out of range");
46         }
47         return arr[index];
48     }
49 
50     T& operator[](int index) { 
51         return at(index);
52     }
53 
54     Vector<T>& operator=(const Vector<T>& other) { 
55         if (this != &other) {
56             delete[] arr;
57             size = other.size;
58             arr = new T[size];
59             for (int i = 0; i < size; i++) {
60                 arr[i] = other.arr[i];
61             }
62         }
63         return *this;
64     }
65 
66     friend void output(const Vector<T>& vec) { 
67         for (int i = 0; i < vec.size; i++) {
68             std::cout << vec.arr[i] << " ";
69         }
70         std::cout << std::endl;
71     }
72 };
73 
74 #endif
View Code

 

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

 

运行结果截图

 

task5.cpp

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<fstream>
 4 
 5 using namespace std;
 6 
 7 void output(ostream &out) {
 8      for(int i=0;i<=26;i++){
 9         for(int j=0;j<=26;j++)
10          {
11             char c,b;
12             if(i==0&&j==0){
13             char d = ' ';
14              out<<setw(2)<<d;
15              }
16             else if(j==0&&i!=0){
17                 out<<setw(2)<<i;
18             }
19             else if(i==0&&j!=0){
20                  char c='a'+j-1;
21                 out<<setw(2)<<c;
22              }
23             else if(i!=0&&j!=0){
24                  char b=(i+j-1+26)%26+'A';
25                  out<<setw(2)<<b;
26              }
27         }
28          out<<endl;
29      }
30  }
31 int main(){
32     output(cout);
33  
34     ofstream outFile("cipher_key.txt");
35     output(outFile);
36     outFile.close();
37  
38     return 0;
39  }
View Code

 

运行结果截图

 

标签:文件,arr,int,Vector,实验,output,include,模板,size
From: https://www.cnblogs.com/chenxiaolong202083290491/p/17908943.html

相关文章

  • Jmeter46 配置元件,读取.properties,.ini, .txt 配置文件,为全局做自动化
     配置元件主要是用与测试前的配置,将配置转换为变量设置到jmetercontext中。而jmeter默认并没有配置元件(.properties)读取器,但是由于jmeter是开源的,我们可以自己定义一个配置元件来读取配置文件。插件下载地址:https://www.testautomationguru.com/jmeter-property-file-reader-......
  • 实验六
    task4.cpp:点击查看代码#include<iostream>#include"vector.hpp"voidtest(){usingnamespacestd;intn;cin>>n;Vector<double>x1(n);for(autoi=0;i<n;++i)x1.at(i)=i*0.7;output......
  • java接口自动化系列(02):测试数据文件设计
     本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/15867903.html说明本次分享的是测试数据存excel中,后续分享测试数据存yaml中测试用例数据示例解释:标题行每个单词首字母大写,因为代码里面反射会用到解释:字段数据Url:只写路径,不需要写ip、端口RequestType:目......
  • 实验7
    任务4#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>intmain(){   longi=0;   charch;   FILE*fp;   fp=fopen("c://data//data4.txt","r");   while(!feof(fp))   {       ch=fgetc(fp);       if(ch!=''&......
  • 实验七
    任务1:#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》","克里斯多夫.夏布特&quo......
  • 实验7 文件应用编程
    1.实验任务1源代码1//将图书信息写入文本文件data1.txt23#include<stdio.h>45#defineN8067typedefstruct{8charname[N];//书名9charauthor[N];//作者10}Book;1112intmain(){13Bookx[]={{"《雕塑家》",......
  • 微信小程序代码构成及每一个代码文件的作用(附图)
    一、微信小程序有四种类型文件:1.JSON配置文件(.json后缀)2.WXML模板文件(.wxml后缀)3.WXSS样式文件(.wxss后缀)4.JS脚本逻辑文件(.js后缀) 二、JSON配置:1.JSON配置:JSON是一种数据格式,用来静态配置。app.json 小程序配置project.config.json 工具配置 page.json页面......
  • 实验7
    task41#include<stdio.h>23intmain(){45inti=0;6chars;78FILE*fp;9fp=fopen("data4.txt","r");1011while(1){12s=fgetc(fp);13if(s==EOF){14......
  • DELPHI模板编程
    DELPHI模板编程procedureTCRUD<T>.execsql(OnTableModel:TTableModel);//执行事务性SQLbeginifreq.Body=nilthenExit;varpool:TDBPool:=GetDBPool(dbid);//databasepooldb:=pool.Lock;trytrytable:=serialize.TSerial<TTab......
  • vscode编译多个C/CPP文件
    修改vscode里面的tasks.json文件,下面是修改好的,参考"args":["-fdiagnostics-color=always","-g",//"${file}", //只执行当前文件"${workspaceFolder}\\*.cpp",//工作区内,执行多个关联cpp文件,但只有一个main()......