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

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

时间:2024-12-17 18:53:51浏览次数:7  
标签:std 文件 const index int Vector 实验 模板 size

task4

Vector.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <stdexcept>
 5 
 6 using std::cout;
 7 using std::endl;
 8 
 9 template<typename T>
10 class Vector {
11 public:
12     Vector(int size0 = 0);
13     Vector(int size0, T same_value);
14     Vector(const Vector<T>& v);
15     ~Vector();
16 
17     int get_size() const;
18     T& at(int index) const;
19     T& at(int index);
20 
21     T& operator[](int index) const;
22     T& operator[](int index);
23 
24     template<typename T1>
25     friend void output(const Vector<T1>& v);
26 
27 private:
28     int size;
29     T* ptr;
30 };
31 
32 template<typename T>
33 Vector<T>::Vector(int size0) : size{ size0 } {
34     if (size < 0)
35         throw std::length_error("vectorInt constructor: negative size");
36 
37     ptr = new T[size];
38 }
39 
40 template<typename T>
41 Vector<T>::Vector(int size0, T same_value) : size{ size0 } {
42     if (size < 0)
43         throw std::length_error("vectorInt constructor: negative size");
44 
45     ptr = new T[size];
46     for (int i = 0; i < size; ++i)
47         ptr[i] = same_value;
48 }
49 
50 template<typename T>
51 Vector<T>::Vector(const Vector<T>& v) : size{ v.size }, ptr{ new T[size] } {//? get_size()
52     for (int i = 0; i < size; ++i)
53         ptr[i] = v.ptr[i];
54 }
55 
56 template<typename T>
57 Vector<T>::~Vector() {
58     delete[] ptr;
59 }
60 
61 template<typename T>
62 int Vector<T>::get_size() const {
63     return size;
64 }
65 
66 template<typename T>
67 T& Vector<T>::at(int index) const {
68     if (index < 0 || index >= size)
69         throw std::out_of_range("vectorInt::operator[](): index out of this->size");
70 
71     return ptr[index];
72 }
73 
74 template<typename T>
75 T& Vector<T>::at(int index) {
76     return static_cast<const Vector<T>*>(this)->at(index);
77 }
78 
79 template<typename T>
80 T& Vector<T>::operator[](int index) const {
81     if (index < 0 || index >= size)
82         throw std::out_of_range("vectorInt::operator[](): index out of this-> size");
83 
84     return ptr[index];
85 }
86 
87 template<typename T>
88 T& Vector<T>::operator[](int index) {
89     return static_cast<const Vector<T>*>(this)->operator[](index);
90 }
91 
92 template<typename T1>
93 void output(const Vector<T1>& v) {
94     for (int i = 0; i < v.size; ++i) {
95         cout << v.at(i) << ", ";
96     }
97     cout << "\b\b \n";
98 }

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) { // 多组输入,按下Ctrl + Z终止
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

result4

 task5

Student.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <string>
 5 #include <iomanip>
 6 
 7 using std::ostream;
 8 using std::istream;
 9 using std::string;
10 using std::setiosflags;
11 using std::setw;
12 using std::ios_base;
13 
14 
15 
16 class Student {
17 public:
18     Student() = default;
19     ~Student() = default;
20 
21     string get_major() const;
22     int get_score() const;
23 
24     friend ostream& operator<<(ostream& out, const Student& s);
25     friend istream& operator>>(istream& in, Student& s);
26 private:
27     string no;
28     string name;
29     string major;
30     int score;
31 };
32 
33 string Student::get_major() const {
34     return major;
35 }
36 
37 int Student::get_score() const {
38     return score;
39 }
40 
41 
42 ostream& operator<<(ostream& out, const Student& s) {
43     out << setiosflags(ios_base::left);
44     out << setw(15) << s.no
45         << setw(15) << s.name
46         << setw(15) << s.major
47         << setw(15) << s.score;
48 
49     return out;
50 }
51 
52 istream& operator>>(istream& in, Student& s) {
53     in >> s.no >> s.name >> s.major >> s.score;
54 
55     return in;
56 }

utils.hpp

 1 #include "Student.hpp"
 2 #include <fstream>
 3 #include <iostream>
 4 #include <vector>
 5 #include <string>
 6 
 7 bool compare_by_solutionInfo(const Student& s1, const Student& s2) {
 8     if (s1.get_major() < s2.get_major())
 9         return true;
10 
11     if (s1.get_major() == s2.get_major())
12         return s1.get_score() > s2.get_score();
13 
14     return false;
15 }
16 
17 // 把vector<Student>对象中的元素插入到输出流out
18 void output(std::ostream& out, const std::vector<Student>& v) {
19     for (auto& i : v) {
20         out << i << std::endl;
21     }
22 }
23 
24 // 把vector<Student>对象中的元素写到filename文件中
25 void save(const std::string& filename, std::vector<Student>& v) {
26     using std::ofstream;
27 
28     ofstream out(filename);
29     if (!out.is_open()) {
30         std::cout << "fail to open file to write\n";
31         return;
32     }
33 
34     output(out, v);
35     out.close();
36 }
37 
38 // 从文件filename读取参赛选手信息到vector<Student>对象
39 void load(const std::string& filename, std::vector<Student>& v) {
40     using std::ifstream;
41 
42     ifstream in(filename);
43     if (!in.is_open()) {
44         std::cout << "fail to open file to read\n";
45         return;
46     }
47 
48     std::string title_line;
49     getline(in, title_line); // 跳过标题行
50 
51     Student t;
52     while (in >> t) {
53         v.push_back(t);
54     }
55 
56     in.close();
57 }

task5.cpp

 1 #include "Student.hpp"
 2 #include "utils.hpp"
 3 #include <iostream>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 void test() {
 8     using namespace std;
 9 
10     vector<Student> v;
11 
12     load("data.txt", v);
13     sort(v.begin(), v.end(), compare_by_solutionInfo);
14     output(cout, v);
15     save("ans.txt", v);
16 }
17 
18 int main() {
19     test();
20 }

