CHENSHAOQIU

导航

 
1. 实验任务4
Vector.hpp源代码
 
 1 #pragma once
 2 #include <iostream>
 3 #include <stdexcept>
 4 using namespace std;
 5 
 6 template <typename T>
 7 class Vector {
 8 public:
 9     Vector(int size);
10     Vector(int size, T value);
11     Vector(const Vector<T>& other);
12     ~Vector();
13     int get_size() const;
14     T& at(int index);
15     const T& at(int index) const;
16     T& operator[](int index);
17     const T& operator[](int index) const;
18     template <typename U>
19     friend void output(const Vector<U>& v);
20 private:
21     int size;
22     T* data;
23 };
24 template <typename T>
25 Vector<T>::Vector(int size) : size(size) {
26     if (size < 0) throw std::length_error("Vector constructor: negative size");
27     data = new T[size]{};
28 }
29 template <typename T>
30 Vector<T>::Vector(int size, T value) : size(size) {
31     if (size < 0) throw std::length_error("Vector constructor: negative size");
32     data = new T[size];
33     for (int i = 0; i < size; ++i) data[i] = value;
34 }
35 template <typename T>
36 Vector<T>::Vector(const Vector<T>& other) : size(other.size) {
37     data = new T[size];
38     for (int i = 0; i < size; ++i) data[i] = other.data[i];
39 }
40 template <typename T>
41 Vector<T>::~Vector() {
42     delete[] data;
43 }
44 template <typename T>
45 int Vector<T>::get_size() const {
46     return size;
47 }
48 template <typename T>
49 T& Vector<T>::at(int index) {
50     if (index < 0 || index >= size) throw std::out_of_range("Vector: index out of range");
51     return data[index];
52 }
53 template <typename T>
54 const T& Vector<T>::at(int index) const {
55     if (index < 0 || index >= size) throw std::out_of_range("Vector: index out of range");
56     return data[index];
57 }
58 template <typename T>
59 T& Vector<T>::operator[](int index) {
60     return at(index);
61 }
62 template <typename T>
63 const T& Vector<T>::operator[](int index) const {
64     return at(index);
65 }
66 template <typename U>
67 void output(const Vector<U>& v) {
68     for (int i = 0; i < v.size; ++i) {
69         std::cout << v.data[i] << " ";
70     }
71     std::cout << std::endl;
72 }
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

 

运行测试截图

 

2. 实验任务5
task5.cpp源码
 
 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <iomanip>
 7 using namespace std;
 8 class Student {
 9 public:
10     int id;
11     string name;
12     string major;
13     int score;
14     friend istream& operator>>(istream& in, Student& s);
15     friend ostream& operator<<(ostream& out, const Student& s);
16 };
17 istream& operator>>(istream& in, Student& s) {
18     in >> s.id >> s.name >> s.major >> s.score;
19     return in;
20 }
21 ostream& operator<<(ostream& out, const Student& s) {
22     out << left << setw(10) << s.id
23         << setw(10) << s.name
24         << setw(10) << s.major
25         << fixed << setprecision(2) << s.score;
26     return out;
27 }
28 int main() {
29     vector<Student> students;
30     const string input_file = "data5.txt";
31     const string output_file = "ans5.txt";
32     ifstream infile(input_file);
33     ofstream outfile(output_file);
34     string header;
35     getline(infile, header);
36     Student temp;
37     while (infile >> temp) {
38         students.push_back(temp);
39     }
40     infile.close();
41     sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
42         if (a.major == b.major) return a.score > b.score;
43         return a.major < b.major;
44         });
45     for (const auto& s : students) {
46         cout << s << endl;
47         for (const auto& s : students) {
48             outfile << s << endl;
49         }
50         outfile.close();
51     }
52     return 0;
53 }
View Code

 

运行结果截图

 

posted on 2024-12-24 14:58  陈少秋  阅读(2)  评论(0编辑  收藏  举报