实验6 模板类、文件I/O及异常处理
实验任务4
Vector.hpp
1 #include<iostream> 2 #include<stdexcept> 3 4 using namespace std; 5 6 template<typename T> 7 class Vector{ 8 private: 9 int size; 10 T *ptr; 11 public: 12 Vector(int s):size(s){ 13 if(s<0) 14 throw length_error("Vector constructor:negative size"); 15 else 16 ptr=new T[size]; 17 } 18 Vector(int s,T value):size(s){ 19 if(s<0) 20 throw length_error("Vector constructor:negative size"); 21 else 22 { 23 ptr=new T[size]; 24 for(int i=0;i<s;i++) 25 *(ptr+i)=value; 26 } 27 } 28 Vector(const Vector<T> &v){ 29 size=v.size; 30 ptr=new T[size]; 31 for(int i=0;i<size;i++) 32 *(ptr+i)=v.ptr[i]; 33 } 34 ~Vector(){delete []ptr;} 35 int get_size(); 36 T& at(int index); 37 T& operator[](int index); 38 friend void output(Vector<T> v){ 39 for(int i=0;i<v.size;i++) 40 cout<<v.ptr[i]<<","; 41 cout<<endl; 42 } 43 }; 44 45 template<typename T> 46 int Vector<T>::get_size(){return size;} 47 48 template<typename T> 49 T& Vector<T>::at(int index){ 50 if (index<0||index>=size) 51 throw out_of_range("Vector:index out of range"); 52 return *(ptr+index); 53 } 54 55 template<typename T> 56 T& Vector<T>::operator[](int index){ 57 if (index<0||index>=size) 58 throw out_of_range("Vector:index out of range"); 59 return *(ptr+index); 60 }
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 16 17 cout << "x1: "; output(x1); 18 19 Vector<int> x2(n, 42); 20 const Vector<int> x3(x2); 21 22 cout << "x2: "; output(x2); 23 cout << "x3: "; output(x3); 24 25 x2.at(0) = 77; 26 x2.at(1) = 777; 27 cout << "x2: "; output(x2); 28 cout << "x3: "; output(x3); 29 } 30 31 void test2() { 32 using namespace std; 33 34 int n, index; 35 while(cout << "Enter n and index: ", cin >> n >> index) { 36 try { 37 Vector<int> v(n, n); 38 v.at(index) = -999; 39 cout << "v: "; output(v); 40 } 41 catch (const exception &e) { 42 cout << e.what() << endl; 43 } 44 } 45 } 46 47 int main() { 48 cout << "测试1: 模板类接口测试\n"; 49 test1(); 50 51 cout << "\n测试2: 模板类异常处理测试\n"; 52 test2(); 53 }
实验任务5
task5.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <vector> 5 #include <tuple> 6 #include <algorithm> 7 #include <string> 8 9 using namespace std; 10 struct Student { 11 int id; 12 string name; 13 string major; 14 int score; 15 }; 16 17 bool operator<(const Student& a, const Student& b) { 18 if (a.major == b.major) { 19 return a.score > b.score; 20 } 21 return a.major < b.major; 22 } 23 24 vector<Student> readStudentsFromFile(const std::string& filename) { 25 vector<Student> students; 26 ifstream file(filename); 27 string line; 28 getline(file, line); 29 while (getline(file, line)) { 30 istringstream iss(line); 31 Student student; 32 iss >> student.id >> student.name >> student.major >> student.score; 33 students.push_back(student); 34 } 35 file.close(); 36 return students; 37 } 38 39 void writeStudentsToFile(const vector<Student>& students, const string& filename) { 40 ofstream file(filename); 41 for (const auto& student : students) { 42 file << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << std::endl; 43 } 44 file.close(); 45 } 46 47 int main() { 48 vector<Student> students = readStudentsFromFile("data5.txt"); 49 sort(students.begin(), students.end()); 50 cout << "学号\t姓名\t专业\t分数" << std::endl; 51 for (const auto& student : students) { 52 cout << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << endl; 53 } 54 writeStudentsToFile(students, "ans5.txt"); 55 return 0; 56 }