result5

 

标签:std,文件,const,index,int,Vector,实验,模板,size
From: https://www.cnblogs.com/nuist0415/p/18612952

相关文章

  • 实验6 模板类、文件I/O和异常处理
    1.实验任务4Vector.hpp1#pragmaonce2#include<iostream>3#include<stdexcept>45usingnamespacestd;67template<typenameT>8classVector{9public:10Vector(intn);11Vector(intn,Tvalue);12Vector(c......
  • 实验6 模板类、文件I/O和异常处理
    task4:Vector.hpp#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public:Vector(intsize,intvalue=0):size{size}{if(size<0)throwlength_error(......
  • vue导出.csv文件
    //安装papaparsenpmipapaparse--saveimport*asPapaparsefrom"papaparse";/***默认导出数据头部*贴别注意格式的问题,不然导出的.cvs文件和上传的.cvs文件解析出来的结果会不一样数组的长度必须保持一致,不够的用空字符站位,如红色部分*/constdefaultCvsDa......
  • OPP实验六
    任务一、 对于约束性模板友元和非约束性模板友元,在语法上的区别在于,templete<typenameT>T的名字与类中是不一样就是非约束性就是不受类的模板的影响相互独立的。由于这里出现了两种的输出方式,通过ostream是iostream和fostream的基类,就可以一个重载使用两个,方便了一点。......
  • 程序设计实验6
    实验任务1 实验任务2实验任务3实验任务41#pragmaonce2#include<iostream>3#include<stdexcept>4#include<iomanip>5usingnamespacestd;6template<typenameT>7classVector{8private:9intsize;10T*ptr;11public:12V......
  • 实验六
    任务4vector.hpp#pragmaonce#include<iostream>#include<stdexcept>usingstd::cout;usingstd::endl;template<typenameT>classVector{ public: Vector(ints); Vector(ints,Tv); Vector(constVector<T>&v); ~Vector();......
  • 实验六
    task4:代码:1#pragmaonce23#include<iostream>4#include<stdexcept>5#include<cmath>67usingnamespacestd;89template<typenameT>10classVector{11private:12intsize;13T*ptr;14......
  • 实验六
    实验任务四代码vector.hpp1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<stdexcept>5#include<iostream>//确保可以使用std::cout和std::endl67template<typenameT>8classVector{9private:10T*data_;11......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4:实验代码:1#pragmaonce2#include<iostream>3#include<stdexcept>4usingnamespacestd;5template<typenameT>6classVector{7public:8Vector(intn,constT&value=T()):size(n){9if(n<0......
  • 实验6
    task1task1.cpp#include"Complex.hpp"#include<iostream>#include<fstream>#include<stdexcept>voidtest1();voidtest2();intmain(){usingnamespacestd;cout<<"测试1:复数模板类测试"<<endl;......