实验一

任务一

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<array>
 5 template<typename T>
 6 void output1(const T &obj)
 7 {
 8     for(auto i:obj)
 9       std::cout<<i<<",";
10     std::cout<<"\b\b\n"; 
11 }
12 template<typename T>
13 void output2(const T &obj)
14 {
15     for(auto p=obj.begin();p!=obj.end();++p)
16         std::cout<<*p<<",";
17     std::cout<<"\b\b\n";
18 }
19 void test_array()
20 {
21     using namespace std;
22     array<int,5> x1;
23     cout<<"x1.size()="<<x1.size()<<endl;
24     x1.fill(42);
25     x1.at(0)=999;
26     x1[4]=-999;
27     cout<<"x1:";
28     output1(x1);
29     cout<<"x1:";
30     output2(x1);
31     array<int,5> x2(x1);
32     cout<<boolalpha<<(x1==x2)<<endl;
33     x2.fill(22);
34     cout<<"x2:";
35     output1(x2);
36     swap(x1,x2);
37     cout<<"x1:";
38     output1(x1);
39     cout<<"x2:";
40     output1(x2); 
41 }
42 void test_vector()
43 {
44     using namespace std;
45     vector<int>v1;
46     cout<<v1.size()<<endl;
47     cout<<v1.max_size()<<endl;
48     v1.push_back(55);
49     cout<<"v1:";
50     output1(v1);
51     vector<int> v2{1,0,5,2};
52     v2.pop_back();
53     v2.erase(v2.begin());
54     v2.insert(v2.begin(),999);
55     v2.insert(v2.end(),-999);
56     cout<<v2.size()<<endl;
57     cout<<"v2:";
58     output2(v2);
59     vector<int> v3(5,42);
60     cout<<"v3:";
61     output1(v3);
62     vector<int> v4(v3.begin(),v3.end()-2);
63     cout<<"v4:";
64     output1(v4);
65     
66 }
67 void test_string()
68 {
69     using namespace std;
70     string s1{"oop"};
71     cout<<s1.size()<<endl;
72     for(auto &i:s1)
73         i-=32;
74     s1+="2023";
75     s1.append(",hello");
76     cout<<s1<<endl;
77     
78 }
79 int main()
80 {
81     using namespace std;
82     cout<<"\n===========arry============="<<endl;
83     test_array();
84     cout<<"\n============vector=========="<<endl;
85     test_vector();
86     cout<<"\n===========string==========="<<endl;
87     test_string();
88 }
View Code1

 

任务二

 1 #include <iostream>
 2 #include <complex>
 3 void test_std_complex() 
 4 {
 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()<< endl;
12   cout << "c1 + c2 = " << c1 + c2 << endl;
13   cout << "c1 - c2 = " << c1 - c2 << endl;
14   cout << "abs(c1) = " << abs(c1) << endl;
15   cout << boolalpha;
16   cout << "c1 == c2: " << (c1 == c2) << endl;
17   cout << "c3 == c2: " << (c3 == c2) << endl;
18   complex<double> c4 = 2;
19   cout << "c4 = " << c4 << endl;
20   c4 += c1;
21   cout << "c4 = " << c4 << endl; 
22 }
23 int main() 
24 {
25 test_std_complex();
26 }
View Code2

 

任务三

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 class T 
 5 {
 6 public:
 7     T(int x = 0, int y = 0); 
 8     T(const T &t); 
 9     T(T &&t); 
10     ~T(); 
11     void set_m1(int x); 
12     int get_m1() const; 
13     int get_m2() const; 
14     void display() const; 
15     friend void func();
16 private:
17     int m1, m2;
18 public:
19     static void disply_count(); 
20 public:
21     static const string doc; 
22     static const int max_count; 
23 private:
24     static int count; 
25 };
26 const string T::doc{"a simple class"};
27 const int T::max_count = 99;
28 int T::count = 0;
29 T::T(int x, int y): m1{x}, m2{y}
30 {
31     ++count;
32     cout << "constructor called.\n";
33 }
34 T::T(const T &t): m1{t.m1}, m2{t.m2} 
35 {
36     ++count;
37     cout << "copy constructor called.\n";
38 }
39 T::T(T &&t): m1{t.m1}, m2{t.m2} 
40 {
41     ++count;
42     cout << "move constructor called.\n";
43 }
44 T::~T() 
45 {
46     --count;
47     cout << "destructor called.\n";
48 }
49 void T::set_m1(int x) 
50 {
51     m1 = x;
52 }
53 int T::get_m1() const 
54 {
55     return m1;
56 }
57 int T::get_m2() const 
58 {
59     return m2;
60 }
61 void T::display() const 
62 {
63     cout << m1 << ", " << m2 << endl;
64 }
65 void T::disply_count() 
66 {
67     cout << "T objects: " << count << endl;
68 }
69 void func() 
70 {
71     T t1;
72     t1.set_m1(55);
73     t1.m2 = 77; 
74     t1.display();
75 }
76 void test() 
77 {
78     cout << "T class info: " << T::doc << endl;
79     cout << "T objects max_count: " << T::max_count << endl;
80     T::disply_count();
81     T t1;
82     t1.display();
83     t1.set_m1(42);
84     T t2{t1};
85     t2.display();
86     T t3{std::move(t1)};
87     t3.display();
88     t1.display();
89     T::disply_count();
90 }
91 int main() 
92 {
93     cout << "============类T============" << endl;
94     test();
95     cout << endl;
96     cout << "============友元函数func()============" << endl;
97     func();
98 }
View Code

 

