任务4
源码:
1 #pragma once 2 3 #include <iostream> 4 #include <stdexcept> 5 6 using namespace std; 7 8 // 模板类声明 9 template<typename T> 10 class Vector { 11 public: 12 Vector(int n): size{n} { 13 if(n < 0) 14 throw length_error("Vector constructor: negtive size"); 15 ptr = new T [size]; 16 } 17 18 Vector(int n, T value): size{n} { 19 if(n < 0) 20 throw length_error("Vector constructor: negtive size"); 21 ptr = new T[size]; 22 for(int i = 0; i < size; i++) 23 ptr[i] = value; 24 } 25 26 Vector(const Vector<T> &v): size{v.size}, ptr{new T[size]} { 27 for(int i = 0; i < size; i++) 28 ptr[i] = v.ptr[i]; 29 } 30 31 ~Vector() { 32 delete [] ptr; 33 } 34 35 int get_size() const { 36 return size; 37 } 38 39 T& at(int index) const { 40 if(index < 0 || index >= size) 41 throw out_of_range("Vector:: at(): index out of range"); 42 return ptr[index]; 43 } 44 45 T& operator[](int index) const{ 46 if(index < 0 || index >= size) 47 throw out_of_range("Vector:: at(): index out of range"); 48 return ptr[index]; 49 } 50 51 private: 52 int size; 53 T *ptr; 54 }; 55 56 template<typename T> 57 void output(const Vector<T> &v) { 58 for(int i = 0; i < v.get_size(); i++) 59 cout << v.at(i) << ", "; 60 cout << "\b\b \n"; 61 }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
运行测试截图:
任务5
源码:
1 #include <iostream> 2 #include <fstream> 3 #include <iomanip> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 8 using std::string; 9 using std::vector; 10 using std::ostream; 11 using std::istream; 12 using std::cout; 13 using std::endl; 14 using std::setw; 15 using std::setprecision; 16 using std::setiosflags; 17 using std::ios_base; 18 19 20 class Student { 21 public: 22 Student() {} 23 ~Student() {} 24 25 string get_major() const { return major; } 26 int get_score() const { return score; } 27 28 friend ostream& operator<<(ostream &out, const Student &s); 29 friend istream& operator>>(istream &in, Student &s); 30 31 private: 32 string no; // 学号 33 string name; // 姓名 34 string major; // 专业 35 int score; // 分数 36 }; 37 38 // 友元函数实现 39 // 重载流插入运算符<< 40 ostream& operator<<(ostream &out, const Student &s) { 41 out << setiosflags(ios_base::left); 42 out << setw(15) << s.no 43 << setw(15) << s.name 44 << setw(15) << s.major 45 << setw(5) << s.score; 46 47 return out; 48 } 49 50 // 重载流提取运算符>> 51 istream& operator>>(istream &in, Student &s) { 52 in >> s.no >> s.name >> s.major >> s.score; 53 54 return in; 55 } 56 57 58 // 排序函数 59 bool compare_by_major(const Student &s1, const Student &s2) { 60 if(s1.get_major() < s2.get_major()) 61 return true; 62 63 if(s1.get_major() == s2.get_major()) 64 return s1.get_score() > s2.get_score(); 65 66 return false; 67 } 68 69 // 把vector<Student>对象中的元素插入到输出流out 70 void output(ostream &out, const vector<Student> &v) { 71 for(auto &i: v) 72 out << i << endl; 73 } 74 75 76 // 把vector<Student>对象中的元素写到filename文件中 77 void save(const string &filename, vector<Student> &v) { 78 using std::ofstream; 79 80 ofstream out(filename); 81 if(!out.is_open()) { 82 cout << "fail to open file to write\n"; 83 return; 84 } 85 86 output(out, v); 87 out.close(); 88 } 89 90 // 从文件filename读取学生信息到vector<Student>对象 91 void load(const string &filename, vector<Student> &v) { 92 using std::ifstream; 93 94 ifstream in(filename); 95 if(!in.is_open()) { 96 cout << "fail to open file to read\n"; 97 return; 98 } 99 100 string title_line; 101 getline(in, title_line); // 跳过标题行 102 103 Student t; 104 while(in >> t) 105 v.push_back(t); 106 107 in.close(); 108 } 109 110 111 void test() { 112 using namespace std; 113 114 vector<Student> v; 115 116 load("data5.txt", v); // 从文件加载选手信息到对象v 117 sort(v.begin(), v.end(), compare_by_major); // 按解题情况排序 118 output(cout, v); // 输出对象v中信息到屏幕 119 save("ans5.txt", v); // 把对象v中选手信息保存到文件 120 } 121 122 int main() { 123 test(); 124 }task5.cpp
运行测试截图:
总结:
文件读写过程中可能由于文本文档格式不同,出现乱码现象,修改格式即可
标签:std,文件,const,int,Vector,实验,using,模板,size From: https://www.cnblogs.com/c-929/p/18612799