#include<iostream> #include<string> #include<vector> #include<array> // 函数模板 // 对满足特定条件的序列类型T对象,使用范围for输出 template<typename T> void output1(const T &obj) { for(auto i: obj) std::cout << i << ", "; std::cout << "\b\b \n"; } // 函数模板 // 对满足特定条件的序列类型T对象,使用迭代器输出 template<typename T> void output2(const T &obj) { for(auto p = obj.begin(); p != obj.end(); ++p) std::cout << *p << ", "; std::cout << "\b\b \n"; } // array模板类基础用法 void test_array() { using namespace std; array<int,5> x1; cout<<"x1.size()="<< x1.size() << endl; x1.fill(42); x1.at(0)=999; x1[4]=-999; cout<<"x1: "; output1(x1); cout << "x1: "; output2(x1); array<int,5> x2 (x1); cout<<boolalpha<<(x1==x2)<<endl; x2.fill(22); cout<<"x2: "; output1(x2); swap(x1,x2); cout<<"x1: "; output1(x1); cout<<"x2: "; output1(x2); } void test_vector() { using namespace std; vector<int> v1; cout<<v1.size()<<endl; cout<<v1.max_size()<<endl; v1.push_back(55); cout<<"v1: "; output1(v1); vector<int> v2{1,0,5,2}; v2.pop_back(); v2.erase(v2.begin()); v2.insert(v2.begin(),999); v2.insert(v2.end(),-999); cout<<v2.size()<<endl; cout<<"v2: "; output2(v2); vector<int> v3(5, 42); cout << "v3: "; output1(v3); vector<int> v4(v3.begin(), v3.end()-2); cout << "v4: "; output1(v4); } void test_string() { using namespace std; string s1{"oop"}; cout << s1.size() << endl; for(auto &i: s1) i -= 32; s1 += "2023"; s1.append(", hello"); cout << s1 << endl; } int main() { using namespace std; cout << "===========测试1: array模板类基础用法===========" << endl; test_array(); cout << "\n===========测试2: vector模板类基础用法===========" << endl; test_vector(); cout << "\n===========测试3: string类基础用法===========" << endl; test_string(); }View Code
task.2
#include<iostream> #include<complex> void test_std_complex() { using namespace std; complex<double> c1{3,4},c2{4.5}; const complex<double> c3{c2}; cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; cout << "c3 = " << c3 << endl; cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag() << endl; cout << "c1 + c2 = " << c1 + c2 << endl; cout << "c1 - c2 = " << c1 - c2 << endl; cout << "abs(c1) = " << abs(c1) << endl; } int main() { test_std_complex(); }View Code
task.3
#include <iostream> #include <string> using namespace std; class T { public: T(int x=0,int y=0); T(const T &t); T(T &&t); ~T(); void set_m1(int x); int get_m1() const; int get_m2() const; void display() const; friend void func(); private: int m1,m2; public: static void display_count(); public: static const string doc; static const int max_count; private: static int count; }; const string T::doc{"a simple class"}; const int T::max_count = 99; int T::count = 0; T::T(int x,int y):m1{x},m2{y} { ++count; cout << "constructor called.\n"; } T::T(const T &t):m1{t.m1},m2{t.m2} { ++count; cout << "copy constructor called.\n"; } T::T(T &&t):m1{t.m1},m2{t.m2} { ++count; cout << "move constructor called.\n"; } T::~T() { --count; cout << "destructor called.\n"; } void T::set_m1(int x) { m1=x; } int T::get_m1() const { return m1; } int T::get_m2() const { return m2; } void T::display() const { cout << m1 << ", " << m2 << endl; } void T::display_count() { cout << "T objects: " << count << endl; } void func() { T t1; t1.set_m1(55); t1.m2=77; t1.display(); } void test() { cout << "T class info: " << T::doc << endl; cout << "T objects max_count: " << T::max_count << endl; T::display_count(); T t1; t1.display(); t1.set_m1(42); T t2{t1}; t2.display(); T t3{std::move(t1)}; t3.display(); t1.display(); T::display_count(); } int main() { cout << "============测试类T============" << endl; test(); cout << endl; cout << "============测试友元函数func()============" << endl; func(); }View Code
task.4
#include <iostream> #include <string> #include <iomanip> using namespace std; // 矩形类Rect的定义 // 待补足 // ××× // 普通函数:输出矩形信息 class Rect { public: static const string doc; private: static int size; double length; double width; public: static int size_info(); public: Rect(double x=2.0,double y=1.0); Rect(const Rect &t); ~Rect(); double len()const; double wide()const; double area()const; double circumference()const; void resize(int times); void resize(int l_times,int w_times); }; const string Rect::doc{"a simple class"}; int Rect::size=0; Rect::Rect(double x,double y):length{x},width{y} { ++size; } Rect::Rect(const Rect &t):length{t.length},width{t.width} { ++size; } Rect::~Rect() { --size; } double Rect::len()const { return length; } double Rect::wide()const { return width; } double Rect::area()const { return length*width; } double Rect::circumference()const { return 2*(length+width); } void Rect::resize(int times) { length/=times; width/=times; } void Rect::resize(int l_times,int w_times) { length/=l_times; width/=w_times; } int Rect::size_info() { return size; } void output(const Rect &r) { cout << "矩形信息: " << endl; cout << fixed << setprecision(2); // 控制输出格式:以浮点数形式输出,小数部分保留两位 cout << "长: " << r.len() << endl; cout << "宽: " << r.wide() << endl; cout << "面积: " << r.area() << endl; cout << "周长: " << r.circumference() << endl; // 补足代码:分行输出矩形长、宽、面积、周长 // ××× } // 测试代码 void test() { cout << "矩形类信息: " << Rect::doc << endl; cout << "当前矩形对象数目: " << Rect::size_info() << endl; Rect r1; output(r1); Rect r2(4, 3); output(r2); Rect r3(r2); r3.resize(2); output(r3); r3.resize(5, 2); output(r3); cout << "当前矩形对象数目: " << Rect::size_info() << endl; } // 主函数 int main() { test(); cout << "当前矩形对象数目: " << Rect::size_info() << endl; }View Code
task.5
#include <iostream> #include <cmath> class Complex { public: Complex(double r = 0, double i = 0) { real = r; imag = i; } Complex(const Complex& x) { real = x.real; imag = x.imag; } double get_real() const { return real; } double get_imag() const { return imag; } void add(const Complex& x) { real += x.real; imag += x.imag; } void show() const { using namespace std; if (real == 0 && imag == 0) { cout << 0; } else if (real == 0) { cout << imag << "i"; } else if (imag == 0) { cout << real; } else { if (imag > 0) { cout << real << " + " << imag << "i"; } else if (imag < 0) { cout << real << " - " << -imag << "i"; } } } friend Complex add(const Complex& c1, const Complex& c2); friend bool is_equal(const Complex& c1, const Complex& c2); friend double abs(const Complex& c1); private: double real; double imag; }; Complex add(const Complex& c1, const Complex& c2) { double real = c1.real + c2.real; double imag = c1.imag + c2.imag; return Complex(real, imag); } bool is_equal(const Complex& c1, const Complex& c2) { if (c1.real == c2.real && c1.imag == c2.imag) return true; else return false; } double abs(const Complex& c1) { return sqrt(c1.real * c1.real + c1.imag * c1.imag); } void test() { using namespace std; Complex c1(3, -4); const Complex c2(4.5); Complex c3(c1); cout << "c1 = "; c1.show(); cout << endl; cout << "c2 = "; c2.show(); cout << endl; cout << "c2.imag = " << c2.get_imag() << endl; cout << "c3 = "; c3.show(); cout << endl; cout << "abs(c1) = "; cout << abs(c1) << endl; cout << boolalpha; cout << "c1 == c3 : " << is_equal(c1, c3) << endl; cout << "c1 == c2 : " << is_equal(c1, c2) << endl; Complex c4; c4 = add(c1, c2); cout << "c4 = c1 + c2 = "; c4.show(); cout << endl; c1.add(c2); cout << "c1 += c2, " << "c1 = "; c1.show(); cout << endl; } int main() { test(); }View Code
标签:const,cout,int,double,void,实验,Rect From: https://www.cnblogs.com/qazmlp10/p/17769425.html