task 1
1 // 标准库string, vector, array基础用法 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <array> 6 // 函数模板 7 // 对满足特定条件的序列类型T对象,使用范围for输出 8 template<typename T> 9 void output1(const T &obj) { 10 for(auto i: obj) 11 std::cout << i << ", "; 12 std::cout << "\b\b \n"; 13 } 14 // 函数模板 15 // 对满足特定条件的序列类型T对象,使用迭代器输出 16 template<typename T> 17 void output2(const T &obj) { 18 for(auto p = obj.begin(); p != obj.end(); ++p) 19 std::cout << *p << ", "; 20 std::cout << "\b\b \n"; 21 } 22 // array模板类基础用法 23 void test_array() { 24 using namespace std; 25 array<int, 5> x1; // 创建一个array对象,包含5个int元素,未初始化 26 cout << "x1.size() = " << x1.size() << endl; // 输出元素个数 27 x1.fill(42); // 把x1的所有元素都用42填充 28 x1.at(0) = 999; // 把下标为0的元素值修改为999 29 x1[4] = -999; // 把下表为4的元素值修改为-999 30 cout << "x1: "; 31 output1(x1); // 调用模板函数output1输出x1 32 cout << "x1: "; 33 output2(x1); // 调用模板函数output1输出x1 34 35 array<int,5> x2{x1}; 36 cout << boolalpha << (x1 == x2) << endl; 37 x2.fill(22); 38 cout << "x2: "; 39 output1(x2); 40 swap(x1, x2); // 交换array对象x1, x2 41 cout << "x1: "; 42 output1(x1); 43 cout << "x2: "; 44 output1(x2); 45 } 46 // vector模板类基础用法 47 void test_vector() { 48 using namespace std; 49 vector<int> v1; 50 cout << v1.size() << endl; // 输出目前元素个数 51 cout << v1.max_size() << endl; // 输出元素个数之最大可能个数 52 v1.push_back(55); // 在v1末尾插入元素 53 cout << "v1: "; 54 output1(v1); 55 vector<int> v2 {1, 0, 5, 2}; 56 v2.pop_back(); // 从v2末尾弹出一个元素 57 v2.erase(v2.begin()); // 删除v2.begin()位置的数据项 58 v2.insert(v2.begin(), 999); // 在v1.begin()之前的位置插入 59 v2.insert(v2.end(), -999); // 在v1.end()之前的位置插入 60 cout << v2.size() << endl; 61 cout << "v2: "; 62 output2(v2); 63 vector<int> v3(5, 42); //创建vector对象,包含5个元素,每个元素值都是42 64 cout << "v3: "; 65 output1(v3); 66 vector<int> v4(v3.begin(), v3.end()-2); // 创建vector对象,以v3对象的[v3.begin(), v3.end()-2)区间作为元素值 67 cout << "v4: "; 68 output1(v4); 69 } 70 // string类基础用法 71 void test_string() { 72 using namespace std; 73 string s1{"oop"}; 74 cout << s1.size() << endl; 75 for(auto &i: s1) 76 i -= 32; 77 s1 += "2023"; 78 s1.append(", hello"); 79 cout << s1 << endl; 80 } 81 int main() { 82 using namespace std; 83 cout << "===========测试1: array模板类基础用法===========" << endl; 84 test_array(); 85 cout << "\n===========测试2: vector模板类基础用法===========" << endl; 86 test_vector(); 87 cout << "\n===========测试3: string类基础用法===========" << endl; 88 test_string(); 89 }
task2
1 #include <iostream> 2 #include <complex> 3 // 测试标准库提供的复数类模板complex 4 void test_std_complex() { 5 using namespace std; 6 complex<double> c1{3, 4}, c2{4.5}; 7 const complex<double> c3{c2}; 8 cout << "c1 = " << c1 << endl; 9 cout << "c2 = " << c2 << endl; 10 cout << "c3 = " << c3 << endl; 11 cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag() 12 << endl; 13 cout << "c1 + c2 = " << c1 + c2 << endl; 14 cout << "c1 - c2 = " << c1 - c2 << endl; 15 cout << "abs(c1) = " << abs(c1) << endl; // abs()是标准库数学函数,对复数取模 16 cout << boolalpha; // 设置bool型值以true/false方式输出 17 cout << "c1 == c2: " << (c1 == c2) << endl; 18 cout << "c3 == c2: " << (c3 == c2) << endl; 19 complex<double> c4 = 2; 20 cout << "c4 = " << c4 << endl; 21 c4 += c1; 22 cout << "c4 = " << c4 << endl; 23 } 24 int main() { 25 test_std_complex(); 26 }
task3
1 // 一个简单的类T:定义、使用 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 // 类T的声明 6 class T { 7 public: 8 T(int x = 0, int y = 0); // 带有默认形值的构造函数 9 T(const T &t); // 复制构造函数 10 T(T &&t); // 移动构造函数 11 ~T(); // 析构函数 12 void set_m1(int x); // 设置T类对象的数据成员m1 13 int get_m1() const; // 获取T类对象的数据成员m1 14 int get_m2() const; // 获取T类对象的数据成员m2 15 void display() const; // 显示T类对象的信息 16 friend void func(); // 声明func()为T类友元函数 17 private: 18 int m1, m2; 19 public: 20 static void disply_count(); // 类方法,显示当前T类对象数目 21 public: 22 static const string doc; // 类属性,用于描述T类 23 static const int max_count; // 类属性,用于描述T类对象的上限 24 private: 25 static int count; // 类属性,用于描述当前T类对象数目 26 }; 27 // 类的static数据成员:类外初始化 28 const string T::doc{"a simple class"}; 29 const int T::max_count = 99; 30 int T::count = 0; 31 // 类T的实现 32 T::T(int x, int y): m1{x}, m2{y} { 33 ++count; 34 cout << "constructor called.\n"; 35 } 36 T::T(const T &t): m1{t.m1}, m2{t.m2} { 37 ++count; 38 cout << "copy constructor called.\n"; 39 } 40 T::T(T &&t): m1{t.m1}, m2{t.m2} { 41 ++count; 42 cout << "move constructor called.\n"; 43 } 44 T::~T() { 45 --count; 46 cout << "destructor called.\n"; 47 } 48 void T::set_m1(int x) { 49 m1 = x; 50 } 51 int T::get_m1() const { 52 return m1; 53 } 54 int T::get_m2() const { 55 return m2; 56 } 57 void T::display() const { 58 cout << m1 << ", " << m2 << endl; 59 } 60 // 类方法 61 void T::disply_count() { 62 cout << "T objects: " << count << endl; 63 } 64 // 友元函数func():实现 65 void func() { 66 T t1; 67 t1.set_m1(55); 68 t1.m2 = 77; // 虽然m2是私有成员,依然可以直接访问 69 t1.display(); 70 } 71 // 测试 72 void test() { 73 cout << "T class info: " << T::doc << endl; 74 cout << "T objects max_count: " << T::max_count << endl; 75 T::disply_count(); 76 T t1; 77 t1.display(); 78 t1.set_m1(42); 79 T t2{t1}; 80 t2.display(); 81 T t3{std::move(t1)}; 82 t3.display(); 83 t1.display(); 84 T::disply_count(); 85 } 86 // 主函数 87 int main() { 88 cout << "============测试类T============" << endl; 89 test(); 90 cout << endl; 91 cout << "============测试友元函数func()============" << endl; 92 func(); 93 }
task4
1 #include <iostream> 2 #include <string> 3 #include <iomanip> 4 using namespace std; 5 // 矩形类Rect的定义 6 // 待补足 7 class Rect { 8 public: 9 Rect(float l= 2.0, float w = 1.0); 10 Rect(const Rect& r); 11 ~Rect(); 12 13 float len() const; 14 float wide() const; 15 float area() const; 16 float circumference() const; 17 void resize(int t); 18 void resize(int lt, int wt); 19 20 21 private: 22 float length, width; 23 public: 24 static int size_info(){ 25 return size; 26 }; 27 public: 28 static const string doc; 29 private: 30 static int size; 31 32 }; 33 34 const string Rect::doc{ "a simple Rect class" }; 35 int Rect::size; 36 37 Rect::Rect(float l, float w) { 38 ++size; 39 } 40 41 42 Rect::Rect(const Rect& r) { 43 ++size; 44 } 45 46 Rect::~Rect() { 47 --size; 48 } 49 50 float Rect::len() const { 51 return length; 52 } 53 54 float Rect::wide() const { 55 return width; 56 } 57 58 float Rect::area() const { 59 float area; 60 return area = length * width; 61 } 62 63 float Rect::circumference() const { 64 float circumference; 65 return circumference = 2 * (length + width); 66 } 67 68 void Rect::resize(int t) { 69 length *= t; 70 width *= t; 71 } 72 73 void Rect::resize(int lt,int wt) { 74 length *= lt; 75 width *= wt; 76 } 77 78 79 // 普通函数:输出矩形信息 80 void output(const Rect& r) { 81 cout << "矩形信息: " << endl; 82 cout << fixed << setprecision(2); // 控制输出格式:以浮点数形式输出,小数部分保留两位 83 // 补足代码:分行输出矩形长、宽、面积、周长 84 cout << "长: " << r.len() << endl; 85 cout << "宽: " << r.wide() << endl; 86 cout << "面积: " << r.area() << endl; 87 cout << "周长: " << r.circumference() << endl; 88 } 89 // 测试代码 90 void test() { 91 cout << "矩形类信息: " << Rect::doc << endl; 92 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 93 Rect r1; 94 output(r1); 95 Rect r2(4, 3); 96 output(r2); 97 Rect r3(r2); 98 r3.resize(2); 99 output(r3); 100 r3.resize(5, 2); 101 output(r3); 102 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 103 } 104 // 主函数 105 int main() { 106 test(); 107 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 108 }
成功运行了,但是发生了运算错误,正在寻找bug。
task5
1 #include <iostream> 2 #include <cmath> 3 // 复数类Complex:定义 4 // 待补足 5 class Complex { 6 private: 7 double real; 8 double imag; 9 10 public: 11 Complex(double r = 0, double i = 0); 12 Complex(const Complex& c); 13 ~Complex(); 14 15 double get_real() const; 16 double get_imag() const; 17 void show() const; 18 void add(Complex const& c1); 19 20 friend Complex add(Complex const& c1, Complex const& c2); 21 friend bool is_equal(Complex const& c1, Complex const& c2); 22 friend double abs(Complex const& c1); 23 }; 24 25 Complex::Complex(double r, double i) :real{ r }, imag{ i }{ 26 27 } 28 Complex::Complex(const Complex& c) { 29 30 } 31 32 Complex::~Complex() { 33 34 } 35 36 double Complex::get_real() const { 37 return real; 38 } 39 40 double Complex::get_imag() const { 41 return imag; 42 } 43 44 void Complex::show() const { 45 using namespace std; 46 cout << real << "+" << imag << "i" << endl; 47 } 48 49 void Complex::add(Complex const& c1) { 50 real += c1.real; 51 imag += c1.imag; 52 } 53 54 Complex add(Complex const& c1, Complex const& c2) { 55 Complex c3; 56 c3.real = c1.real + c2.real; 57 c3.imag = c1.imag + c2.imag; 58 return c3; 59 } 60 61 bool is_equal(Complex const& c1, Complex const& c2) { 62 if (c1.real == c2.real && c1.imag == c2.imag) 63 return true; 64 else 65 return false; 66 } 67 68 double abs(Complex const& c1) { 69 return sqrt(c1.real * c1.real + c1.imag * c1.imag); 70 } 71 // 复数类Complex: 测试 72 void test() { 73 using namespace std; 74 Complex c1(3, -4); 75 const Complex c2(4.5); 76 Complex c3(c1); 77 cout << "c1 = "; 78 c1.show(); 79 cout << endl; 80 cout << "c2 = "; 81 c2.show(); 82 cout << endl; 83 cout << "c2.imag = " << c2.get_imag() << endl; 84 cout << "c3 = "; 85 c3.show(); 86 cout << endl; 87 cout << "abs(c1) = "; 88 cout << abs(c1) << endl; 89 cout << boolalpha; 90 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 91 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 92 Complex c4; 93 c4 = add(c1, c2); 94 cout << "c4 = c1 + c2 = "; 95 c4.show(); 96 cout << endl; 97 c1.add(c2); 98 cout << "c1 += c2, " << "c1 = "; 99 c1.show(); 100 cout << endl; 101 } 102 int main() { 103 test(); 104 }
与task4同样的错误。
本次实验发生了很多错误,希望能在课后的交流中发现这些bug,以免下次重现。
标签:const,int,void,float,Complex,实验,Rect From: https://www.cnblogs.com/Serenity-X/p/17774956.html