實驗1 C++

dingdong。發表於2024-10-14

task1:

實驗1 C++
 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 C++
 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 C++
 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 C++
 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 C++
 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 C++
 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

執行結果截圖:

相關文章