实验1
#include<iostream> #include<string> #include<vector> #include<algorithm> template<typename T> void output(const T& c); void test1(); void test2(); void test3(); int main() { std::cout << "ceshi1\n"; test1(); std::cout << "\nceshi2\n"; test2(); std::cout << "\nceshi3\n"; test3(); } template<typename T> void output(const T& c) { for (auto& i : c) std::cout << i << ' '; std::cout << '\n'; } void test1() { using namespace std; string s0{ "0123456789" }; cout << "s0=" << s0 << endl; string s1(s0); reverse(s1.begin(), s1.end()); cout << "s1=" << s1 << endl; string s2(s0.size(), ' '); reverse_copy(s0.begin(), s0.end(), s2.begin()); cout << "s2=" << s2 << endl; } void test2() { using namespace std; vector<int>v0{ 2,0,4,9 }; cout << "v0:"; output(v0); vector<int>v1{ v0 }; reverse(v1.begin(), v1.end()); cout << "v1:"; output(v1); vector<int>v2{ v0 }; reverse_copy(v0.begin(), v0.end(), v2.begin()); cout << "v2:"; output(v2); } void test3() { using namespace std; vector<int>v0{ 0,1,2,3,4,5,6,7,8,9 }; cout << "v0:"; output(v0); vector<int>v1{ v0 }; rotate(v1.begin(), v1.begin() + 1, v1.end()); cout << "v1:"; output(v1); vector<int>v2{ v0 }; rotate(v2.begin(), v2.begin() + 2, v2.end()); cout << "v2:"; output(v2); vector<int>v3{ v0 }; rotate(v3.begin(), v3.end() - 1, v3.end()); cout << "v3:"; output(v3); vector<int>v4{ v0 }; rotate(v4.begin(), v4.end() - 2, v4.end()); cout << "v4:"; output(v4); }
问题1:reverse 是直接反转,reverse_copy是复制并反转
问题2:通过指定一个“新首元素”来循环移动序列。它的参数是 (起始, 新首元素, 末尾)
。
#include<vector> #include<algorithm> #include<numeric> #include<iomanip> #include<cstdlib> #include<ctime> #include <iostream> template<typename T> void output(const T& c); int generate_random_number(); void test1(); void test2(); int main() { std::srand(std::time(0)); std::cout << "ceshi1\n"; test1(); std::cout << "\nceshi2\n"; test2(); } template<typename T> void output(const T& c) { for (auto& i : c) std::cout << i << ' '; std::cout << '\n'; } int generate_random_number() { return std::rand() % 101; } void test1() { using namespace std; vector<int>v0(10); generate(v0.begin(), v0.end(), generate_random_number); cout << "v0:"; output(v0); vector<int>v1{ v0 }; sort(v1.begin(), v1.end()); cout << "v1:"; output(v1); vector<int>v2{ v0 }; sort(v2.begin() + 1, v2.end() - 1); cout << "v2:"; output(v2); } void test2() { using namespace std; vector<int>v0(10); generate(v0.begin(), v0.end(), generate_random_number); cout << "v0:"; output(v0); auto min_iter = min_element(v0.begin(), v0.end()); auto max_iter = max_element(v0.begin(), v0.end()); cout << "zuixioazhi:" << *min_iter << endl; cout << "zuidazhi:" << *max_iter << endl; auto ans = minmax_element(v0.begin(), v0.end()); cout << "zuixiaozhi:" << *(ans.first) << endl; cout << "zuidazhi:" << *(ans.second) << endl; double avg1 = accumulate(v0.begin(), v0.end(), 0.0) / v0.size(); cout << "junzhi:" << fixed << setprecision(2) << avg1 << endl; sort(v0.begin(), v0.end()); double avg2 = accumulate(v0.begin() + 1, v0.end() - 1, 0.0) / (v0.size() - 2); cout << "qudiaozuidazhihezuixiaozhihoujunzhi:" << avg2 << endl; }
问题1:生成函数填充一个数组;
问题2:更简洁
问题3:等同
1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cctype> 5 unsigned char func(unsigned char c); 6 void test1(); 7 void test2(); 8 int main() { 9 std::cout << "ceshi1\n"; 10 test1(); 11 std::cout << "\nceshi2\n"; 12 test2(); 13 } 14 unsigned char func(unsigned char c) { 15 if (c == 'z') 16 return 'a'; 17 if (c == 'Z') 18 return 'A'; 19 if (std::isalpha(c)) 20 return static_cast<unsigned char>(c + 1); 21 return c; 22 } 23 void test1() { 24 std::string s1{ "Hello world 2049" }; 25 std::cout << "s1=" << s1 << '\n'; 26 std::string s2; 27 for (auto c : s1) 28 s2 += std::tolower(c); 29 std::cout << "s2=" << s2 << '\n'; 30 std::string s3; 31 for (auto c : s1) 32 s3 += std::toupper(c); 33 std::cout << "s3=" << s3 << "\n"; 34 } 35 void test2() { 36 std::string s1{ "I LOVE COSMOS!" }; 37 std::cout << "s1=" << s1 << '\n'; 38 std::string s2(s1.size(), ' '); 39 std::transform(s1.begin(), s1.end(), s2.begin(), func); 40 std::cout << "s2=" << s2 << '\n'; 41 42 }
问题1:将字母后移一位
问题2:tolower将字母全变成小写,toupper将字母全变成大写
问题3:输入起始,输入结束,输出起始,转换函数;s1自身被修改;
1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 bool is_palindrome(const std::string& s); 5 bool is_palindrome_ignore_case(const std::string& s); 6 int main() { 7 using namespace std; 8 string s; 9 while (cin >> s) { 10 cout << boolalpha 11 << "区分大小写:" << is_palindrome(s) << "\n" 12 << "不区分大小写:" << is_palindrome_ignore_case(s) << "\n\n"; 13 } 14 } 15 bool is_palindrome(const std::string& s) { 16 std::string s1{ s }; 17 reverse(s1.begin(), s1.end()); 18 if (s1 == s) { 19 return true; 20 } 21 else 22 return false; 23 24 } 25 bool is_palindrome_ignore_case(const std::string& s) { 26 std::string s1; 27 for (auto c : s) { 28 s1 += std::tolower(c); 29 std::string s2{ s1 }; 30 reverse(s2.begin(), s2.end()); 31 if (s1 == s2) { 32 return true; 33 } 34 else 35 return false; 36 } 37 }
问题1:使用 std::getline()
来替代 cin >> s
1 #include <iostream> 2 #include<string> 3 #include<algorithm> 4 std::string dec2n(int x, int n = 2); 5 int main() { 6 int x; 7 while (std::cin >> x) { 8 std::cout << "十进制: " << x << '\n' 9 << "二进制: " << dec2n(x) << '\n' 10 << "八进制: " << dec2n(x, 8) << '\n' 11 << "十二进制: " << dec2n(x, 12) << '\n' 12 << "十六进制: " << dec2n(x, 16) << '\n' 13 << "三十二进制: " << dec2n(x, 32) << "\n\n"; 14 } 15 } 16 std::string dec2n(int x, int n) { 17 if (x == 0) { 18 return "0"; 19 } 20 21 std::string s; 22 const std::string a = { "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" }; 23 while (x > 0) { 24 int b; 25 b = x % n; 26 s += a[b]; 27 x = x / n; 28 } 29 std::reverse(s.begin(), s.end()); 30 return s; 31 }
1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 using namespace std; 5 int main() { 6 string a{ " 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" }; 7 cout << " 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" << endl; 8 for (int i = 1; i <= 26; i++) { 9 rotate(a.begin(), a.begin() + 2, a.end()); 10 if (i < 10) { 11 cout << " " << i << a << endl; 12 } 13 else cout << i << a << endl; 14 } 15 return 0; 16 }
1 #include<iostream> 2 #include<cstdlib> 3 #include<ctime> 4 #include<iomanip> 5 #include<string> 6 using namespace std; 7 int main() { 8 srand(time(0)); 9 int a = 0; 10 for (int i = 0; i < 10; i++) { 11 int x = rand() % 10 + 1; 12 int y = rand() % 10 + 1; 13 int o = rand() % 4; 14 char f; 15 int b; 16 switch (o) { 17 case 0: 18 f = '+'; 19 b = x + y; 20 break; 21 case 1: 22 f = '-'; 23 if (x < y) { swap(x, y); } 24 b = x - y; 25 break; 26 case 2: 27 f = '*'; 28 b = x * y; 29 break; 30 case 3: 31 if (x < y) { 32 swap(x, y); 33 } 34 if (x % y != 0) { 35 do { 36 x = rand() % 10 + 1; 37 y = rand() % 10 + 1; 38 } while (x % y != 0 || y == 0); 39 } 40 f = '/'; 41 b = x / y; 42 break; 43 } 44 cout << x << " " << f << " " << y << "="; 45 int c; 46 cin >> c; 47 if (c == b) { 48 a++; 49 } 50 } 51 double s =(double)(a) / 10 * 100; 52 cout << "\n正确率:" << fixed << setprecision(2) << s << "%" << endl; 53 return 0; 54 }