首页 > 其他分享 >实验6

实验6

时间:2024-12-21 20:30:51浏览次数:4  
标签:index return people int Vector 实验 include

任务4:

 1 #pragma once
 2 #include<iostream>
 3 #include<stdexcept>
 4 
 5 using namespace std;
 6 
 7 template<typename T>
 8 class Vector {
 9     private:
10         int size;
11         T* ptr;
12     public:
13         Vector(int n) {
14             if (n < 0) {
15                 throw length_error("Vector constructor:negative size");
16             } else {
17                 ptr = new T[size];
18             }
19         }
20 
21         Vector(int n, T value) {
22             if (n < 0) {
23                 throw length_error("Vector constructor:negative size");
24             } else {
25                 ptr = new T[size];
26                 for (int i = 0; i < size; i++) {
27                     ptr[i] = value;
28 
29                 }
30             }
31         }
32 
33 
34 
35         ~Vector(){delete[] ptr;}
36         
37         
38         Vector(const Vector<T>& v){
39     for (int i = 0; i < size; i++) {
40         ptr[i] = v.ptr[i];
41     }
42 }
43 
44 
45         int get_size() {
46             return size;
47         }
48 
49         T& at(int index)const{
50     if (index < 0 || index >= size) {
51         throw out_of_range("Vector:index out of range");
52     } else {
53         return ptr[index];
54     }
55 }
56 
57         T& at(int index);
58 
59         T& operator[](int index)const;
60         T& operator[](int index) {
61     return static_cast<const Vector*>(this)->at(index);
62 
63 }
64 
65         template<typename T1>                                 
66                 friend void output(const Vector<T1>& v);
67 };
68 
69 
70 template<typename T>
71 T& Vector<T>::operator[](int index)const {
72     if (index < 0 || index >= size) {
73         throw out_of_range("Vector:index out of range");
74     } else {
75         return ptr[index];
76     }
77 
78 }
79 template<typename T>
80 T& Vector<T>::operator[](int index) {
81     return static_cast<const Vector*>(this)->operator[](index);
82 
83 }
84 
85 template<typename T>
86 void output(const Vector<T>& v) {
87     for (int i = 0; i < v.size; i++) {
88         cout << v.ptr[i] << ", ";
89     }
90     cout <<"\b\b " << endl;
91 
92 }
Vector.hpp
 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: ";
16     output(x1);
17     Vector<int> x2(n, 42);
18     const Vector<int> x3(x2);
19     cout << "x2: ";
20     output(x2);
21     cout << "x3: ";
22     output(x3);
23     x2.at(0) = 77;
24     x2.at(1) = 777;
25     cout << "x2: ";
26     output(x2);
27     cout << "x3: ";
28     output(x3);
29 }
30 
31 void test2() {
32     using namespace std;
33     int n, index;
34     while(cout << "Enter n and index: ", cin >> n >> index) { // 多组输入,按下Ctrl+Z终止
35         try {
36             Vector<int> v(n, n);
37             v.at(index) = -999;
38             cout << "v: ";
39             output(v);
40         } catch (const exception &e) {
41             cout << e.what() << endl;
42         }
43     }
44 }
45 int main() {
46     cout << "测试1: 模板类接口测试\n";
47     test1();
48     cout << "\n测试2: 模板类异常处理测试\n";
49     test2();
50 }
task4.cpp

 

结果:

 

