实验1 现代c++编程初体验

实验1 :

 

  1 #include<iostream>
  2 #include<string>
  3 #include<vector>
  4 #include<algorithm>
  5 
  6 //模板函数声明
  7 template<typename T>
  8 void output(const T &c);
  9 
 10 void test1();
 11 void test2();
 12 void test3();
 13 
 14 int main(){
 15     std::cout<<"测试1:\n";
 16     test1();
 17     
 18     std::cout<<"\n测试2:\n";
 19     test2();
 20     
 21     std::cout<<"\n测试3:\n";
 22     test3();
 23      
 24 } 
 25 
 26 //输出容器对象c中的元素
 27 template<typename T>
 28 void output(const T &c){
 29     for(auto &i:c)
 30     std::cout<<i<<' ';
 31     std::cout<<'\n';
 32 } 
 33 
 34 //测试1:组合使用算法库,迭代器,string反转字符串
 35 void test1(){
 36     using namespace std;
 37     
 38     string s0{"0123456789"};
 39     cout<<"s0="<<s0<<endl;
 40     
 41     string s1{s0};
 42     //反转s1自身
 43     reverse(s1.begin(),s1.end());
 44     cout<<"s1="<<s1<<endl;
 45     
 46     string s2(s0.size(),' '); /////
 47     //将s0反转后结果拷贝到s2,s0自身不变
 48     reverse_copy(s0.begin(),s0.end(),s2.begin());
 49     cout<<"s2="<<s2<<endl;
 50 } 
 51 
 52 //测试2:组合使用算法库,迭代器,vector反转动态数组对象vector内数据
 53 void test2(){
 54     using namespace std;
 55     
 56     vector<int>v0{2,0,4,9};
 57     cout<<"v0:";
 58     output(v0);
 59     
 60     vector<int>v1{v0};
 61     reverse(v1.begin(),v1.end());
 62     cout<<"v1:";
 63     output(v1);
 64     
 65     vector<int>v2{v0};
 66     reverse_copy(v0.begin(),v0.end(),v2.begin());
 67     cout<<"v2:";
 68     output(v2); 
 69 } 
 70 
 71 //测试3:组合使用算法库,迭代器,vector实现元素旋转移位
 72 void test3(){
 73     using namespace std;
 74     
 75     vector<int>v0{0,1,2,3,4,5,6,7,8,9};
 76     cout<<"v0:";
 77     output(v0);
 78     
 79     vector<int>v1{v0};
 80     //将【v1.begin(),v1.end()】区间内元素循环左移1位
 81      rotate(v1.begin(),v1.begin()+1,v1.end());
 82      cout<<"v1:";
 83      output(v1);
 84      
 85      vector<int>v2{v0};
 86      //将【v1.begin(),v1.end()】区间内元素循环左移2位
 87      rotate(v2.begin(),v2.begin()+2,v2.end());
 88      cout<<"v2:";
 89      output(v2);
 90      
 91       vector<int>v3{v0};
 92      //将【v1.begin(),v1.end()】区间内元素循环右移1位
 93      rotate(v3.begin(),v3.end()-1,v3.end());
 94      cout<<"v3:";
 95      output(v3);
 96      
 97       vector<int>v4{v0};
 98      //将【v1.begin(),v1.end()】区间内元素循环右移2位
 99      rotate(v4.begin(),v4.end()-2,v4.end());
100      cout<<"v4:";
101      output(v4);
102 } 

屏幕截图 2025-10-10 225159

问题1:reserve() 先将s0数组赋值给s1,然后对s1进行反转操作; reserve_copy()  先将s0反转,然后将结果赋值给s2,s0本身还是保持不变

问题2:rotate()算法是将需要改变顺序的部分截断,然后复制到该数组的开头或者结尾;第一个参数代表这个数组的开头,第二个参数代表截断的位置以及可以表示出左移或是后移,第三个参数代表数组的结尾

 

实验2:

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<numeric>
 5 #include<iomanip>
 6 #include<cstdlib>
 7 #include<ctime>
 8 
 9 //模板函数声明
