OOP實驗一
任務1:
源碼:
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 // 聲明 7 // 模板函數聲明 8 template<typename T> 9 void output(const T& c); 10 // 普通函數聲明 11 void test1(); 12 void test2(); 13 void test3(); 14 int main() { 15 cout << "測試1: \n"; 16 test1(); 17 cout << "\n測試2: \n"; 18 test2(); 19 cout << "\n測試3: \n"; 20 test3(); 21 } 22 // 函數實現 23 // 輸出容器對象c中的元素 24 template <typename T> 25 void output(const T& c) { 26 for (auto& i : c) 27 cout << i << " "; 28 cout << endl; 29 } 30 // 測試1 31 // 組合使用算法庫、迭代器、string反轉字符串 32 void test1() { 33 string s0{ "0123456789" }; 34 cout << "s0 = " << s0 << endl; 35 string s1{ s0 }; 36 reverse(s1.begin(), s1.end()); // 反轉指定迭代器區間的元素 37 cout << "s1 = " << s1 << endl; 38 string s2{ s0 }; 39 reverse_copy(s0.begin(), s0.end(), s2.begin()); // 將指定迭代區間的元素拷貝到指定迭代器開始的目標區間,并且在復制過程中反轉次序 40 cout << "s2 = " << s2 << endl; 41 } 42 // 測試2 43 // 組合使用算法庫、迭代器、vector反轉動態數組對象vector內數據 44 void test2() { 45 vector<int> v0{ 2, 0, 4, 9 }; 46 cout << "v0: "; 47 output(v0); 48 vector<int> v1{ v0 }; 49 reverse(v1.begin(), v1.end()); 50 cout << "v1: "; 51 output(v1); 52 vector<int> v2{ v0 }; 53 reverse_copy(v0.begin(), v0.end(), v2.begin()); 54 cout << "v2: "; 55 output(v2); 56 } 57 // 測試3 58 // 組合使用算法庫、迭代器、vector實現元素旋轉移位 59 void test3() { 60 vector<int> v0{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 61 cout << "v0: "; 62 output(v0); 63 vector<int> v1{ v0 }; 64 rotate(v1.begin(), v1.begin() + 1, v1.end()); // 旋轉指定迭代器區間[v1.begin(), v1.end())之間的數據項,旋轉后從迭代器v1.begin() + 1位置的數據項開始 65 cout << "v1: "; 66 output(v1); 67 vector<int> v2{ v0 }; 68 rotate(v2.begin(), v2.begin() + 2, v2.end()); 69 cout << "v2: "; 70 output(v2); 71 vector<int> v3{ v0 }; 72 rotate(v3.begin(), v3.end() - 1, v3.end()); 73 cout << "v3: "; 74 output(v3); 75 vector<int> v4{ v0 }; 76 rotate(v4.begin(), v4.end() - 2, v4.end()); 77 cout << "v4: "; 78 output(v4); 79 }
結果:

任務2:
源碼:
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 #include <numeric> 6 #include <iomanip> 7 using namespace std; 8 // 函數聲明 9 // 模板函數聲明 10 template<typename T> 11 void output(const T& c); 12 // 普通函數聲明 13 int rand_int_100(); 14 void test1(); 15 void test2(); 16 int main() { 17 cout << "測試1: \n"; 18 test1(); 19 cout << "\n測試2: \n"; 20 test2(); 21 } 22 // 函數實現 23 // 輸出容器對象c中的元素 24 template <typename T> 25 void output(const T& c) { 26 for (auto& i : c) 27 cout << i << " "; 28 cout << endl; 29 } 30 // 返回[0, 100]區間內的一個隨機整數 31 int rand_int_100() { 32 return rand() % 101; 33 } 34 // 測試1 35 // 對容器類對象指定迭代器區間進行賦值、排序 36 void test1() { 37 vector<int> v0(10); // 創建一個動態數組對象v0, 對象大小為10 38 generate(v0.begin(), v0.end(), rand_int_100); // 產生[0, 100]之間的隨機整數賦值給指定迭代器區間[v0.begin(), v0.end())內的每個數據項 39 cout << "v0: "; 40 output(v0); 41 vector<int> v1{ v0 }; 42 sort(v1.begin(), v1.end()); // 對指定迭代器區間[v1.begin(), v1.end())內數據項進行升序排序 43 cout << "v1: "; 44 output(v1); 45 vector<int> v2{ v0 }; 46 sort(v2.begin() + 1, v2.end() - 1); // 對指定迭代器區間[v1.begin()+1,v1.end() - 1)內數據項進行升序排序 47 cout << "v2: "; 48 output(v2); 49 } 50 // 測試2 51 // 對容器類對象指定迭代器區間進行賦值、計算最大值/最小值/均值 52 void test2() { 53 vector<int> v0(10); 54 generate(v0.begin(), v0.end(), rand_int_100); 55 cout << "v0: "; 56 output(v0); 57 auto iter1 = min_element(v0.begin(), v0.end()); 58 cout << "最小值: " << *iter1 << endl; 59 auto iter2 = max_element(v0.begin(), v0.end()); 60 cout << "最大值: " << *iter2 << endl; 61 auto ans = minmax_element(v0.begin(), v0.end()); 62 cout << "最小值: " << *(ans.first) << endl; 63 cout << "最大值: " << *(ans.second) << endl; 64 double avg1 = accumulate(v0.begin(), v0.end(), 0) / v0.size(); 65 cout << "均值: " << fixed << setprecision(2) << avg1 << endl; 66 cout << endl; 67 vector<int> v1{ v0 }; 68 cout << "v0: "; 69 output(v0); 70 sort(v1.begin(), v1.end()); 71 double avg2 = accumulate(v1.begin() + 1, v1.end() - 1, 0) / (v1.size() - 2); 72 cout << "去掉最大值、最小值之后,均值: " << avg2 << endl; 73 }
結果:

任務3:
源碼:

結果:

任務4:
源碼:

結果:

任務5:
源碼:

結果:

任務6:
源碼:
1 #include<iostream> 2 #include<cstdlib> 3 #include<ctime> 4 #include<iomanip> 5 int main() { 6 using namespace std; 7 char sym[] = { '+','-','*','/' }; 8 int num = 0; 9 int i; 10 srand((unsigned int)time(NULL)); 11 for (i = 0; i < 10; i++) 12 { 13 int a, b, c; 14 a = rand() % 9 + 1; 15 b = rand() % 9 + 1; 16 c = rand() % 4; 17 if (c == 1 && a < b) 18 { 19 i--; 20 continue; 21 } 22 else if (c == 3 && a % b != 0) 23 { 24 i--; 25 continue; 26 } 27 int re; 28 cout << a << sym[c] << b << "="; 29 cin >> re; 30 int right; 31 switch (c) { 32 case 0: 33 right = a + b; 34 break; 35 case 1: 36 right = a - b; 37 break; 38 case 2: 39 right = a * b; 40 break; 41 case 3: 42 right = a / b; 43 break; 44 } 45 if (right == re) { 46 num++; 47 } 48 } 49 double rate = num * 1.0 / 10; 50 cout << "正確率:" << setiosflags(ios::fixed) << setprecision(2) << 100.0 * rate << "%" << endl; 51 }
結果:



浙公網安備 33010602011771號