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

实验内容4

Vector.hpp

复制代码
 1 #pragma once
 2 #include<iostream>
 3 #include<stdexcept>
 4 
 5 using namespace std;
 6 
 7 template<typename T>
 8 
 9 class Vector {
10     private:
11         int size;
12         T* ptr;
13         
14     public:
15         Vector(int size, int value = 0) :size { size } {
16             if (size < 0) {
17                 throw length_error("Vector constructor: negative size");
18             }
19             ptr = new T[size];
20             for (int i = 0; i < size; i++) {
21                 ptr[i] = value;
22             }
23         }
24         int get_size() const {
25             return size;
26         }
27         T& at(int index) const {
28             if (index < 0 || index >= size) throw out_of_range("Vector: index out of range");
29             return ptr[index];
30         }
31         T& at(int index) {
32             return static_cast<const Vector<T>*>(this)->at(index);
33         }
34         T& operator[](int index)const {
35             if (index < 0 || index >= size) throw out_of_range("Vector: index out of range");
36             return ptr[index];
37         }
38         T& operator[](int index) {
39             return static_cast<const Vector<T>*>(this)->operator[](index);
40         }
41 };
42 
43 template<typename T1>
44 void output(const Vector<T1>& v) {
45     for (int i = 0; i < v.get_size(); i++) {
46         cout << v[i] << ", ";
47     }
48     cout << endl;
49 }
复制代码

 

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 }
复制代码

 

运行结果截图

 

实验内容5

task5.cpp

复制代码
 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 #include<vector>
 5 #include<iomanip>
 6 #include<algorithm>
 7 
 8 using namespace std;
 9 
10 class student{
11 public:
12     student() = default;
13     ~student() = default;
14 
15     string get_major()const {
16         return major;
17     }
18 
19     int get_score()const {
20         return score;
21     }
22 
23     friend ostream& operator<<(ostream& out, const student& s) {
24         out << setiosflags(ios_base::left);
25         out << setw(10) << s.number
26             << setw(10) << s.name
27             << setw(10) << s.major
28             << setw(10) << s.score;
29 
30         return out;
31     }
32 
33     friend istream& operator>>(istream& in,  student& s) {
34         in >>s.number >> s.name >> s.major >> s.score;
35 
36         return in;
37     }
38 
39 private:
40     string number;
41     string name;
42     string major;
43     int score;
44 
45 };
46 
47 bool compare(const student& s1, const student& s2) {
48     if (s1.get_major() < s2.get_major())
49         return true;
50 
51     if (s1.get_major() == s2.get_major())
52         return s1.get_score() > s2.get_score();
53 
54     return false;
55 }
56 
57 void output(ostream& out, const vector<student>& v) {
58     for (auto& i : v)
59         out << i << endl;
60 }
61 
62 void save(const string& filename, vector<student>& v) {
63     ofstream out(filename);
64     if (!out.is_open()) {
65         cout << "写入文件失败\n";
66         return;
67     }
68 
69     output(out, v);
70     out.close();
71 }
72 
73 void load(const string& filename, vector<student>& v) {
74     ifstream in(filename);
75     if (!in.is_open()) {
76         cout << "读取文件失败\n";
77         return;
78     }
79 
80     string title_line;
81     getline(in, title_line);
82 
83     student t;
84     while (in >> t)
85         v.push_back(t);
86 
87     in.close();
88 }
89 
90 int main() {
91     vector<student>v;
92 
93     load("data5.txt", v);
94     sort(v.begin(), v.end(), compare);
95     output(cout, v);
96     save("ans5.txt", v);
97 }
复制代码

 

运行结果显示:

 

posted @   徐睿泽  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示