实验6

任务4:

 1 #pragma once
 2 #include<iostream>
 3 #include<stdexcept>
 4 
 5 using namespace std;
 6 
 7 template<typename T>
 8 class Vector {
 9     private:
10         int size;
11         T* ptr;
12     public:
13         Vector(int n) {
14             if (n < 0) {
15                 throw length_error("Vector constructor:negative size");
16             } else {
17                 ptr = new T[size];
18             }
19         }
20 
21         Vector(int n, T value) {
22             if (n < 0) {
23                 throw length_error("Vector constructor:negative size");
24             } else {
25                 ptr = new T[size];
26                 for (int i = 0; i < size; i++) {
27                     ptr[i] = value;
28 
29                 }
30             }
31         }
32 
33 
34 
35         ~Vector(){delete[] ptr;}
36         
37         
38         Vector(const Vector<T>& v){
39     for (int i = 0; i < size; i++) {
40         ptr[i] = v.ptr[i];
41     }
42 }
43 
44 
45         int get_size() {
46             return size;
47         }
48 
49         T& at(int index)const{
50     if (index < 0 || index >= size) {
51         throw out_of_range("Vector:index out of range");
52     } else {
53         return ptr[index];
54     }
55 }
56 
57         T& at(int index);
58 
59         T& operator[](int index)const;
60         T& operator[](int index) {
61     return static_cast<const Vector*>(this)->at(index);
62 
63 }
64 
65         template<typename T1>                                 
66                 friend void output(const Vector<T1>& v);
67 };
68 
69 
70 template<typename T>
71 T& Vector<T>::operator[](int index)const {
72     if (index < 0 || index >= size) {
73         throw out_of_range("Vector:index out of range");
74     } else {
75         return ptr[index];
76     }
77 
78 }
79 template<typename T>
80 T& Vector<T>::operator[](int index) {
81     return static_cast<const Vector*>(this)->operator[](index);
82 
83 }
84 
85 template<typename T>
86 void output(const Vector<T>& v) {
87     for (int i = 0; i < v.size; i++) {
88         cout << v.ptr[i] << ", ";
89     }
90     cout <<"\b\b " << endl;
91 
92 }
Vector.hpp
 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: ";
16     output(x1);
17     Vector<int> x2(n, 42);
18     const Vector<int> x3(x2);
19     cout << "x2: ";
20     output(x2);
21     cout << "x3: ";
22     output(x3);
23     x2.at(0) = 77;
24     x2.at(1) = 777;
25     cout << "x2: ";
26     output(x2);
27     cout << "x3: ";
28     output(x3);
29 }
30 
31 void test2() {
32     using namespace std;
33     int n, index;
34     while(cout << "Enter n and index: ", cin >> n >> index) { // 多组输入,按下Ctrl+Z终止
35         try {
36             Vector<int> v(n, n);
37             v.at(index) = -999;
38             cout << "v: ";
39             output(v);
40         } catch (const exception &e) {
41             cout << e.what() << endl;
42         }
43     }
44 }
45 int main() {
46     cout << "测试1: 模板类接口测试\n";
47     test1();
48     cout << "\n测试2: 模板类异常处理测试\n";
49     test2();
50 }
task4.cpp

 

结果:

 

任务5:

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<iomanip>
 5 
 6 using namespace std;
 7 
 8 class people
 9 {
10 private:
11     int id;
12     string name;
13     string major;
14     int score;
15 public:
16      people() = default;
17     ~ people() = default;
18 
19     string get_major()const{
20         return major;
21     }
22     int get_score()const{
23         return score;
24     }
25 
26     friend ostream& operator<<(ostream& out, people& p);
27     friend istream& operator>>(istream& in, people& p);
28 };
29 
30 ostream& operator<<(ostream& out,  people& p){
31     out << setiosflags(ios_base::left);
32     out << setw(10) << p.id
33         << setw(10) << p.name
34         << setw(10) << p.major
35         << setw(10) << p.score << endl;
36     return out;
37 
38 }
39 
40 istream& operator>>(istream& in, people& p){
41     in >> p.id >> p.name >> p.major >> p.score;
42     return in;
43 }
people.hpp
 1 #pragma once
 2 #include"people.hpp"
 3 #include<iostream>
 4 #include<string>
 5 #include<fstream>
 6 #include<vector>
 7 
 8 bool paixu(const  people& p1, const  people& p2)
 9 {
10     if (p1.get_major() == p2.get_major()){
11         return p1.get_score() > p2.get_score();
12     }
13     if (p1.get_major() < p2.get_major()){
14         return true;
15     }
16     return false;
17 }
18 
19 void output(ostream& out, const vector< people>& v)
20 {
21     for (auto i : v){
22         out << i;
23     }
24 }
25 
26 void save(const string& filename, vector< people>& v)
27 {
28     ofstream out(filename);
29     if (!out.is_open())
30     {
31         cout << "文件写入失败" << endl;
32         exit(0);
33     }
34     output(out, v);
35     out.close();
36 }
37 
38 void load(const string& filename, vector< people>& v)
39 {
40     string firstline;
41      people p;
42     ifstream in(filename);
43     if (!in.is_open()){
44         cout << "文件读出失败" << endl;
45         exit(0);
46     }
47 
48     getline(in, firstline);
49     while (in >> p){
50         v.push_back(p);
51     }
52 
53     in.close();
54 }
tools.hpp
 1 #include"people.hpp"
 2 #include"tools.hpp"
 3 #include<string>
 4 #include<vector>
 5 #include<algorithm>
 6 #include<iostream>
 7 
 8 int main()
 9 {
10     vector< people> v;
11     load("data5.txt", v);
12     sort(v.begin(), v.end(), paixu);
13     output(cout, v);
14     save("ans.txt", v);
15     return 0;
16 }
task5.cpp

 

结果:

 

 

posted @ 2024-12-21 20:28  Bling888  阅读(7)  评论(0编辑  收藏  举报