实验6 模板类、文件I/O和异常处理
实验任务4:
实验代码:
1 #pragma once 2 #include<iostream> 3 #include<stdexcept> 4 using namespace std; 5 template<typename T> 6 class Vector{ 7 public: 8 Vector(int n,const T& value=T()):size(n){ 9 if(n<0) 10 { 11 throw length_error("Vector constructor:negative size!"); 12 } 13 else 14 { 15 x=new T[n]; 16 for(int i=0;i<n;i++) 17 { 18 x[i]=value; 19 } 20 } 21 } 22 Vector(const Vector<T>& other):size(other.size){ 23 x=new T[size]; 24 for(int i=0;i<size;i++) 25 { 26 x[i]=other.x[i]; 27 } 28 } 29 ~Vector(){ 30 delete[] x; 31 } 32 33 34 int get_size() const{ 35 return size; 36 } 37 38 39 T& at(int index){ 40 if(index>=size) 41 { 42 throw out_of_range("Vector:index out of range!"); 43 } 44 else 45 return x[index]; 46 } 47 const T& at(int index) const 48 { 49 if(index>=size) 50 { 51 throw out_of_range("Vector:index out of range!"); 52 } 53 else 54 return x[index]; 55 } 56 57 58 T& operator[](int i){ 59 return at(i); 60 } 61 const T& operator[](int i)const{ 62 return at(i); 63 } 64 65 66 friend void output(const Vector<T>& a) 67 { 68 int i; 69 for( i=0;i<a.size-1;i++) 70 { 71 cout<<a[i]<<", "; 72 } 73 cout<<a[i]; 74 cout<<endl; 75 } 76 private: 77 int size; 78 T* x; 79 };
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& s1, const Student& s2) { 18 if (s1.major < s2.major) { 19 return true; 20 } 21 else if (s1.major == s2.major) { 22 return s1.score > s2.score; 23 } 24 return false; 25 } 26 27 int main() { 28 std::vector<Student> students; 29 std::ifstream in("data5.txt"); 30 31 32 std::string line; 33 34 std::getline(in, line); 35 36 while (std::getline(in, line)) { 37 std::istringstream iss(line); 38 Student student; 39 iss >> student.id >> student.name >> student.major >> student.score; 40 students.push_back(student); 41 } 42 43 in.close(); 44 45 std::sort(students.begin(), students.end(), compareStudents); 46 47 std::ofstream out("ans5.txt"); 48 49 for (const auto& student : students) { 50 std::cout << student.id << " " << student.name << " " << student.major << " " << student.score << std::endl; 51 out << student.id << " " << student.name << " " << student.major << " " << student.score << std::endl; 52 } 53 54 out.close(); 55 56 return 0; 57 }
运行结果截图: