实验6

task4

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<stdexcept>
 5 
 6 using namespace std;
 7 template<typename T>
 8 class Vector {
 9 public:
10     Vector(int size = 0, T value = T()) :m_size(size) {
11         if (size < 0) {
12             throw std::length_error("Vector constructor:negative size");
13         }
14         m_data = new T[size];
15         for (int i = 0;i < size;++i) {
16             m_data[i] = value;
17         }
18     }
19 
20     Vector(const Vector<T>& other) :m_size(other.m_size) {
21         m_data = new T[m_size];
22         for (int i = 0;i < m_size;++i) {
23             m_data[i] = other.m_data[i];
24         }
25     }
26     ~Vector() {
27         delete[]m_data;
28     }
29 
30     int get_size()const {
31         return m_size;
32     }
33 
34     T& at(int index) {
35         if (index < 0 || index >= m_size) {
36             throw std::out_of_range("Vector:index out of range");
37         }
38         return m_data[index];
39     }
40 
41     T& operator[](int index) {
42         if(index<0||index>=m_size){
43             throw std::out_of_range("Vector:index out of range");
44         }
45         return m_data[index];
46     }
47 
48     friend std::ostream& operator<<(std::ostream& os, const Vector<T>& v) {
49         for (int i = 0;i < v.m_size;++i) {
50             os << v.m_data[i];
51             if (i < v.m_size - 1) {
52                 os << ",";
53             }
54         }
55         return os;
56     }
57     
58     friend void output(const Vector<T>& v) {
59     std::cout << v << std::endl;
60 }
61 
62 private:
63     T* m_data;
64     int m_size;
65 };
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: ";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 }
task4.cpp

task5

 1 #include<iostream>
 2 #include<string>
 3 #include<fstream>
 4 #include<algorithm>
 5 #include<vector>
 6 
 7 using namespace std;
 8 
 9 class Grade{
10     public:
11         Grade(const string &n,const string &na,const string &ma,int score);
12         ~Grade()=default;
13         
14         int get_score() const{return score;}
15         string get_major() const{return major;}
16         string get_no()const{return no;}
17         string get_name()const{return name;}
18     private:
19         string no;
20         string name;
21         string major;
22         int score;
23 };
24 
25 Grade::Grade(const string &n,const string &na,const string &ma,int score):no{n},name{na},major{ma},score{score}{}
26 
27 bool compare_Grade(const Grade& a,const Grade& b){
28     if(a.get_major()<b.get_major())return true;
29     if(a.get_major()>b.get_major())return false;
30     return a.get_score()>b.get_score();
31 }
32  
33 int main(){
34     vector<Grade>grades;
35     ifstream in("data5.txt");
36     if(in.is_open()){
37         string line;
38         getline(in,line);
39          
40         string no,name,major;
41         int score;
42         while(in>>no>>name>>major>>score){
43             grades.push_back(Grade(no,name,major,score));
44         }
45         in.close();
46     }else{
47         cout<<"fail to open file to read\n";
48         return 1; 
49     }
50     
51     sort(grades.begin(),grades.end(),compare_Grade);
52     
53     ofstream out("ans5.txt");
54     if(out.is_open()){
55         for(const Grade&g:grades){
56             out<<g.get_no()<<" "<<g.get_name()<<" "<<g.get_major()<<" "<<g.get_score()<<endl;
57             cout<<g.get_no()<<" "<<g.get_name()<<" "<<g.get_major()<<" "<<g.get_score()<<endl;
58         }
59     }else{
60         cout<<"fail to open file to write\n";
61         return 1;
62     }
63     return 0;
64 }
65  
task5.cpp

 

 

posted @ 2024-12-16 15:59  _mao  阅读(6)  评论(0编辑  收藏  举报