实验6

实验任务4

Vector.hpp
 1 #ifndef VECTOR_HPP
 2 #define VECTOR_HPP
 3 
 4 
 5 #include <iostream>
 6 #include <stdexcept>
 7 #include <memory>
 8 
 9 template <typename T>
10 class Vector
11 {
12 private:
13     T *data;
14     size_t size;
15 
16 public:
17     explicit Vector(int n) : size(n)
18     {
19         if (n < 0)
20         {
21             throw std::length_error("Size cannot be negative");
22         }
23         data = new T[size];
24     }
25 
26     Vector(int n, const T &value) : size(n)
27     {
28         if (n < 0)
29         {
30             throw std::length_error("Size cannot be negative");
31         }
32         data = new T[size];
33         for (size_t i = 0; i < size; ++i)
34         {
35             data[i] = value;
36         }
37     }
38 
39     Vector(const Vector<T> &other) : size(other.size)
40     {
41         data = new T[size];
42         for (size_t i = 0; i < size; ++i)
43         {
44             data[i] = other.data[i];
45         }
46     }
47 
48     ~Vector()
49     {
50         delete[] data;
51     }
52     
53     size_t get_size() const
54     {
55         return size;
56     }
57 
58     T &at(size_t index)
59     {
60         if (index >= size)
61         {
62             throw std::out_of_range("Index out of range");
63         }
64         return data[index];
65     }
66 
67     T &operator[](size_t index)
68     {
69         if (index >= size)
70         {
71             throw std::out_of_range("Index out of range");
72         }
73         return data[index];
74     }
75 
76     template <typename U>
77     friend void output(const Vector<U> &v);
78 };
79 
80 template <typename T>
81 void output(const Vector<T> &v)
82 {
83     for (size_t i = 0; i < v.size; ++i)
84     {
85         std::cout << v.data[i] << " ";
86     }
87     std::cout << std::endl;
88 }
89 
90 #endif
Vector.hpp
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     std::cout << "²âÊÔ1: Ä£°åÀà½Ó¿Ú²âÊÔ\n";
47     test1();
48 
49     std::cout << "\n²âÊÔ2: Ä£°åÀàÒì³£´¦Àí²âÊÔ\n";
50     test2();
51 }
task4.cpp

测试运行代码

实验任务5

 1 #include <iostream>
 2 #include <fstream>
 3 #include <vector>
 4 #include <string>
 5 #include <algorithm>
 6 #include <sstream>
 7 #include <iomanip> 
 8 
 9 class Student
10 {
11 public:
12     int id;
13     std::string name;
14     std::string major;
15     int score;
16 
17     Student(int id, std::string name, std::string major, int score)
18         : id(id), name(name), major(major), score(score) {}
19 
20     void print() const
21     {
22         std::cout << id << "\t" << name << "\t" << major << "\t" << score << std::endl;
23     }
24 };
25 
26 bool compare(const Student &s1, const Student &s2)
27 {
28     if (s1.major == s2.major)
29     {
30         return s1.score > s2.score;
31     }
32     return s1.major < s2.major;
33 }
34 
35 int main()
36 {
37     std::ifstream infile("data5.txt");
38     std::ofstream outfile("ans5.txt");
39 
40     if (!infile.is_open())
41     {
42         std::cerr << "无法打开输入文件!" << std::endl;
43         return 1;
44     }
45 
46     std::vector<Student> students;
47     std::string line;
48 
49     std::getline(infile, line);
50 
51     while (std::getline(infile, line))
52     {
53         std::istringstream iss(line);
54         int id;
55         std::string name, major;
56         int score;
57 
58         iss >> id >> name >> major >> score;
59 
60         students.push_back(Student(id, name, major, score));
61     }
62 
63     std::sort(students.begin(), students.end(), compare);
64 
65     for (const auto &student : students)
66     {
67         student.print();
68         outfile << student.id << "\t  " << student.name << "\t  " << student.major << "\t  " << student.score << std::endl;
69     }
70 
71         infile.close();
72         outfile.close();
73 
74         return 0;
75 }
task5.cpp

测试运行代码

 

 

posted @ 2024-12-16 10:31  It-just-works  阅读(6)  评论(0编辑  收藏  举报