任務(wù)1:
源代碼task1.cpp
// 現(xiàn)代C++標(biāo)準(zhǔn)庫、算法庫體驗(yàn) // 本例用到以下內(nèi)容: // 1. 字符串string, 動(dòng)態(tài)數(shù)組容器類vector、迭代器 // 2. 算法庫:反轉(zhuǎn)元素次序、旋轉(zhuǎn)元素 // 3. 函數(shù)模板、const引用作為形參 #include <iostream> #include <string> #include <vector> #include <algorithm> // 模板函數(shù)聲明 template<typename T> void output(const T &c); void test1(); void test2(); void test3(); int main() { std::cout << "測試1: \n"; test1(); std::cout << "\n測試2: \n"; test2(); std::cout << "\n測試3: \n"; test3(); } // 輸出容器對象c中的元素 template <typename T> void output(const T &c) { for(auto &i : c) std::cout << i << ' '; std::cout << '\n'; } // 測試1:組合使用算法庫、迭代器、string反轉(zhuǎn)字符串 void test1() { using namespace std; string s0{"0123456789"}; cout << "s0 = " << s0 << endl; string s1(s0); // 反轉(zhuǎn)s1自身 reverse(s1.begin(), s1.end()); cout << "s1 = " << s1 << endl; string s2(s0.size(), ' '); // 將s0反轉(zhuǎn)后結(jié)果拷貝到s2, s0自身不變 reverse_copy(s0.begin(), s0.end(), s2.begin()); cout << "s2 = " << s2 << endl; } // 測試2:組合使用算法庫、迭代器、vector反轉(zhuǎn)動(dòng)態(tài)數(shù)組對象vector內(nèi)數(shù)據(jù) void test2() { using namespace std; vector<int> v0{2, 0, 4, 9}; cout << "v0: "; output(v0); vector<int> v1{v0}; reverse(v1.begin(), v1.end()); cout << "v1: "; output(v1); vector<int> v2{v0}; reverse_copy(v0.begin(), v0.end(), v2.begin()); cout << "v2: "; output(v2); } // 測試3:組合使用算法庫、迭代器、vector實(shí)現(xiàn)元素旋轉(zhuǎn)移位 void test3() { using namespace std; vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; cout << "v0: "; output(v0); vector<int> v1{v0}; // 將[v1.begin(), v1.end())區(qū)間內(nèi)元素循環(huán)左移1位 rotate(v1.begin(), v1.begin()+1, v1.end()); cout << "v1: "; output(v1); vector<int> v2{v0}; // 將[v1.begin(), v1.end())區(qū)間內(nèi)元素循環(huán)左移2位 rotate(v2.begin(), v2.begin()+2, v2.end()); cout << "v2: "; output(v2); vector<int> v3{v0}; // 將[v1.begin(), v1.end())區(qū)間內(nèi)元素循環(huán)右移1位 rotate(v3.begin(), v3.end()-1, v3.end()); cout << "v3: "; output(v3); vector<int> v4{v0}; // 將[v1.begin(), v1.end())區(qū)間內(nèi)元素循環(huán)右移2位 rotate(v4.begin(), v4.end()-2, v4.end()); cout << "v4: "; output(v4); }
運(yùn)行結(jié)果截圖:

問題 1:reverse 原地反轉(zhuǎn)元素,reverse_copy 反轉(zhuǎn)后復(fù)制到新范圍且不修改原容器。
問題 2:rotate 將 [first, middle) 與 [middle, last) 交換順序,參數(shù)分別是范圍起始、分割點(diǎn)、范圍結(jié)束迭代器。
任務(wù)2:
源代碼task2.cpp
#include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <iomanip> #include <cstdlib> #include <ctime> // 模板函數(shù)聲明 template<typename T> void output(const T &c); int generate_random_number(); void test1(); void test2(); int main() { std::srand(std::time(0)); // 添加隨機(jī)種子 std::cout << "測試1: \n"; test1(); std::cout << "\n測試2: \n"; test2(); } // 輸出容器對象c中的元素 template <typename T> void output(const T &c) { for(auto &i: c) std::cout << i << ' '; std::cout << '\n'; } // 返回[0, 100]區(qū)間內(nèi)的一個(gè)隨機(jī)整數(shù) int generate_random_number() { return std::rand() % 101; } // 測試1:對容器類對象指定迭代器區(qū)間賦值、排序 void test1() { using namespace std; vector<int> v0(10); // 創(chuàng)建一個(gè)動(dòng)態(tài)數(shù)組對象v0, 對象大小為10 generate(v0.begin(), v0.end(), generate_random_number); // 生成隨機(jī)數(shù)填充v0 cout << "v0: "; output(v0); vector<int> v1{v0}; sort(v1.begin(), v1.end()); // 對整個(gè)vector排序 cout << "v1: "; output(v1); vector<int> v2{v0}; sort(v2.begin()+1, v2.end()-1); // 只對中間部分排序,不包含首尾元素 cout << "v2: "; output(v2); } // 測試2:對容器類對象指定迭代器區(qū)間賦值、計(jì)算最大值/最小值/均值 void test2() { using namespace std; vector<int> v0(10); generate(v0.begin(), v0.end(), generate_random_number); cout << "v0: "; output(v0); // 求最大值和最小值 auto min_iter = min_element(v0.begin(), v0.end()); auto max_iter = max_element(v0.begin(), v0.end()); cout << "最小值: " << *min_iter << endl; cout << "最大值: " << *max_iter << endl; // 同時(shí)求最大值和最小值 auto ans = minmax_element(v0.begin(), v0.end()); cout << "最小值: " << *(ans.first) << endl; cout << "最大值: " << *(ans.second) << endl; // 求平均值 double avg1 = accumulate(v0.begin(), v0.end(), 0.0) / v0.size(); cout << "均值: " << fixed << setprecision(2) << avg1 << endl; sort(v0.begin(), v0.end()); double avg2 = accumulate(v0.begin()+1, v0.end()-1, 0.0) / (v0.size()-2); cout << "去掉最大值、最小值之后,均值: " << avg2 << endl; }
運(yùn)行結(jié)果截圖:

