任务四、
vector.hpp
#pragma once #include <iostream> using namespace std; template<typename T> class Vector { public: Vector(int n) :size{ n } { p = new T[n]; } ~Vector() { delete[] p; } Vector(const Vector<T> & newp); Vector(int n, T value) :size{ n } { p = new T[size]; for (auto i = 0; i < size; i++) p[i] = value; } int get_size()const { return size; } T& at(int i) { return p[i]; } T& operator[](int index) { return p[index]; } private: int size; T* p; template<typename T1> friend void output( Vector<T1>& x); }; template<typename T> Vector<T>::Vector(const Vector<T>& newp) : size{ newp.size } { p = new T[size]; for (auto i = 0; i < size; i++) p[i] = newp.p[i]; } template<typename T1> void output(Vector<T1>& x) { for (auto i = 0; i < x.size; i++) cout << x.at(i) << ", "; cout << endl; };
task.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(); }
任务五、
#include<fstream> #include<iostream> #include<iomanip> #define N 27 using namespace std; char form[N][N]; void output(ostream& out) { for(int num=0;num<1;num++) for (int i = 0; i < N; i++) { if (i == 0) cout << " "; else cout << setw(2) << form[i][0] - '0'; for (int j = 1; j < N; j++) { cout << setw(2) << form[i][j]; } cout << endl; } } int main() { ofstream out; out.open("cipher_key.txt"); if (!out.is_open()) { cout << "fail to open the cipher_key.txt\n"; return 1; } form[0][0] = ' '; for (int i = 1; i < N; i++) { form[0][i] = 'a' + i - 1; form[i][0] = i + '0'; } int j; for (int i = 1; i < N; i++) { for (j = 1; j + i < N; j++) form[i][j] = form[0][j] - 32 + form[i][0] - '0'; for (j; j + i >= N && j < N; j++) form[i][j] = form[0][j] - 58 + form[i][0] - '0'; } output(out); }
标签:int,Vector,实验,x2,output,include,size From: https://www.cnblogs.com/shmily-cwh/p/16942794.html