任务4
1 #include <iostream> 2 #include "Vector1.hpp" 3 4 void test() { 5 using namespace std; 6 7 int n; 8 cin >> n; 9 10 Vector<double> x1(n); 11 for(auto i = 0; i < n; ++i) 12 x1.at(i) = i * 0.7; 13 14 output(x1); 15 16 Vector<int> x2(n, 42); 17 Vector<int> x3(x2); 18 19 output(x2); 20 output(x3); 21 22 x2.at(0) = 77; 23 output(x2); 24 25 x3[0] = 999; 26 output(x3); 27 } 28 29 int main() { 30 test(); 31 }task41.cpp
1 #pragma once 2 #include<iostream> 3 #include<stdexcept> 4 template<typename T> 5 class Vector{ 6 public: 7 Vector(size_t s){ 8 size=s; 9 data=new T[size]; 10 } 11 Vector(size_t s,const T &value){ 12 size=s; 13 data=new T[size]; 14 for (size_t i=0;i<size;i++){ 15 data[i]=value; 16 } 17 } 18 Vector(const Vector& other){ 19 size=other.size; 20 data=new T[size]; 21 for(size_t i=0;i<size;i++){ 22 data[i]=other.data[i]; 23 } 24 } 25 ~Vector(){delete[] data;} 26 size_t get_size() const{ 27 return size; 28 } 29 T& at(size_t index) const{ 30 if(index>=size){ 31 throw std::out_of_range("0"); 32 } 33 return data[index];} 34 T& operator[](size_t index) const{ 35 if(index>=size){ 36 throw std::out_of_range("false"); 37 } 38 return data[index]; 39 } 40 friend void output(const Vector& vec){ 41 for(size_t i=0;i<vec.size;i++){ 42 std::cout<<vec.data[i]<<std::endl; 43 } 44 } 45 private: 46 T* data; 47 size_t size; 48 49 };Vector1.hpp
任务5
1 #include<iostream> 2 #include<iomanip> 3 #include<fstream> 4 5 using namespace std; 6 7 void output(ostream &out) { 8 for(int i=0;i<=26;i++){ 9 for(int j=0;j<=26;j++) 10 { 11 char m,n; 12 if(i==0&&j==0){ 13 char t = ' '; 14 out<<setw(2)<<t; 15 } 16 else if(j==0&&i!=0){ 17 out<<setw(2)<<i; 18 } 19 else if(i==0&&j!=0){ 20 char m='a'+j-1; 21 out<<setw(2)<<m; 22 } 23 else if(i!=0&&j!=0){ 24 char n=(i+j-1+26)%26+'A'; 25 out<<setw(2)<<n; 26 } 27 28 } 29 out<<endl; 30 } 31 } 32 int main(){ 33 34 output(cout); 35 36 ofstream outFile("cipher_key.txt"); 37 output(outFile); 38 outFile.close(); 39 40 return 0; 41 }task5.cpp
标签:Vector,实验,x3,x2,output,include,size From: https://www.cnblogs.com/shaoshuai-nuist/p/17894022.html