实验6 模板类、文件I/O和异常处理
task4
Vector.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <stdexcept> 5 6 using std::cout; 7 template <typename T> 8 9 class Vector { 10 public: 11 Vector(int n) : size(n) { 12 if (size < 0) { 13 throw std::length_error("Vector constructor: negative size"); 14 } 15 16 ptr = new T[size]; 17 } 18 19 Vector(int n, T value) : size(n) { 20 if (n < 0) { 21 throw std::length_error("Vector constructor: negative size"); 22 } 23 24 ptr = new T[size]; 25 for (int i = 0; i < size; ++i) { 26 ptr[i] = value; 27 } 28 } 29 30 Vector(const Vector<T>& vi) : size(vi.size),ptr{new int[size]} { 31 for (int i = 0; i < size; ++i) { 32 ptr[i] = vi.ptr[i]; 33 } 34 } 35 ~Vector() { 36 delete[] ptr; 37 } 38 39 int get_size() const { 40 return size; 41 } 42 43 T& at(int index) const { 44 if (index < 0 || index >= size) { 45 throw std::out_of_range("Vector::at(): index out of this->size"); 46 } 47 return ptr[index]; 48 } 49 50 T& at(int index) { 51 return static_cast<const Vector*>(this)->at(index); 52 } 53 54 T& operator[](int index) const { 55 if (index < 0||index >= size) { 56 throw std::out_of_range("Vector::operator[]():index out of this->size"); 57 } 58 return ptr[index]; 59 } 60 61 T& operator[](int index) { 62 return static_cast<const Vector*>(this)->operator[](index); 63 } 64 65 friend std::ostream& operator<<(std::ostream& out, const Vector<T>& v) { 66 for (int i = 0; i < v.size; ++i) { 67 out << v.ptr[i]; 68 if (i < v.size - 1) { 69 out << ","; 70 } 71 } 72 return out; 73 } 74 75 76 friend void output(const Vector<T>& v) { 77 std::cout << v << std::endl; 78 } 79 80 private: 81 T* ptr; 82 int size; 83 };
task4.cpp
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 cout << "测试1: 模板类接口测试\n"; 47 test1(); 48 49 cout << "\n测试2: 模板类异常处理测试\n"; 50 test2(); 51 }
运行结果:
task5
task5.cpp
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& s1, const Student& s2) { 18 if (s1.major < s2.major) { 19 return true; 20 } else if (s1.major == s2.major) { 21 return s1.score > s2.score; 22 } 23 return false; 24 } 25 26 // 从文件读取学生成绩信息到vector 27 void loadData(const std::string& filename, std::vector<Student>& students) { 28 std::ifstream inFile(filename); 29 if (!inFile) { 30 std::cerr << "无法打开文件: " << filename << std::endl; 31 return; 32 } 33 34 std::string line; 35 // 跳过标题行 36 std::getline(inFile, line); 37 38 while (std::getline(inFile, line)) { 39 std::istringstream iss(line); 40 Student student; 41 iss >> student.id >> student.name >> student.major >> student.score; 42 students.push_back(student); 43 } 44 45 inFile.close(); 46 } 47 48 // 将学生成绩信息保存到文件 49 void saveData(const std::string& filename, const std::vector<Student>& students) { 50 std::ofstream outFile(filename); 51 if (!outFile) { 52 std::cerr << "无法打开文件: " << filename << std::endl; 53 return; 54 } 55 56 // 写入标题行 57 outFile << "学号\t姓名\t专业\t分数\n"; 58 59 for (const auto& student : students) { 60 outFile << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << "\n"; 61 } 62 63 outFile.close(); 64 } 65 66 // 输出学生成绩信息到屏幕 67 void printData(const std::vector<Student>& students) { 68 // 输出标题行 69 std::cout << "学号\t姓名\t专业\t分数\n"; 70 71 for (const auto& student : students) { 72 std::cout << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << "\n"; 73 } 74 } 75 76 int main() { 77 std::vector<Student> students; 78 loadData("data5.txt", students); 79 80 std::sort(students.begin(), students.end(), compareStudents); 81 82 printData(students); 83 84 saveData("ans5.txt", students); 85 86 return 0; 87 }
运行结果: