cpp 实验6
1 vector.h 2 #pragma once 3 #include<iostream> 4 #include<stdexcept> 5 using namespace std; 6 template<typename T> 7 class Vector { 8 private: 9 int size; 10 T* ptr; 11 public: 12 Vector(int size, T value =T() ) :size{ size } { 13 if (size < 0) { 14 throw length_error("negative size"); 15 } 16 ptr = new T[size]; 17 for (int i = 0; i < size; i++) { 18 ptr[i] = value; 19 } 20 } 21 int get_size()const { return size; } 22 T& at(int index)const { 23 if (index < 0 || index >= size) throw out_of_range("index out of range"); 24 return ptr[index]; 25 } 26 T& at(int index) { 27 return static_cast<const Vector<T>*>(this)->at(index); 28 } 29 T& operator[](int index)const { 30 if (index < 0 || index >= size) throw out_of_range("index out of range"); 31 return ptr[index]; 32 } 33 T& operator[](int index) { 34 return static_cast<const Vector<T>*>(this)->operator[](index); 35 } 36 }; 37 template<typename T1> 38 void output(const Vector<T1>& v) { 39 for (int i = 0; i < v.get_size();i++) { 40 cout << v[i]<<", "; 41 } 42 cout << endl; 43 } 44 45 46 47 48 task4 cpp 49 #include <iostream> 50 #include "Vector.h" 51 void test1() { 52 int n; 53 cout << "Enter n: "; 54 cin >> n; 55 56 Vector<double> x1(n); 57 for (auto i = 0; i < n; ++i) 58 x1.at(i) = i * 0.7; 59 cout << "x1: "; output(x1); 60 Vector<int> x2(n, 42); 61 const Vector<int> x3(x2); 62 cout << "x2: "; output(x2); 63 cout << "x3: "; output(x3); 64 x2.at(0) = 77; 65 x2.at(1) = 777; 66 cout << "x2: "; output(x2); 67 cout << "x3: "; output(x3); 68 } 69 void test2() { 70 using namespace std; 71 int n, index; 72 while (cout << "Enter n and index: ", cin >> n >> index) { 73 try { 74 Vector<int> v(n, n); 75 v.at(index) = -999; 76 cout << "v: "; output(v); 77 } 78 catch (const exception& e) { 79 cout << e.what() << endl; 80 } 81 } 82 } 83 int main() { 84 cout << "测试1: 模板类接口测试\n"; 85 test1(); 86 cout << "\n测试2: 模板类异常处理测试\n"; 87 test2(); 88 }
1 #include<iostream> 2 #include<fstream> 3 #include<algorithm> 4 #include<vector> 5 #include<iomanip> 6 using namespace std; 7 struct stu 8 { 9 string no; 10 string name; 11 string major; 12 int score; 13 stu(string no,string name,string major,int score):no{no},name{name},major{major},score{score}{} 14 }; 15 bool compare(stu x, stu y) { 16 if(x.major!=y.major) 17 return x.major < y.major; 18 else{ 19 return x.score > y.score; 20 } 21 } 22 int main() { 23 ifstream input("data5.txt"); 24 if (!input.is_open()) { 25 cout << "无法加载文件中的数据" << endl; 26 return 0; 27 } 28 string no, name, major; 29 int score; 30 vector<stu> v; 31 while (input >> no >> name >> major >> score) { 32 v.push_back(stu(no, name, major, score)); 33 } 34 input.close(); 35 sort(v.begin(), v.end(), compare); 36 ofstream output("ans5.txt"); 37 if (!output.is_open()) { 38 cout << "文件加载失败" << endl; 39 } 40 for (auto i : v) { 41 cout << setiosflags(ios_base::left); 42 cout << setw(10) << i.no << setw(10) << i.name << setw(10) << i.major << setw(10) << i.score << endl; 43 output << i.no << " " << i.name << " " << i.major << " " << i.score << endl; 44 } 45 output.close(); 46 return 0; 47 }