10 template<typename T>
11 void output(const T &c);
12 
13 int generate_random_number();
14 void test1();
15 void test2();
16 
17 int main(){
18     std::srand(std::time(0)); // 添加随机种子
19     std::cout<<"测试1:\n";
20     test1();
21     
22     std::cout<<"测试2:\n";
23     test2(); 
24 } 
25 
26 //输出容器c中的对象
27 template<typename T>
28 void output(const T &c){
29     for(auto &i:c)
30     std::cout<<i<<' ';
31     std::cout<<'\n'; 
32 } 
33 
34 //返回【0,100】区间内的一个随机整数
35 int generate_random_number(){
36     return std::rand()%101;
37 } 
38 
39 //测试1:对容器类对象指定迭代器区间赋值,排序
40 void test1(){
41     using namespace std;
42     
43     vector<int>v0(10); //创建一个动态数组元素v0,对象大小为10
44     generate(v0.begin(),v0.end(),generate_random_number); //生成随机数填充v0
45     cout<<"v0:";
46     output(v0); 
47     
48     vector<int>v1{v0};
49     sort(v1.begin(),v1.end()); //对整个vector排序
50     cout<<"v1:";
51     output(v1);
52     
53     vector<int>v2{v0};
54     sort(v2.begin()+1,v2.end()-1); //只对中间部分排序,不包含首尾元素
55     cout<<"v2:";
56     output(v2);
57 }
58 //测试2:对容器类对象指定迭代器区间赋值,计算最大值、/最小值/均值
59 void test2(){
60     using namespace std;
61     
62     vector<int>v0(10);
63     generate(v0.begin(),v0.end(),generate_random_number);
64     cout<<"v0:";
65     output(v0);
66     
67     //求最大值和最小值
68     auto min_iter=min_element(v0.begin(),v0.end());
69     auto max_iter=max_element(v0.begin(),v0.end());
70     cout<<"最小值:"<<*min_iter<<endl;
71     cout<<"最大值:"<<*max_iter<<endl;
72      
73       //同时求最大值和最小值
74     auto ans=minmax_element(v0.begin(),v0.end());
75     cout<<"最小值:"<<*(ans.first)<<endl;
76     cout<<"最小值:"<<*(ans.second)<<endl;
77     
78     //求均值
79     double avg1=accumulate(v0.begin(),v0.end(),0.0)/v0.size();
80     cout<<"均值:"<<fixed<<setprecision(2)<<avg1<<endl;
81     
82     sort(v0.begin(),v0.end());
83     double avg2=accumulate(v0.begin()+1,v0.end()-1,0.0);
84     cout<<"去掉最大值,最小值之后,均值:"<<avg2<<endl;
85      
86 } 

屏幕截图 2025-10-11 085958

 

问题1:generate算法生成某个区间内的随机数来填充某个数组

问题2:minmax_element()可以一步求出最大值和最小值,其结果有两个值,第一个是最小值,第二个是最大值,代码较简单

问题3:效果相同   lambda表达式第一个参数[ ]是捕获列表,第二个参数( )是参数列表,与普通函数相同,如果不需要参数即省略 第三个参数是函数体

 

实验3:

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<cctype>
 5 
 6 unsigned char func(unsigned char c);
 7 
 8 void test1();
 9 void test2();
10 
11 int main(){
12     std::cout<<"测试1:字符串大小写转换\n";
13     test1();
14     
15     std::cout<<"\n测试2:字符变换\n";
16     test2();
17     
18 } 
19 
20 unsigned char func(unsigned char c){
21     if(c=='z')
22     return 'a';
23     
24     if(c=='Z')
25     return 'A';
26     
27     if(std::isalpha(c))
28     return static_cast<unsigned char>(c+1);
29     
30     return c;
31 }
32 
33 void test1(){
34     std::string s1{"Hello World 2049!"};
35     std::cout<<"s1="<<s1<<'\n';
36     
37     std::string s2;
38     for(auto  c:s1)
39         s2+=std::tolower(c);/////
40     std::cout<<"s2="<<s2<<'\n';
41     
42     std::string s3;
43     for(auto  c:s1)
44         s3+=std::toupper(c);/////
45     std::cout<<"s3="<<s3<<'\n';
46 }
47 
48 void test2(){
49     std::string s1{"I love comos!"};
50     std::cout<<"s1="<<s1<<'\n';
51     
52     std::string s2(s1.size(),' ');
53     std::transform(s1.begin(),s1.end(),s2.begin(),func);
54     std::cout<<"s2="<<s2<<'\n';
55 }

屏幕截图 2025-10-11 093358

问题1:func()功能是将字符串中的字符改变为比自身ASCII编码值大1的字符

问题2:tolower 作用是如果所给字符是大写则改为小写,若是小写则保持不变;    toupper作用是如果所给字符是小写则改为大写,若是大写则保持不变

问题3:第一个参数的意义是被处理的字符串的开始位置,第二个参数的意义是被处理的字符串的结束位置,第三个参数的意义是处理后的字符串的开始存储位置,第四个参数的意义是对于该字符串进行的操作;   s2没有输出结果

 