任務(wù)3:
源代碼task3.cpp
#include <iostream> #include <string> #include <algorithm> #include <cctype> unsigned char func(unsigned char c); void test1(); void test2(); int main() { std::cout << "測試1: 字符串大小寫轉(zhuǎn)換\n"; test1(); std::cout << "\n測試2: 字符變換\n"; test2(); } unsigned char func(unsigned char c) { if(c == 'z') return 'a'; if(c == 'Z') return 'A'; if(std::isalpha(c)) return static_cast<unsigned char>(c+1); return c; } void test1() { std::string s1{"Hello World 2049!"}; std::cout << "s1 = " << s1 << '\n'; std::string s2; for(auto c: s1) s2 += std::tolower(c); std::cout << "s2 = " << s2 << '\n'; std::string s3; for(auto c: s1) s3 += std::toupper(c); std::cout << "s3 = " << s3 << '\n'; } void test2() { std::string s1{"I love cosmos!"}; std::cout << "s1 = " << s1 << '\n'; std::string s2(s1.size(), ' '); std::transform(s1.begin(), s1.end(), s2.begin(), func); std::cout << "s2 = " << s2 << '\n'; }
運(yùn)行結(jié)果截圖:

任務(wù)4:
源代碼task4.cpp
#include <iostream> #include <string> #include <algorithm> #include <cctype> bool is_palindrome(const std::string &s); bool is_palindrome_ignore_case(const std::string &s); int main() { using namespace std; string s; while (cin >> s) { cout << boolalpha << "區(qū)分大小寫: " << is_palindrome(s) << "\n" << "不區(qū)分大小寫: " << is_palindrome_ignore_case(s) << "\n\n"; } } bool is_palindrome(const std::string &s) { int left = 0, right = s.size() - 1; while (left < right) { if (s[left] != s[right]) { return false; } left++; right--; } return true; } bool is_palindrome_ignore_case(const std::string &s) { int left = 0, right = s.size() - 1; while (left < right) { char c1 = tolower(s[left]); char c2 = tolower(s[right]); if (c1 != c2) { return false; } left++; right--; } return true; }
運(yùn)行結(jié)果截圖:

問題:用std::getline(cin, s)替代cin >> s,可輸入含空格的字符串。
任務(wù)5:
源代碼task5.cpp
#include <iostream> #include <string> #include <algorithm> std::string dec2n(int x, int n = 2); int main() { int x; while (std::cin >> x) { std::cout << "十進(jìn)制: " << x << '\n' << "二進(jìn)制: " << dec2n(x) << '\n' << "八進(jìn)制: " << dec2n(x, 8) << '\n' << "十二進(jìn)制: " << dec2n(x, 12) << '\n' << "十六進(jìn)制: " << dec2n(x, 16) << '\n' << "三十二進(jìn)制: " << dec2n(x, 32) << "\n\n"; } } std::string dec2n(int x, int n) { if (x == 0) { return "0"; } std::string result; while (x > 0) { int remainder = x % n; char digit; if (remainder < 10) { digit = '0' + remainder; } else { digit = 'A' + remainder - 10; } result.push_back(digit); x = x / n; } std::reverse(result.begin(), result.end()); return result; }
運(yùn)行結(jié)果截圖:

任務(wù)6:
源代碼task6.cpp
#include <iostream> #include <string> #include <cctype> using namespace std; int main() { string src = "abcdefghijklmnopqrstuvwxyz"; cout << src << endl; for (int line = 1; line <= 26; line++) { string cipher = src.substr(line) + src.substr(0, line); for (char& ch : cipher) { ch = toupper(ch); } printf("%2d%s\n", line, cipher.c_str()); } return 0; }
運(yùn)行結(jié)果截圖:

任務(wù)7:
源代碼task7.cpp
#include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; int main() { srand(time(0)); int correct = 0; for (int i = 1; i <= 10; i++) { int a = rand() % 10 + 1; int b = rand() % 10 + 1; int op = rand() % 4; int result, user_ans; switch (op) { case 0: result = a + b; cout << a << " + " << b << " = "; break; case 1: if (a < b) swap(a, b); result = a - b; cout << a << " - " << b << " = "; break; case 2: result = a * b; cout << a << " * " << b << " = "; break; case 3: while (b == 0 || a % b != 0) { b = rand() % 10 + 1; a = rand() % 10 + 1; } result = a / b; cout << a << " / " << b << " = "; break; } cin >> user_ans; if (user_ans == result) { correct++; } } double accuracy = (double)correct / 10 * 100; cout << "正確率:" << fixed << setprecision(2) << accuracy << "%" << endl; return 0; }
運(yùn)行結(jié)果截圖:

浙公網(wǎng)安備 33010602011771號(hào)