实验一,现代C++编程初体验

andongni51 / 2024-10-11 / 原文

一、实验目的

 体验C++的标准库,算法库用法。数据表示,分支循环,函数和标准库等,编程解决简单基础问题。

二、实验准备

 第二章C++语言简单设计

第三章函数

第九章 函数模板

 

三、实验内容

 

1. 实验任务1

代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<algorithm>
 5 
 6 using namespace std;
 7 
 8 template <typename T>
 9 void output (const T &c);
10 void test1();
11 void test2();
12 void test3();
13 
14 int main(){
15     cout <<"测试1:\n" ;
16     test1();
17     
18     cout <<"\n测试2:\n";
19     test2();
20     
21     cout <<"\n测试3:\n";
22     test3();
23     
24     
25 } 
26 template <typename T>
27 void output(const T &c){
28     for (auto &i:c)
29         cout << i  << " ";
30         cout <<endl;
31         
32 }
33 void test1(){
34     string s0{"0123456789"};
35     cout <<"s0 = "<<s0<<endl;
36     
37     string s1{s0};
38     reverse(s1.begin(),    s1.end());
39     cout <<"s1 = "<<s1<<endl;
40     
41     string s2{s0};
42     reverse_copy(s0.begin(),s0.end(),s2.begin());
43      
44     cout << "s2 = "<<s1<<endl;
45 }
46 
47 void test2()
48 {
49     vector <int> v0{2,0,4,9};
50     cout<<"v0: ";
51     output(v0);
52     
53     vector<int> v1{v0};
54     reverse(v1.begin(),v1.end());
55     cout <<"v1: ";
56     output(v1);
57     
58     vector<int> v2{v0};
59     reverse_copy(v0.begin(),v0.end(),v2.begin());
60     cout<<"v2: ";
61     output(v2);
62 }
63 
64 void test3(){
65     vector<int> v0{0,1,2,3,4,5,6,7,8,9};
66     cout<<"v0: " ;
67     output(v0);
68     
69     vector<int> v1{v0};
70     rotate(v1.begin(),v1.begin()+1,v1.end());
71     cout << "v1: ";
72     output(v1);
73     
74     vector<int>v2{v0};
75     rotate(v2.begin(),v2.begin()+2,v2.end());
76     cout<<"v2: ";
77     output(v2);
78 
79     vector<int> v3{v0};
80     rotate(v3.begin(),v3.end()-1,v3.end());
81     cout <<"v3: " ;
82     output(v3);
83     
84     vector<int>v4{v0};
85     rotate (v4.begin(),v4.end()-2,v4.end( ));
86     cout<<"v4: ";
87     output(v4);
88     
89 }
90 
91 
92  

 

 

 

运行截图:

 

 

 

问题回答:

 

 

 

2. 实验任务2

代码:

 

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<algorithm>
 5 #include<numeric>
 6 #include<iomanip>
 7 
 8 
 9 using namespace std;
10 
11 template < typename T>
12 void output (const T&c);
13 
14 
15 int rand_int_100();
16 void test1();
17 void test2();
18 
19 int main(){
20     cout << "测试1:\n";
21     test1();
22     
23     cout<<"\n测试2: \n";
24     test2();
25     
26 }
27 
28 template <typename T>
29 void output (const T&c){
30     
31     for (auto &i:c)
32         cout<< i  << " ";
33     cout<<endl;
34 }
35 
36 
37 int rand_int_100(){
38     
39     return rand()%101;
40     
41 }
42 
43 void test1(){
44     vector<int> v0(10);
45     generate(v0.begin(),v0.end(),rand_int_100);
46     cout << "v0: ";
47     output(v0);
48     vector<int> v1{v0};
49     sort(v1.begin(),v1.end());
50     
51     cout << "v1: ";
52     output(v1);
53     
54     vector <int> v2{v0};
55     sort(v2.begin()+1,v2.end()-1);
56     
57     cout << "v2: ";
58     output(v2);
59      
60 }
61 
62 void test2(){
63     
64     
65     vector <int> v0(10);
66     generate(v0.begin(),v0.end(),rand_int_100);
67     cout<< "v0: ";
68     output(v0);
69     
70     auto iter1 = min_element (v0.begin(),v0.end());
71     cout<<"最小值:"<< *iter1<<endl;
72     
73     auto iter2 = max_element(v0.begin(),v0.end());
74     cout<<"最大值:"<< *iter2<<endl;
75     
76     auto ans = minmax_element(v0.begin(),v0.end());
77     cout<<"最小值:"<< *(ans.first)<<endl;
78     cout<<"最大值:"<<*(ans.second)<<endl;
79     double avg1= accumulate (v0.begin(),v0.end(),0)/v0.size();
80     cout<<"均值:"<<fixed<<setprecision(2)<<avg1<<endl;
81     
82     cout << endl;
83     vector <int> v1{v0};
84     cout<<"v0: ";
85     output(v0);
86     sort(v1.begin(),v1.end());
87     double avg2 = accumulate (v1.begin()+1,v1.end()-1,0)/(v1.size()-2);
88     cout<<"去掉最大值、最小值之后,均值:"<<avg2<<endl; 
89      
90 }
91  

 

运行截图:

 

 

 

问题回答:

 

 

 

3. 实验任务3

代码:

 

 

运行截图:

 

 

 问题回答:

 

 

 

4. 实验任务4

代码:

 

 

运行截图:

 

 

 问题回答:

 

 

 

5. 实验任务5

代码:

 

 

运行截图:

 

 

 问题回答:

 

 

 

6. 实验任务6

代码:

 

 

运行截图:

 

 

 问题回答:

 

 

 

7. 实验任务7

代码:

 

 

运行截图:

 

 

 问题回答:

 

 

 

8. 实验任务8

代码:

 

 

运行截图:

 

 

 问题回答:

 

 

 

四、实验结论

1. 实验任务1

 

2. 实验任务2

 

3. 实验任务3

 

4. 实验任务4

 

5. 实验任务5

 

6. 实验任务6

 

7. 实验任务7

 

8. 实验任务8

 

五、实验总结(选)