實驗1 現代C++程式設計初體驗

花落遥發表於2024-10-09

1. 實驗任務1

 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 }

2. 實驗任務2

 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 // 函式宣告
11 // 模板函式宣告
12 template<typename T>
13 void output(const T& c);
14 
15 // 普通函式宣告
16 int rand_int_100();
17 void test1();
18 void test2();
19 
20 int main() {
21     cout << "測試1: \n";
22     test1();
23 
24     cout << "\n測試2: \n";
25     test2();
26 }
27 
28 // 函式實現
29 // 輸出容器物件c中的元素
30 template <typename T>
31 void output(const T& c) {
32     for (auto& i : c)
33         cout << i << " ";
34     cout << endl;
35 }
36 
37 // 返回[0, 100]區間內的一個隨機整數
38 int rand_int_100() {
39     return rand() % 101;
40 }
41 
42 // 測試1
43 // 對容器類物件指定迭代器區間進行賦值、排序
44 void test1() {
45     vector<int> v0(10);  // 建立一個動態陣列物件v0, 物件大小為10
46     generate(v0.begin(), v0.end(), rand_int_100); // 產生[0, 100]之間的隨機整數賦值給指定迭代器區間[v0.begin(), v0.end())內的每個資料項
47     cout << "v0: ";
48     output(v0);
49 
50     vector<int> v1{ v0 };
51     sort(v1.begin(), v1.end()); // 對指定迭代器區間[v1.begin(), v1.end())內資料項進行升序排序
52     cout << "v1: ";
53     output(v1);
54 
55     vector<int> v2{ v0 };
56     sort(v2.begin() + 1, v2.end() - 1); // 對指定迭代器區間[v1.begin()+1, v1.end()-1)內資料項進行升序排序
57     cout << "v2: ";
58     output(v2);
59 }
60 
61 // 測試2
62 // 對容器類物件指定迭代器區間進行賦值、計算最大值/最小值/均值
63 void test2() {
64     vector<int> v0(10);
65     generate(v0.begin(), v0.end(), rand_int_100);
66     cout << "v0: ";
67     output(v0);
68 
69     auto iter1 = min_element(v0.begin(), v0.end());
70     cout << "最小值: " << *iter1 << endl;
71 
72     auto iter2 = max_element(v0.begin(), v0.end());
73     cout << "最大值: " << *iter2 << endl;
74 
75     auto ans = minmax_element(v0.begin(), v0.end());
76     cout << "最小值: " << *(ans.first) << endl;
77     cout << "最大值: " << *(ans.second) << endl;
78     double avg1 = accumulate(v0.begin(), v0.end(), 0) / v0.size();
79     cout << "均值: " << fixed << setprecision(2) << avg1 << endl;
80 
81     cout << endl;
82 
83     vector<int> v1{ v0 };
84     cout << "v0: ";
85     output(v0);
86     sort(v1.begin(), v1.end());
87     double avg2 = accumulate(v1.begin() + 1, v1.end() - 1, 0) / (v1.size() - 2);
88     cout << "去掉最大值、最小值之後,均值: " << avg2 << endl;
89 }

3. 實驗任務3

 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 // 函式is_palindrom定義
16 // 待補足
17 // 
18 //方案一:逐個比較
19 /*using std::string;
20 bool is_palindrome(string s)
21 {
22     int len = s.size()-1;
23     for (int i = 0; i <= len; i++,len--)
24     {
25         if (s[i] != s[len])
26             return false;
27     }
28     return true;
29 }*/
30 
31 //方案二:全部反轉
32 using std::string;
33 bool is_palindrome(string s)
34 {
35     string reversed_s = s;
36     std::reverse(reversed_s.begin(), reversed_s.end());
37     return s == reversed_s;
38 }

兩個方法輸出結果一致

4. 實驗任務4

 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 
10     int x;
11     while (cin >> x) {
12         cout << "十進位制: " << x << endl;
13         cout << "二進位制: " << dec2n(x) << endl;
14         cout << "八進位制: " << dec2n(x, 8) << endl;
15         cout << "十六進位制: " << dec2n(x, 16) << endl << endl;
16     }
17 }
18 
19 // 函式dec2n定義
20 // 待補足
21 using std::string;
22 string dec2n(int x, int n)
23 {
24     string result;
25     if (x == 0)
26         result = "0";
27     while (x>0) 
28     {
29         int r = x % n; 
30         if (r >= 10) 
31         {
32             result += (char)(r - 10 + 'A'); // A-F  
33         }
34         else 
35         {
36             result += std::to_string(r); //to_string把數字轉化為字串
37         }
38         x /= n;
39     }
40     std::reverse(result.begin(), result.end()); // 反轉結果  
41     return result;
42 }

5. 實驗任務5

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<vector>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     //輸出第一行
10     cout << setw(2) << " ";
11     for (int i = 97; i <= 122; i++)
12     {
13         cout << setw(2) << static_cast<char>(i);
14     }
15     cout << endl;
16 
17     vector<char> letters(26);
18     for (int i = 0; i < 26; i++)
19     {
20         letters[i] = static_cast<char>(65 + i);
21     }
22 
23     for (int i = 1; i <= 26; i++)//輸出每一行
24     {
25         char t = letters[0];
26         letters.erase(letters.begin());
27         letters.push_back(t);
28         cout << setw(2) << i;
29         for (int j = 0; j < 26; j++)
30         {
31             cout << setw(2) << letters[j];
32         }
33         cout << endl;
34     }
35     return 0;
36 }

6. 實驗任務6

 1 #include<iostream>
 2 #include<random>
 3 #include<iomanip> 
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     mt19937 generator;
10     uniform_int_distribution<int> dis1(1, 10);
11     uniform_int_distribution<int> dis2(1, 4);
12     //不是,這東西要全背?
13     int count = 1, right = 0;
14     while (count <= 10)
15     {
16         int a = dis1(generator), b = dis1(generator), c = dis2(generator),answer;
17         if (c == 1)
18         {
19             cout << a << " " << "+" << " " << b << " " << "=";
20             cin >> answer;
21             if (answer == a + b)
22                 right++;
23             count++;
24             //本來還想寫cout<<endl; 後來發現cin後會按回車手動換行
25         }
26 
27         else if (c == 2 && a > b)
28         {
29             cout << a << " " << "-" << " " << b << " " << "=";
30             cin >> answer;
31             if (answer == a - b)
32                 right++;
33             count++;
34         }
35 
36         else if (c == 3)
37         {
38             cout << a << " " << "*" << " " << b << " " << "=";
39             cin >> answer;
40             if (answer == a * b)
41                 right++;
42             count++;
43         }
44 
45         else if (c == 4 && a % b == 0)
46         {
47             cout << a << " " << "/" << " " << b << " " << "=";
48             cin >> answer;
49             if (answer == a / b)
50                 right++;
51             count++;
52         }
53     }
54     cout << "正確率:" << fixed<< setprecision(2)<<(right * 10.0) << "%" << endl;
55     return 0;
56 }

實驗總結

C++的確比C方便很多,尤其是在那個:

std::reverse(reversed_s.begin(), reversed_s.end())

反轉字串,還有:

result += std::to_string(r)

數字轉為字串,不然我還要寫for迴圈去一個一個調換,一位一位轉化錄入。

《實驗1》裡的網站也很好用,詳細介紹各種資料結構和方法。

不過我還要做好2點:

1.多敲程式碼,更加熟練的運用這些資料結構和方法。

2.使用它們的同時也要知道其背後的實現邏輯(應該和資料結構課程有關),以後還有很多類需要我自己定義,許多方法需要我自己敲。

相關文章