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

任务一

源代码

 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 
11 void test1();
12 void test2();
13 void test3();
14 
15 int main() {
16     cout << "测试1: \n";
17     test1();
18 
19     cout << "\n测试2: \n";
20     test2();
21 
22     cout << "\n测试3: \n";
23     test3();
24 }
25 
26 
27 //输出c中的元素,c容器对象
28 template<typename T>
29 void output(const T& c) {
30     for (auto& i : c)
31         cout << i << " ";
32     cout << endl;
33 }
34 
35 
36 
37 //组合使用算法库,迭代器,string反转字符串
38 void test1() {
39     string s0{ "0123456789" };
40     cout << "s0=" << s0 << endl;
41 
42     string s1{ s0 };
43     reverse(s1.begin(), s1.end());
44     cout << "s1 = " << s1 << endl;
45 
46     string s2{ s0 };
47     reverse_copy(s0.begin(), s0.end(), s2.begin());
48     cout << "s2 = " << s2 << endl;
49 }
50 
51 
52 
53 //vector反转动态数组
54 void test2() {
55     vector<int>v0{ 2,0,4,9 };
56     cout << "v0: ";
57     output(v0);
58 
59     vector<int>v1{ v0 };
60     reverse(v1.begin(), v1.end());
61     cout << "v1:";
62     output(v1);
63 
64     vector<int>v2{ v0 };
65     reverse_copy(v0.begin(), v0.end(), v2.begin());
66     cout << "v2:";
67     output(v2);
68 }
69 
70 
71 //vector元素旋转移位
72 void test3() {
73     vector<int>v0{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
74     cout << "v0:";
75     output(v0);
76 
77     vector<int>v1{ v0 };
78     rotate(v1.begin(), v1.begin() + 1, v1.end());
79     cout << "v1:";
80     output(v1);
81 
82     vector<int> v2{ v0 };
83     rotate(v2.begin(), v2.begin() + 2, v2.end());
84     cout << "v2: ";
85     output(v2);
86 
87     vector<int> v3{ v0 };
88     rotate(v3.begin(), v3.end() - 1, v3.end());
89     cout << "v3: ";
90     output(v3);
91 
92     vector<int> v4{ v0 };
93     rotate(v4.begin(), v4.end() - 2, v4.end());
94     cout << "v4: ";
95     output(v4);
96 
97 }
View Code

运行结果

 

 

任务二

源代码

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <algorithm>
 5 #include <numeric>
 6 #include <iomanip>
 7 
 8 using namespace std;
 9 
10 template<typename T>
11 void output(const T& c);
12 
13 int rand_int_100();
14 void test1();
15 void test2();
16 
17 int main() {
18     cout << "测试1: \n";
19     test1();
20 
21     cout << "\n测试2: \n";
22     test2();
23 }
24 
25 //输出容器对象c中的元素
26 template<typename T>
27 void output(const T& c) {
28     for (auto& i : c)
29         cout << i << " ";
30     cout << endl;
31 }
32 
33 
34 //返回[0,100]内一随机整数
35 int rand_int_100() {
36     return rand() % 101;
37 }
38 
39 //赋值并排序
40 void test1() {
41     vector<int>v0(10);
42     generate(v0.begin(), v0.end(), rand_int_100);
43     cout << "v0:";
44     output(v0);
45 
46     vector<int> v1{ v0 };
47     sort(v1.begin(), v1.end());//默认是升序排序
48     cout << "v1: ";
49     output(v1);
50 
51     vector<int>v2{ v0 };
52     sort(v2.begin() + 1, v2.end() - 1);
53     cout << "v2:";
54     output(v2);
55 }
56 
57 //赋值并求最值和均值
58 void test2() {
59     vector<int>v0(10);//不是{},()是指定vector的大小
60     generate(v0.begin(), v0.end(), rand_int_100);
61     cout << "v0:";
62     output(v0);
63 
64     auto iter1 = min_element(v0.begin(), v0.end());
65     cout << "最小值: " << *iter1 << endl;
66 
67     auto iter2 = max_element(v0.begin(), v0.end());
68     cout << "最大值: " << *iter2 << endl;
69 
70     auto ans = minmax_element(v0.begin(), v0.end());
71     cout << "最小值:" << *(ans.first) << endl;
72     cout << "最大值:" << *(ans.second) << endl;
73 
74     double avg1 = accumulate(v0.begin(), v0.end(), 0) / v0.size();
75     cout << "均值:" << fixed << setprecision(2) << avg1 << endl;
76 
77     cout << endl;
78     vector<int>v1{ v0 };
79     cout << "v0:";
80     output(v0);
81     sort(v1.begin(), v1.end());
82     double avg2 = accumulate(v1.begin() + 1, v1.end() - 1, 0) / (v1.size() - 2);
83     cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
84 }
View Code

运行结果

 

 

任务三

源代码

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 bool is_palindrome(std::string s);
 5 
 6 int main()
 7 {
 8     using namespace std;
 9     string s;
10     while (cin >> s)
11         cout << boolalpha << is_palindrome(s) << endl;
12 }
13 bool is_palindrome(std::string s)
14 {
15     std::string s0{ s };
16     reverse(s0.begin(), s0.end());
17     if (s0 == s)
18         return true;
19     else
20         return false;
21 }
View Code

运行结果

 

 

任务四

源代码

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 
 5 //用字符串流实现
 6 #include<sstream>
 7 #include<bitset>
 8 
 9 
10 std::string dec2n(int x, int n = 2);
11 
12 int main() {
13     using namespace std;
14     int x;
15     while (cin >> x) {
16         cout << "十进制: " << x << endl;
17         cout << "二进制: " << dec2n(x) << endl;
18         cout << "八进制: " << dec2n(x, 8) << endl;
19         cout << "十六进制: " << dec2n(x, 16) << endl << endl;
20     }
21 }
22 
23 std::string dec2n(int x, int n) {
24     using namespace std;
25     
26     stringstream num;
27 
28     //二进制
29     if (n == 2)
30     {
31         num.str("");//清空stringstream
32         num << bitset<8>(x).to_string();
33         //bitset<int>限制位数,to_string将int类型转化为string
34         //但是多余的0要怎么去掉?
35         return num.str();
36     }
37 
38     if (n == 8)
39     {
40         num.str("");
41         num << oct << x;//oct转化为八进制
42         return num.str();
43     }
44 
45     if (n == 16)
46     {
47         num.str("");
48         num << hex << uppercase << x;//hex十六进制,uppercase大写
49         return num.str();
50     }
51 }
View Code

//可以尝试用栈实现

 

运行结果

 

 

任务五

 源代码

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<algorithm>
 5 #include<iomanip>
 6 
 7 using namespace std;
 8 
 9 template<typename T>
10 void output(const T& c) {
11     for (auto& i : c)
12         cout << i << " ";
13     cout << endl;
14 }
15 
16 void test1();
17 
18 int main()
19 {
20     test1();
21     return 0;
22 }
23 
24 void test1()
25 {
26     //用字符串初始化vector
27     string str1 = "abcdefghijklmnopqrstuvwxyz";
28     //string str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
29     vector<char> vec1(str1.begin(), str1.end());
30     //vector<char> vec2(str2.begin(), str2.end());
31 
32     //输出第一列
33     cout << setw(3)<<" ";
34     output(str1);
35 
36     //转换为大写字母
37     for (auto& c : vec1) {
38         if (std::islower(c)) {
39             c = toupper(static_cast<unsigned char>(c));
40         }
41     }
42     int i = 1;
43     for (i = 1; i < 27; i++)
44     {
45         cout <<setw(2)<< i;
46         cout << " ";
47         rotate(vec1.begin(), vec1.begin() + 1, vec1.end());
48         output(vec1);
49     }
50 }
View Code

 

运行截图

 

 

任务六

 

源代码

 1 #include<iostream>
 2 #include<random>
 3 #include<string>
 4 #include<iomanip>
 5 
 6 using namespace std;
 7 
 8 void test();
 9 
10 int main() {
11     test();
12     return 0;
13 }
14 
15 void test() {
16     //参考了ai
17     random_device rd;//获取随机数种子
18     mt19937 gen(rd());//随机数生成器
19 
20     uniform_int_distribution<>dis(1, 10);//限定范围在1到10
21 
22     int correct = 0;
23 
24     for (int i = 0; i < 10; i++) {
25         int n1 = dis(gen);
26         int n2 = dis(gen);
27 
28         char op=0;//加减乘除
29         int ans=0;//答案
30 
31 
32         ////如果不能整除
33         //if ((op == '/') && (n1 % n2) != 0) {
34         //    do {
35         //        n2 = dis(gen);
36         //    } while (n1 % n2 != 0);
37         //}
38 
39         do {
40             int rad = gen()%4;
41             switch (rad) {
42             case 0:op = '/'; break;
43             case 1:op = '+'; break;
44             case 2:op = '*'; break;
45             case 3:op = '-'; break;
46             }
47 
48             //要让n1比n2大
49             if (op == '-')
50             {
51                 if (n1 <= n2) {
52                     swap(n1, n2);
53                 }
54             }
55         } while ((op == '/') && (n1 % n2 != 0));
56 
57 
58 
59         //计算答案
60         switch (op){
61         case '+':ans = n1 + n2; break;
62         case'-':ans = n1 - n2; break;
63         case'*':ans = n1 * n2; break;
64         case'/':ans = n1 / n2; break;
65         }
66 
67 
68         //显示题目,获取答案
69         cout << n1 << " " << op << " " << n2 << " = ";
70         int ans_user;
71         cin >> ans_user;
72 
73 
74         //判断,统计正确个数
75         if (ans_user == ans)correct++;
76     }
77     
78     //输出正确率
79     double crt = static_cast<double>(correct) / 10 * 100;
80     cout << fixed << setprecision(2) << "正确率:" << crt << "%" << endl;
81 }
View Code

 

运行截图(出现➗的概率好小啊)

 

 

 

 

实验总结

 

posted @ 2024-10-09 20:18  姚润勰  阅读(13)  评论(0编辑  收藏  举报