实验1 C++

task1:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 // 声明
 9 // 模板函数声明
10 template<typename T>
11 void output(const T &c);
12 
13 // 普通函数声明
14 void test1();
15 void test2();
16 void test3();
17 
18 int main() {
19     cout << "测试1: \n";
20     test1();
21 
22     cout << "\n测试2: \n";
23     test2();
24 
25     cout << "\n测试3: \n";
26     test3();
27 }
28 
29 // 函数实现
30 // 输出容器对象c中的元素
31 template <typename T>
32 void output(const T &c) {
33     for(auto &i: c)
34         cout << i << " ";
35     cout << endl;
36 }
37 
38 // 测试1
39 // 组合使用算法库、迭代器、string反转字符串
40 void test1() {
41     string s0{"0123456789"};
42     cout << "s0 = " << s0 << endl;
43 
44     string s1{s0};
45     reverse(s1.begin(), s1.end());  // 反转指定迭代器区间的元素
46     cout << "s1 = " << s1 << endl;
47 
48     string s2{s0};
49     reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝到指定迭代器开始的目标区间,并且在复制过程中反转次序
50     cout << "s2 = " << s2 << endl;
51 }
52 
53 // 测试2
54 // 组合使用算法库、迭代器、vector反转动态数组对象vector内数据
55 void test2() {
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
72 // 组合使用算法库、迭代器、vector实现元素旋转移位
73 void test3() {
74     vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
75     cout << "v0: ";
76     output(v0);
77 
78     vector<int> v1{v0};
79     rotate(v1.begin(), v1.begin()+1, v1.end());  // 旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
80     cout << "v1: ";
81     output(v1);
82 
83     vector<int> v2{v0};
84     rotate(v2.begin(), v2.begin()+2, v2.end());
85     cout << "v2: ";
86     output(v2);
87 
88     vector<int> v3{v0};
89     rotate(v3.begin(), v3.end()-1, v3.end());
90     cout << "v3: ";
91     output(v3);
92 
93     vector<int> v4{v0};
94     rotate(v4.begin(), v4.end()-2, v4.end());
95     cout << "v4: ";
96     output(v4);
97 }
View Code

 

运行结果截图:

 

task2:

 

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 // 声明
 9 // 模板函数声明
10 template<typename T>
11 void output(const T &c);
12 
13 // 普通函数声明
14 void test1();
15 void test2();
16 void test3();
17 
18 int main() {
19     cout << "测试1: \n";
20     test1();
21 
22     cout << "\n测试2: \n";
23     test2();
24 
25     cout << "\n测试3: \n";
26     test3();
27 }
28 
29 // 函数实现
30 // 输出容器对象c中的元素
31 template <typename T>
32 void output(const T &c) {
33     for(auto &i: c)
34         cout << i << " ";
35     cout << endl;
36 }
37 
38 // 测试1
39 // 组合使用算法库、迭代器、string反转字符串
40 void test1() {
41     string s0{"0123456789"};
42     cout << "s0 = " << s0 << endl;
43 
44     string s1{s0};
45     reverse(s1.begin(), s1.end());  // 反转指定迭代器区间的元素
46     cout << "s1 = " << s1 << endl;
47 
48     string s2{s0};
49     reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝到指定迭代器开始的目标区间,并且在复制过程中反转次序
50     cout << "s2 = " << s2 << endl;
51 }
52 
53 // 测试2
54 // 组合使用算法库、迭代器、vector反转动态数组对象vector内数据
55 void test2() {
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
72 // 组合使用算法库、迭代器、vector实现元素旋转移位
73 void test3() {
74     vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
75     cout << "v0: ";
76     output(v0);
77 
78     vector<int> v1{v0};
79     rotate(v1.begin(), v1.begin()+1, v1.end());  // 旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
80     cout << "v1: ";
81     output(v1);
82 
83     vector<int> v2{v0};
84     rotate(v2.begin(), v2.begin()+2, v2.end());
85     cout << "v2: ";
86     output(v2);
87 
88     vector<int> v3{v0};
89     rotate(v3.begin(), v3.end()-1, v3.end());
90     cout << "v3: ";
91     output(v3);
92 
93     vector<int> v4{v0};
94     rotate(v4.begin(), v4.end()-2, v4.end());
95     cout << "v4: ";
96     output(v4);
97 }
View Code

 

运行结果截图:

 

 

task3:

 

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 
 5 bool is_palindrome(std::string s);
 6 
 7 int main() {
 8     using namespace std;
 9     string s;
10 
11     while (cin >> s)  // 多组输入,直到按下Ctrl+Z后结束测试
12         cout << boolalpha << is_palindrome(s) << endl;
13 }
14 
15 bool is_palindrome(std::string s) {
16     std::string s1(s);
17     std::reverse(s.begin(), s.end());
18     if (s == s1)return true;
19     else return false;
20 }
21 // 函数is_palindrom定义
22 // 待补足
23 // ×××
View Code

 

运行结果截图:

 

 

task4:

 

 1 #include <iostream>  
 2 #include <string>  
 3 #include <algorithm>  
 4 
 5 std::string dec2n(int x, int n = 2);
 6 
 7 int main() {
 8     using namespace std;
 9     int x;
10 
11     while (cin >> x) {
12         cout << "十进制: " << x << endl;
13         cout << "二进制: " << dec2n(x, 2) << endl;
14         cout << "八进制: " << dec2n(x, 8) << endl;
15         cout << "十六进制: " << dec2n(x, 16) << endl << endl;
16     }
17 }
18  
19 std::string dec2n(int x, int n) {
20     if (x == 0) return "0";
21     const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
22     std::string result;
23     bool isNegative = x < 0;
24     if (isNegative) x = -x;  
25     while (x > 0) {
26         result += digits[x % n];
27         x /= n;
28     }
29     if (isNegative) result += '-'; 
30     std::reverse(result.begin(), result.end());  
31     return result;
32 }
View Code

 

运行结果截图:

 

 

task5:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 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         char result = 'A' + i;
13         cout << result << " ";
14     }
15     cout << endl;
16 }
17 
18 void test();
19 
20 int main() {
21     cout <<"   a b c d e f g h i j k l m n o p q r s t u v w x y z" << endl;
22     test();
23     return 0;
24 }
25 
26 void test() {
27     /*char v0[26],v1[26];
28     for (int i = 0; i <= 25; i++) {
29         v0[i] = 'A' + i;
30     }
31     rotate(v0.begin(), v0.begin + 1, v0.end());*/
32     vector<int>v0{ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25 };
33     for (int i = 0; i < 26; i++) {
34         cout << setw(2) << i + 1 << ' ';
35         rotate(v0.begin(), v0.begin() + 1, v0.end());
36         output(v0);
37     }
38 }
View Code

 

