task1:
1 //Complex.hpp 2 #pragma once 3 4 #include <iostream> 5 #include <stdexcept> 6 7 // 声明 8 //////////////////////////////////////////////////// 9 // 复数模板类声明 10 template<typename T> 11 class Complex { 12 public: 13 Complex(T r = 0, T i = 0); 14 Complex(const Complex<T> &c); 15 16 T get_real() const; 17 T get_imag() const; 18 19 // 重载+=为成员函数 20 Complex<T>& operator+=(const Complex<T> &c); 21 22 // 重载<<、>>为友元函数 23 template<typename T1> 24 friend std::ostream& operator<<(std::ostream &out, const Complex<T1> &c); 25 26 template<typename T1> 27 friend std::istream& operator>>(std::istream &in, Complex<T1> &c); 28 29 private: 30 T real, imag; 31 }; 32 33 // 普通函数声明 34 // 重载+用于Complex类型 35 template<typename T> 36 Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2); 37 38 // 重载==用于Complex类型 39 template<typename T> 40 bool operator==(const Complex<T> &c1, const Complex<T> &c2); 41 42 43 // 实现 44 //////////////////////////////////////////////////// 45 // 成员函数模板实现 46 template<typename T> 47 Complex<T>::Complex(T r, T i): real{r}, imag{i} { 48 } 49 50 template<typename T> 51 Complex<T>::Complex(const Complex<T> &c): real{c.real}, imag{c.imag} { 52 } 53 54 template<typename T> 55 T Complex<T>::get_real() const { 56 return real; 57 } 58 59 template<typename T> 60 T Complex<T>::get_imag() const { 61 return imag; 62 } 63 64 // 重载+=为成员函数 65 template<typename T> 66 Complex<T>& Complex<T>::operator+=(const Complex<T> &c) { 67 real += c.real; 68 imag += c.imag; 69 70 return *this; 71 } 72 73 /////////////////////////////////////// 74 // 友元函数模板实现 75 template<typename T1> 76 std::ostream& operator<<(std::ostream &out, const Complex<T1> &c) { 77 if(c.imag >= 0) 78 out << c.real << " + " << c.imag << "i"; 79 else 80 out << c.real << " - " << -c.imag << "i"; 81 82 return out; 83 } 84 85 template<typename T1> 86 std::istream& operator>>(std::istream &in, Complex<T1> &c) { 87 in >> c.real >> c.imag; 88 89 return in; 90 } 91 92 /////////////////////////////////////// 93 // 普通函数模板实现 94 // 重载+用于Complex类型 95 template<typename T> 96 Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2) { 97 return Complex<T>(c1.get_real()+c2.get_real(), 98 c1.get_imag()+c2.get_imag()); 99 } 100 101 // 重载==用于Complex类型 102 template<typename T> 103 bool operator==(const Complex<T> &c1, const Complex<T> &c2) { 104 return c1.get_real() == c2.get_real() && 105 c1.get_imag() && c2.get_imag(); 106 }View Code
1 //task1.cpp 2 #include "Complex.hpp" 3 #include <iostream> 4 #include <fstream> 5 #include <stdexcept> 6 7 void test1(); 8 void test2(); 9 10 int main() { 11 using namespace std; 12 13 cout << "测试1: 复数模板类测试" << endl; 14 test1(); 15 16 cout << "\n测试2: 文件I/O测试" << endl; 17 test2(); 18 } 19 20 void test1() { 21 using namespace std; 22 23 Complex<double> c1{3.5, 2}, c2; 24 cout << "Enter c2: "; 25 cin >> c2; 26 cout << "c1 = " << c1 << endl; 27 cout << "c2 = " << c2 << endl; 28 cout << "c1 == c2: " << boolalpha << (c1 == c2) << endl; 29 30 cout << "c1 + c2 = " << c1 + c2 << endl; 31 c1 += c2; 32 cout << "c1.real = " << c1.get_real() << endl; 33 cout << "c1.imag = " << c1.get_imag() << endl; 34 35 cout << "c1 == c2: " << boolalpha << (c1 == c2) << endl; 36 } 37 38 void test2() { 39 using namespace std; 40 41 Complex<int> c1{1, 2}, c2{9, -7}; 42 ofstream out("ans.txt"); 43 if(!out.is_open()) { 44 cout << "fail to open file ans.txt to write\n"; 45 return; 46 } 47 48 out << "c1 = " << c1 << endl; 49 out << "c2 = " << c2 << endl; 50 out << "c1 + c2 = " << c1 + c2 << endl; 51 out << "(c1 == c2) = " << boolalpha << (c1 == c2) << endl; 52 53 out.close(); 54 cout << "测试ok!" << endl; 55 }View Code
代码:
截图:
task 2:
代码:
1 //Contestent.hpp 2 #pragma once 3 4 #include <iostream> 5 #include <iomanip> 6 #include <string> 7 8 using std::string; 9 using std::ostream; 10 using std::istream; 11 using std::setw; 12 using std::setprecision; 13 using std::setiosflags; 14 using std::ios_base; 15 16 // Contestant类声明 17 class Contestant { 18 public: 19 Contestant() = default; 20 ~Contestant() = default; 21 22 int get_num() const { return num; } 23 float get_time_usage() const { return time_usage; } 24 25 friend ostream& operator<<(ostream &out, const Contestant &c); 26 friend istream& operator>>(istream &in, Contestant &c); 27 28 private: 29 string no; // 学号 30 string name; // 姓名 31 string major; // 专业 32 int num; // 解题数 33 float time_usage; // 总用时 34 }; 35 36 // 友元函数实现 37 // 重载流插入运算符<< 38 ostream& operator<<(ostream &out, const Contestant &c) { 39 out << setiosflags(ios_base::left); 40 out << setw(15) << c.no 41 << setw(15) << c.name 42 << setw(15) << c.major 43 << setw(5) << c.num 44 << setprecision(2) << c.time_usage; 45 46 return out; 47 } 48 49 // 重载流提取运算符>> 50 istream& operator>>(istream &in, Contestant &c) { 51 in >> c.no >> c.name >> c.major >> c.num >> c.time_usage; 52 53 return in; 54 }View Code
1 //utils.hpp 2 #include "Contestant.hpp" 3 #include <fstream> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 8 // 排序函数 9 // 按解题数比较,解题数相同的情况下,按总用时比较,总用时越少,排名越靠前 10 bool compare_by_solutionInfo(const Contestant &c1, const Contestant &c2) { 11 if(c1.get_num() > c2.get_num()) 12 return true; 13 14 if(c1.get_num() == c2.get_num()) 15 return c1.get_time_usage() < c2.get_time_usage(); 16 17 return false; 18 } 19 20 // 把vector<Constestant>对象中的元素插入到输出流out 21 void output(std::ostream &out, const std::vector<Contestant> &v) { 22 for(auto &i: v) 23 out << i << std::endl; 24 } 25 26 27 // 把vector<Contestant>对象中的元素写到filename文件中 28 void save(const std::string &filename, std::vector<Contestant> &v) { 29 using std::ofstream; 30 31 ofstream out(filename); 32 if(!out.is_open()) { 33 std::cout << "fail to open file to write\n"; 34 return; 35 } 36 37 output(out, v); 38 out.close(); 39 } 40 41 // 从文件filename读取参赛选手信息到vector<Contestant>对象 42 void load(const std::string &filename, std::vector<Contestant> &v) { 43 using std::ifstream; 44 45 ifstream in(filename); 46 if(!in.is_open()) { 47 std::cout << "fail to open file to read\n"; 48 return; 49 } 50 51 std::string title_line; 52 getline(in, title_line); // 跳过标题行 53 54 int first_column; 55 Contestant t; 56 while(in >> first_column >> t) 57 v.push_back(t); 58 59 in.close(); 60 }View Code
//task2.cpp #include "Contestant.hpp" #include "utils.hpp" #include <iostream> #include <vector> #include <algorithm> void test() { using namespace std; vector<Contestant> v; load("data2.txt", v); // 从文件加载选手信息到对象v sort(v.begin(), v.end(), compare_by_solutionInfo); // 按解题情况排序 output(cout, v); // 输出对象v中信息到屏幕 save("ans.txt", v); // 把对象v中选手信息保存到文件 } int main() { test(); }
截图:
task3:
代码:
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 }View Code
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 }View Code
截图:
task4:
代码:
1 //task4.cpp 2 #include <iostream> 3 #include "Vector.hpp" 4 5 void test1() { 6 using namespace std; 7 8 int n; 9 cout << "Enter n: "; 10 cin >> n; 11 12 Vector<double> x1(n); 13 for(auto i = 0; i < n; ++i) 14 x1.at(i) = i * 0.7; 15 16 cout << "x1: "; output(x1); 17 18 Vector<int> x2(n, 42); 19 const Vector<int> x3(x2); 20 21 cout << "x2: "; output(x2); 22 cout << "x3: "; output(x3); 23 24 x2.at(0) = 77; 25 x2.at(1) = 777; 26 cout << "x2: "; output(x2); 27 cout << "x3: "; output(x3); 28 } 29 30 void test2() { 31 using namespace std; 32 33 int n, index; 34 while(cout << "Enter n and index: ", cin >> n >> index) { 35 try { 36 Vector<int> v(n, n); 37 v.at(index) = -999; 38 cout << "v: "; output(v); 39 } 40 catch (const exception &e) { 41 cout << e.what() << endl; 42 } 43 } 44 } 45 46 int main() { 47 cout << "测试1: 模板类接口测试\n"; 48 test1(); 49 50 cout << "\n测试2: 模板类异常处理测试\n"; 51 test2(); 52 }View Code
//vector.hpp #include<iostream> #include<stdexcept> using namespace std; template<typename T> class Vector{ private: int size; T *ptr; public: Vector(int s):size(s){ if(s<0) throw length_error("Vector constructor:negative size"); else ptr=new T[size]; } Vector(int s,T value):size(s){ if(s<0) throw length_error("Vector constructor:negative size"); else { ptr=new T[size]; for(int i=0;i<s;i++) *(ptr+i)=value; } } Vector(const Vector<T> &v){ size=v.size; ptr=new T[size]; for(int i=0;i<size;i++) *(ptr+i)=v.ptr[i]; } ~Vector(){delete []ptr;} int get_size(); T& at(int index); T& operator[](int index); friend void output(Vector<T> v){ for(int i=0;i<v.size;i++) cout<<v.ptr[i]<<","; cout<<endl; } }; template<typename T> int Vector<T>::get_size(){return size;} template<typename T> T& Vector<T>::at(int index){ if (index<0||index>=size) throw out_of_range("Vector:index out of range"); return *(ptr+index); } template<typename T> T& Vector<T>::operator[](int index){ if (index<0||index>=size) throw out_of_range("Vector:index out of range"); return *(ptr+index); }
截图:
task5:
代码:
1 #include"student.hpp" 2 #include<vector> 3 #include <fstream> 4 #include <iostream> 5 #include<string> 6 #include <algorithm> 7 using namespace std; 8 9 void output(ostream &out,vector<student>& v) { 10 for(auto& i:v) 11 out<<i<<endl; 12 } 13 14 15 void save(const string &filename,vector<student> &v) { 16 using std::ofstream; 17 18 ofstream out(filename); 19 if(!out.is_open()) { 20 std::cout << "fail to open file to write\n"; 21 exit(0); 22 } 23 24 output(out, v); 25 out.close(); 26 } 27 28 bool compare_by_score(student &s1,student &s2) { 29 if(s1.get_score() > s2.get_score()) 30 return true; 31 32 if(s1.get_score() == s2.get_score()) 33 return s1.get_major() < s2.get_major(); 34 35 return false; 36 } 37 38 void load(const string &filename,vector<student> &v) { 39 using std::ifstream; 40 41 ifstream in(filename); 42 if(!in.is_open()) { 43 std::cout << "fail to open file to read\n"; 44 exit(0); 45 } 46 47 std::string title_line; 48 getline(in, title_line); 49 50 student t; 51 while(in >> t) 52 v.push_back(t); 53 54 in.close(); 55 } 56 57 int main(){ 58 vector<student> v; 59 load("data5.txt", v); 60 sort(v.begin(), v.end(), compare_by_score); 61 output(cout, v); 62 save("ans5.txt", v); 63 return 0; 64 }View Code
1 #pragma once 2 #include <iomanip> 3 #include <iostream> 4 #include<string> 5 #include<fstream> 6 #include<algorithm> 7 using namespace std; 8 class student{ 9 private: 10 int number; 11 string name; 12 string major; 13 int score; 14 public: 15 student()=default; 16 ~student()=default; 17 int get_score(){return score;} 18 string get_major(){return major;} 19 friend ostream& operator<<(ostream &out,student &s); 20 friend istream& operator>>(istream &in,student &s); 21 }; 22 ostream& operator<<(ostream &out,student &s){ 23 out<<left; 24 out<<setw(15)<<s.number<<setw(15)<<s.name<<setw(15)<<s.major<<setw(15)<<s.score; 25 return out; 26 } 27 istream& operator>>(istream &in,student &s){ 28 in>>s.number>>s.name>>s.major>>s.score; 29 return in; 30 }View Code
截图:
标签:std,文件,const,get,Complex,实验,return,include,模板 From: https://www.cnblogs.com/nofear77/p/18621406