实验六
任务一
代码
1 #pragma once 2 3 #include <iostream> 4 #include <stdexcept> 5 6 // 声明 7 //////////////////////////////////////////////////// 8 // 复数模板类声明 9 template<typename T> 10 class Complex { 11 public: 12 Complex(T r = 0, T i = 0); 13 Complex(const Complex<T> &c); 14 15 T get_real() const; 16 T get_imag() const; 17 18 // 重载+=为成员函数 19 Complex<T>& operator+=(const Complex<T> &c); 20 21 // 重载<<、>>为友元函数 22 template<typename T1> 23 friend std::ostream& operator<<(std::ostream &out, const Complex<T1> &c); 24 25 template<typename T1> 26 friend std::istream& operator>>(std::istream &in, Complex<T1> &c); 27 28 private: 29 T real, imag; 30 }; 31 32 // 普通函数声明 33 // 重载+用于Complex类型 34 template<typename T> 35 Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2); 36 37 // 重载==用于Complex类型 38 template<typename T> 39 bool operator==(const Complex<T> &c1, const Complex<T> &c2); 40 41 42 // 实现 43 //////////////////////////////////////////////////// 44 // 成员函数模板实现 45 template<typename T> 46 Complex<T>::Complex(T r, T i): real{r}, imag{i} { 47 } 48 49 template<typename T> 50 Complex<T>::Complex(const Complex<T> &c): real{c.real}, imag{c.imag} { 51 } 52 53 template<typename T> 54 T Complex<T>::get_real() const { 55 return real; 56 } 57 58 template<typename T> 59 T Complex<T>::get_imag() const { 60 return imag; 61 } 62 63 // 重载+=为成员函数 64 template<typename T> 65 Complex<T>& Complex<T>::operator+=(const Complex<T> &c) { 66 real += c.real; 67 imag += c.imag; 68 69 return *this; 70 } 71 72 /////////////////////////////////////// 73 // 友元函数模板实现 74 template<typename T1> 75 std::ostream& operator<<(std::ostream &out, const Complex<T1> &c) { 76 if(c.imag >= 0) 77 out << c.real << " + " << c.imag << "i"; 78 else 79 out << c.real << " - " << -c.imag << "i"; 80 81 return out; 82 } 83 84 template<typename T1> 85 std::istream& operator>>(std::istream &in, Complex<T1> &c) { 86 in >> c.real >> c.imag; 87 88 return in; 89 } 90 91 /////////////////////////////////////// 92 // 普通函数模板实现 93 // 重载+用于Complex类型 94 template<typename T> 95 Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2) { 96 return Complex<T>(c1.get_real()+c2.get_real(), 97 c1.get_imag()+c2.get_imag()); 98 } 99 100 // 重载==用于Complex类型 101 template<typename T> 102 bool operator==(const Complex<T> &c1, const Complex<T> &c2) { 103 return c1.get_real() == c2.get_real() && 104 c1.get_imag() && c2.get_imag(); 105 }
1 #include "Complex.hpp" 2 #include <iostream> 3 #include <fstream> 4 #include <stdexcept> 5 6 void test1(); 7 void test2(); 8 9 int main() { 10 using namespace std; 11 12 cout << "测试1: 复数模板类测试" << endl; 13 test1(); 14 15 cout << "\n测试2: 文件I/O测试" << endl; 16 test2(); 17 } 18 19 void test1() { 20 using namespace std; 21 22 Complex<double> c1{3.5, 2}, c2; 23 cout << "Enter c2: "; 24 cin >> c2; 25 cout << "c1 = " << c1 << endl; 26 cout << "c2 = " << c2 << endl; 27 cout << "c1 == c2: " << boolalpha << (c1 == c2) << endl; 28 29 cout << "c1 + c2 = " << c1 + c2 << endl; 30 c1 += c2; 31 cout << "c1.real = " << c1.get_real() << endl; 32 cout << "c1.imag = " << c1.get_imag() << endl; 33 34 cout << "c1 == c2: " << boolalpha << (c1 == c2) << endl; 35 } 36 37 void test2() { 38 using namespace std; 39 40 Complex<int> c1{1, 2}, c2{9, -7}; 41 ofstream out("ans.txt"); 42 if(!out.is_open()) { 43 cout << "fail to open file ans.txt to write\n"; 44 return; 45 } 46 47 out << "c1 = " << c1 << endl; 48 out << "c2 = " << c2 << endl; 49 out << "c1 + c2 = " << c1 + c2 << endl; 50 out << "(c1 == c2) = " << boolalpha << (c1 == c2) << endl; 51 52 out.close(); 53 cout << "测试ok!" << endl; 54 }
截图
任务二
代码
Contetant.hpp
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 // Contestant类声明 16 class Contestant { 17 public: 18 Contestant() = default; 19 ~Contestant() = default; 20 21 int get_num() const { return num; } 22 float get_time_usage() const { return time_usage; } 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 num; // 解题数 32 float time_usage; // 总用时 33 }; 34 35 // 友元函数实现 36 // 重载流插入运算符<< 37 ostream& operator<<(ostream &out, const Contestant &c) { 38 out << setiosflags(ios_base::left); 39 out << setw(15) << c.no 40 << setw(15) << c.name 41 << setw(15) << c.major 42 << setw(5) << c.num 43 << setprecision(2) << c.time_usage; 44 45 return out; 46 } 47 48 // 重载流提取运算符>> 49 istream& operator>>(istream &in, Contestant &c) { 50 in >> c.no >> c.name >> c.major >> c.num >> c.time_usage; 51 52 return in; 53 }
1 #include "Contestant.hpp" 2 #include <fstream> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 7 // 排序函数 8 // 按解题数比较,解题数相同的情况下,按总用时比较,总用时越少,排名越靠前 9 bool compare_by_solutionInfo(const Contestant &c1, const Contestant &c2) { 10 if(c1.get_num() > c2.get_num()) 11 return true; 12 13 if(c1.get_num() == c2.get_num()) 14 return c1.get_time_usage() < c2.get_time_usage(); 15 16 return false; 17 } 18 19 // 把vector<Constestant>对象中的元素插入到输出流out 20 void output(std::ostream &out, const std::vector<Contestant> &v) { 21 for(auto &i: v) 22 out << i << std::endl; 23 } 24 25 26 // 把vector<Contestant>对象中的元素写到filename文件中 27 void save(const std::string &filename, std::vector<Contestant> &v) { 28 using std::ofstream; 29 30 ofstream out(filename); 31 if(!out.is_open()) { 32 std::cout << "fail to open file to write\n"; 33 return; 34 } 35 36 output(out, v); 37 out.close(); 38 } 39 40 // 从文件filename读取参赛选手信息到vector<Contestant>对象 41 void load(const std::string &filename, std::vector<Contestant> &v) { 42 using std::ifstream; 43 44 ifstream in(filename); 45 if(!in.is_open()) { 46 std::cout << "fail to open file to read\n"; 47 return; 48 } 49 50 std::string title_line; 51 getline(in, title_line); // 跳过标题行 52 53 int first_column; 54 Contestant t; 55 while(in >> first_column >> t) 56 v.push_back(t); 57 58 in.close(); 59 }
1 #include "Contestant.hpp" 2 #include "utils.hpp" 3 #include <iostream> 4 #include <vector> 5 #include <algorithm> 6 7 8 void test() { 9 using namespace std; 10 11 vector<Contestant> v; 12 13 load("data2.txt", v); // 从文件加载选手信息到对象v 14 sort(v.begin(), v.end(), compare_by_solutionInfo); // 按解题情况排序 15 output(cout, v); // 输出对象v中信息到屏幕 16 save("ans.txt", v); // 把对象v中选手信息保存到文件 17 } 18 19 int main() { 20 test(); 21 }
截图
任务三
代码
1 #include "Triangle.hpp" 2 #include <iostream> 3 #include <fstream> 4 5 void test() { 6 using namespace std; 7 8 cout << "从文件读入三角形三边边长,计算面积" << endl; 9 10 ifstream in("data3.txt"); 11 if(!in.is_open()) { 12 cout << "fail to open file to read\n"; 13 return; 14 } 15 16 double a,b,c; 17 do { 18 cout << "三角形边长: "; 19 in >> a >> b >> c; 20 cout << a << " " << b << " " << c << endl; 21 22 try { 23 Triangle t(a, b, c); 24 cout << "三角形面积: " << t.area() << endl << endl; 25 }catch(const exception &e) { 26 cout << "error: " << e.what() << endl << endl; 27 } 28 29 if(in.peek() == EOF) 30 break; 31 } while(1); 32 33 in.close(); 34 } 35 36 int main() { 37 test(); 38 }
1 #include <iostream> 2 #include <stdexcept> 3 #include <cmath> 4 5 using namespace std; 6 7 class Triangle { 8 public: 9 Triangle(double s1, double s2, double s3); 10 ~Triangle() = default; 11 12 double area() const; 13 14 private: 15 double a, b, c; 16 }; 17 18 Triangle::Triangle(double s1, double s2, double s3): a{s1}, b{s2}, c{s3} { 19 if(a <= 0 || b <= 0 || c <= 0) 20 throw invalid_argument("边长出现负值"); 21 22 if(a+b <= c || b+c <= a || a+c <= b) 23 throw invalid_argument("不满足任意两边之和大于第三边"); 24 } 25 26 double Triangle::area() const { 27 double s = (a + b + c)/2; 28 return sqrt(s*(s-a)*(s-b)*(s-c)); 29 }
截图
任务四
代码
1 #pragma once 2 3 #include <iostream> 4 #include <stdexcept> 5 6 using namespace std; 7 8 template<typename T> 9 class Vector{ 10 public: 11 Vector(int n):len{n} { 12 if(n < 0) { 13 throw length_error("集合长度不能为负数"); 14 } 15 arr = new T[n]; 16 }; 17 Vector(int n,T val):len{n} { 18 if(n < 0) { 19 throw length_error("集合长度不能为负数"); 20 } 21 arr = new T[n]; 22 for(int i = 0;i < n;i++) { 23 arr[i] = val; 24 } 25 }; 26 Vector(Vector<T> &v):len{v.get_size()} { 27 arr = new T[len]; 28 for(int i = 0;i < len;i++) { 29 arr[i] = v.at(i); 30 } 31 } 32 33 ~Vector() { 34 delete[] arr; 35 } 36 37 int get_size() { 38 return len; 39 } 40 41 T& at(int i){ 42 if(i >= len) { 43 throw invalid_argument("下标越界"); 44 } 45 return *(arr+i); 46 } 47 48 T& at(int i) const{ 49 if(i >= len) { 50 throw invalid_argument("下标越界"); 51 } 52 return *(arr+i); 53 } 54 55 56 T& operator [](int i){ 57 if(i >= len) { 58 throw invalid_argument("下标越界"); 59 } 60 return arr[i]; 61 } 62 template<typename T1> 63 friend void output(Vector<T1> &v); 64 65 template<typename T1> 66 friend void output(const Vector<T1> &v); 67 68 private: 69 T* arr; 70 int len; 71 }; 72 73 template<typename T> 74 void output(Vector<T> &v) { 75 cout << "{"; 76 for(int i = 0;i < v.len;i++) { 77 if(i == v.len - 1) { 78 cout << v.at(i); 79 } 80 cout << v.at(i) << ","; 81 } 82 cout << "}" << endl; 83 } 84 85 template<typename T> 86 void output(const Vector<T> &v) { 87 cout << "{"; 88 for(int i = 0;i < v.len;i++) { 89 if(i == v.len - 1) { 90 cout << v.at(i); 91 }else{ 92 cout << v.at(i) << ","; 93 } 94 } 95 cout << "}" << endl; 96 }
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 }
截图
任务五
代码
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <vector> 5 #include <iomanip> 6 #include <ctime> 7 #include <fstream> 8 using namespace std; 9 10 class Stu{ 11 public: 12 string id; 13 string name; 14 string major; 15 int score; 16 Stu(){ 17 } 18 Stu(string id,string name,string major,int score) { 19 this->id = id; 20 this->name = name; 21 this->major = major; 22 this->score = score; 23 } 24 }; 25 26 int main() { 27 ifstream in("data5.txt"); 28 29 if(!in.is_open()) { 30 cout << "fail to open file" << endl; 31 } 32 33 string title; 34 getline(in,title); 35 36 Stu s; 37 vector<Stu> vect; 38 39 while(in >> s.id >> s.name >> s.major >> s.score) { 40 // cout << s.id << " " << s.name << " " << s.major << " " << s.score << endl; 41 vect.push_back(s); 42 } 43 Stu temp; 44 for(int i = 0;i < vect.size() - 1;i++) { 45 for(int j = 0;j < vect.size() - 1 - i;j++) { 46 if(vect[j].major > vect[j+1].major) { 47 temp = vect[j]; 48 vect[j] = vect[j+1]; 49 vect[j+1] = temp; 50 } else if(vect[j].major == vect[j+1].major) { 51 if(vect[j].score < vect[j+1].score) { 52 temp = vect[j]; 53 vect[j] = vect[j+1]; 54 vect[j+1] = temp; 55 } 56 } 57 } 58 } 59 60 for(int i = 0;i < vect.size();i++) { 61 cout << vect[i].id << "\t" << vect[i].name << "\t" << vect[i].major << "\t" << vect[i].score << endl; 62 } 63 64 ofstream out("data5.txt"); 65 66 out << title << endl; 67 for(int i = 0;i < vect.size();i++) { 68 out << vect[i].id << "\t" << vect[i].name << "\t" << vect[i].major << "\t" << vect[i].score << endl; 69 } 70 71 in.close(); 72 out.close(); 73 74 75 return 0; 76 }
截图