任务5:

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<iomanip>
 5 
 6 using namespace std;
 7 
 8 class people
 9 {
10 private:
11     int id;
12     string name;
13     string major;
14     int score;
15 public:
16      people() = default;
17     ~ people() = default;
18 
19     string get_major()const{
20         return major;
21     }
22     int get_score()const{
23         return score;
24     }
25 
26     friend ostream& operator<<(ostream& out, people& p);
27     friend istream& operator>>(istream& in, people& p);
28 };
29 
30 ostream& operator<<(ostream& out,  people& p){
31     out << setiosflags(ios_base::left);
32     out << setw(10) << p.id
33         << setw(10) << p.name
34         << setw(10) << p.major
35         << setw(10) << p.score << endl;
36     return out;
37 
38 }
39 
40 istream& operator>>(istream& in, people& p){
41     in >> p.id >> p.name >> p.major >> p.score;
42     return in;
43 }
people.hpp
 1 #pragma once
 2 #include"people.hpp"
 3 #include<iostream>
 4 #include<string>
 5 #include<fstream>
 6 #include<vector>
 7 
 8 bool paixu(const  people& p1, const  people& p2)
 9 {
10     if (p1.get_major() == p2.get_major()){
11         return p1.get_score() > p2.get_score();
12     }
13     if (p1.get_major() < p2.get_major()){
14         return true;
15     }
16     return false;
17 }
18 
19 void output(ostream& out, const vector< people>& v)
20 {
21     for (auto i : v){
22         out << i;
23     }
24 }
25 
26 void save(const string& filename, vector< people>& v)
27 {
28     ofstream out(filename);
29     if (!out.is_open())
30     {
31         cout << "文件写入失败" << endl;
32         exit(0);
33     }
34     output(out, v);
35     out.close();
36 }
37 
38 void load(const string& filename, vector< people>& v)
39 {
40     string firstline;
41      people p;
42     ifstream in(filename);
43     if (!in.is_open()){
44         cout << "文件读出失败" << endl;
45         exit(0);
46     }
47 
48     getline(in, firstline);
49     while (in >> p){
50         v.push_back(p);
51     }
52 
53     in.close();
54 }
tools.hpp
 1 #include"people.hpp"
 2 #include"tools.hpp"
 3 #include<string>
 4 #include<vector>
 5 #include<algorithm>
 6 #include<iostream>
 7 
 8 int main()
 9 {
10     vector< people> v;
11     load("data5.txt", v);
12     sort(v.begin(), v.end(), paixu);
13     output(cout, v);
14     save("ans.txt", v);
15     return 0;
16 }
task5.cpp

 

结果:

 

 

标签:index,return,people,int,Vector,实验,include
From: https://www.cnblogs.com/Bling888/p/18621140

相关文章

  • 《操作系统真相还原》实验记录1.2——print.S打印函数
    一、print.S文件说明put_char函数(每次只打印一个字符)是各种打印函数的核心1.1功能说明put_char函数的处理流程备份寄存器现场;获取光标坐标值,光标坐标值是下一个可打印字符的位置;为了在光标处打印字符,需要读取光标坐标寄存器,获取光标坐标值。获取待打印的字符;......
  • 实验6 模板类、文件I/O和异常处理
    任务一:Complex.hpp#pragmaonce#include<iostream>#include<stdexcept>//声明//////////////////////////////////////////////////////复数模板类声明template<typenameT>classComplex{public:Complex(Tr=0,Ti=0);Complex(const......
  • 实验六
    task4代码Vector.hpp点击查看代码#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public:Vector(intsize,intvalue=0):size{size}{if(size<0)throwlengt......
  • 实验6 模板类、文件I/O和异常处理
    任务一Complex.hpp#pragmaonce#include<iostream>#include<stdexcept>//声明//////////////////////////////////////////////////////复数模板类声明template<typenameT>classComplex{public:Complex(Tr=0,Ti=0);Complex(const......
  • 实验六
    task4源代码:#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;......
  • 实验六 C++
    任务四:Vector.hpp:#pragmaonce#ifndefVECTOR_HPP#defineVECTOR_HPP#include<iostream>#include<stdexcept>//为异常类提供支持#include<memory>//为std::unique_ptr提供支持template<typenameT>classVector{private:std::uniqu......
  • 大型数据库应用技术:实验1 熟悉常用的Linux操作和Hadoop操作
    实验1熟悉常用的Linux操作和Hadoop操作1.实验目的Hadoop运行在Linux系统上,因此,需要学习实践一些常用的Linux命令。本实验旨在熟悉常用的Linux操作和Hadoop操作,为顺利开展后续其他实验奠定基础。2.实验平台(1)操作系统:Linux(建议Ubuntu16.04或Ubuntu18.04);(2)Hadoop版本:3.1.3。3.......
  • 实验6
    任务一1#include<stdio.h>2#include<string.h>3#defineN34typedefstructstudent{5intid;6charname[20];7charsubject[20];8doubleperf;9doublemid;......
  • 实验6 模板类、文件I/O和异常处理
    实验任务41#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<iostream>5#include<stdexcept>67template<typenameT>8classVector{9private:10T*data;11size_tsize;1213public:14//构造函数,动态指定大小15......
  • 百度机器翻译SDK实验
    软件构造实验作业实验名称:班级:信2205-3    学号:20223753    姓名:邓睿智 实验一:百度机器翻译SDK实验一、实验要求实验一:百度机器翻译SDK实验(2024.11.15日完成)  任务一:下载配置百度翻译Java相关库及环境(占10%)。    任务二:了解百度翻译相关功能并进行总结......