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

一、实验目的

 体验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

代码:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<vector>
 5 
 6 
 7 
 8 bool is_palindrome(const std::string &s);
 9 
10 
11 int main(){
12     using namespace std;
13     string s;
14     
15     while (cin>>s ){
16     cout<< boolalpha << is_palindrome(s) << endl; 
17     }
18     
19   return 0;
20   
21 }
22 bool is_palindrome(const std::string &s){
23         
24         std::string s1{s};
25         std::reverse(s1.begin(),s1.end());
26             return s1==s;
27                     
28         
29     }

 

 

运行截图:

 

 

 

 问题回答:

 

 

 

4. 实验任务4

代码:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 string dec2n(int x,int n=2){
 8     
 9     string result;
10     
11     if (n==2||n==8){
12         while (x>0) {
13             int y=x%n;
14             result+=(y+'0');
15             x/=n;
16         }
17         reverse(result.begin(),result.end());
18             return  result;
19     }
20     
21     
22     if( n==16){
23         
24         while(x>0){
25             int z=x%16;
26             if(z<10)        {
27                 result+='0'+z;
28             }
29             else{
30                 result+='A'+(z-10);
31             }
32             x/=16;
33         }
34         reverse(result.begin(),result.end());
35             return  result;
36         }
37         
38         
39 }
40 
41     
42     
43     
44 
45 int main(){
46     
47     using namespace std;
48     
49     int x;
50     while (cin >>x){
51         
52         cout<<"十进制:"<< x <<endl;
53         cout <<"二进制:"<< dec2n(x)<<endl; 
54         cout <<"八进制:"<< dec2n(x,8)<<endl;
55         cout <<"十六进制:"<< dec2n(x,16) <<endl<<endl;
56     }
57     return 0;
58 }

 

 

运行截图:

 

 

 

 问题回答:

 

 

 

5. 实验任务5

代码:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<iomanip>
 4 using namespace std;
 5 int main(){
 6     
 7     
 8 
 9 
10     char a;
11     cout<<setw(2)<<' ';
12     for (int i =0;i< 26;i++)
13     {
14         a='a'+i;
15         cout <<setw(2) <<a;
16         
17     }
18     cout << endl;
19     
20     for (int i=1;i<27;i++)
21     {
22         cout <<setw(2)<<i;
23         for (int j=0;j<26;j++)
24         {
25             int t=(i+j)%26;
26             a='A'+t;
27             cout<<setw(2)<<a;
28          } 
29             cout<<endl;
30     }
31     
32     
33             
34     
35             
36         
37     return  0 ;
38 } 

 

 

运行截图:

 

 

 

 问题回答:

 

 

 

6. 实验任务6

代码:

 

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<ctime>
 4 #include<iomanip>
 5 
 6 using namespace std;
 7 
 8 int getrandomnum(int min, int max)
 9 {
10     return rand ()%(max-min +1)+min;
11     
12 }
13 
14 int calculate(int a, int b, char operation)
15 {
16     
17     switch (operation)
18     {
19         case '+':
20             return a+b;
21         case '-':
22             return a-b;
23         case '*':
24             return a*b;
25         case '/':
26             return a/b;
27         default:
28             return 0;
29     }
30 }
31  int main()
32 {
33      srand(static_cast<unsigned int > (time(0)));
34      
35      const int shuliang=10;
36      int rightanswers=0;
37      
38      int a,b,answer;
39          
40     char operation;
41      
42      for (int i=0;i<shuliang;++i)
43      {
44          
45          
46          int num1=getrandomnum(1,4);
47          if (num1==1)
48          {
49              operation='+';
50              a=getrandomnum(1,10);
51              b=getrandomnum(1,10);
52              
53          }
54          else if(num1==2)
55          {
56              operation='-';
57              a=getrandomnum(2,10);
58              b=getrandomnum(1,a-1);
59              
60          }
61          else if (num1==3)
62          {
63              operation='*';
64              a=getrandomnum(1,10);
65              b=getrandomnum(1,10);
66          }
67          else if(num1==4)
68          {
69              operation='/';
70              b=getrandomnum(1,10);
71              a=b*getrandomnum(1,1);
72          }
73          cout<<setw(2)<<a<<setw(2)<<operation<<setw(2)<<b<<setw(2)<<"=";
74      
75      
76      cin>>answer;
77 
78      
79 
80  int rightanswer= calculate(a,b,operation);
81  if (answer==rightanswer)
82              {
83      rightanswers++;
84      
85              }
86     }
87 double accuracy=(static_cast<double>(rightanswers)/shuliang)*100;
88  cout<<"正确率: "<<fixed<< setprecision(2)<<accuracy<<"%"<<endl;
89 
90  return 0;
91 }

 

 

运行截图:

 

 

posted @ 2024-10-11 18:26  安东尼23  阅读(8)  评论(0编辑  收藏  举报