实验6
实验任务4:

 
1 #include<iostream> 2 #include<stdexcept> 3 4 using namespace std; 5 6 template<typename T> 7 class Vector{ 8 public: 9 Vector(int n); 10 Vector(int n,T value); 11 Vector(const Vector<T> &v); 12 ~Vector(); 13 14 int get_size() const{ 15 return size; 16 } 17 18 T& at(int i){ 19 if(i<0||i>=size){ 20 throw out_of_range("Vector::at():index out of size"); 21 } 22 23 return data[i]; 24 } 25 26 T& operator[](int i); 27 28 template<typename T1> 29 friend void output(const Vector<T1> &v); 30 31 private: 32 int size; 33 T *data; 34 35 }; 36 37 template<typename T> 38 Vector<T>::Vector(int n){ 39 if(n<0){ 40 throw length_error("Vector constructor: negative size"); 41 } 42 size=n; 43 data=new T[n]; 44 } 45 46 template<typename T> 47 Vector<T>::Vector(int n,T value){ 48 if(n<0){ 49 throw length_error("Vector constructor: negative size"); 50 } 51 size=n; 52 data=new T[n]; 53 for(int i=0;i<n;i++){ 54 data[i]=value; 55 } 56 } 57 58 template<typename T> 59 Vector<T>::Vector(const Vector<T> &v){ 60 size=v.size; 61 data=new T[size]; 62 for(int i=0;i<size;i++){ 63 data[i]=v.data[i]; 64 } 65 } 66 67 template<typename T> 68 Vector<T>::~Vector(){ 69 delete[] data; 70 } 71 72 template<typename T> 73 T& Vector<T>::operator[](int i){ 74 if(i<0||i>=size){ 75 throw out_of_range("Vector::at():index out of size"); 76 } 77 78 return data[i]; 79 } 80 81 template<typename T1> 82 void output(const Vector<T1> &v){ 83 for(int i=0;i<v.size;i++){ 84 cout<<v.data[i]; 85 if(i!=v.size-1){ 86 cout<<","; 87 } 88 } 89 cout<<endl; 90 } 91 #include <iostream> 92 #include "Vector.hpp" 93 94 void test1() { 95 using namespace std; 96 97 int n; 98 cout << "Enter n: "; 99 cin >> n; 100 101 Vector<double> x1(n); 102 for(auto i = 0; i < n; ++i) 103 x1.at(i) = i * 0.7; 104 105 cout << "x1: "; output(x1); 106 107 Vector<int> x2(n, 42); 108 const Vector<int> x3(x2); 109 110 cout << "x2: "; output(x2); 111 cout << "x3: "; output(x3); 112 113 x2.at(0) = 77; 114 x2.at(1) = 777; 115 cout << "x2: "; output(x2); 116 cout << "x3: "; output(x3); 117 } 118 119 void test2() { 120 using namespace std; 121 122 int n, index; 123 while(cout << "Enter n and index: ", cin >> n >> index) { 124 try { 125 Vector<int> v(n, n); 126 v.at(index) = -999; 127 cout << "v: "; output(v); 128 } 129 catch (const exception &e) { 130 cout << e.what() << endl; 131 } 132 } 133 } 134 135 int main() { 136 cout << "测试1: 模板类接口测试\n"; 137 test1(); 138 139 cout << "\n测试2: 模板类异常处理测试\n"; 140 test2(); 141 }
实验任务5:


 
1 #pragma once 2 3 #include <iostream> 4 #include <iomanip> 5 #include <string> 6 7 using std::string; 8 using std::ostream; 9 using std::istream; 10 using std::setw; 11 using std::setprecision; 12 using std::setiosflags; 13 using std::ios_base; 14 15 16 class Contestant { 17 public: 18 Contestant() = default; 19 ~Contestant() = default; 20 21 int get_score() const { return score; } 22 string get_major() const {return major ;} 23 24 friend ostream& operator<<(ostream &out, const Contestant &c); 25 friend istream& operator>>(istream &in, Contestant &c); 26 27 private: 28 string no; 29 string name; 30 string major; 31 int score; 32 33 }; 34 35 36 ostream& operator<<(ostream &out, const Contestant &c) { 37 out << setiosflags(ios_base::left); 38 out << setw(15) << c.no 39 << setw(15) << c.name 40 << setw(15) << c.major 41 << setw(5) << c.score; 42 43 44 return out; 45 } 46 47 48 istream& operator>>(istream &in, Contestant &c) { 49 in >> c.no >> c.name >> c.major >> c.score; 50 51 return in; 52 } 53 #include "Contestant.hpp" 54 #include <fstream> 55 #include <iostream> 56 #include <string> 57 #include <vector> 58 59 60 61 bool compare_by_solutionInfo(const Contestant &c1, const Contestant &c2) { 62 if(c1.get_major() < c2.get_major()){ 63 return true; 64 } 65 if(c1.get_major()==c2.get_major()){ 66 return c1.get_score()>c2.get_score(); 67 } 68 69 70 71 72 return false; 73 74 75 } 76 77 78 void output(std::ostream &out, const std::vector<Contestant> &v) { 79 for(auto &i: v) 80 out << i << std::endl; 81 } 82 83 84 85 void save(const std::string &filename, std::vector<Contestant> &v) { 86 using std::ofstream; 87 88 ofstream out(filename); 89 if(!out.is_open()) { 90 std::cout << "fail to open file to write\n"; 91 return; 92 } 93 94 output(out, v); 95 out.close(); 96 } 97 98 99 void load(const std::string &filename, std::vector<Contestant> &v) { 100 using std::ifstream; 101 102 ifstream in(filename); 103 if(!in.is_open()) { 104 std::cout << "fail to open file to read\n"; 105 return; 106 } 107 108 std::string title_line; 109 getline(in, title_line); 110 111 int first_column; 112 Contestant t; 113 while(in >> t) 114 v.push_back(t); 115 116 in.close(); 117 } 118 #include "Contestant.hpp" 119 #include "utils.hpp" 120 #include <iostream> 121 #include <vector> 122 #include <algorithm> 123 124 125 void test() { 126 using namespace std; 127 128 vector<Contestant> v; 129 130 load("data5.txt", v); 131 sort(v.begin(), v.end(), compare_by_solutionInfo); 132 output(cout, v); 133 save("ans5.txt", v); 134 } 135 136 int main() { 137 test(); 138 }
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号