实验1 现代C++编程初体验
task1:
1 // 现代C++标准库、算法库体验 2 // 本例用到以下内容: 3 // 1. 字符串string, 动态数组容器类vector、迭代器 4 // 2. 算法库:反转元素次序、旋转元素 5 // 3. 函数模板、const引用作为形参 6 #include <iostream> 7 #include <string> 8 #include <vector> 9 #include <algorithm> 10 using namespace std; 11 // 声明 12 // 模板函数声明 13 template<typename T> 14 void output(const T &c); 15 // 普通函数声明 16 void test1(); 17 void test2(); 18 void test3(); 19 int main() { 20 cout << "测试1: \n"; 21 test1(); 22 cout << "\n测试2: \n"; 23 test2(); 24 cout << "\n测试3: \n"; 25 test3(); 26 } 27 // 函数实现 28 // 输出容器对象c中的元素 29 template <typename T> 30 void output(const T &c) { 31 for(auto &i: c) 32 cout << i << " "; 33 cout << endl; 34 } 35 // 测试1 36 // 组合使用算法库、迭代器、string反转字符串 37 void test1() { 38 string s0 {"0123456789"}; 39 cout << "s0 = " << s0 << endl; 40 string s1 {s0}; 41 reverse(s1.begin(), s1.end()); // 反转指定迭代器区间的元素 42 cout << "s1 = " << s1 << endl; 43 string s2 {s0}; 44 reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝到指定迭代器开始的目标区间,并且在复制过程中反转次序 45 cout << "s2 = " << s2 << endl; 46 } 47 // 测试2 48 // 组合使用算法库、迭代器、vector反转动态数组对象vector内数据 49 void test2() { 50 vector<int> v0 {2, 0, 4, 9}; 51 cout << "v0: "; 52 output(v0); 53 vector<int> v1 {v0}; 54 reverse(v1.begin(), v1.end()); 55 cout << "v1: "; 56 output(v1); 57 vector<int> v2 {v0}; 58 reverse_copy(v0.begin(), v0.end(), v2.begin()); 59 cout << "v2: "; 60 output(v2); 61 } 62 // 测试3 63 // 组合使用算法库、迭代器、vector实现元素旋转移位 64 void test3() { 65 vector<int> v0 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 66 cout << "v0: "; 67 output(v0); 68 vector<int> v1 {v0}; 69 rotate(v1.begin(), v1.begin()+1, v1.end()); // 旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始 70 cout << "v1: "; 71 output(v1); 72 vector<int> v2 {v0}; 73 rotate(v2.begin(), v2.begin()+2, v2.end()); 74 cout << "v2: "; 75 output(v2); 76 vector<int> v3 {v0}; 77 rotate(v3.begin(), v3.end()-1, v3.end()); 78 cout << "v3: "; 79 output(v3); 80 vector<int> v4 {v0}; 81 rotate(v4.begin(), v4.end()-2, v4.end()); 82 cout << "v4: "; 83 output(v4); 84 }task2:
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 }
task3:
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 bool is_palindrome(std::string s); 5 int main() { 6 using namespace std; 7 string s; 8 while(cin >> s) // 多组输入,直到按下Ctrl+Z后结束测试 9 cout << boolalpha << is_palindrome(s) << endl; 10 } 11 // 函数is_palindrom定义 12 // 待补足 13 // ××× 14 bool is_palindrome(std::string s) { 15 std::string reversed=s; 16 std::reverse(reversed.begin(),reversed.end()); 17 return reversed==s; 18 }task4:
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 std::string dec2n(int x, int n = 2); 5 int main() { 6 using namespace std; 7 int x; 8 while(cin >> x) { 9 cout << "十进制: " << x << endl; 10 cout << "二进制: " << dec2n(x) << endl; 11 cout << "八进制: " << dec2n(x, 8) << endl; 12 cout << "十六进制: " << dec2n(x, 16) << endl << endl; 13 } 14 } 15 // 函数dec2n定义 16 // 待补足 17 // ××× 18 std::string dec2n(int x, int n){ 19 std::string result; 20 while(x) { 21 int remainder = x % n; 22 if (remainder < 10) { 23 result += std::to_string(remainder); 24 } else { 25 result += static_cast<char>('A' + (remainder - 10)); 26 } 27 x /= n; 28 } 29 std::reverse(result.begin(), result.end()); 30 return result; 31 }
task5:
1 #include<iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <cctype> 5 #include<iomanip> 6 using namespace std; 7 template<class T> 8 void output(const T& c); 9 10 template<class T> 11 void output(const T& c) { 12 for (auto &i : c) { 13 cout << i << " "; 14 } 15 cout << endl; 16 } 17 void test(); 18 int main(){ 19 test(); 20 } 21 void test(){ 22 vector<char> v0{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 23 cout<<" "; 24 output(v0); 25 std::transform(v0.begin(), v0.end(), v0.begin(), ::toupper); 26 int i=1; 27 for(i=1;i<=26;i++){ 28 cout << setw(2)<<i<<" "; 29 vector<char> v1{ v0 }; 30 rotate(v1.begin(),v1.begin()+i,v1.end()); 31 output(v1); 32 } 33 }
task6:
1 #include<iostream> 2 #include<cstdlib> 3 #include<ctime> 4 5 using namespace std; 6 int main() { 7 int m=0; 8 int w; 9 for(int i=0; i<10; i++) { 10 srand(time(0)); 11 char s= "+-*/" [rand()%4]; 12 int answer=0; 13 int a,b,c; 14 c=1; 15 switch(s) { 16 case '+': 17 a=rand()%10+1; 18 b=rand()%10+1; 19 w=a+b; 20 break; 21 case '-': 22 a=rand()%10+1; 23 b=rand()%a+1; 24 w=a-b; 25 break; 26 case '*': 27 a=rand()%10+1; 28 b=rand()%10+1; 29 w=a*b; 30 break; 31 case '/': 32 a=rand()%10+1; 33 while(c) { 34 b=rand()%10+1; 35 for(int j=1; j<=10; j++) { 36 if(j*b==a) { 37 c=0; 38 w=a/b; 39 break; 40 } 41 } 42 } 43 44 } 45 cout << a << ' '<< s << ' '<<b<<'='; 46 cin>>answer; 47 cout<<endl; 48 if(answer==w) { 49 m++; 50 } 51 52 53 } 54 cout<<"正确率:"<<m*10<<"%"<<endl; 55 return 0; 56 }标签:begin,初体验,end,cout,编程,C++,v0,v1,include From: https://www.cnblogs.com/nuist0177/p/18453542