task4
vector.hpp
#include <iostream> #include <stdexcept> using namespace std; template <typename T> class Vector { private: T* data; int size; public: Vector() : data(nullptr), size(0) {} Vector(int n) : size(n) { data = new T[n]; for (int i = 0; i < n; i++) { data[i] = T(); } } Vector(const Vector& other) : size(other.size) { data = new T[size]; for (int i = 0; i < size; i++) { data[i] = other.data[i]; } } Vector(int n, T t):size{n} { data= new T[n]; for (int i = 0; i < n; i++) data[i] = t; } ~Vector() { delete[] data; } int get_size() const { return size; } T& at(int i) { if (i < 0 || i >= size) { throw out_of_range("Index out of range"); } return data[i]; } T& operator[](int i) { return at(i); } friend void output(const Vector& v) { for (int i = 0; i < v.size; i++) { cout << v.data[i] << " "; } cout << endl; } };View Code
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.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(); }View Code
运行结果
task5
#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(); }View Code
标签:文件,int,Vector,实验,output,include,data,模板,size From: https://www.cnblogs.com/PerfectAllKill/p/17909333.html