实验6
1.实验任务4
1 #include <iostream> 2 #include <stdexcept> 3 4 template <typename T> 5 class Vector { 6 private: 7 T* data; 8 size_t size; 9 public: 10 Vector(size_t n) : size(n) { 11 data = new T[size]; 12 } 13 Vector(size_t n, const T& value) : size(n) { 14 data = new T[size]; 15 for (size_t i = 0; i < size; ++i) { 16 data[i] = value; 17 } 18 } 19 Vector(const Vector& other) : 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() { 26 delete[] data; 27 } 28 29 size_t get_size() const { 30 return size; 31 } 32 33 T& at(size_t i) { 34 if (i >= size) { 35 throw std::out_of_range("Index out of range"); 36 } 37 return data[i]; 38 } 39 T& operator[](size_t i) { 40 if (i >= size) { 41 throw std::out_of_range("Index out of range"); 42 } 43 return data[i]; 44 } 45 friend void output(const Vector& v) { 46 for (size_t i = 0; i < v.size; ++i) { 47 std::cout << v.data[i] << " "; 48 } 49 std::cout << std::endl; 50 } 51 };
1 #include <iostream> 2 #include "Vector.hpp" 3 4 void test() { 5 using namespace std; 6 int n; 7 cin >> n; 8 Vector<double> x1(n); 9 for (auto i = 0; i < n; ++i) 10 x1.at(i) = i * 0.7; 11 output(x1); 12 Vector<int> x2(n, 42); 13 Vector<int> x3(x2); 14 output(x2); 15 output(x3); 16 x2.at(0) = 77; 17 output(x2); 18 x3[0] = 999; 19 output(x3); 20 21 int main() { 22 test(); 23 return 0; 24 }
2.实验任务5
1 #include <iostream> 2 #include <fstream> 3 #include <iomanip> 4 5 using namespace std; 6 7 void printCharRow(std::ostream& out, int row, char baseChar) 8 { 9 for (int i = 0; i < 26; i++) { 10 char n = baseChar + (row + i) % 26; 11 out << setw(2) << n << ' '; 12 } 13 out << endl; 14 } 15 16 void output(std::ostream& out) 17 { 18 for (int i = 0; i < 26; i++) { 19 if (i == 0) { 20 out << ' ' << setw(2) << ' '; 21 } 22 else { 23 out << setw(2) << i << ' '; 24 } 25 printCharRow(out, i, (i == 0) ? 'a' : 'A'); 26 } 27 } 28 29 int main() 30 { 31 cout << "屏幕上打印出:\n" << endl; 32 output(cout); 33 34 ofstream outfile("cipher_key.txt"); 35 if (outfile.is_open()) { 36 output(outfile); 37 outfile.close(); 38 } 39 else { 40 cout << "无法打开文件 cipher_key.txt" << endl; 41 } 42 43 return 0; 44 }
1 a b c d e f g h i j k l m n o p q r s t u v w x y z 2 1 B C D E F G H I J K L M N O P Q R S T U V W X Y Z A 3 2 C D E F G H I J K L M N O P Q R S T U V W X Y Z A B 4 3 D E F G H I J K L M N O P Q R S T U V W X Y Z A B C 5 4 E F G H I J K L M N O P Q R S T U V W X Y Z A B C D 6 5 F G H I J K L M N O P Q R S T U V W X Y Z A B C D E 7 6 G H I J K L M N O P Q R S T U V W X Y Z A B C D E F 8 7 H I J K L M N O P Q R S T U V W X Y Z A B C D E F G 9 8 I J K L M N O P Q R S T U V W X Y Z A B C D E F G H 10 9 J K L M N O P Q R S T U V W X Y Z A B C D E F G H I 11 10 K L M N O P Q R S T U V W X Y Z A B C D E F G H I J 12 11 L M N O P Q R S T U V W X Y Z A B C D E F G H I J K 13 12 M N O P Q R S T U V W X Y Z A B C D E F G H I J K L 14 13 N O P Q R S T U V W X Y Z A B C D E F G H I J K L M 15 14 O P Q R S T U V W X Y Z A B C D E F G H I J K L M N 16 15 P Q R S T U V W X Y Z A B C D E F G H I J K L M N O 17 16 Q R S T U V W X Y Z A B C D E F G H I J K L M N O P 18 17 R S T U V W X Y Z A B C D E F G H I J K L M N O P Q 19 18 S T U V W X Y Z A B C D E F G H I J K L M N O P Q R 20 19 T U V W X Y Z A B C D E F G H I J K L M N O P Q R S 21 20 U V W X Y Z A B C D E F G H I J K L M N O P Q R S T 22 21 V W X Y Z A B C D E F G H I J K L M N O P Q R S T U 23 22 W X Y Z A B C D E F G H I J K L M N O P Q R S T U V 24 23 X Y Z A B C D E F G H I J K L M N O P Q R S T U V W 25 24 Y Z A B C D E F G H I J K L M N O P Q R S T U V W X 26 25 Z A B C D E F G H I J K L M N O P Q R S T U V W X Y