实验1 现代C++编程初体验
实验1:
task.cpp
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 7 template<typename T> 8 void output(const T &c); 9 10 void test1(); 11 void test2(); 12 void test3(); 13 int main() { 14 cout << "测试1: \n"; 15 test1(); 16 cout << "\n测试2: \n"; 17 test2(); 18 cout << "\n测试3: \n"; 19 test3(); 20 } 21 22 template <typename T> 23 void output(const T &c) { 24 for(auto &i: c) 25 cout << i << " "; 26 cout << endl; 27 } 28 29 void test1() { 30 string s0{"0123456789"}; 31 cout << "s0 = " << s0 << endl; 32 string s1{s0}; 33 reverse(s1.begin(), s1.end()); 34 cout << "s1 = " << s1 << endl; 35 string s2{s0}; 36 reverse_copy(s0.begin(), s0.end(), s2.begin()); 37 38 cout << "s2 = " << s2 << endl; 39 } 40 41 void test2() { 42 vector<int> v0{2, 0, 4, 9}; 43 cout << "v0: "; 44 output(v0); 45 vector<int> v1{v0}; 46 reverse(v1.begin(), v1.end()); 47 cout << "v1: "; 48 output(v1); 49 vector<int> v2{v0}; 50 reverse_copy(v0.begin(), v0.end(), v2.begin()); 51 cout << "v2: "; 52 output(v2); 53 } 54 55 void test3() { 56 vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 57 cout << "v0: "; 58 output(v0); 59 vector<int> v1{v0}; 60 rotate(v1.begin(), v1.begin()+1, v1.end()); 61 cout << "v1: "; 62 output(v1); 63 vector<int> v2{v0}; 64 rotate(v2.begin(), v2.begin()+2, v2.end()); 65 cout << "v2: "; 66 output(v2); 67 vector<int> v3{v0}; 68 rotate(v3.begin(), v3.end()-1, v3.end()); 69 cout << "v3: "; 70 output(v3); 71 vector<int> v4{v0}; 72 rotate(v4.begin(), v4.end()-2, v4.end()); 73 cout << "v4: "; 74 output(v4); 75 }
运行结果测试截图
实验2
task.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]之间的随机整数赋值给指定迭代器区间[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 }
运行结果测试截图
实验3
task.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 12 bool is_palindrome(std::string s) 13 { 14 std::string s_{s}; 15 reverse(s.begin(),s.end()); 16 if(s_==s) return true; 17 return false; 18 }
运行结果测试截图
实验4
task.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 char res[10000]={}; 22 itoa(x,res,n); 23 std::string s; 24 for(auto &i:res) s+=i; 25 return s; 26 }
运行结果测试
实验5
task.cpp
1 #include <iostream> 2 #include <iomanip> 3 4 using namespace std; 5 int main() 6 { 7 cout<<setw(2)<<" "; 8 for(int i=1;i<27;i++) cout<<setw(2)<<char(96+i); 9 int l=0,cnt=64; 10 while(l<26){ 11 l++; 12 cnt++; 13 cout<<endl<<setw(2)<<l; 14 for(int i=1;i<27;i++) { 15 cnt++; 16 if(cnt>90) cnt-=26; 17 cout<<setw(2)<<char(cnt); 18 } 19 } 20 return 0; 21 }
运行结果测试
实验6
task.cpp
1 #include <iostream> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <iomanip> 5 6 using namespace std; 7 8 int comp(char opra,int a,int b){ 9 if(opra == '+') return a+b; 10 else if (opra == '-') return a-b; 11 else if (opra == '*') return a*b; 12 else return a/b; 13 } 14 15 int main() 16 { 17 18 char sym[4]={'+','-','*','/'}; 19 int curr = 0; 20 for(int i=0;i<10;i++){ 21 srand(time(0)); 22 char opra = sym[rand()%4]; 23 int a,b; 24 if(opra == '-'){ 25 a = rand()%9+2; 26 b = rand()%(a-1)+1; 27 } 28 else if(opra == '/'){ 29 b = rand()%10+1; 30 a = b*(rand()%10/b+1); 31 } 32 else a = rand()%10+1,b=rand()%10+1; 33 cout<<a<<" "<<opra<<" "<<b<<" = "; 34 int ans; 35 cin>>ans; 36 if(ans == comp(opra,a,b)) curr++; 37 } 38 cout<<"正确率:"<<1.0*curr/10*100<<setprecision(2)<<"%"; 39 return 0; 40 41 42 }
运行结果测试