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

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

时间:2024-12-16 20:54:56浏览次数:4  
标签:std 文件 int Vector 实验 student using include 模板

task4:

Vector.hpp源代码:
 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 template <typename T>
 6 class Vector
 7 {
 8 public:
 9     Vector(int nn) :n{ nn } {
10         if (n < 0)throw length_error("Vector: negative size");
11         elem = (T*)malloc(n * sizeof(T));
12         if (!elem)exit(OVERFLOW);
13     }
14     Vector(int nn,T val) :n{ nn } {
15         if (n < 0)throw length_error("Vector: negative size");
16         elem = (T*)malloc(n * sizeof(T));
17         if (!elem)exit(OVERFLOW);
18         for (int i=0;i<n;i++)
19         {
20             elem[i] = val;
21         }
22     }
23     Vector(const Vector<T>& b){
24         n = b.n;
25         elem = (T*)malloc(n * sizeof(T));
26         if (!elem)exit(OVERFLOW);
27         for(int i=0;i<n;i++)
28         {
29             elem[i] = b.elem[i];
30         }
31     }
32     ~Vector() {
33         n = 0;
34         free(elem);
35     }
36     int getsize()
37     {
38         return n;
39     }
40     T& at(int i)
41     {
42         if (i < 0||i>=n)throw out_of_range("Vector: index out of range");
43         return elem[i];
44     }
45     T& operator[](int i)
46     {
47         if (i < 0 || i>=n)throw out_of_range("Vector: index out of range");
48         return elem[i];
49     }
50     template <typename T1>
51     friend void output(const Vector<T1> a);
52 private:
53     T* elem;
54     int n;
55 };
56 template <typename T>
57 void output(const Vector<T> a)
58 {
59     for (int i=0;i<a.n;i++)
60     {
61         cout << a.elem[i] << " ";
62     }
63     cout << endl;
64 }
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

 

运行结果:

 

task5: task5.cpp源码:
 1 #include <iostream>
 2 #include <iomanip>
 3 #include <string>
 4 #include<vector>
 5 #include <fstream>
 6 #include <algorithm>
 7 
 8 using std::string;
 9 using std::ostream;
10 using std::istream;
11 using std::setw;
12 using std::setprecision;
13 using std::setiosflags;
14 using std::ios_base;
15 
16 class student {
17 public:
18     student() = default;
19     ~student() = default;
20     int get_score() const { return s; }
21     friend ostream& operator<<(ostream& out, const student& c);
22     friend istream& operator>>(istream& in, student& c);
23     friend bool cmp1(student& a, student& b);
24     friend bool cmp2(student& a, student& b);
25 private:
26     string no; // 学号
27     string name; // 姓名
28     string major; // 专业
29     int s; // 解题数
30 };
31 ostream& operator<<(ostream& out, const student& c) {
32     out << setiosflags(ios_base::left);
33     out << setw(15) << c.no
34         << setw(20) << c.name
35         << setw(15) << c.major
36         << setw(5) << c.s;
37     return out;
38 }
39 istream& operator>>(istream& in, student& c) {
40     in >> c.no >> c.name >> c.major >> c.s;
41     return in;
42 }
43 
44 void output(std::ostream& out, const std::vector<student>& v) {
45     for (auto& i : v)
46         out << i << std::endl;
47 }
48 void save(const std::string& filename, std::vector<student>& v) {
49     using std::ofstream;
50     ofstream out(filename);
51     if (!out.is_open()) {
52         std::cout << "fail to open file to write\n";
53         return;
54     }
55     output(out, v);
56     out.close();
57 }
58 
59 void load(const std::string& filename, std::vector<student>& v) {
60     using std::ifstream;
61     ifstream in(filename);
62     if (!in.is_open()) {
63         std::cout << "fail to open file to read\n";
64         return;
65     }
66     std::string title_line;
67     getline(in, title_line); 
68     student t;
69     while (in >> t)
70         v.push_back(t);
71     in.close();
72 }
73 bool cmp1(student& a, student& b)
74 {
75     return a.major.compare(b.major) < 0;
76 }
77 bool cmp2(student& a, student& b)
78 {
79     return a.s > b.s;
80 }
81 void Sort(std::vector<student>& v)
82 {
83     sort(v.begin(), v.end(), cmp2);
84     sort(v.begin(), v.end(), cmp1);
85 }
86 void test() {
87     using namespace std;
88     vector<student> v;
89     load("data5.txt", v);
90     Sort(v);
91     output(cout, v);
92     save("ans5.txt", v); 
93 }
94 int main() {
95     test();
96 }
View Code

date5.txt:

 

运行结果:

 

 

标签:std,文件,int,Vector,实验,student,using,include,模板
From: https://www.cnblogs.com/the-existent/p/18611093

相关文章

  • 实验六
    任务四:1#include<stdio.h>2#defineN1034typedefstruct5{6charisbn[20];//isbn号7charname[80];//书名8charauthor[80];//作者9doublesales_price;//售价10intsales_count;//销售册数11}......
  • Python程序设计——实验与实践
    三、PY_03_03PY_03_06PY_03_07四、PY_04_02PY_04_03PY_04_05PY_04_07PY_04_08......
  • 实验六
    实验1Complex.hpp#pragmaonce#include<iostream>#include<stdexcept>//声明//////////////////////////////////////////////////////复数模板类声明template<typenameT>classComplex{public:Complex(Tr=0,Ti=0);Complex(constComplex<......
  • 20222314 2024-2025-1 《网络与系统攻防技术》实验八实验报告
    202223142024-2025-1《网络与系统攻防技术》实验八实验报告1.实验内容1.1Web前端HTML能正常安装、启停Apache。理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML1.2Web前端javascipt理解JavaScript的基本功能,理解DOM在1的基础上,编写JavaScript验证用户名、密......
  • NiceGUI打包后避免在当前目录生成文件夹
    直接在Import之后加上这句代码即可os.environ["WEBVIEW2_USER_DATA_FOLDER"]=os.path.join(os.path.expanduser("~"),"Example_APP")示例代码:importosfromniceguiimportui#user_data_folder=os.path.join(os.path.expanduser("~"),......
  • 实验6
    4.实验任务4完整的task4.c源码1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9......
  • 模板性能问题排查方法
    1.概述模板的加载速度受到很多因素影响,如果一个模板预览的时候,加载较慢,该如何去分析问题原因呢?2.排查步骤2.1查看数据集查询速度大部分模板加载慢,都是因为sql执行速度比较慢。那么如何验证sql速度快慢呢?可以使用以下几种方案。1)在设计器的数据集中直接查询,人为感受sq......
  • 实验六 模板类,文件I/O和异常处理
    任务4代码:Vector.hpp:1#pragmaonce2#include<iostream>3#include<stdexcept>4#include<cmath>56usingnamespacestd;78template<typenameT>9classVector{10public:11Vector(ints):size{s}{12......
  • 实验六
    任务4:代码:#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public:Vector(intn,constT&value=T()):size(n){if(n<0){......
  • Win11系统电脑d3dx9_43.dll丢失?d3dx9_43.dll文件找不到的修复方法
    在使用Win11电脑的过程中,不少用户可能会遇到d3dx9_43.dll文件丢失的问题,这可能会导致一些游戏或应用程序无法正常运行,给我们的使用带来诸多不便。不过别担心,下面就为大家详细介绍几种解决该问题的有效方法。一、重新安装相关程序或游戏许多时候,d3dx9_43.dll文件丢失是......