实验6 模板类、文件I/O和异常处理
task4:
Vector.hpp
#pragma once #include<iostream> #include<stdexcept> using namespace std; template <typename T> class Vector { public: Vector(int size, int value = 0) :size{ size } { if (size < 0)throw length_error("negative size"); else { ptr = new T[size]; for (int i = 0; i < size; i++) { ptr[i] = value; } } } int get_size()const { return size; } T& at(int val) { if (val >= size)throw out_of_range("index out of range"); else return ptr[val]; } T& operator[](int val) { if (val<0 || val>=size)throw out_of_range("index out of range"); else return ptr[val]; } friend void output(const Vector<T> &x) { for (int i = 0; i < x.get_size(); i++) { cout << x.ptr[i] << ", "; } cout << endl; } Vector(const Vector<T>& x) :size(x.size), ptr(new T[size]) { for (int i = 0; i < size; i++) { ptr[i] = x.ptr[i]; } } private: int size; T* ptr; };
task4.cpp
#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(); }

task5:
#include<iostream> #include<iomanip> #include<vector> #include<algorithm> #include<fstream> #include<string> using namespace std; class st { private: string num; string name; string mj; int grade; public: st() = default; ~st() = default; int get_grade()const { return grade; } string get_mj()const { return mj; } friend ostream& operator<<(ostream& out, const st& s) { out << setiosflags(ios_base::left); out << setw(15) << s.num << setw(15) << s.name << setw(15) << s.mj << setw(15) << s.grade; return out; } friend istream& operator>>(istream& in, st& s) { in >> s.num >> s.name >> s.mj >> s.grade; return in; } }; void output(ostream& out, const vector<st>& s) { for (auto& i : s) out << i << endl; } void load(const string& filename, vector<st>& s) { ifstream in(filename); if (!in.is_open()) { cout << "fail to open file to read\n"; return; } string title_line; getline(in, title_line); st t; while (in >> t) s.push_back(t); in.close(); } void save(const string& filename, vector<st>& s) { ofstream out(filename); if (!out.is_open()) { cout << "fail to open file to write\n"; return; } output(out, s); out.close(); } bool compare(st& s1, st& s2) { if (s1.get_mj() <s2.get_mj()) return true; if (s1.get_mj() == s2.get_mj())return s1.get_grade() > s2.get_grade(); return false; } int main() { vector<st>s; load("data5.txt", s); sort(s.begin(), s.end(), compare); output(cout, s); save("ans1.txt", s); }


浙公网安备 33010602011771号