实验6 模板类、文件I/O和异常处理
实验任务4
1 #ifndef VECTOR_HPP 2 #define VECTOR_HPP 3 4 #include <iostream> 5 #include <stdexcept> 6 7 template<typename T> 8 class Vector { 9 private: 10 T* data; 11 size_t size; 12 13 public: 14 // 构造函数,动态指定大小 15 Vector(size_t n) { 16 if (n < 0) { 17 std::cerr << "Vector constructor: negative size" << std::endl; 18 } 19 size = n; 20 data = new T[size]; 21 } 22 23 // 构造函数,动态指定大小并初始化每个数据项 24 Vector(size_t n, const T& value) { 25 if (n < 0) { 26 std::cerr << "Vector constructor: negative size" << std::endl; 27 } 28 size = n; 29 data = new T[size]; 30 for (size_t i = 0; i < size; ++i) { 31 data[i] = value; 32 } 33 } 34 35 // 深拷贝构造函数 36 Vector(const Vector& other) { 37 size = other.size; 38 data = new T[size]; 39 for (size_t i = 0; i < size; ++i) { 40 data[i] = other.data[i]; 41 } 42 } 43 44 // 析构函数 45 ~Vector() { 46 delete[] data; 47 } 48 49 // 返回动态数组对象中数据项个数 50 size_t get_size() const { 51 return size; 52 } 53 54 // at() 方法,支持下标访问并处理越界异常 55 T& at(size_t i) { 56 if (i >= size) { 57 throw std::out_of_range("Index out of range"); 58 } 59 return data[i]; 60 } 61 62 const T& at(size_t i) const { 63 if (i >= size) { 64 throw std::out_of_range("Index out of range"); 65 } 66 return data[i]; 67 } 68 69 // 重载 [] 运算符 70 T& operator[](size_t i) { 71 return at(i); 72 } 73 74 const T& operator[](size_t i) const { 75 return at(i); 76 } 77 78 // 友元函数,用于输出 Vector 对象中的数据项 79 friend void output(const Vector& vec) { 80 for (size_t i = 0; i < vec.size; ++i) { 81 std::cout << vec.data[i] << " "; 82 } 83 std::cout << std::endl; 84 } 85 }; 86 87 #endif // VECTOR_HPP
1 #include <iostream> 2 #include "Vector.hpp" 3 4 void test1() { 5 using namespace std; 6 7 int n; 8 cout << "Enter n: "; 9 cin >> n; 10 11 Vector<double> x1(n); 12 for(auto i = 0; i < n; ++i) 13 x1.at(i) = i * 0.7; 14 15 cout << "x1: "; output(x1); 16 17 Vector<int> x2(n, 42); 18 const Vector<int> x3(x2); 19 20 cout << "x2: "; output(x2); 21 cout << "x3: "; output(x3); 22 23 x2.at(0) = 77; 24 x2.at(1) = 777; 25 cout << "x2: "; output(x2); 26 cout << "x3: "; output(x3); 27 } 28 29 void test2() { 30 using namespace std; 31 32 int n, index; 33 while(cout << "Enter n and index: ", cin >> n >> index) { 34 try { 35 Vector<int> v(n, n); 36 v.at(index) = -999; 37 cout << "v: "; output(v); 38 } 39 catch (const exception &e) { 40 cout << e.what() << endl; 41 } 42 } 43 } 44 45 int main() { 46 using namespace std; 47 cout << "测试1: 模板类接口测试\n"; 48 test1(); 49 50 cout << "\n测试2: 模板类异常处理测试\n"; 51 test2(); 52 }
运行测试截图
实验任务5
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <vector> 5 #include <algorithm> 6 #include <string> 7 8 // 定义学生结构体 9 struct Student { 10 std::string id; 11 std::string name; 12 std::string major; 13 int score; 14 }; 15 16 // 比较函数,用于按专业字典序和分数降序排序 17 bool compareStudents(const Student& a, const Student& b) { 18 if (a.major != b.major) { 19 return a.major < b.major; // 先按专业字典序排序 20 } else { 21 return a.score > b.score; // 专业相同时,按分数降序排序 22 } 23 } 24 25 // 从文件中读取学生数据 26 std::vector<Student> readStudentsFromFile(const std::string& filename) { 27 std::vector<Student> students; 28 std::ifstream file(filename); 29 std::string line; 30 31 // 跳过标题行 32 if (std::getline(file, line)) { 33 while (std::getline(file, line)) { 34 std::istringstream iss(line); 35 Student student; 36 std::getline(iss, student.id, '\t'); 37 std::getline(iss, student.name, '\t'); 38 std::getline(iss, student.major, '\t'); 39 std::getline(iss, line, '\t'); // 读取分数前的制表符和分数后的换行符或空格等 40 std::stringstream ss(line); 41 ss >> student.score; 42 students.push_back(student); 43 } 44 } 45 46 file.close(); 47 return students; 48 } 49 50 // 将学生数据写入文件 51 void writeStudentsToFile(const std::vector<Student>& students, const std::string& filename) { 52 std::ofstream file(filename); 53 file << "学号\t姓名\t专业\t分数" << std::endl; 54 for (const auto& student : students) { 55 file << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << std::endl; 56 } 57 file.close(); 58 } 59 60 // 打印学生数据到屏幕 61 void printStudentsToScreen(const std::vector<Student>& students) { 62 for (const auto& student : students) { 63 std::cout << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << std::endl; 64 } 65 } 66 67 int main() { 68 std::vector<Student> students = readStudentsFromFile("data5.txt"); 69 70 // 对学生数据进行排序 71 std::sort(students.begin(), students.end(), compareStudents); 72 73 // 打印排序后的学生数据到屏幕 74 printStudentsToScreen(students); 75 76 // 将排序后的学生数据写入文件 77 writeStudentsToFile(students, "ans5.txt"); 78 79 return 0; 80 }
运行测试截图