实验六

实验任务四

代码

vector.hpp

  1 #ifndef VECTOR_HPP
  2 #define VECTOR_HPP
  3 
  4 #include <stdexcept>
  5 #include <iostream> // 确保可以使用std::cout和std::endl
  6 
  7 template <typename T>
  8 class Vector {
  9 private:
 10     T* data_;
 11     int size_;
 12 public:
 13     // 默认构造函数
 14     Vector(): data_(nullptr), size_(0) {}
 15 
 16     // 构造函数:指定大小
 17     Vector(int n) : data_(nullptr), size_(0) {
 18         if (n < 0) {
 19             throw std::length_error("negative size");
 20         }
 21         if (n > 0) {
 22             data_ = new T[n](); // 默认初始化
 23             size_ = n;
 24         }
 25     }
 26 
 27     // 构造函数:指定大小和初始值
 28     Vector(int n, const T &value) : data_(nullptr), size_(0) {
 29         if (n < 0) {
 30             throw std::length_error("negative size");
 31         }
 32         if (n > 0) {
 33             data_ = new T[n];
 34             size_ = n;
 35             for (int i = 0; i < n; ++i) {
 36                 data_[i] = value;
 37             }
 38         }
 39     }
 40 
 41     // 拷贝构造函数:深拷贝
 42     Vector(const Vector<T> &other) : data_(nullptr), size_(0) {
 43         if (other.size_ > 0) {
 44             data_ = new T[other.size_];
 45             size_ = other.size_;
 46             for (int i = 0; i < size_; ++i) {
 47                 data_[i] = other.data_[i];
 48             }
 49         }
 50     }
 51 
 52     // 析构函数
 53     ~Vector() {
 54         delete[] data_;
 55     }
 56 
 57     // 返回大小
 58     int get_size() const {
 59         return size_;
 60     }
 61 
 62     // at()函数,带范围检查
 63     T& at(int i) {
 64         if (i < 0 || i >= size_) {
 65             throw std::out_of_range("Vector: index out of range");
 66         }
 67         return data_[i];
 68     }
 69 
 70     const T& at(int i) const {
 71         if (i < 0 || i >= size_) {
 72             throw std::out_of_range("Vector: index out of range");
 73         }
 74         return data_[i];
 75     }
 76 
 77     // 重载[],同样进行范围检查
 78     T& operator[](int i) {
 79         if (i < 0 || i >= size_) {
 80             throw std::out_of_range("Vector: index out of range");
 81         }
 82         return data_[i];
 83     }
 84 
 85     const T& operator[](int i) const {
 86         if (i < 0 || i >= size_) {
 87             throw std::out_of_range("Vector: index out of range");
 88         }
 89         return data_[i];
 90     }
 91 
 92     // 友元函数:输出Vector内容
 93     friend void output(const Vector<T> &v) {
 94         for (int i = 0; i < v.size_; ++i) {
 95             std::cout << v.data_[i];
 96             if (i < v.size_ - 1) std::cout << ", ";
 97         }
 98         std::cout << std::endl;
 99     }
100 };
101 
102 #endif

task4.cpp

 1 #include <iostream>
 2 #include "Vector.hpp"
 3 using namespace std;
 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 }

编译结果

 

实验任务五

代码

task5.cpp

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 struct Student {
 9     int id;
10     string name;
11     string major;
12     int score;
13 };
14 
15 int main() {
16     ifstream fin("data5.txt");
17     if (!fin) {
18         cout << "无法打开data5.txt文件" << endl;
19         return 1;
20     }
21 
22     {
23         string header;
24         getline(fin, header);
25     }
26 
27     vector<Student> students;
28     Student temp;
29 
30     while (fin >> temp.id >> temp.name >> temp.major >> temp.score) {
31         students.push_back(temp);
32     }
33     fin.close();
34 
35   
36     sort(students.begin(), students.end(), [](const Student &a, const Student &b) {
37         if (a.major == b.major) {
38             return a.score > b.score;
39         }
40         return a.major < b.major;
41     });
42 
43 
44     for (auto &s : students) {
45         cout << s.id << "\t" << s.name << "\t" << s.major << "\t" << s.score << endl;
46     }
47 
48     ofstream fout("ans5.txt");
49     for (auto &s : students) {
50         fout << s.id << "\t" << s.name << "\t" << s.major << "\t" << s.score << "\n";
51     }
52     fout.close();
53 
54     return 0;
55 }

 

编译结果

 

 

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