实验1

任务1:

源代码:

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

截图:

image

Q1:reverse 直接反转原容器元素,reverse_copy 不修改原容器,将反转结果复制到目标容器。

Q2:rotate 通过将 [begin, mid) 与 [mid,end) 区间元素互换实现循环移位;三个参数分别是区间起始、旋转中点、区间结束的迭代器。

 

任务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 << "\n测试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: "; output(v0);
46 
47     vector<int> v1{v0};
48     sort(v1.begin(), v1.end()); // 对整个vector排序
49     cout << "v1: "; output(v1);
50 
51     vector<int> v2{v0};
52     sort(v2.begin()+1, v2.end()-1); // 只对中间部分排序,不包含首尾元素
53     cout << "v2: "; output(v2);
54 }
55 
56 // 测试2:对容器类对象指定迭代器区间赋值、计算最大值/最小值/均值
57 void test2() {
58     using namespace std;
59 
60     vector<int> v0(10);  
61     generate(v0.begin(), v0.end(), generate_random_number); 
62     cout << "v0: "; output(v0);
63 
64     // 求最大值和最小值
65     auto min_iter = min_element(v0.begin(), v0.end());
66     auto max_iter = max_element(v0.begin(), v0.end());
67     cout << "最小值: " << *min_iter << endl;
68     cout << "最大值: " << *max_iter << endl;
69 
70     // 同时求最大值和最小值
71     auto ans = minmax_element(v0.begin(), v0.end());
72     cout << "最小值: " << *(ans.first) << endl;
73     cout << "最大值: " << *(ans.second) << endl;
74 
75     // 求平均值
76     double avg1 = accumulate(v0.begin(), v0.end(), 0.0) / v0.size();
77     cout << "均值: " << fixed << setprecision(2) << avg1 << endl;
78 
79     sort(v0.begin(), v0.end());
80     double avg2 = accumulate(v0.begin()+1, v0.end()-1, 0.0) / (v0.size()-2);
81     cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
82 }
task2

截图:

image

Q1:通过调用第三个参数指定的函数生成值,填充区间内的元素。

Q2:minmax_element 一次遍历即可同时获取最大和最小值,效率高于分别调用 min_element 和 max_element。

Q3:lambda 表达式适用于需临时使用的函数逻辑,使得代码更简洁;自定义函数更适合复杂的场景。

 

任务3:

源代码:

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

截图:

image

Q1:func 函数将字母字符转为下一个字母(z 转为 a,Z 转为 A),非字母字符保持不变。

Q2:tolower 将字符转为小写,toupper 将字符转为大写。

Q3:transform 的 4 个参数分别是源区间起始、源区间结束、目标区间起始、变换函数;若第 3 个参数改为 s1.begin (),则会在原字符串上直接修改。

 

任务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 
21 // 函数is_palindrome定义
22 bool is_palindrome(const std::string &s){
23     int left = 0;
24     int right = s.size() - 1;
25     while (left < right) {
26         if (s[left] != s[right]) {
27             return false;
28         }
29         left++;
30         right--;
31     }
32     return true;
33 }
34 
35 
36 // 函数is_palindrome_ignore_case定义
37 bool is_palindrome_ignore_case(const std::string &s){
38     int left = 0;
39     int right = s.size() - 1;
40     while (left < right) {
41         if (std::tolower(s[left]) != std::tolower(s[right])) {
42             return false;
43         }
44         left++;
45         right--;
46     }
47     return true;
48 }
task4

截图:

image

Q:将 cin >> s 改为 getline(cin, s) 。

 

任务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 // 函数dec2n定义
20 std::string dec2n(int x, int n) {
21     if (x == 0) {
22         return "0";
23     }
24     std::string result;
25     while (x > 0) {
26         int tem = x % n; 
27         char c = (tem < 10) ? (tem + '0') : (tem - 10 + 'A');
28         result.push_back(c);
29         x = x / n;
30     }
31     std::reverse(result.begin(), result.end());
32     return result;
33 }
task5

截图:

image

 

任务6:

源代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<iomanip>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int main(){
 8     string s{"abcdefghigklmnopqrstuvwxyz"};
 9     cout<<"   ";
10     for(auto i:s)
11         cout<<right<<setw(3)<<i;
12     cout<<endl;
13     string s2;
14     for(auto c:s)
15         s2+=toupper(c);
16     for(int i=1;i<=26;i++){
17         cout<<setw(3)<<i;
18         rotate(s2.begin(), s2.begin()+1, s2.end());
19         for(auto i:s2)
20             cout<<setw(3)<<i;
21         cout<<endl;
22     }
23     return 0;
24 } 
task6

截图:

image

 

任务7:

源代码:

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <numeric>
 5 #include <iomanip>
 6 #include <cstdlib>
 7 #include <ctime>
 8 using namespace std;
 9 
10 int main() {
11     srand(time(0));
12     int correct = 0;
13 
14     for (int i = 1; i <= 10; i++) {
15         int a, b, result;
16         char op;
17         
18         a = rand() % 10 + 1;
19         b = rand() % 10 + 1;
20         
21         int tmp = rand() % 4;
22         switch (tmp) {
23             case 0:
24                 op = '+';
25                 result = a + b;
26                 break;
27             case 1:
28                 op = '-';
29                 if (a < b) {
30                     swap(a, b);
31                 }
32                 result = a - b;
33                 break;
34             case 2:
35                 op = '*';
36                 result = a * b;
37                 break;
38             case 3:
39                 op = '/';
40                 a = (rand() % 10 + 1) * b;
41                 if (a > 10) {
42                     a = b;
43                 }
44                 result = a / b;
45                 break;
46         }
47         cout <<a<< " " << op << " " << b << " = ";
48         int write;
49         cin >> write;
50 
51         if (write == result) {
52             correct++;
53         }
54     }
55 
56     double accuracy = (static_cast<double>(correct) / 10) * 100;
57     cout << "\n正确率: " << fixed << setprecision(2) << accuracy << "%" << endl;
58     return 0;
59 }
task7

截图:

image

 

实验总结:

在代码探索中学习到了许多库函数,如 reverse,rotate,generate,minmax_element,tolower ,toupper,transform,getline。

 

posted @ 2025-10-15 20:16  lei1459  阅读(17)  评论(1)    收藏  举报