任务四

 1 #include <iostream>
 2 #include <string>
 3 #include <iomanip>
 4 using namespace std;
 5 class Rect
 6 {
 7 public:
 8     static const string doc;
 9     Rect(double x=2.0,double y=1.0);
10     Rect(Rect &r);
11     double len(){return length;}
12     double wide(){return width;}
13     double area(){return(length*width);}
14     static int size_info(){return size;}
15     double circumference(){return 2*(length+width);}
16     void resize(int times);
17     void resize(int l_times,int w_times);
18     ~Rect();
19 private:
20     double length,width;
21     static int size;    
22 }; 
23 const string Rect::doc("a simple rect class");
24 int Rect::size=0;
25 Rect::Rect(double x,double y)
26 {
27     length=x;
28     width=y;
29     size++;
30 }
31 void Rect::resize(int times)
32 {
33     length=length*times;
34     width=width*times;
35 }
36 void Rect::resize(int l_times,int w_times)
37 {
38     length=length*l_times;
39     width=width*w_times; 
40 }
41 Rect::Rect(Rect &r)
42 {
43     length=r.length;
44     width=r.width;
45     size++;
46 }
47 Rect::~Rect()
48 {
49     --size;
50 }
51 void output(const Rect &r) 
52 {
53     cout << "矩形信息: " << endl;
54     cout << fixed << setprecision(2); 
55     Rect a;
56     a=r;
57     cout<<"长度"<<a.len()<<endl;
58     cout<<"宽度"<<a.wide()<<endl; 
59     cout<<"周长"<<a.circumference()<<endl;
60     cout<<"面积"<<a.area()<<endl; 
61 }
62 void test() 
63 {
64     cout << "矩形类信息: " << Rect::doc << endl;
65     cout << "当前矩形对象数目: " << Rect::size_info()<< endl;
66     Rect r1;
67     output(r1);
68     Rect r2(4, 3);
69     output(r2);
70     Rect r3(r2);
71     r3.resize(2);
72     output(r3);
73     r3.resize(5, 2);
74     output(r3);
75     cout << "当前矩形对象数目: " << Rect::size_info()<< endl;
76 }
77 int main() 
78 {
79     test();
80     cout << "当前矩形对象数目: " << Rect::size_info()<< endl;
81 }
View Code

 

 

任务五

 

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 class Complex
 5 {
 6 public:
 7     Complex(double a = 0, double b = 0) :a(a), b(b) {}
 8     Complex(const Complex& x) ;
 9     double get_real() { return a; }
10     double get_imag() const { return b; }
11     void show() const;
12     void add(const Complex &z);
13     ~Complex(){};
14     friend Complex add(const Complex &k,const Complex &l);
15     friend bool is_equal(const Complex &asd,const Complex &zxc);
16     friend double abs(const Complex &x);
17 
18 private:
19     double a, b;
20 };
21 Complex::Complex(const Complex& x)
22 {
23     a = x.a;
24     b = x.b;
25 }
26 void Complex::show() const
27 {
28     if (b > 0)
29         cout << a << "+" << b << "i" ;
30     else if (b < 0)
31         cout << a << b << "i" ;
32     else
33         cout << a ;
34 }
35 void Complex::add(const Complex &z)
36 {
37     a = a + z.a;
38     b = b + z.b;
39 }
40 Complex add(const Complex &k,const Complex &l)
41 {
42     Complex s;
43     s.a=k.a+l.a;s.b=k.b+l.b;
44     return (s);
45 }
46 bool is_equal(const Complex &asd,const Complex &zxc)
47 {
48     if (asd.a == zxc.a && asd.b == zxc.b)
49         return (true);
50     else
51         return (false);
52 }
53 double abs(const Complex &x)
54 {
55     return(sqrt(x.a * x.a + x.b * x.b));
56 }
57 
58 
59 void test()
60 {
61     using namespace std;
62     Complex c1(3, -4);
63     const Complex c2(4.5);
64     Complex c3(c1);
65     cout << "c1 = ";
66     c1.show();
67     cout << endl;
68     cout << "c2 = ";
69     c2.show();
70     cout << endl;
71     cout << "c2.imag = " << c2.get_imag() << endl;
72     cout << "c3 = ";
73     c3.show();
74     cout << endl;
75     cout << "abs(c1) = ";
76     cout << abs(c1) << endl;
77     cout << boolalpha;
78     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
79     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
80     Complex c4;
81     c4 = add(c1, c2);
82     cout << "c4 = c1 + c2 = ";
83     c4.show();
84     cout << endl;
85     c1.add(c2);
86     cout << "c1 += c2, " << "c1 = ";
87     c1.show();
88     cout << endl;
89 }
90 int main()
91 {
92     test();
93 }
View Code

 

 

posted @ 2023-10-15 11:02  dmsx  阅读(45)  评论(0)    收藏  举报