首页 > 其他分享 >实验六

实验六

时间:2023-12-16 16:11:35浏览次数:22  
标签:int Vector 实验 output include ptr size

task4源代码:

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<stdexcept>
 5 #include<cassert>
 6 #include<iomanip>
 7 
 8 using namespace std;
 9 
10 template <typename T>
11 class Vector {
12     public:
13         Vector(int size){
14     if(size < 0) {
15         throw length_error("Vector constructor:negative size");
16     }
17     this->size = size;
18     ptr = new T[this->size];
19 }
20 
21         Vector(int size, T value) {
22     if(size < 0) {
23         throw length_error("Vector construtor:negative size");
24     }
25     this->size = size;
26     ptr = new T[size];
27     for(auto i=0;i<size;i++) {
28         ptr[i] = value;
29     }
30 }
31 
32         Vector(const Vector<T>& x) {
33     size = x.size;
34     ptr = new T[size];
35     for(auto i=0;i<size;i++) {
36         ptr[i] = x.ptr[i];
37     }
38 }
39         
40         ~Vector() { delete[] ptr; }
41         
42         int get_size() { return size; }
43         
44         T& at(int i) const {
45             assert(i >= 0 && i < size);
46             return(ptr[i]);
47         }
48         
49         T& operator[](int i) { return ptr[i]; }
50         
51         friend void output(Vector<T> x) {
52             for(auto i=0;i<x.get_size();i++) {
53                 cout << x.at(i) << ", ";
54             }
55             cout << "\t\t\n";
56         }
57         
58     private:
59         int size;
60         T* ptr;
61 };
Vector.hpp
 1 #include"Vector.hpp"
 2 #include<iostream>
 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     
15     output(x1);
16     
17     Vector<int> x2(n, 42);
18     Vector<int> x3(x2);
19     
20     output(x2);
21     output(x3);
22     
23     x2.at(0) = 77;
24     output(x2);
25     
26     x3[0] = 999;
27     output(x3);
28 }
29 
30 int main() {
31     test();
32 }
task4.cpp

 

task4运行结果:

 

task5源代码:

 1 #include<iostream>
 2 #include<fstream>
 3 #include<iomanip>
 4 
 5 using namespace std;
 6 
 7 void output(std::ostream& output) {
 8     output << "  ";
 9     for(int i=97;i<123;i++) {
10         output << setw(2) << char(i);
11     }
12     output << endl;
13     
14     for(int i=1;i<=26;i++) {
15         output << setw(2) << i;
16         
17         for(int j=i;j<i+26;j++) {
18             output << setw(2) << char(65 + j % 26);
19         }
20         output << endl;
21     }
22 }
23 
24 int main() {
25     ofstream out;
26     output(cout);
27     out.open("cipher_key.txt");
28     output(out);
29     return 0;
30 }
task5.cpp

 

task5运行结果:

 

 

标签:int,Vector,实验,output,include,ptr,size
From: https://www.cnblogs.com/atry/p/17904946.html

相关文章

  • 实验7
    //将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》",......
  • 实验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......
  • 实验七
    task41#include<stdio.h>2intmain(){3FILE*fp;4longcount=0;5chart;6if((fp=fopen("data4.txt","r"))==NULL){7printf("error\n");8return1;9}10while(!f......
  • 【腾讯云云上实验室】用向量数据库在金融信用数据库分析中的实战运用
    一、前言这篇文章将带领读者探索数据库的多样化解决方案及其演进历程,特别关注向量数据库的重要性和在实际项目中的应用。通过深入剖析腾讯云向量数据库及其在金融信用数据库分析中的实战运用,为读者提供全面而实用的指南,帮助他们理解、应用和掌握这一技术领域的关键要点。二、数......
  • 实验6 c语言结构体,枚举应用编程
    task4源代码1#include<stdio.h>2#include<string.h>3#defineN1045typedefstruct{6charisbn[20];//isbn号7charname[80];//书名8charauthor[80];//作者9doublesales_price;//......
  • 实验六
    task41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • 实验六
    实验四 源代码1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count......
  • 什么是LIS系统?实验室信息管理系统是什么?
    在医疗信息化改革的这个大背景下,很多医院都在不断完善信息化建设,有些人还不知道医院实验室信息管理系统。本文章仅以个人的理解整理出来,本文从LIS系统定义、LIS系统实现了哪些功能、LIS系统解决了实验室实际工作中的哪些问题,进行分享;希望能帮助更多基础医疗单位了解【实验室信息管......
  • 实验6
     #include<stdio.h>#include<string.h>#defineN10//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doubleperf;//平时成绩doublemid;//期中成绩doublef......
  • 实验六
    task_4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voido......