实验任务4
#include<iostream> #include<stdexcept> template<typename T> class Vector{ private: T* data; int num; public: Vector(int size); Vector(int size,const T& value); Vector(const Vector<T>& other); ~Vector(); int get_size()const; T& at(int index); const T& at(int index)const; T& operator[](int index); const T& operator[](int index)const; }; template<typename T> Vector<T>::Vector(int size){ if(size<0){ throw std::length_error("Vectro constructor:negative size"); } num=size; data=new T[num]; } template<typename T> Vector<T>::Vector(int size,const T&value){ if(size<0){ throw std::length_error("Vector constructor:negative size"); } num=size; data=new T[num]; for(int i=0;i<num;++i){ data[i]=value; } } template<typename T> Vector<T>::Vector(const Vector<T>& other){ num=other.num; data=new T[num]; for(int i=0;i<num;++i){ data[i]=other.data[i]; } } template<typename T> Vector<T>::~Vector(){ delete[] data; } template<typename T> int Vector<T>::get_size()const{ return num; } template<typename T> T& Vector<T>::at(int index){ if(index>=num){ throw std::out_of_range("Vector:index out of range"); } return data[index]; } template<typename T> const T&Vector<T>::at(int index)const{ if(index>=num){ throw std::out_of_range("Vector:Index out of range"); } return data[index]; } template<typename T> T& Vector<T>::operator[](int index){ if(index>=num){ throw std::out_of_range("Vector:Index out of range"); } return data[index]; } template<typename T> const T& Vector<T>::operator[](int index)const{ if(index>=num){ throw std::out_of_range("Vector:Index out of range"); } return data[index]; } template<typename T> std::ostream& operator<<(std::ostream& os,const Vector<T>& v){ for(int i=0;i<v.get_size();++i){ os<<v[i]; if(i<v.get_size()-1){ os<<", "; } } return os; } template<typename T> void output(const Vector<T>& v){ std::cout<<v<<std::endl; }
实验任务5
#include <iostream> #include <fstream> #include <sstream> #include <vector> #include <algorithm> #include <string> #include <tuple> struct Student { int id; std::string name; std::string major; int score; }; bool compareStudents(const Student& a, const Student& b) { if (a.major < b.major) return true; else if (a.major == b.major && a.score > b.score) return true; return false; } int main() { std::vector<Student> students; std::ifstream inputFile("D:\\c++\\printf()的用法\\printf()的用法\\data5.txt"); if (!inputFile) { std::cerr << "无法打开输入文件" << std::endl; return -1; } std::string line; std::getline(inputFile, line); while (std::getline(inputFile, line)) { std::istringstream iss(line); Student student; iss >> student.id >> student.name >> student.major >> student.score; students.push_back(student); } inputFile.close(); std::sort(students.begin(), students.end(), compareStudents); std::ofstream outputFile("D:\\c++\\printf()的用法\\printf()的用法\\ans5.txt"); if (!outputFile) { std::cerr << "无法打开输出文件" << std::endl; return -1; } outputFile << "学号\t姓名\t专业\t分数\n"; for (const auto& student : students) { std::cout << student.id << '\t' << student.name << '\t' << student.major << '\t' << student.score << '\n'; outputFile << student.id << '\t' << student.name << '\t' << student.major << '\t' << student.score << '\n'; } outputFile.close(); return 0; }