首页 > 其他分享 >实验6

实验6

时间:2023-12-17 15:22:50浏览次数:33  
标签:int Vector 实验 output include ptr size

任务4

#include <iostream>
#include "Vector.hpp"
using namespace std;

template<typename T>
void output(const Vector<T>& v){
    for(auto i=0;i<v.get_size();i++){
        cout << v.at(i) << ", ";


    }
    cout << "\b\b \n";
}

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();
}
View Code
#include<iostream>
#include<stdexcept>
#include <cassert>
using namespace std;
template<typename T>
class Vector
{
private:
    int size;
    T* ptr;
public:

    Vector(int size)
    {
        if(size<0)
            throw std::length_error("vector constructor:negative size");
        this->size = size;
        ptr = new T[this->size];
    }
    Vector(int size, T value)
    {
        if(size<0)
            throw std::length_error("vector constructor:negative size");
        this->size = size; ptr = new T[size];
        for (auto i = 0; i < size; i++)
        {
            ptr[i] = value;
        }

    }
    Vector(const Vector<T>& x)
    {
        size = x.size;
        ptr = new T[size];
        for (auto i = 0; i < size; i++)
        {
            ptr[i] = x.ptr[i];
        }
    }
    ~Vector()
    {
        delete[] ptr;
    }
    int get_size() { return size; }
    T& at(int i)
    {
        assert(i >= 0 && i < size);
        return(ptr[i]);
    }
    T& at(int i) const
    {
        assert(i >= 0 && i < size);
        return(ptr[i]);
    }
     T& operator[](int i)
    {
        return ptr[i];
    }
};
View Code

任务5

 #include<iostream>
 #include<fstream>
 #include<iomanip>
 
 using namespace std;
 
 void output(std::ostream& output) {
     output << "  ";
     for(int i=97;i<123;i++) {
         output << setw(2) << char(i);
     }
     output << endl;
     
     for(int i=1;i<=26;i++) {
         output << setw(2) << i;
         
         for(int j=i;j<i+26;j++) {
             output << setw(2) << char(65 + j % 26);
         }
         output << endl;     }
 }
 


 int main(){
     
     output(cout);
     ofstream outFile("cipher_key.txt");
     output(outFile);
   outFile.close();
 
     return 0;
 }
 
View Code

 

标签:int,Vector,实验,output,include,ptr,size
From: https://www.cnblogs.com/xun-1/p/17909120.html

相关文章

  • 实验6 C语言结构体、枚举应用编程
    实验任务1源代码:1#include<stdio.h>2#include<string.h>3#defineN10//运行程序输入测试时,可以把这个数组改小一些输入测试45typedefstructstudent{6intid;//学号7charname[20];//姓名8......
  • 实验六
    #pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:intsize;T*ptr;public:Vector(intn):size{n}{if(n<0)throwstd::leng......
  • 实验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......