运行结果截图:

 

 

task6:

 

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<ctime>
 4 #include<iomanip>
 5 
 6 int main() {
 7     std::srand(static_cast<unsigned int>(std::time(0)));
 8 
 9     const int numQuestions = 10;
10     int correctAnswers = 0;
11 
12     for (int i = 0; i < numQuestions; ++i) {
13         int operand1 = std::rand() % 10 + 1;
14         int operand2 = std::rand() % 10 + 1;
15         char operation;
16 
17         do {
18             operation = "+-*/"[std::rand() % 4];
19             if (operation == '-') {
20                 while (operand1 <= operand2) {
21                     operand1 = std::rand() % 10 + 1;
22                     operand2 = std::rand() % 10 + 1;
23                 }
24             }
25             else if (operation == '/') {
26                 while (operand1 % operand2 != 0) {
27                     operand1 = std::rand() % 10 + 1;
28                     operand2 = std::rand() % 10 + 1;
29                 }
30             }
31         } while ((operation == '-' && operand1 <= operand2) || (operation == '/' && operand1 % operand2 != 0));
32         int correctResult;
33         switch (operation) {
34         case '+':correctResult = operand1 + operand2; break;
35         case '-':correctResult = operand1 - operand2; break;
36         case '*':correctResult = operand1 * operand2; break;
37         case '/':correctResult = operand1 / operand2; break;
38         }
39 
40         std::cout << operand1 << " " << operation << " " << operand2 << " = ";
41 
42         int userAnswer;
43         std::cin >> userAnswer;
44         
45         if (userAnswer == correctResult) {
46             ++correctAnswers;
47         }
48     }
49 
50     double accuracy = static_cast<double>(correctAnswers) / numQuestions * 100;
51     std::cout << "你的正确率是: " << std::fixed << std::setprecision(2) << accuracy << "%" << std::endl;
52 
53     return 0;
54 }
View Code

 

运行结果截图:

 

 

posted @ 2024-10-14 18:13  dingdong。  阅读(2)  评论(0编辑  收藏  举报