实验任务一
task1.cpp
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 44 string s2{s0}; 45 reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝 46 47 cout << "s2 = " << s2 << endl; 48 } 49 // 测试2 50 // 组合使用算法库、迭代器、vector反转动态数组对象vector内数据 51 void test2() { 52 vector<int> v0{2, 0, 4, 9}; 53 cout << "v0: "; 54 output(v0); 55 vector<int> v1{v0}; 56 reverse(v1.begin(), v1.end()); 57 cout << "v1: "; 58 output(v1); 59 vector<int> v2{v0}; 60 reverse_copy(v0.begin(), v0.end(), v2.begin()); 61 cout << "v2: "; 62 output(v2); 63 } 64 // 测试3 65 // 组合使用算法库、迭代器、vector实现元素旋转移位 66 void test3() { 67 vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 68 cout << "v0: "; 69 output(v0); 70 71 vector<int> v1{v0}; 72 rotate(v1.begin(), v1.begin()+1, v1.end()); // 旋转指定迭代器区间 73 cout << "v1: "; 74 output(v1); 75 vector<int> v2{v0}; 76 rotate(v2.begin(), v2.begin()+2, v2.end()); 77 cout << "v2: "; 78 output(v2); 79 vector<int> v3{v0}; 80 rotate(v3.begin(), v3.end()-1, v3.end()); 81 cout << "v3: "; 82 output(v3); 83 vector<int> v4{v0}; 84 rotate(v4.begin(), v4.end()-2, v4.end()); 85 cout << "v4: "; 86 output(v4); 87 }
运行测试
实验任务二
task2.cpp
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]之间的随机整数 39 40 cout << "v0: "; 41 output(v0); 42 vector<int> v1{v0}; 43 sort(v1.begin(), v1.end()); // 对指定迭代器区间[v1.begin(), v1.end())内数据 44 cout << "v1: "; 45 output(v1); 46 vector<int> v2{v0}; 47 sort(v2.begin()+1, v2.end()-1); 48 49 cout << "v2: "; 50 output(v2); 51 } 52 // 测试2 53 // 对容器类对象指定迭代器区间进行赋值、计算最大值/最小值/均值 54 void test2() { 55 vector<int> v0(10); 56 generate(v0.begin(), v0.end(), rand_int_100); 57 cout << "v0: "; 58 output(v0); 59 auto iter1 = min_element(v0.begin(), v0.end()); 60 cout << "最小值: " << *iter1 << endl; 61 auto iter2 = max_element(v0.begin(), v0.end()); 62 cout << "最大值: " << *iter2 << endl; 63 auto ans = minmax_element(v0.begin(), v0.end()); 64 cout << "最小值: " << *(ans.first) << endl; 65 cout << "最大值: " << *(ans.second) << endl; 66 double avg1 = accumulate(v0.begin(), v0.end(), 0)/v0.size(); 67 cout << "均值: " << fixed << setprecision(2) << avg1 << endl; 68 cout << endl; 69 vector<int> v1{v0}; 70 cout << "v0: "; 71 output(v0); 72 sort(v1.begin(), v1.end()); 73 double avg2 = accumulate(v1.begin()+1, v1.end()-1, 0)/(v1.size()-2); 74 cout << "去掉最大值、最小值之后,均值: " << avg2 << endl; 75 }
运行测试
实验任务三
task3.cpp
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 bool is_palindrome(std::string s){ 12 int len=s.size(); 13 int i; 14 for (i=0;i<len/2;i++) 15 if(s[i] == s[len-1-i]){ 16 return true; 17 } 18 else{ 19 return false; 20 } 21 }
实验任务4
task4.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 19 std::string dec2n(int x,int n){ 20 std::string s="0123456789ABCDEF"; 21 std::string result; 22 int num; 23 if (x==0) return "0"; 24 25 while (x>0){ 26 num=x%n; 27 result=s[num]+result; 28 x=x/n; 29 } 30 return result; 31 }
实验任务5
task5.cpp
1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<iomanip> 5 #include<vector> 6 7 int main(){ 8 using namespace std; 9 cout<<" "; 10 vector<char> arr; 11 for (int i=0;i<26;i++){ 12 cout<<" "; 13 cout<<char('a'+i); 14 arr.push_back(char('A'+i)); 15 } 16 cout<<endl; 17 for(int i=1;i<=26;i++){ 18 cout<<setw(2)<<setfill(' ')<<i; 19 rotate(arr.begin(),arr.begin()+1,arr.end()); 20 for (char x:arr){ 21 cout<<' '; 22 cout<<x; 23 } 24 cout<<endl; 25 } 26 return 0; 27 }
实验任务6
task6.cpp
1 #include<iostream> 2 #include<stdlib.h> 3 #include<ctime> 4 #include<string> 5 #include<algorithm> 6 #include<iomanip> 7 using namespace std; 8 9 int result(int n); 10 11 int main(){ 12 srand(time(NULL)); 13 double ans=0; 14 for (int i=0;i<10;i++){ 15 int sym=rand()%4; 16 ans += result(sym); 17 } 18 cout<<"正确率: "<<fixed<<setprecision(2)<<ans/0.1<<"%"<<endl; 19 return 0; 20 } 21 22 int result(int n){ 23 int get; 24 if (n==0){ 25 int a=rand()%10+1; 26 int b=rand()%10+1; 27 int res=a+b; 28 cout<<a<<" + "<<b<<" = "; 29 cin>>get; 30 if (get==res){ 31 return 1; 32 } 33 else{ 34 return 0; 35 } 36 } 37 else if (n==1){ 38 int a=rand()%10+1; 39 int b=rand()%10+1; 40 int res=a*b; 41 cout<<a<<" * "<<b<<" = "; 42 cin>>get; 43 if (get==res){ 44 return 1; 45 } 46 else{ 47 return 0; 48 } 49 } 50 else if (n==2){ 51 int a=rand()%10+1; 52 int b,res; 53 do{ 54 b=rand()%10+1; 55 }while(a%b); 56 res=a/b; 57 cout<<a<<" / "<<b<<" = "; 58 cin>>get; 59 if (get==res){ 60 return 1; 61 } 62 else{ 63 return 0; 64 } 65 } 66 else if (n==3){ 67 int a=rand()%10+1; 68 int b,res; 69 do{ 70 b=rand()%10+1; 71 }while(a<b); 72 res=a-b; 73 cout<<a<<" - "<<b<<" = "; 74 cin>>get; 75 if (get==res){ 76 return 1; 77 } 78 else{ 79 return 0; 80 } 81 } 82 }
标签:begin,初体验,cout,int,编程,c++,v0,v1,include From: https://www.cnblogs.com/wyhzmd/p/18453476