实验任务4:
1.代码:
vector.hpp:
1 #pragma once 2 #include <iostream> 3 #include <stdexcept> 4 using std::cout; 5 using std::endl; 6 7 template<typename T> 8 class Vector { 9 public: 10 Vector(T n); 11 Vector(T n, T value); 12 Vector(const Vector &vi); 13 ~Vector(); 14 T& at(T index); 15 T& at(T index) const; 16 T get_size() const; 17 T& operator[](T index); 18 T& operator[](T index) const; 19 private: 20 T size; 21 T *ptr; 22 }; 23 24 template<typename T> 25 Vector<T>::Vector(T n): size{n} { 26 if(n < 0) 27 throw std::length_error("vector constructor: negative size"); 28 ptr = new T[(int)size]; 29 } 30 31 template<typename T> 32 Vector<T>::Vector(T n, T value): size{n} { 33 if(n < 0) 34 throw std::length_error("vector constructor: negative size"); 35 ptr = new T[size]; 36 for(auto i = 0; i < size; ++i) 37 ptr[i] = value; 38 } 39 40 template<typename T> 41 Vector<T>::Vector(const Vector &vi): size{vi.size}, ptr{new T[size]} 42 { 43 for(auto i = 0; i < size; ++i) 44 ptr[i] = vi.ptr[i]; 45 } 46 47 template<typename T> 48 Vector<T>::~Vector() { 49 delete [] ptr; 50 } 51 52 template<typename T> 53 T Vector<T>::get_size() const { 54 return size; 55 } 56 57 template<typename T> 58 T& Vector<T>::at(T index) { 59 if(index < 0 || index >= size) 60 throw std::out_of_range("vector::at()"); 61 return ptr[(int)index]; 62 } 63 64 template<typename T> 65 T& Vector<T>::at(T index) const { 66 if(index < 0 || index >= size) 67 throw std::out_of_range("vector::operator[]()"); 68 return ptr[(int)index]; 69 } 70 71 template<typename T> 72 T& Vector<T>::operator[](T index) { 73 if(index < 0 || index >= size) 74 throw std::out_of_range("vector::operator[]()"); 75 return ptr[index]; 76 } 77 78 template<typename T> 79 T& Vector<T>::operator[](T index) const { 80 if(index < 0 || index >= size) 81 throw std::out_of_range("vector::operator[]()"); 82 return ptr[index]; 83 84 }View Code
task4.cpp:
1 #include <iostream> 2 #include "Vector.hpp" 3 using namespace std; 4 5 template<typename T> 6 void output(const Vector<T>& v){ 7 for(auto i=0;i<v.get_size();i++){ 8 cout << v.at(i) << ", "; 9 10 11 } 12 cout << "\b\b \n"; 13 } 14 15 void test() { 16 using namespace std; 17 int n; 18 cin >> n; 19 Vector<double> x1(n); 20 for(auto i = 0; i < n; ++i) 21 x1.at(i) = i * 0.7; 22 output(x1); 23 Vector<int> x2(n, 42); 24 Vector<int> x3(x2); 25 output(x2); 26 output(x3); 27 x2.at(0) = 77; 28 output(x2); 29 x3[0] = 999; 30 output(x3); 31 } 32 int main() { 33 test(); 34 }View Code
2.图片:
实验任务5:
1.代码:
task5.cpp:
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 c,b; 12 if(i==0&&j==0){ 13 char d = ' '; 14 out<<setw(2)<<d; 15 } 16 else if(j==0&&i!=0){ 17 out<<setw(2)<<i; 18 } 19 else if(i==0&&j!=0){ 20 char c='a'+j-1; 21 out<<setw(2)<<c; 22 } 23 else if(i!=0&&j!=0){ 24 char b=(i+j-1+26)%26+'A'; 25 out<<setw(2)<<b; 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 }View Code
2.图片:
标签:std,文件,index,Vector,实验,template,ptr,模板,size From: https://www.cnblogs.com/yili123/p/17895786.html