实验六
#include <iostream> #include <stdexcept> template <typename T> class Vector { private: int size; T *vec; public: Vector<T>() = default; Vector<T>(int s); Vector<T>(int s, T t); Vector<T>(const Vector<T> &v); ~Vector<T>(); int get_size() const; T& at(int i); T& operator[](int i); template <typename T1> friend void output(const Vector<T1> &v); }; template <typename T> Vector<T>::Vector(int s) : size{s} { vec = new T[s]; } template <typename T> Vector<T>::Vector(int s, T t) : size{s} { vec = new T[s]; for (int i = 0; i < s; i++) vec[i] = t; } template <typename T> Vector<T>::Vector(const Vector<T> &v) : size{v.size} { vec = new T[v.size]; for (int i = 0; i < v.size; i++) vec[i] = v.vec[i]; } template <typename T> Vector<T>::~Vector() { delete[] vec; } template <typename T> int Vector<T>::get_size() const { return size; } template <typename T> T& Vector<T>::at(int i) { if (i > size) throw std::out_of_range("下标越界"); else return vec[i]; } template <typename T> T& Vector<T>::operator[](int i) { if (i > size) throw std::out_of_range("下标越界"); else return vec[i]; } template <typename T1> void output(const Vector<T1> &v) { for (int i = 0; i < v.size; i++) std::cout << v.vec[i] << ", "; std::cout << "\b\b \n"; }
#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.618; output(x1); Vector<int> x2(n, 777); Vector<int> x3(x2); output(x2); output(x3); x2.at(0) = 42; output(x2); x3[0] = 666; output(x3); } int main() { test(); }
#include<iostream> #include<fstream> void output(std::ostream &out) { for (int i = 0; i <= 26; i++) { out.width(2); if (i == 0) out << ' '; else out << i; for (int j = 0; j <= 26; j++) { out << ' '; if (i == 0) out << (char)('a' + (i + j) % 26); else out << (char)('A' + (i + j) % 26); } out << '\n'; } } void output() { output(std::cout); std::ofstream ofile; ofile.open("cipher_key.txt"); output(ofile); ofile.close(); } int main() { output(); }