首页 > 其他分享 >实验六

实验六

时间:2022-12-07 14:58:05浏览次数:48  
标签:const template int Vector 实验 base output

#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<bits/stdc++.h>

using namespace std;

template<typename T>
class Vector{
    
private:
    
    int length;
    T * base;
    
public:
    
    Vector(const int & n, const T & x);
    Vector(const int & n);
    Vector(const Vector<T> & obj);
    ~ Vector();
    
    T & at(const int & w) const ;
    T & operator [] (const int & w) const ;
    int get_size() const;

    template<typename TT>
    friend void output(const Vector<TT> & obj);
};

template<typename T>
Vector<T>::Vector(const int & n){
    length = n;
    base = new T[n];
}

template<typename T>
Vector<T>::Vector(const int & n, const T & x){
    length = n;
    base = new T[n];
    for(int i = 0; i < n; i ++)
        base[i] = x;
}

template<typename T>
Vector<T>::Vector(const Vector<T> & obj){
    int n = obj.get_size();
    length = n;
    base = new T[n];
    for(int i = 0; i < n; i ++)
        base[i] = obj[i];
}

template<typename T>
T & Vector<T>::at(const int & w) const {
    return base[w];
}

template<typename T>
T & Vector<T>::operator [] (const int & w) const {
    return base[w];
}

template<typename T>
int Vector<T>::get_size() const {
    return length;
}

template<typename T>
Vector<T>::~Vector(){
    delete [] base;
}

template<typename TT>
void output(const Vector<TT> & obj){
    for(int i = 0; i < obj.get_size(); i ++)
        cout << obj[i] << " ";
    cout << endl;
}

#include<fstream>
#include<iostream>
#include<bits/stdc++.h>

using namespace std;

void output(ofstream &out, string To = "con"){

    out.open(To);
    
    if(!out.is_open()){
        cout << "open fail!!\n";
        return ;
    }

    out << "  ";
    for(char c = 'a'; c <= 'z'; c ++)
        out << ' ' << c;
    out << endl;
    
    for(int i = 1; i <= 26; i ++){
        out << setw(2) << right << i;
        for(int drt = 1; drt <= 26; drt ++)
            out << ' ' << (char)((drt + i - 1) % 26 + 'A');
        out << endl;
    }

    out.close();
}

int main(){
    
    ofstream out;
    
    output(out);
    output(out, "cipher_key.txt");
}

 

标签:const,template,int,Vector,实验,base,output
From: https://www.cnblogs.com/qazmlp10/p/16963049.html

相关文章

  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......
  • 实验6
    #include<fstream>#include<iostream>#include<bits/stdc++.h>usingnamespacestd;voidoutput(ofstream&out,stringTo="con"){out.open(To);......
  • uml实验总结
    昨天终于完成了uml,所以实验报告,在这次我学了uml九大图,学会了用例图、类图、顺序图、活动图、协作图、状态机图和部署图,我发现报告是很难写,系统算简单的,说明书报告也难写,花......
  • 实验6 模板类和文件IO
    实验任务3task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,......
  • 实验六
    task3_1#include<iostream>#include<vector>template<typenameT>voidoutput(constT&obj){for(auto&item:obj)std::cout<<item<<",";std::cout<<"......
  • 实验6
    task3:3.1代码:#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100......
  • 实验六
    实验六task4vector.hpp#pragmaonce#include<iostream>usingnamespacestd;template<typenameT>classVector{public: Vector(){}; Vector(intn):size{n......
  • 实验六
    Task3:task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99......
  • Python实验报告——第6章 函数
    实例01:输出每日一帖(共享版) 在IDLE中创建一个名称为function_tips.py的文件,然后在该文件中创建一个名称为function_tips的函数,在该函数中,从励志文字列表中获取一条......
  • 实验六
    task3测试结果:      源代码:1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingname......