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

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

时间:2023-12-13 21:47:25浏览次数:38  
标签:std 文件 index Vector 实验 template ptr 模板 size

实验任务4:
1.代码:
vector.hpp:

 1 #pragma once
 2 #include <iostream>
 3 #include <stdexcept>
 4 using std::cout;
 5 using std::endl;
 6 
 7 template<typename T>
 8 class Vector {
 9 public:
10 Vector(T n);
11 Vector(T n, T value);
12 Vector(const Vector &vi);
13 ~Vector();
14 T& at(T index);
15 T& at(T index) const;
16 T get_size() const;
17 T& operator[](T index);
18 T& operator[](T index) const;
19 private:
20 T size;
21 T *ptr;
22 };
23 
24 template<typename T>
25 Vector<T>::Vector(T n): size{n} {
26 if(n < 0)
27 throw std::length_error("vector constructor: negative size");
28 ptr = new T[(int)size];
29 }
30 
31 template<typename T>
32 Vector<T>::Vector(T n, T value): size{n} {
33 if(n < 0)
34 throw std::length_error("vector constructor: negative size");
35 ptr = new T[size];
36 for(auto i = 0; i < size; ++i)
37 ptr[i] = value;
38 }
39 
40 template<typename T>
41 Vector<T>::Vector(const Vector &vi): size{vi.size}, ptr{new T[size]}
42 {
43 for(auto i = 0; i < size; ++i)
44 ptr[i] = vi.ptr[i];
45 }
46 
47 template<typename T>
48 Vector<T>::~Vector() {
49 delete [] ptr;
50 }
51 
52 template<typename T>
53 T Vector<T>::get_size() const {
54 return size;
55 }
56 
57 template<typename T>
58 T& Vector<T>::at(T index) {
59 if(index < 0 || index >= size)
60 throw std::out_of_range("vector::at()");
61 return ptr[(int)index];
62 }
63 
64 template<typename T>
65 T& Vector<T>::at(T index) const {
66 if(index < 0 || index >= size)
67 throw std::out_of_range("vector::operator[]()");
68 return ptr[(int)index];
69 }
70 
71 template<typename T>
72 T& Vector<T>::operator[](T index) {
73 if(index < 0 || index >= size)
74 throw std::out_of_range("vector::operator[]()");
75 return ptr[index];
76 }
77 
78 template<typename T>
79 T& Vector<T>::operator[](T index) const {
80 if(index < 0 || index >= size)
81 throw std::out_of_range("vector::operator[]()");
82 return ptr[index];
83 
84 }
View Code

task4.cpp:

 1 #include <iostream>
 2 #include "Vector.hpp"
 3 using namespace std;
 4 
 5 template<typename T>
 6 void output(const Vector<T>& v){
 7     for(auto i=0;i<v.get_size();i++){
 8         cout << v.at(i) << ", ";
 9         
10 
11     }
12     cout << "\b\b \n";
13 }
14 
15 void test() {
16 using namespace std;
17 int n;
18 cin >> n;
19 Vector<double> x1(n);
20 for(auto i = 0; i < n; ++i)
21 x1.at(i) = i * 0.7;
22 output(x1);
23 Vector<int> x2(n, 42);
24 Vector<int> x3(x2);
25 output(x2);
26 output(x3);
27 x2.at(0) = 77;
28 output(x2);
29 x3[0] = 999;
30 output(x3);
31 }
32 int main() {
33 test();
34 }
View Code

 

2.图片:

 

 

 

实验任务5:
1.代码:

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         }
29         out<<endl;
30     }
31 }
32 int main(){
33     
34     output(cout);
35 
36     ofstream outFile("cipher_key.txt");
37     output(outFile);
38     outFile.close();
39 
40     return 0;
41 }
View Code

 

2.图片:

 

 

标签:std,文件,index,Vector,实验,template,ptr,模板,size
From: https://www.cnblogs.com/yili123/p/17895786.html

相关文章

  • 实验6 模板类、文件I/O和异常处理
    实验任务4Vector.hpp#ifndefVECTOR_HPP#defineVECTOR_HPP#include<iostream>#include<stdexcept>template<typenameT>classVector;template<typenameU>voidoutput(constVector<U>&vec);template<typenameT&......
  • 实验6 C语言结构体、枚举应用编程
    1.实验任务1【验证性实验】2.实验任务2【验证性实验】3.实验任务3【验证性实验】4.实验任务4task4源代码:1#include<stdio.h>2#include<stdlib.h>3#defineN1045typedefstruct{6charisbn[20];//isbn号7charname[80];......
  • 实验六
    任务四task4.cpp1#include<iostream>2#include"vector.hpp"3voidtest()4{5usingnamespacestd;6intn;7cin>>n;89Vector<double>x1(n);10for(autoi=0;i<n;++i)11......
  • 实验六
    #include<stdio.h>#include<string.h>#include<stdlib.h>typedefstruct{charname[20];//姓名charphone[12];//手机号intvip;//是否为紧急联系人,是取1;否则取0}Contact;//函数声明voidset_vip_contact(Contactx[],......
  • 开发Chrome扩展程序,核心manifest 文件(上)
    大家好,我是dom哥。我正在写关于Chrome扩展开发的系列文章,感兴趣的可以点个小星星。Chrome在全球浏览器市场份额独占6成,无论是对普通用户还是开发者,都是电脑里的必备利器。Chrome无论是在性能还是UI交互方面都非常出色,而Chrome扩展则为开发者提供了接口,让开发者有能力......
  • 实验7
    //P286例8.17//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块//打印不及格学生信息和所有学生信息程分别调用#include<stdio.h>#include<string.h>#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstu......
  • Chrome扩展的核心:manifest 文件(上)
    大家好,我是dom哥。我正在写关于Chrome扩展开发的系列文章,感兴趣的可以点个小星星。Chrome在全球浏览器市场份额独占6成,无论是对普通用户还是开发者,都是电脑里的必备利器。Chrome无论是在性能还是UI交互方面都非常出色,而Chrome扩展则为开发者提供了接口,让开发者有能力自......
  • common-fileupload组件实现java文件上传和下载
    简介:文件上传和下载是javaweb中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到本地。对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直......
  • 保姆级教程利用免费内网穿透工具快速实现远程访问SMB文件共享
    当你需要在远程网络中访问SMB(ServerMessageBlock)共享资源时,你可以使用Solopace.Gem来建立安全且便捷的连接。Solopace.Gem是一款专为远程访问和网络连接而设计的工具,它能够轻松地穿越NAT(网络地址转换)和防火墙,如果运营商的防火墙让你能够安全地远程访问SMB共享文件夹。本教程将指......
  • `/etc/gitlab/gitlab-secrets.json`文件丢失时Gitlab恢复办法
    当/etc/gitlab/gitlab-secrets.json文件丢失时如果您没有备份secrets文件,则必须完成几个步骤才能使GitLab重新正常工作。secrets文件负责存储包含必需的敏感信息的列的加密密钥。如果密钥丢失,GitLab将无法解密这些列,从而阻止对以下项目的访问:CI/CD变量Kubernetes/GCP集成自......