<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      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  陳少秋  閱讀(11)  評論(0)    收藏  舉報
       
      主站蜘蛛池模板: 日本无码欧美一区精品久久| 亚洲男人的天堂网站| 美乳丰满人妻无码视频| 芜湖县| 少妇爽到呻吟的视频| 欧美精品国产一区二区三区| 国产啪视频免费观看视频| 国产成熟女人性满足视频| 龙州县| 免费无码va一区二区三区| 亚洲AV高清一区二区三区尤物| 中文字幕99国产精品| 人妻在线无码一区二区三区| 精品中文人妻在线不卡| 亚洲av色在线播放一区| 国产av成人精品播放| av日韩在线一区二区三区| 久久婷婷五月综合色国产免费观看| 亚洲人成小说网站色在线| 国产精品中文一区二区| 菏泽市| 亚洲精品成人一二三专区| 精品久久久久久无码免费| 亚洲国产亚洲国产路线久久| 中文无码妇乱子伦视频| 成年午夜免费韩国做受视频| 成人午夜在线观看刺激| 99福利一区二区视频| 九九热在线这里只有精品| 欧美黑人又粗又大又爽免费| 无码无需播放器av网站| 亚洲欧美在线观看品| 伊人中文在线最新版天堂| 国产亚洲tv在线观看| 国产精品自拍自在线播放| 日韩在线成年视频人网站观看| 国产福利在线观看免费第一福利| 成人午夜在线观看刺激| 镇远县| 国产在线精彩自拍视频| 人妻中出无码一区二区三区|