实验6 模板类、文件I/O和异常处理

任务4

Vector.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <stdexcept>
 5 
 6 using namespace std;
 7 
 8 template<typename T>
 9 class Vector {
10     public:
11         Vector(int n,T value=0);
12         ~Vector();
13         Vector(const Vector<T> &v);
14         int get_size() const;
15         T& at(int i) ;
16         T& operator[](int i);
17         
18         template<typename T1>
19         friend void output(Vector<T1> &v);
20         
21         template<typename T2>
22         friend void output(const Vector<T2> &v) ;
23     private:
24         T *arr;
25         int length;
26 }; 
27 
28 template<typename T>
29 Vector<T>::Vector(int n,T value): length{n} {
30     if(n<0)
31         throw length_error("长度不能为负!");
32     arr = new T[n];
33     for(int i=0;i<n;i++)
34     {
35         arr[i]=value;
36     }
37 }
38 
39 template<typename T>
40 Vector<T>::~Vector(){
41     delete[] arr;
42 }
43 
44 template<typename T>
45 Vector<T>::Vector(const Vector<T> &v){
46     length=v.length;
47     arr = new T[length];
48     for(int i=0;i<length;i++)
49     {
50         arr[i]=v.arr[i];
51     }
52 }
53 
54 template<typename T>
55 int Vector<T>::get_size() const{
56     return length;
57 }
58 
59 template<typename T>
60 T& Vector<T>::at(int i) {
61     if(i>=length)
62         throw overflow_error("数组越界!");
63     return arr[i];
64 }
65 
66 template<typename T>
67 T& Vector<T>::operator[](int i){
68     return arr[i];
69 }
70 
71 template<typename T1>
72 void output( Vector<T1> &v){
73     for(int i=0;i<v.length;i++)
74     {
75         cout<<v.arr[i]<<" ";
76     }
77     cout<<endl;
78 }
79 
80 template<typename T2>
81 void output(const Vector<T2> &v) {
82     for(int i=0;i<v.length;i++)
83     {
84         cout<<v.arr[i]<<" ";
85     }
86     cout<<endl;
87 }
View Code

task4.cpp

 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     cout << "测试1: 模板类接口测试\n";
47     test1();
48 
49     cout << "\n测试2: 模板类异常处理测试\n";
50     test2();
51 }
View Code

运行结果

 

 

任务5

task5.cpp

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <iomanip>
  4 #include <string>
  5 #include <vector>
  6 #include <fstream>
  7 
  8 using std::string;
  9 using std::vector;
 10 using std::ostream;
 11 using std::istream;
 12 using std::setw;
 13 using std::setprecision;
 14 using std::setiosflags;
 15 using std::ios_base;
 16 
 17 class Score {
 18 public:
 19     Score() = default;
 20     ~Score() = default;
 21 
 22     int get_score() const { return score; }
 23     string get_major() const { return major; }
 24 
 25     friend ostream& operator<<(ostream &out, const Score &c);
 26     friend istream& operator>>(istream &in, Score &c);
 27 
 28 private:
 29     string no;          // 学号
 30     string name;        // 姓名
 31     string major;       // 专业
 32     int score;            // 分数 
 33 };
 34 
 35 ostream& operator<<(ostream &out, const Score &c) {
 36     out << setiosflags(ios_base::left);
 37     out << setw(15) << c.no
 38         << setw(15) << c.name
 39         << setw(15) << c.major
 40         << setw(5) << c.score;
 41     
 42     return out;
 43 }
 44 
 45 istream& operator>>(istream &in, Score &c) {
 46     in >> c.no >> c.name >> c.major >> c.score;
 47 
 48     return in;
 49 }
 50 
 51 bool compare_by_score(const Score &c1, const Score &c2) {
 52     if(c1.get_major()<c2.get_major())
 53         return true;
 54     
 55     if(c1.get_major()==c2.get_major())
 56         return c1.get_score() > c2.get_score();
 57     
 58     return false;
 59 }
 60 
 61 // 把对象中的元素插入到输出流out
 62 void output(ostream &out, const vector<Score> &v) {
 63     for(auto &i: v)
 64         out << i << std::endl;
 65 }
 66 
 67 
 68 // 把对象中的元素写到filename文件中
 69 void save(const string &filename, vector<Score> &v) {
 70     using std::ofstream;
 71 
 72     ofstream out(filename);
 73     if(!out.is_open()) {
 74         std::cout << "fail to open file to write\n";
 75         return;
 76     }
 77 
 78     output(out, v);
 79     out.close();
 80 }
 81 
 82 // 从文件filename读取对象
 83 void load(const string &filename, vector<Score> &v) {
 84     using std::ifstream;
 85 
 86     ifstream in(filename);
 87     if(!in.is_open()) {
 88         std::cout << "fail to open file to read\n";
 89         return;
 90     }
 91 
 92     std::string title_line;
 93     getline(in, title_line);     // 跳过标题行
 94 
 95     Score t;
 96     while(in >> t) 
 97         v.push_back(t);
 98 
 99     in.close();
100 }
101 
102 void test() {
103 
104     vector<Score> v;
105 
106     load("data5.txt", v);   
107     sort(v.begin(), v.end(),compare_by_score); 
108     output(std::cout, v);    // 输出对象v中信息到屏幕
109     save("ans5.txt", v); 
110 }
111 
112 int main() {
113     test();
114 }
View Code

运行结果

 

 

posted @ 2024-12-17 17:09  shorekeeper  阅读(4)  评论(0编辑  收藏  举报