Task1.cpp
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 }Complex.hpp
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 }task1.cpp
运行结果截图:
Task2.cpp
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 }Contestant.hpp
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 }utils.hpp
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 }task2.cpp
1 序号 学号 姓名 专业 解题数(道) 总用时(小时) 2 1 204942005 Jeny 未来专业1 5 5 3 2 204942302 Alex 未来专业2 8 4 4 3 204942059 Bob 未来专业2 8 5 5 4 204942111 Hellen 未来专业3 7 3.5 6 5 204942017 chappie 未来专业4 4 4.5 7 6 204942075 Shaw 未来专业5 8 4.5 8 7 204942076 Thomas 未来专业6 3 3.5 9 8 204942078 Jennie 未来专业7 5 5 10 9 204942079 Tibby 未来专业7 7 3.5 11 10 204942080 Vermont 未来专业8 7 4data2.txt
运行结果截图:
Task3.cpp
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 }task3.cpp
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 }Triangle.hpp
运行结果截图:
Task4.cpp
1 #pragma once 2 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 s):size(s){ 13 if(s<0) 14 throw length_error("Vector constructor:negative size"); 15 else 16 ptr=new T[size]; 17 } 18 Vector(int s,T value):size(s){ 19 if(s<0) 20 throw length_error("Vector constructor:negative size"); 21 else 22 { 23 ptr=new T[size]; 24 for(int i=0;i<s;i++) 25 *(ptr+i)=value; 26 } 27 } 28 Vector(const Vector<T> &v){ 29 size=v.size; 30 ptr=new T[size]; 31 for(int i=0;i<size;i++) 32 *(ptr+i)=v.ptr[i]; 33 } 34 ~Vector(){delete []ptr;} 35 int get_size(); 36 T& at(int index); 37 T& operator[](int index); 38 friend void output(Vector<T> v){ 39 for(int i=0;i<v.size;i++) 40 cout<<v.ptr[i]<<","; 41 cout<<endl; 42 } 43 }; 44 template<typename T> 45 int Vector<T>::get_size(){return size;} 46 template<typename T> 47 T& Vector<T>::at(int index){ 48 if (index<0||index>=size) 49 throw out_of_range("Vector:index out of range"); 50 return *(ptr+index); 51 } 52 template<typename T> 53 T& Vector<T>::operator[](int index){ 54 if (index<0||index>=size) 55 throw out_of_range("Vector:index out of range"); 56 return *(ptr+index); 57 }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
运行结果截图:
Task5.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <vector> 5 #include <string> 6 #include <algorithm> 7 8 // 定义一个结构体来存储学生信息 9 struct Student { 10 std::string id; 11 std::string name; 12 std::string major; 13 int score; 14 15 // 重载小于运算符,用于按专业字典序排序,如果专业相同则按分数降序排序 16 bool operator<(const Student& other) const { 17 if (major != other.major) { 18 return major < other.major; // 按专业字典序排序 19 } else { 20 return score > other.score; // 如果专业相同,则按分数降序排序 21 } 22 } 23 }; 24 25 // 从文件中读取学生信息 26 std::vector<Student> readStudentsFromFile(const std::string& filename) { 27 std::vector<Student> students; 28 std::ifstream infile(filename); 29 std::string line; 30 31 // 跳过标题行 32 if (std::getline(infile, line)) { 33 while (std::getline(infile, line)) { 34 std::stringstream ss(line); 35 std::string tempId, tempName, tempMajor, tempScore; 36 37 // 读取并解析每一行的数据 38 if (std::getline(ss, tempId, '\t') && 39 std::getline(ss, tempName, '\t') && 40 std::getline(ss, tempMajor, '\t') && 41 std::getline(ss, tempScore, '\t')) { 42 43 Student student; 44 student.id = tempId; 45 student.name = tempName; 46 student.major = tempMajor; 47 student.score = std::stoi(tempScore); // 将分数字符串转换为整数 48 students.push_back(student); 49 } 50 } 51 } 52 53 infile.close(); 54 return students; 55 } 56 57 // 将学生信息输出到屏幕和文件 58 void writeStudentsToOutput(const std::vector<Student>& students, const std::string& outputFilename) { 59 std::ofstream outfile(outputFilename); 60 61 // 输出标题行 62 outfile << "学号\t姓名\t专业\t分数" << std::endl; 63 64 // 输出学生信息 65 for (const auto& student : students) { 66 outfile << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << std::endl; 67 } 68 69 outfile.close(); 70 71 // 同时输出到屏幕 72 for (const auto& student : students) { 73 std::cout << student.id << "\t" << student.name << "\t" << student.major << "\t" << student.score << std::endl; 74 } 75 } 76 77 int main() { 78 std::vector<Student> students = readStudentsFromFile("data5.txt"); 79 80 // 对学生信息进行排序 81 std::sort(students.begin(), students.end()); 82 83 // 输出排序后的学生信息到屏幕和文件 84 writeStudentsToOutput(students, "ans5.txt"); 85 86 return 0; 87 }task5.cpp
1 学号 姓名 专业 分数 2 1001 抖森 Acting 80 3 1002 宝爷 Music 97 4 1003 毛怪 Acting 75 5 1004 大眼仔 Acting 82 6 1005 无脸男 Acting 85 7 1006 裘花 Acting 89 8 1007 小李子 Acting 92 9 1008 甜茶 Acting 91 10 1009 囧瑟夫 Acting 88 11 1010 霉霉 Music 96task5.txt
运行结果截图:
标签:std,const,get,Complex,实验,return,include From: https://www.cnblogs.com/yuannauy/p/18612878