首页 > 其他分享 >实验六 模板类和文件IO

实验六 模板类和文件IO

时间:2022-12-06 20:47:26浏览次数:33  
标签:const template int Vector 实验 IO output base 模板

Task4

//VEctor.hpp
#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; }
//task6.cpp
#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(); }

测试结果

 

 

 

Task5

// Task5.cpp
#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,实验,IO,output,base,模板
From: https://www.cnblogs.com/lyhy/p/16960439.html

相关文章

  • poj1651 Multiplication Puzzle--区间dp
    原题链接:​​http://poj.org/problem?id=1651​​题意:给出N个数,每次从中抽出一个数(第一和最后一个不能抽),每次的得分为抽出的数与相邻两个数的乘积,直到只剩下首尾两个数为......
  • 包层次的时间同步实验
    一、实验目的理解掌握时间同步的重要意义了解时间同步的基本方法,理解TPSN协议机理掌握TinyOS定时器相关接口和组件的使用4.能够在TinyOS中实现多节点间的时间......
  • Spring框架之IOC入门
    1.开发环境IDEA版本:2022.1.4Maven版本:3.8.6Spring版本:6.0.2 2.案例分析2.1自定义Man类2.2自定义Company类2.3自定义CompanyTest类2.4分析图 3.案......
  • SRP实验
    一、实验目的学习源路由协议的机制理解节点在源路由协议中的几种角色掌握源路由协议相关接口SourceRouteSend、SourceRoutePacket的使用 二、实验要求功能:root......
  • 1769.minimum-number-of-operations-to-move-all-balls-to-each-box 移动所有球到每个
    问题描述1769.移动所有球到每个盒子所需的最小操作数解题思路暴力求解,时间复杂度为\(\Theta(n^2)\);可以考虑利用前缀和来降低时间复杂度:设nums[i]是前i+1个盒子里......
  • Dissemination实验
    一、实验目的学习少量数据在传感网中可靠传递的方法掌握TinyOS中DisseminationValue、DisseminationUpdate接口和组件的使用理解Dissemination的机制 二、实......
  • LEEP节点无线链路质量评估实验
    一、实验目的理解节点通信链路质量的影响因素理解提升链路质量的一般方法学习TinyOS系统中CC2530/CC2538节点发送功率的设置体验不同发送功率、不同通信距离、不......
  • 基础IO
    写在前面现在我们要开始Linux的第二个大模块,基础IO.在这里你将真正的认识到文件究竟是什么,了解到文件系统,更可以解决我们之前一直避而不谈的缓冲区,我们一起来看看吧.文......
  • 实验6
    task3_1:#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101......
  • 无线传感器网络技术-实验一-TINYOS/6LOWPAN 编程基础
    【实验目的】1.掌握TinyOS/6LoWPAN开发环境的搭建方法2.掌握VisualStudioCode中TinyOS编程的相关设置3.掌握VSCode中CC2530节点(平台名称cc2530zn)和CC......