实验6
任务4
Vector.hpp
1 #pragma once 2 #include <iostream> 3 using namespace std; 4 5 template <typename T> 6 class Vector { 7 private: 8 int size_; 9 T* data_; 10 11 public: 12 13 Vector(int n); 14 15 Vector(int n, T value); 16 17 Vector(const Vector<T>& v) : size_(v.size_), data_(new T[v.size_]) { 18 for (int i = 0; i < size_; ++i) { 19 data_[i] = v.data_[i]; 20 } 21 } 22 23 ~Vector() {} 24 25 int size() const { return size_; } 26 27 T& at(int index) { 28 if (index < 0 || index >= size_) { 29 throw out_of_range("Vector: Index out of range"); 30 } 31 return data_[index]; 32 } 33 34 const T& at(int index) const { 35 if (index < 0 || index >= size_) { 36 throw out_of_range("Vector: Index out of range"); 37 } 38 return data_[index]; 39 } 40 41 T& operator[](int index) { return data_[index]; } 42 const T& operator[](int index) const { return data_[index]; } 43 44 template <typename T1> 45 friend void output(const Vector<T1>& v); 46 }; 47 48 49 template <typename T> 50 void output(const Vector<T>& v) { 51 for (int i = 0; i < v.size_; ++i) { 52 cout << v.data_[i]; 53 if (i != v.size_ - 1) cout << ", "; 54 } 55 cout << endl; 56 } 57 template<typename T> 58 Vector<T>::Vector(int n) : size_(n){ 59 if (n < 0) { 60 throw length_error("Vector constructor: negative size"); 61 } 62 else 63 { 64 data_ = new T[size_]; 65 } 66 } 67 template<typename T> 68 Vector<T>::Vector(int n, T value) : size_(n) { 69 if (n < 0) { 70 throw length_error("Vector constructor: negative size"); 71 } 72 else 73 { 74 data_ = new T[size_]; 75 for (int i = 0; i < size_; i++) 76 { 77 data_[i] = value; 78 79 } 80 } 81 }
task.cpp
1 #include <iostream> 2 #include "Vector.hpp" 3 void test1() { 4 using namespace std; 5 6 int n; 7 cout << "Enter 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 cout << "x1: "; output(x1); 15 16 Vector<int> x2(n, 42); 17 const Vector<int> x3(x2); 18 19 cout << "x2: "; output(x2); 20 cout << "x3: "; output(x3); 21 22 x2.at(0) = 77; 23 x2.at(1) = 777; 24 cout << "x2: "; output(x2); 25 cout << "x3: "; output(x3); 26 } 27 28 void test2() { 29 using namespace std; 30 31 int n, index; 32 while(cout << "Enter n and index: ", cin >> n >> index) { 33 try { 34 Vector<int> v(n, n); 35 v.at(index) = -999; 36 cout << "v: "; output(v); 37 } 38 catch (const exception &e) { 39 cout << e.what() << endl; 40 } 41 } 42 } 43 44 int main() { 45 cout << "测试1: 模板类接口测试\n"; 46 test1(); 47 48 cout << "\n测试2: 模板类异常处理测试\n"; 49 test2(); 50 }
任务5
task.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <vector> 4 #include <string> 5 #include <algorithm> 6 using namespace std; 7 8 struct Student { 9 int id; 10 string name; 11 string major; 12 int score; 13 }; 14 15 16 void readFile(const string& filename, vector<Student>& students) { 17 ifstream infile(filename); 18 19 20 Student temp; 21 while (infile >> temp.id >> temp.name >> temp.major >> temp.score) { 22 students.push_back(temp); 23 } 24 25 infile.close(); 26 } 27 28 29 void writeFile(const string& filename, const vector<Student>& students) { 30 ofstream outfile(filename); 31 32 for (const auto& student : students) { 33 outfile << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << endl; 34 } 35 36 outfile.close(); 37 } 38 39 40 void printStudents(const vector<Student>& students) { 41 for (const auto& student : students) { 42 cout << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << endl; 43 } 44 } 45 46 int main() { 47 vector<Student> students; 48 49 50 readFile("C:\\code_C++\\oop\\project10\\data5.txt", students); 51 52 53 sort(students.begin(), students.end(), [](const Student& a, const Student& b) { 54 if (a.major != b.major) { 55 return a.major < b.major; 56 } 57 return a.score > b.score; 58 }); 59 60 61 printStudents(students); 62 63 64 writeFile("C:\\code_C++\\oop\\project10\\ans5.txt", students); 65 66 67 return 0; 68 }
总结:
通过两个实验,深入理解和掌握 C++ 中模板类的使用与宽字符处理的相关技巧:
- 测试模板类的接口设计、数据操作、异常处理,验证模板类的灵活性和健壮性。
- 基于文件输入输出,实现中文文本的读取与排序,探索字符编码的处理技巧,尤其是在多语言环境中处理中文乱码问题。