首页 > 其他分享 >实验6 模板类、文件I/O和异常处理

实验6 模板类、文件I/O和异常处理

时间:2024-12-17 17:19:52浏览次数:7  
标签:文件 return index int Vector 实验 include 模板 size

实验内容4

Vector.hpp

 1 #pragma once
 2 #include<iostream>
 3 #include<stdexcept>
 4 
 5 using namespace std;
 6 
 7 template<typename T>
 8 
 9 class Vector {
10     private:
11         int size;
12         T* ptr;
13         
14     public:
15         Vector(int size, int value = 0) :size { size } {
16             if (size < 0) {
17                 throw length_error("Vector constructor: negative size");
18             }
19             ptr = new T[size];
20             for (int i = 0; i < size; i++) {
21                 ptr[i] = value;
22             }
23         }
24         int get_size() const {
25             return size;
26         }
27         T& at(int index) const {
28             if (index < 0 || index >= size) throw out_of_range("Vector: index out of range");
29             return ptr[index];
30         }
31         T& at(int index) {
32             return static_cast<const Vector<T>*>(this)->at(index);
33         }
34         T& operator[](int index)const {
35             if (index < 0 || index >= size) throw out_of_range("Vector: index out of range");
36             return ptr[index];
37         }
38         T& operator[](int index) {
39             return static_cast<const Vector<T>*>(this)->operator[](index);
40         }
41 };
42 
43 template<typename T1>
44 void output(const Vector<T1>& v) {
45     for (int i = 0; i < v.get_size(); i++) {
46         cout << v[i] << ", ";
47     }
48     cout << endl;
49 }

 

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 }

 

运行结果截图

 

实验内容5

task5.cpp

 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 #include<vector>
 5 #include<iomanip>
 6 #include<algorithm>
 7 
 8 using namespace std;
 9 
10 class student{
11 public:
12     student() = default;
13     ~student() = default;
14 
15     string get_major()const {
16         return major;
17     }
18 
19     int get_score()const {
20         return score;
21     }
22 
23     friend ostream& operator<<(ostream& out, const student& s) {
24         out << setiosflags(ios_base::left);
25         out << setw(10) << s.number
26             << setw(10) << s.name
27             << setw(10) << s.major
28             << setw(10) << s.score;
29 
30         return out;
31     }
32 
33     friend istream& operator>>(istream& in,  student& s) {
34         in >>s.number >> s.name >> s.major >> s.score;
35 
36         return in;
37     }
38 
39 private:
40     string number;
41     string name;
42     string major;
43     int score;
44 
45 };
46 
47 bool compare(const student& s1, const student& s2) {
48     if (s1.get_major() < s2.get_major())
49         return true;
50 
51     if (s1.get_major() == s2.get_major())
52         return s1.get_score() > s2.get_score();
53 
54     return false;
55 }
56 
57 void output(ostream& out, const vector<student>& v) {
58     for (auto& i : v)
59         out << i << endl;
60 }
61 
62 void save(const string& filename, vector<student>& v) {
63     ofstream out(filename);
64     if (!out.is_open()) {
65         cout << "写入文件失败\n";
66         return;
67     }
68 
69     output(out, v);
70     out.close();
71 }
72 
73 void load(const string& filename, vector<student>& v) {
74     ifstream in(filename);
75     if (!in.is_open()) {
76         cout << "读取文件失败\n";
77         return;
78     }
79 
80     string title_line;
81     getline(in, title_line);
82 
83     student t;
84     while (in >> t)
85         v.push_back(t);
86 
87     in.close();
88 }
89 
90 int main() {
91     vector<student>v;
92 
93     load("data5.txt", v);
94     sort(v.begin(), v.end(), compare);
95     output(cout, v);
96     save("ans5.txt", v);
97 }

 

运行结果显示:

 

标签:文件,return,index,int,Vector,实验,include,模板,size
From: https://www.cnblogs.com/xuruize/p/18613016

相关文章

  • 实验六
    1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<iostream>5#include<stdexcept>//用于异常处理67usingnamespacestd;89template<typenameT>10classVector{//类名修改为MyVector,避免与std::vector冲突11priva......
  • 实验 6
    实验6实验任务4//Vector.hpp#include<iostream>#include<stdexcept>#include<memory>template<typenameT>classVector{public://构造函数,动态指定大小explicitVector(intsize){if(size<0){throwstd......
  • 实验6
    实验任务4: 1#include<iostream>2#include<stdexcept>34usingnamespacestd;56template<typenameT>7classVector{8public:9Vector(intn);10Vector(intn,Tvalue);11Vector(constVector<T&g......
  • 实验6
    实验任务4Vector.hpp代码点击查看代码#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public: Vector(intn); Vector(intn,Tvalue); Vector(constVector<T>&v); ~Vector(); intget_siz......
  • 实验6
    实验任务4:实验代码:Vector.hpp:1#pragmaonce23#include<iostream>4#include<stdexcept>56usingnamespacestd;78template<typenameT>9classVector{10public:11Vector(intn,intvalue=0);12Vector(const......
  • 实验6 模板类、文件I/O和异常处理
    任务4Vector.hpp1#pragmaonce23#include<iostream>4#include<stdexcept>56usingnamespacestd;78template<typenameT>9classVector{10public:11Vector(intn,Tvalue=0);12~Vector();13......
  • Linux文件属性 --- 文件.目录、软硬链接、字符设备文件
    目录 七种文件类型1、普通文件和目录 2、链接文件 2.1硬链接 2.2软链接  3、字符设备文件 七种文件类型 Linux的文件属性中一共有以下七种类型 :符号类型含义解释-普通文件纯文本文件(ASCII)和二进制文件(binary)d目录类似于Windows的文件夹l符号链接文件ln–......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4:代码:Vector.hpp查看代码#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public:Vector(intsize);Vector(intsize,Tvalue);Vector(constVector<T>&......
  • 实验6
    task41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • 实验6 模板类、文件I\O和异常处理
    任务4源码:1#pragmaonce23#include<iostream>4#include<stdexcept>56usingnamespacestd;78//模板类声明9template<typenameT>10classVector{11public:12Vector(intn):size{n}{13if(n<0)14......