task4
源码:
1 #include<iostream> 2 #include <stdexcept> 3 #include <algorithm> 4 5 template<typename T> 6 class Vector{ 7 private: 8 T* data; 9 size_t size; //无符号整数类型 10 11 public: 12 Vector(int n=0, T value = T());//后面调用默认构造函数 13 //拷贝构造函数 14 Vector(const Vector<T>& other); 15 ~Vector(); 16 17 //获取数据项个数 18 size_t get_size() const; 19 20 //at() 21 T& at(int index); 22 const T& at(int index) const; 23 24 //重载[]操作符 25 T& operator[](int index); 26 const T& operator[](int index) const; 27 28 template <typename T1> 29 friend void output(const Vector<T1>& vec); 30 }; 31 32 33 template <typename T> 34 Vector<T>::Vector(int n, T value) { 35 if(n < 0) { 36 throw std::length_error("Vector constructor: negative size"); 37 } 38 size = n; 39 data = new T[size]; 40 //将动态数组 data 中的每个元素都初始化为 value 的值 41 std::fill(data, data+size, value); 42 } 43 44 template <typename T> 45 Vector<T>::Vector(const Vector<T>& other){ 46 size = other.size; 47 data = new T[size]; 48 std::copy(other.data, other.data+size, data);//深拷贝 49 } 50 51 template <typename T> 52 Vector<T>::~Vector(){ 53 delete[] data; 54 } 55 56 template <typename T> 57 size_t Vector<T>::get_size() const{ 58 return size; 59 } 60 61 62 //at() 63 template <typename T> 64 T& Vector<T>::at(int index){ 65 if(index < 0 || index >= size) { 66 throw std::out_of_range("Vector: Index is out of range"); 67 } 68 return data[index]; 69 } 70 71 template <typename T> 72 const T& Vector<T>::at(int index) const { 73 if (index < 0 || index >= size) { 74 throw std::out_of_range("Index is out of range"); 75 } 76 return data[index]; 77 } 78 79 80 // 重载 [] 操作符 81 template <typename T> 82 T& Vector<T>::operator[](int index) { 83 if (index < 0 || index >= size) { 84 throw std::out_of_range("Index is out of range"); 85 } 86 return data[index]; 87 } 88 89 // const 重载 [] 操作符 90 template <typename T> 91 const T& Vector<T>::operator[](int index) const { 92 if (index < 0 || index >= size) { 93 throw std::out_of_range("Index is out of range"); 94 } 95 return data[index]; 96 } 97 98 template <typename T> 99 void output(const Vector<T>& vec) { 100 for (size_t i = 0; i < vec.size; ++i) { 101 std::cout << vec.data[i] << " "; 102 } 103 std::cout << std::endl; 104 }Vector.hpp
1 #include <iostream> 2 #include "Vector.hpp" 3 4 using namespace std; 5 6 void test1() { 7 8 int n; 9 cout << "Enter n: "; 10 cin >> n; 11 12 Vector<double> x1(n); 13 for(auto i = 0; i < n; ++i) 14 x1.at(i) = i * 0.7; 15 16 cout << "x1: "; output(x1); 17 18 Vector<int> x2(n, 42); 19 const Vector<int> x3(x2); 20 21 cout << "x2: "; output(x2); 22 cout << "x3: "; output(x3); 23 24 x2.at(0) = 77; 25 x2.at(1) = 777; 26 cout << "x2: "; output(x2); 27 cout << "x3: "; output(x3); 28 } 29 30 void test2() { 31 using namespace std; 32 33 int n, index; 34 while(cout << "Enter n and index: ", cin >> n >> index) { 35 try { 36 Vector<int> v(n, n); 37 v.at(index) = -999; 38 cout << "v: "; output(v); 39 } 40 catch (const exception &e) { 41 cout << e.what() << endl; 42 } 43 } 44 } 45 46 int main() { 47 std::cout << "测试1:模板类接口测试\n"; 48 test1(); 49 50 std::cout << "\n测试2:模板类异常处理测试\n"; 51 test2(); 52 }task4.cpp
运行结果:
task5
源码:
1 #include <iostream> 2 #include <fstream> 3 #include <vector> 4 #include <string> 5 #include <algorithm> 6 7 using namespace std; 8 9 class Student { 10 public: 11 string student_id; 12 string name; 13 string major; 14 int score; 15 16 Student(string id, string n, string m, int s): 17 student_id(id), name(n), major(m), score(s) {} 18 19 void print() const { 20 cout << student_id << "\t" << name << "\t" << major << "\t" << score << endl; 21 } 22 }; 23 24 25 class StudentManager { 26 private: 27 vector<Student> students; 28 29 public: 30 void readData(const string& filename) { 31 ifstream infile(filename); 32 if (!infile.is_open()) { 33 cerr << "can't open the file " << filename << endl; 34 return; 35 } 36 37 //跳过标题行 38 string line; 39 getline(infile, line); 40 41 //读取数据(通过'\t'实现) 42 while (getline(infile, line)) { 43 size_t pos = 0; 44 string student_id, name, major; 45 int score; 46 47 //获取学号 48 pos = line.find('\t'); 49 student_id = line.substr(0, pos); 50 line.erase(0, pos + 1); 51 52 //获取姓名 53 pos = line.find('\t'); 54 name = line.substr(0, pos); 55 line.erase(0, pos + 1); 56 57 //获取专业 58 pos = line.find('\t'); 59 major = line.substr(0, pos); 60 line.erase(0, pos + 1); 61 62 //获取分数 63 score = stoi(line); 64 65 //创建学生对象并添加到vector 66 students.push_back(Student(student_id, name, major, score)); 67 } 68 infile.close(); 69 } 70 71 void sortStudents() { 72 sort(students.begin(), students.end(), 73 [](const Student& a, const Student& b) { 74 if(a.major != b.major) { 75 return a.major < b.major; 76 } 77 return a.score > b.score; 78 }); 79 } 80 81 void printToScreen() { 82 cout << "学号\t姓名\t专业\t分数" << endl; 83 for(const auto& student : students) { 84 student.print(); 85 } 86 } 87 88 void writeToFile(const string& filename) { 89 ofstream outfile(filename); 90 if(!outfile.is_open()) { 91 cerr << "can't open the file " << filename << endl; 92 return; 93 } 94 95 outfile << "学号\t姓名\t专业\t分数" << endl; 96 for(const auto& student : students) { 97 outfile << student.student_id << "\t" 98 << student.name << "\t" 99 << student.major << "\t" 100 << student.score << endl; 101 } 102 outfile.close(); 103 } 104 };students.hpp
1 #include"students.hpp" 2 3 int main() { 4 StudentManager manager; 5 6 //读取数据 7 manager.readData("data5.txt"); 8 9 //排序 10 manager.sortStudents(); 11 12 //输出到屏幕 13 manager.printToScreen(); 14 15 //写入文件 16 manager.writeToFile("ans5.txt"); 17 18 return 0; 19 }task5.cpp
运行结果:
标签:文件,const,index,int,Vector,实验,data,模板,size From: https://www.cnblogs.com/yrx0415/p/18610262