实验六
任务4:
代码:
#pragma once #include<iostream> #include<stdexcept> using namespace std; template<typename T> class Vector{ public: Vector(int n,const T& value=T()):size(n){ if(n<0) { throw length_error("大小是负数!"); } else { x=new T[n]; for(int i=0;i<n;i++) { x[i]=value; } } } Vector(const Vector<T>& other):size(other.size){ x=new T[size]; for(int i=0;i<size;i++) { x[i]=other.x[i]; } } ~Vector(){ delete[] x; } int get_size() const{ return size; } T& at(int index){ if(index>=size) { throw out_of_range("下标访问越界!"); } else return x[index]; } const T& at(int index) const { if(index>=size) { throw out_of_range("下标访问越界!"); } else return x[index]; } T& operator[](int i){ return at(i); } const T& operator[](int i)const{ return at(i); } friend void output(const Vector<T>& a) { for(int i=0;i<a.size;i++) { cout<<a[i]<<" "; } cout<<endl; } private: int size; T* x; };
#include <iostream> #include "Vector.hpp" void test1() { using namespace std; int n; cout << "Enter n: "; cin >> n; Vector<double> x1(n); for(auto i = 0; i < n; ++i) x1.at(i) = i * 0.7; cout << "x1: "; output(x1); Vector<int> x2(n, 42); const Vector<int> x3(x2); cout << "x2: "; output(x2); cout << "x3: "; output(x3); x2.at(0) = 77; x2.at(1) = 777; cout << "x2: "; output(x2); cout << "x3: "; output(x3); } void test2() { using namespace std; int n, index; while(cout << "Enter n and index: ", cin >> n >> index) { try { Vector<int> v(n, n); v.at(index) = -999; cout << "v: "; output(v); } catch (const exception &e) { cout << e.what() << endl; } } } int main() { cout << "测试1: 模板类接口测试\n"; test1(); cout << "\n测试2: 模板类异常处理测试\n"; test2(); }
运行截图:
任务5:
代码:
#include<iostream> #include<fstream> #include<string> #include<vector> #include<algorithm> #include<iomanip> using namespace std; class information{ private: int id; string name; string major;//专业 int score; public: information()=default; ~information()=default; friend ostream& operator<<(ostream& out,const information& a) { out<<setiosflags(ios_base::left); out<<setw(10)<<a.id <<setw(10)<<a.name <<setw(10)<<a.major <<setw(10)<<a.score; return out; } friend istream& operator>>(istream& in,information& a) { in>>a.id >>a.name>>a.major>>a.score; return in; } string get_major()const {return major;} int get_score() const{return score;} }; bool compare(const information& a,const information& b) { if(a.get_major()<b.get_major()) return true; if(a.get_major()==b.get_major()){ return a.get_score()>b.get_score(); } return false; } void output(ostream& out,const vector<information>& v) { for(const auto &i:v) { out<<i<<endl; } } void save(const string &filename,vector<information>& v) { ofstream outfile(filename); if(!outfile.is_open()) { cout<<"文件打开失败"; return; } output(outfile,v); outfile.close(); } void load(const string& filename,vector<information>& v) { ifstream infile(filename); if(!infile.is_open()) { cout<<"文件打开失败"; return; } string line; getline(infile,line);//跳过标题行 information a; while(infile>>a) v.push_back(a); infile.close(); } int main() { vector<information> v; load("data.txt",v); sort(v.begin(),v.end(),compare); output(cout,v); save("data1.txt",v); }
运行截图: