实验任务4:
Vector.hpp源码:
1 #pragma once 2 #include <iostream> 3 #include <stdexcept> 4 using namespace std; 5 6 template <typename T> 7 class Vector { 8 public: 9 Vector(int n) :size(n) 10 { 11 if (n < 0) 12 throw length_error("Vector constructor: negtive size"); 13 pointer = new T[size]; 14 } 15 Vector(int n, const T& value) :size(n) 16 { 17 if (n < 0) 18 throw length_error("Vector constructor: negtive size"); 19 pointer = new T[size]; 20 for (int i = 0; i < size; i++) 21 pointer[i] = value; 22 } 23 Vector(const Vector<T>& other) :size(other.size), pointer(new T[size]) 24 { 25 for (int i = 0; i < size; i++) 26 pointer[i] = other.pointer[i]; 27 } 28 ~Vector() = default; 29 30 int get_size() const { return size; } 31 T& at(int index) const 32 { 33 if (index < 0 || index >= size) 34 throw out_of_range("vector:index out of range"); 35 return pointer[index]; 36 } 37 T& operator[](int index) const 38 { 39 if (index < 0 || index >= size) 40 throw out_of_range("vector:index out of range"); 41 return pointer[index]; 42 } 43 44 friend void output(const Vector<T>& vec) 45 { 46 for (int i = 0; i < vec.get_size(); i++) 47 cout << vec.at(i) << ","; 48 cout << "\b\b\n"; 49 } 50 private: 51 int size; 52 T* pointer; 53 };
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) { 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
运行结果:
实验任务5:
task5.cpp源码:
1 #include <iostream> 2 #include <fstream> 3 #include <vector> 4 #include <algorithm> 5 #include <iomanip> 6 #include <string> 7 8 using namespace std; 9 10 //定义学生类 11 class student { 12 string id; 13 string name; 14 string major; 15 int score; 16 public: 17 student(string id, string name, string major, int score) : id(id), name(name), major(major), score(score) {} 18 ~student()=default; 19 20 string get_id() {return id;} 21 string get_name() {return name;} 22 string get_major() {return major;} 23 int get_score() {return score;} 24 }; 25 //排序 26 bool compare(student a, student b) { 27 if(a.get_major() < b.get_major()) 28 return true; 29 else if(a.get_major() == b.get_major()) 30 return a.get_score() > b.get_score(); 31 return false; 32 33 } 34 int main() { 35 //读入数据到file 36 ifstream file("data5.txt"); 37 if (!file.is_open()) 38 { 39 cout << "Failed to open file." << endl; 40 return 0; 41 } 42 //跳过标题 43 string line; 44 getline(file, line); 45 //读入数据到stu 46 string id, name, major; 47 int score; 48 vector<student> stu; 49 while (file >> id >> name >> major >> score) 50 { 51 student s(id, name, major, score); 52 stu.push_back(s); 53 } 54 //关掉文件 55 file.close(); 56 //排序 57 sort(stu.begin(), stu.end(), compare); 58 //输出到文件result5.txt 59 ofstream out("result5.txt"); 60 if (!out.is_open()) 61 { 62 cout << "Failed to open output file." << endl; 63 return 0; 64 } 65 //在控制台输出结果并输出到文件 66 for (auto s : stu) 67 { 68 cout << left << setw(10) << s.get_id() << setw(10) << s.get_name() << setw(10) << s.get_major() << setw(10) << s.get_score() << endl; 69 out << left << setw(10) << s.get_id() << setw(10) << s.get_name() << setw(10) << s.get_major() << setw(10) << s.get_score() << endl; 70 } 71 //关掉文件 72 out.close(); 73 74 return 0; 75 }
运行结果:
标签:文件,major,get,int,index,Vector,实验,模板,size From: https://www.cnblogs.com/wuxin404/p/18609566