现代C++编程初体验
实验任务1:
task1.cpp
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 }
task1 结果
实验任务2:
task2.cpp
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 }
task2 结果
实验任务 3:
task 3.cpp
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 bool is_palindrome(std::string s){ 19 int length=s.length(); 20 for(int i=0;i<length/2;i++) 21 { 22 if(s[i]!=s[length-1-i]) 23 { 24 return false; 25 } 26 } 27 return true; 28 }
task 3实验结果
实验任务 4:
task 4.cpp
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 std::string dec2n(int x,int n) 19 { 20 if (x == 0) { 21 return "0"; 22 } 23 24 std::string result = ""; 25 while (x > 0) { 26 int y = x % n; 27 if (y < 10) { 28 result = std::to_string(y) + result; 29 } else { 30 char digit = 'A' + y - 10; 31 result = digit + result; 32 } 33 x /= n; 34 } 35 return result; 36 }
task 4实验结果
实验任务 5:
task 5.cpp
1 #include <iostream> 2 #include <iomanip> 3 4 using namespace std; 5 6 int main() 7 { 8 9 int i, j; 10 11 cout << " "; 12 for (i = 0;i < 26;i++) 13 cout << setw(2) << (char)('a' + i); 14 cout << endl; 15 16 for (i = 1;i < 27;i++) 17 { 18 cout << setw(2) << i; 19 for (j = 0;j < 26;j++) 20 { 21 if ('A' +i +j <= 'Z') 22 cout << setw(2) << (char)('A' + i + j); 23 else 24 cout << setw(2) << (char)('A' + i + j - 26); 25 } 26 cout << endl; 27 } 28 29 return 0; }
task 5实验结果
实验任务6:
task 6.cpp
1 #include <iostream> 2 #include <cstdlib> 3 #include <ctime> 4 #include <iomanip> 5 6 int RandomNumber(int min, int max) { 7 return rand() % (max - min + 1) + min; 8 } 9 10 char RandomOperator() { 11 int op = RandomNumber(1, 4); 12 switch (op) { 13 case 1: 14 return '+'; 15 case 2: 16 return '-'; 17 case 3: 18 return '*'; 19 case 4: 20 return '/'; 21 } 22 return '+'; 23 } 24 25 bool checkSub(int a, int b) { 26 return a > b; 27 } 28 29 bool check(int a, int b) { 30 return a % b == 0; 31 } 32 33 int main() { 34 srand(static_cast<unsigned int>(time(nullptr))); 35 int correctCount = 0; 36 for (int i = 0; i < 10; ++i) { 37 int num1 = RandomNumber(1, 10); 38 int num2 = RandomNumber(1, 10); 39 char op = RandomOperator(); 40 while ((op == '-' &&!checkSub(num1, num2)) || (op == '/' &&!check(num1, num2))) { 41 num1 = RandomNumber(1, 10); 42 num2 = RandomNumber(1, 10); 43 } 44 int answer; 45 std::cout << num1 << " " << op << " " << num2 << " = "; 46 int userAnswer; 47 std::cin >> userAnswer; 48 switch (op) { 49 case '+': 50 answer = num1 + num2; 51 break; 52 case '-': 53 answer = num1 - num2; 54 break; 55 case '*': 56 answer = num1 * num2; 57 break; 58 case '/': 59 answer = num1 / num2; 60 break; 61 } 62 if (userAnswer == answer) { 63 correctCount++; 64 } 65 } 66 double percentage = (double(correctCount) / 10) * 100; 67 std::cout << std::fixed << std::setprecision(2); 68 std::cout << "正确率: " << percentage << "%\n"; 69 return 0; 70 }
task 6实验结果