实验1 现代C++编程初体验
实验任务1:
代码:
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 6 using namespace std; 7 template<typename T> 8 void output(const T &c); 9 void test1(); 10 void test2(); 11 void test3(); 12 13 int main() { 14 cout << "测试1: \n"; 15 test1(); 16 17 cout << "\n测试2: \n"; 18 test2(); 19 20 cout << "\n测试3: \n"; 21 test3(); 22 } 23 24 template <typename T> 25 void output(const T &c) { 26 for(auto &i: c) 27 cout << i << " "; 28 cout << endl; 29 } 30 31 void test1() { 32 string s0{"0123456789"}; 33 cout << "s0 = " << s0 << endl; 34 35 string s1{s0}; 36 reverse(s1.begin(), s1.end()); 37 cout << "s1 = " << s1 << endl; 38 39 string s2{s0}; 40 reverse_copy(s0.begin(), s0.end(), s2.begin()); 41 cout << "s2 = " << s2 << endl; 42 } 43 44 void test2() { 45 vector<int> v0{2, 0, 4, 9}; 46 cout << "v0: "; 47 output(v0); 48 49 vector<int> v1{v0}; 50 reverse(v1.begin(), v1.end()); 51 cout << "v1: "; 52 output(v1); 53 54 vector<int> v2{v0}; 55 reverse_copy(v0.begin(), v0.end(), v2.begin()); 56 cout << "v2: "; 57 output(v2); 58 } 59 void test3() { 60 vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 61 cout << "v0: "; 62 output(v0); 63 64 vector<int> v1{v0}; 65 rotate(v1.begin(), v1.begin()+1, v1.end()); 66 cout << "v1: "; 67 output(v1); 68 69 vector<int> v2{v0}; 70 rotate(v2.begin(), v2.begin()+2, v2.end()); 71 cout << "v2: "; 72 output(v2); 73 74 vector<int> v3{v0}; 75 rotate(v3.begin(), v3.end()-1, v3.end()); 76 cout << "v3: "; 77 output(v3); 78 79 vector<int> v4{v0}; 80 rotate(v4.begin(), v4.end()-2, v4.end()); 81 cout << "v4: "; 82 output(v4); 83 }
运行结果:
实验任务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 template<typename T> 10 void output(const T &c); 11 12 int rand_int_100(); 13 void test1(); 14 void test2(); 15 16 int main() { 17 cout << "测试1: \n"; 18 test1(); 19 20 cout << "\n测试2: \n"; 21 test2(); 22 } 23 24 25 template <typename T> 26 void output(const T &c) { 27 for(auto &i: c) 28 cout << i << " "; 29 cout << endl; 30 } 31 int rand_int_100() { 32 return rand() % 101; 33 } 34 35 void test1() { 36 vector<int> v0(10); 37 generate(v0.begin(), v0.end(), rand_int_100); 38 cout << "v0: "; 39 output(v0); 40 41 vector<int> v1{v0}; 42 sort(v1.begin(), v1.end()); 43 cout << "v1: "; 44 output(v1); 45 46 vector<int> v2{v0}; 47 sort(v2.begin()+1, v2.end()-1); 48 cout << "v2: "; 49 output(v2); 50 } 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 58 auto iter1 = min_element(v0.begin(), v0.end()); 59 cout << "最小值: " << *iter1 << endl; 60 61 auto iter2 = max_element(v0.begin(), v0.end()); 62 cout << "最大值: " << *iter2 << endl; 63 64 auto ans = minmax_element(v0.begin(), v0.end()); 65 cout << "最小值: " << *(ans.first) << endl; 66 cout << "最大值: " << *(ans.second) << endl; 67 double avg1 = accumulate(v0.begin(), v0.end(), 0)/v0.size(); 68 cout << "均值: " << fixed << setprecision(2) << avg1 << endl; 69 70 cout << endl; 71 72 vector<int> v1{v0}; 73 cout << "v0: "; 74 output(v0); 75 sort(v1.begin(), v1.end()); 76 double avg2 = accumulate(v1.begin()+1, v1.end()-1, 0)/(v1.size()-2); 77 cout << "去掉最大值、最小值之后,均值: " << avg2 << endl; 78 }
运行结果:
实验任务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 while (cin >> s) // 多组输入,直到按下Ctrl+Z后结束测试 11 cout << boolalpha << is_palindrome(s) << endl; 12 } 13 14 // 函数is_palindrome定义 15 bool is_palindrome(std::string s) { 16 // 判断字符串是否和反转后一致(区分大小写) 17 return s == std::string(s.rbegin(), s.rend()); 18 }
运行结果:
实验任务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 int x; 10 while (cin >> x) { 11 cout << "十进制: " << x << endl; 12 cout << "二进制: " << dec2n(x) << endl; 13 cout << "八进制: " << dec2n(x, 8) << endl; 14 cout << "十六进制: " << dec2n(x, 16) << endl << endl; 15 } 16 } 17 18 std::string dec2n(int x, int n) { 19 if (x == 0) return "0"; 20 std::string result; 21 const char digits[] = "0123456789ABCDEF"; 22 while (x > 0) { 23 result += digits[x % n]; 24 x /= n; 25 } 26 std::reverse(result.begin(), result.end()); 27 return result; 28 }
运行结果:
实验任务5:
代码:
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 cout<<" "; 5 for(char c='a';c<='z';++c){ 6 cout<<c<<" "; 7 } 8 cout<<endl; 9 for(int i=0;i<26;i++){ 10 cout<<(i+1<10 ? " ":"")<<i+1<<" "; 11 for(int j=0;j<26;j++){ 12 char letter ='A'+(i+j)%26; 13 cout<<letter<<" "; 14 } 15 cout<<endl; 16 } 17 return 0; 18 }
实验结果:
实验任务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 int count = 1, right = 0; 13 while (count <= 10) 14 { 15 int a = dis1(generator), b = dis1(generator), c = dis2(generator),answer; 16 if (c == 1) 17 { 18 cout << a << " " << "+" << " " << b << " = "; 19 cin >> answer; 20 if (answer == a + b) 21 right++; 22 count++; 23 } 24 25 else if (c == 2 && a > b) 26 { 27 cout << a << " " << "-" << " " << b << " = "; 28 cin >> answer; 29 if (answer == a - b) 30 right++; 31 count++; 32 } 33 34 else if (c == 3) 35 { 36 cout << a << " " << "*" << " " << b << " = "; 37 cin >> answer; 38 if (answer == a * b) 39 right++; 40 count++; 41 } 42 43 else if (c == 4 && a % b == 0) 44 { 45 cout << a << " " << "/" << " " << b << " = "; 46 cin >> answer; 47 if (answer == a / b) 48 right++; 49 count++; 50 } 51 } 52 cout << "正确率:" << fixed<< setprecision(2)<<(right * 10.0) << "%" << endl; 53 return 0; 54 }
实验结果:
实验总结:
1.在完成实验任务时总出现些许小错误,有输出格式的错误,有写代码时打字的错误,导致做实验的速度下降。
2.任务5、6时部分代码不能通过自己已有的知识完成,通过查阅书籍和网络辅助完成。
3.下次尽量把博客写美观~~~///(^v^)\\\~~~