实验1 现代C++基础编程
任务1:
源代码task1.cpp
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 6 using namespace std; 7 8 template<typename T> 9 void output(const T &c); 10 11 void test1(); 12 void test2(); 13 void test3(); 14 15 int main() { 16 cout << "测试1: \n"; 17 test1(); 18 19 cout << "\n测试2: \n"; 20 test2(); 21 22 cout << "\n测试3: \n"; 23 test3(); 24 } 25 26 template <typename T> 27 void output(const T &c) { 28 for(auto &i: c) 29 cout << i << " "; 30 cout << endl; 31 } 32 33 void test1() { 34 string s0{"0123456789"}; 35 cout << "s0 = " << s0 << endl; 36 37 string s1{s0}; 38 reverse(s1.begin(), s1.end()); 39 cout << "s1 = " << s1 << endl; 40 41 string s2{s0}; 42 reverse_copy(s0.begin(), s0.end(), s2.begin()); 43 cout << "s2 = " << s2 << endl; 44 } 45 46 void test2() { 47 vector<int> v0{2, 0, 4, 9}; 48 cout << "v0: "; 49 output(v0); 50 51 vector<int> v1{v0}; 52 reverse(v1.begin(), v1.end()); 53 cout << "v1: "; 54 output(v1); 55 56 vector<int> v2{v0}; 57 reverse_copy(v0.begin(), v0.end(), v2.begin()); 58 cout << "v2: "; 59 output(v2); 60 } 61 62 void test3() { 63 vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 64 cout << "v0: "; 65 output(v0); 66 67 vector<int> v1{v0}; 68 rotate(v1.begin(), v1.begin()+1, v1.end()); 69 cout << "v1: "; 70 output(v1); 71 72 vector<int> v2{v0}; 73 rotate(v2.begin(), v2.begin()+2, v2.end()); 74 cout << "v2: "; 75 output(v2); 76 77 vector<int> v3{v0}; 78 rotate(v3.begin(), v3.end()-1, v3.end()); 79 cout << "v3: "; 80 output(v3); 81 82 vector<int> v4{v0}; 83 rotate(v4.begin(), v4.end()-2, v4.end()); 84 cout << "v4: "; 85 output(v4); 86 }
运行结果截图:
任务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 template<typename T> 11 void output(const T &c); 12 13 int rand_int_100(); 14 void test1(); 15 void test2(); 16 17 int main() { 18 cout << "测试1: \n"; 19 test1(); 20 21 cout << "\n测试2: \n"; 22 test2(); 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 32 int rand_int_100() { 33 return rand() % 101; 34 } 35 36 void test1() { 37 vector<int> v0(10); 38 generate(v0.begin(), v0.end(), rand_int_100); 39 cout << "v0: "; 40 output(v0); 41 42 vector<int> v1{v0}; 43 sort(v1.begin(), v1.end()); 44 cout << "v1: "; 45 output(v1); 46 47 vector<int> v2{v0}; 48 sort(v2.begin()+1, v2.end()-1); 49 cout << "v2: "; 50 output(v2); 51 } 52 53 void test2() { 54 vector<int> v0(10); 55 generate(v0.begin(), v0.end(), rand_int_100); 56 cout << "v0: "; 57 output(v0); 58 59 auto iter1 = min_element(v0.begin(), v0.end()); 60 cout << "最小值: " << *iter1 << endl; 61 62 auto iter2 = max_element(v0.begin(), v0.end()); 63 cout << "最大值: " << *iter2 << endl; 64 65 auto ans = minmax_element(v0.begin(), v0.end()); 66 cout << "最小值: " << *(ans.first) << endl; 67 cout << "最大值: " << *(ans.second) << endl; 68 double avg1 = accumulate(v0.begin(), v0.end(), 0)/v0.size(); 69 cout << "均值: " << fixed << setprecision(2) << avg1 << endl; 70 71 cout << endl; 72 73 vector<int> v1{v0}; 74 cout << "v0: "; 75 output(v0); 76 sort(v1.begin(), v1.end()); 77 double avg2 = accumulate(v1.begin()+1, v1.end()-1, 0)/(v1.size()-2); 78 cout << "去掉最大值、最小值之后,均值: " << avg2 << endl; 79 }
运行结果截图:
任务3:
源代码task3.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) 12 cout << boolalpha << is_palindrome(s) << endl; 13 } 14 15 bool is_palindrome(std::string s) 16 { 17 using namespace std; 18 string t{s}; 19 reverse(t.begin(),t.end()); 20 if(s.compare(t)==0) 21 { 22 return true; 23 } 24 else 25 { 26 return false; 27 } 28 29 }
运行结果截图:
任务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 { 21 std::string a=""; 22 do{ 23 int temp=0; 24 temp=x%n; 25 if(temp>=0&&temp<=9){ 26 a+=(temp+'0'); 27 }else{ 28 a+=(temp+'A'-10); 29 } 30 x/=n; 31 }while(x); 32 reverse(a.begin(),a.end()); 33 return a; 34 }
运行结果截图:
任务5:
源代码task5.cpp
1 #include<iostream> 2 #include<iomanip> 3 #include<algorithm> 4 5 using namespace std; 6 7 void output(string &c){ 8 for(auto &i: c){ 9 cout << setw(2) << i; 10 } 11 cout << endl; 12 } 13 14 int main() 15 { 16 string s1{"abcdefghijklmnopqrstuvwxyz"}; 17 string s2{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; 18 cout << " "; 19 output(s1); 20 int i=1; 21 while(i<=26) 22 { 23 cout << setw(2) << i; 24 rotate(s2.begin(),s2.begin()+1,s2.end()); 25 output(s2); 26 i++; 27 } 28 29 return 0; 30 31 }
运行结果显示:
任务6:
源代码task6.cpp
1 #include <iostream> 2 #include <cstdlib> 3 #include <ctime> 4 #include <iomanip> 5 6 using namespace std; 7 8 int getRandomInt() { 9 return rand() % 10 + 1; 10 } 11 12 pair<string, int> generateProblem() { 13 int num1 = getRandomInt(); 14 int num2 = getRandomInt(); 15 char operators[] = {'+', '-', '*', '/'}; 16 char op; 17 int correctAnswer; 18 19 do { 20 op = operators[rand() % 4]; 21 22 if (op == '-') { 23 while (num1 <= num2) { 24 num1 = getRandomInt(); 25 num2 = getRandomInt(); 26 } 27 correctAnswer = num1 - num2; 28 } else if (op == '/') { 29 while (num1 % num2 != 0) { 30 num1 = getRandomInt(); 31 num2 = getRandomInt(); 32 } 33 correctAnswer = num1 / num2; 34 } else if(op == '+'){ 35 correctAnswer = num1 + num2; 36 }else{ 37 correctAnswer = num1 * num2; 38 } 39 } while (op == '/' && num1 / num2 < 1); 40 41 string problem = to_string(num1) + " " + string(1, op) + " " + to_string(num2); 42 return make_pair(problem, correctAnswer); 43 } 44 45 int main() { 46 srand(static_cast<unsigned int>(time(0))); 47 int correctCount = 0; 48 49 for (int i = 0; i < 10; ++i) { 50 pair<string, int> problem = generateProblem(); 51 cout << problem.first << " = "; 52 int userAnswer; 53 cin >> userAnswer; 54 55 if (userAnswer == problem.second) { 56 ++correctCount; 57 } 58 } 59 60 double accuracy = static_cast<double>(correctCount) / 10 * 100; 61 cout << fixed << setprecision(2) << "正确率: " << accuracy << "%" << endl; 62 63 return 0; 64 }
运行结果显示:
实验总结
通过实验1我感受到了使用C++标准库编写程序的便利,并对对象的封装、函数有了更深的了解