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

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

时间:2023-12-16 18:33:20浏览次数:24  
标签:文件 out Vector 实验 output include data 模板 size

实验任务4

Vector.hpp

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

运行测试结果

 实验任务5

task5.cpp

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <fstream>
 4 
 5 using namespace std;
 6 
 7 void output(ostream &out) {
 8     
 9     const int numLines = 26;
10 
11     for (int i = 0; i < numLines; ++i) {
12         out << setw(2) << right << i + 1 << ' '; 
13         for (int j = 0; j < 26; ++j) 
14         {
15             char ch = 'A' + (i + j) % 26;
16             out << ch << ' '; 
17         }
18         out << endl;
19     }
20 }
21 
22 int main() {
23     
24     cout << "输出矩阵:\n" 
25          << "   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" 
26          << endl;
27          
28     output(cout);
29 
30     ofstream outFile("cipher_key.txt");
31     
32     if (outFile.is_open()) 
33     {
34         output(outFile);
35         outFile.close();
36     } 
37     else 
38     {
39         return 1;
40     }
41 
42     return 0;
43 }
View Code

运行测试结果

 

标签:文件,out,Vector,实验,output,include,data,模板,size
From: https://www.cnblogs.com/rssn/p/17894051.html

相关文章

  • 实验7
    //将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》",......
  • 区间素数筛模板
    例题素数密度template<typenameT>structsegment_sieve{ vector<bool>is_prime,is_prime_small; vector<T>prime; segment_sieve(){ is_prime.resize(1000010); is_prime_small.resize(1000100); } //对区间[a,b]内的整数执行筛法,is_prime[i-a]=true---......
  • 实验六
    task4源代码:1#pragmaonce23#include<iostream>4#include<stdexcept>5#include<cassert>6#include<iomanip>78usingnamespacestd;910template<typenameT>11classVector{12public:13Vecto......
  • 实验7
    //将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》",......
  • tsconfig.json文件配置
    tsconfig.json配置TypeScript使用tsconfig.json文件作为其配置文件,当一个目录中存在tsconfig.json文件,则认为该目录为TypeScript项目的根目录。基础字段files-设置要编译的文件的名称;['./src/main.tsx']include-设置需要进行编译的文件,支持路径模式匹配;['src']......
  • 实验7_文件应用编程
    task4#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intcnt=0;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoop......
  • Hexo 主题开发之自定义模板
    关于Hexo如何开发主题包的教程在已经是大把的存在了,这里就不在赘述了。这边文章主要讲的是作为一个主题的开发者,如何让你的主题具有更好的扩展性,在用户自定义修改主题后,能够更加平易升级主题。问题所在Hexo提供两种方式安装主题包:直接在themes目录下直接存放主题包文件,这......
  • Golang 配置文件动态更变(viper)
    一.下载包gogetgithub.com/spf13/viper二.源码1funcLoadConf(fpnamestring){2ini:=viper.New()3ini.SetConfigFile(fpname)45ini.SetDefault("database.dbname","esaletest")6ini.SetDefault("database.dbhos......
  • SQLServer 添加数据文件
    限制:1.backup语句正在运行时,不能添加或删除文件2.可以为每个数据库指定最多32767个文件和32767个文件组一、新增文件之前查看usetestselectfile_id,file_guid,type,type_desc,data_space_id,name,physical_name,state,state_descfromsys.database_filesSELECT......
  • python操作文件夹
    帮我写一个python程序遍历文件夹下所有的压缩文件,如ziprar7z结尾的文件等等当然可以!以下是一个Python程序,它可以遍历指定文件夹下的所有压缩文件,如zip、rar、7z等等。你可以根据需要修改代码中的路径和文件类型。请注意,这个程序需要安装第三方库patool和pyunpack,你可以使用pip......