任务四
代码:
Vector.hpp:
#pragma once #include<iostream> using namespace std; template <class T> class Vector{ public: Vector(int size0):size{size0} {p=new T[size];} Vector(int size0,T value):size{size0} { p=new T[size]; for(int i=0;i<size;i++) p[i]=value; } Vector(const Vector &v):size{v.size} { p=new T[size]; for(int i=0;i<size;i++) p[i]=v.p[i]; } ~Vector() {delete[] p;} int get_size() const {return size;} T& at(int i) { if(i>=0 && i<size) return p[i]; } T &operator[](int i) { if(i>=0 && i<size) return p[i]; } friend void output(Vector<T> &v) { for(int i=0;i<v.size;i++) cout << v.p[i] << " "; cout << endl; } private: int size; T value; T *p; };
task4.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(); }
结果截图:
任务五
代码:
task4.cpp:
#include<iostream> #include<fstream> #include<string> #include<iomanip> using namespace std; void output(ostream &out) { int n = 26; out << " "; for(auto i='a';i<='z';i++) { out << setw(2) << i; } out << endl; for(auto i=1;i<=26;i++) { out << setw(2) << i; n++; for(int j=n;j<n+26;j++) { int ch=j%26+65; out << setw(2) << char(ch); } out << endl; } } int main() { ofstream out; output(cout); out.open("cipher_key.txt"); output(out); out.close(); }
运行结果截图:
标签:int,Vector,实验,IO,x3,x2,output,include,模板 From: https://www.cnblogs.com/whwssr/p/16960543.html