实验4:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 
 5 bool is_palindrome(const std::string &s);
 6 bool is_palindrome_ignore_case(const std::string &s);
 7 
 8 int main(){
 9     using namespace std;
10     string s;
11     
12     //多组输入,直到按下ctrl+Z结束测试
13     while(cin>>s){
14         cout<<boolalpha
15             <<"区分大小写:"<<is_palindrome(s)<<"\n"
16             <<"不区分大小写:"<<is_palindrome_ignore_case(s)<<"\n\n"; 
17     } 
18 }
19 
20 bool is_palindrome(const std::string &s){
21     std::string s1{s};
22     
23     reverse(s1.begin(),s1.end());
24     
25     if(s1==s)
26         return true;
27     else
28         return false;
29     
30 }
31 
32 bool is_palindrome_ignore_case(const std::string &s){
33     std::string s1{s};
34     std::string s2;
35     
36     for(auto c:s1)
37         s2+=std::tolower(c);
38     
39     std::string s3{s2};
40     reverse(s3.begin(),s3.end());
41         
42     if(s2==s3)
43         return true;
44     else
45         return false;
46     
47 }

屏幕截图 2025-10-11 170303

问题: 可以使用std::getline函数

 

实验5:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 
 5 std::string dec2n(int x,int n=2);
 6 
 7 int main(){
 8     int x;
 9     while(std::cin>>x){
10         std::cout<<"十进制:"<<x<<"\n"
11                 << "二进制:"<<dec2n(x)<<"\n"
12                 <<"八进制:"<<dec2n(x,8)<<"\n"
13                 <<"十二进制:"<<dec2n(x,12)<<"\n"
14                 <<"十六进制:"<<dec2n(x,16)<<"\n"
15                 <<"三十二进制:"<<dec2n(x,32)<<"\n\n";
16     }
17 } 
18 
19 std::string dec2n(int x,int n){
20     std::string ans;
21     std::string t{"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
22     int result;
23     
24     if(x==0)
25     return "0";
26     
27      while(x>0){
28          result=x%n;
29          ans+=t[result];
30          x=x/n;
31      }
32      
33      reverse(ans.begin(),ans.end());
34      
35      return ans;
36 }

屏幕截图 2025-10-12 171602

 

 

实验6:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 
 5 void print(int x);
 6 int main(){
 7     
 8      for(int i=0;i<=26;++i)
 9          print(i);
10     
11 }
12 
13 void print(int x){
14     std::string c{"abcdefghijklmnopqrstuvwxyz"};
15     
16     std::string c1;
17     for(auto i:c)
18         c1+=std::toupper(i);
19         
20     if(x==0){
21         std::cout<<"  ";
22         for(auto i:c)
23             std::cout<<' '<<i;
24             
25         std::cout<<"\n";
26     }
27     else{
28         if(x<=9)
29         std::cout<<" "<<x;
30         else
31         std::cout<<x;
32         
33         rotate(c1.begin(),c1.begin()+x,c1.end());
34         for(auto i:c1)
35             std::cout<<" "<<i;
36         std::cout<<"\n";
37     }
38 }

屏幕截图 2025-10-12 175013

 

实验7:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<ctime>
 5 #include<cstdlib>
 6 #include<iomanip>
 7 
 8 int main(){
 9     int n=10;
10     int num1,num2; 
11     int ans;
12     double x=0.0;
13     int result;
14     int temp;
15     srand((unsigned int)time(NULL));
16     
17     char sign[]={'+','-','*','/'};
18     
19     while(n>=1){
20         char randomsign=sign[rand()%4];
21         if(randomsign=='+'){
22             num1=rand()%10+1;
23             num2=rand()%10+1;
24             ans=num1+num2;
25             std::cout<<num1<<"+"<<num2<<"=";
26             std::cin>>result;
27             if(ans==result)
28                 x++;
29             n--;
30         
31         }
32         else if(randomsign=='-'){
33             num1=rand()%10+1;
34             num2=rand()%num1+1;
35             ans=num1-num2;
36             std::cout<<num1<<"-"<<num2<<"=";
37             std::cin>>result;
38             if(ans==result)
39                 x++;
40             n--;
41         
42         }
43         else if(randomsign=='*'){
44             num1=rand()%10+1;
45             num2=rand()%10+1;
46             ans=num1*num2;
47             std::cout<<num1<<"*"<<num2<<"=";
48             std::cin>>result;
49             if(ans==result)
50                 x++;
51             n--;
52         }
53         else{
54             num2=rand()%10+1;
55             do{
56                 temp=rand()%10+1;
57                 num1=temp*num2;
58             }while(num1>10);
59             ans=num1/num2;
60             std::cout<<num1<<"/"<<num2<<"=";
61             std::cin>>result;
62             if(ans==result)
63                 x++;
64             n--;
65         }
66     } 
67     std::cout<<x;
68     x*=10;
69     std::cout<<"正确率:";
70     std::cout<<std::fixed<<std::setprecision(2)<<x<<"%";
71 
72 }

屏幕截图 2025-10-12 231940

 

posted @ 2025-10-12 23:20  sunishope  阅读(10)  评论(1)    收藏  举报