首页 > 其他分享 >实验六

实验六

时间:2023-12-17 14:25:35浏览次数:26  
标签:index int Vector 实验 include ptr size

#pragma once

#include<iostream>
#include<stdexcept>

using namespace std;

template<typename T>
class Vector
{
private:
    int size;
    T* ptr;
public:

    Vector(int n):size{n}
    {
        if (n < 0)
            throw std::length_error("vector constructor:negative size");

        ptr = new T[size];
    }
    Vector(int n, T value):size{n}
    {
        if (n < 0)
            throw std::length_error("vector constructor:negative size");

        ptr = new T[size];
        for (auto i = 0; i < size; ++i)
        {
            ptr[i] = value;
        }

    }
    Vector(const Vector<T>& vi):size{vi.size},ptr{new T[size]}
    {
        for (auto i = 0; i < size; i++)
        {
            ptr[i] = vi.ptr[i];
        }
    }
    ~Vector()
    {
        delete[] ptr;
    }
    int get_size() const { return size; }
    T& at(int index)
    {
        if (index < 0 || index >= size)
            throw std::out_of_range("Vector::at()");

        return ptr[index];
    }
    T& at(int index) const
    {
        if (index < 0 || index >= size)
            throw std::out_of_range("Vector::at()");

        return ptr[index];
    }
    T& operator[](int index)
    {
        return ptr[index];
    }
    friend void output(Vector<T>& v)
    {
        for (auto i = 0; i < v.get_size(); i++)
            cout << v.at(i) << ",";
        cout << "\b\b\n";
    }
};
#include <iostream>
#include "Vector.hpp"

void test() {
    using namespace std;

    int n;
    cin >> n;

    Vector<double> x1(n);
    for(auto i = 0; i < n; ++i)
        x1.at(i) = i * 0.7;

    output(x1);

    Vector<int> x2(n, 42);
    Vector<int> x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;
    output(x2);

    x3[0] = 999;
    output(x3);
}

int main() {
    test();
}

 

#include<iostream>
#include<iomanip>
#include<fstream>

using namespace std;

void output(ostream &out) {
     for(int i=0;i<=26;i++){
        for(int j=0;j<=26;j++)
         {
            char c,b;
            if(i==0&&j==0){
            char d = ' ';
             out<<setw(2)<<d;
             }
            else if(j==0&&i!=0){
                out<<setw(2)<<i;
            }
            else if(i==0&&j!=0){
                 char c='a'+j-1;
                out<<setw(2)<<c;
             }
            else if(i!=0&&j!=0){
                 char b=(i+j-1+26)%26+'A';
                 out<<setw(2)<<b;
             }
        }
         out<<endl;
     }
 }
int main(){
    output(cout);

    ofstream outFile("cipher_key.txt");
    output(outFile);
    outFile.close();

    return 0;
 }

 

标签:index,int,Vector,实验,include,ptr,size
From: https://www.cnblogs.com/tantivy/p/17909016.html

相关文章

  • 实验7
    4.实验任务4task4.c源码:#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(&qu......
  • 实验6
    4.实验任务4task4.c源码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册......
  • 鸿蒙小车之多任务调度实验
    说到鸿蒙我们都会想到华为mate60:遥遥领先!我们一直领先!我们这个小车也是采用的是鸿蒙操作系统,学习鸿蒙小车,让你遥遥领先于你的同学。@TOC前言本专栏将依次介绍鸿蒙小车的内核实验,硬件实验,wifi实验。一、什么是任务?为什么要有任务任务是操作系统(RTOS)中的基本组成单元,它们为嵌入式......
  • 实验七
    task4.c#include<stdio.h>intmain(){ charch; intcnt=0;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){ printf("failtoopen\n"); return1; }while(1){ch=fgetc(fp);......
  • 实验6-模板类、文件I/O和异常处理
    Vector.hpp1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<iostream>5#include<stdexcept>67template<classT>8classVector{9private:10T*arr;11intsize;1213public:14Vector():arr(nu......
  • 实验六
    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......
  • 实验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[]={{"《雕塑家》",......
  • 实验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......