实验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 string s2{s0}; 41 reverse_copy(s0.begin(),s0.end(),s2.begin()); 42 cout<<"s2 = "<<s2<<endl; 43 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<<"测试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 30 cout<<endl; 31 } 32 33 34 int rand_int_100(){ 35 return rand() % 101; 36 } 37 38 void test1() { 39 vector<int> v0(10); 40 generate(v0.begin(),v0.end(),rand_int_100); 41 cout<<"v0: "; 42 output(v0); 43 44 vector<int> v1{v0}; 45 sort(v1.begin(),v1.end()); 46 cout<<"v1: "; 47 output(v1); 48 49 vector<int> v2{v0}; 50 sort(v2.begin()+1,v2.end()-1); 51 cout<<"v2:"; 52 output(v2); 53 } 54 55 void test2(){ 56 vector<int> v0(10); 57 generate(v0.begin(),v0.end(),rand_int_100); 58 cout<<"v0: "; 59 output(v0); 60 61 auto iter1=min_element(v0.begin(),v0.end()); 62 cout<<"最小值:"<<*iter1<<endl; 63 64 auto iter2=max_element(v0.begin(),v0.end()); 65 cout<<"最大值:"<<*iter2<<endl; 66 67 auto ans = minmax_element(v0.begin(),v0.end()); 68 cout<<"最小值:"<<*(ans.first)<<endl; 69 cout<<"最大值:"<<*(ans.second)<<endl; 70 double avg1 = accumulate(v0.begin(),v0.end(),0)/v0.size(); 71 cout<<"均值:"<<fixed<<setprecision(2)<<avg1<<endl; 72 73 cout<<endl; 74 75 vector<int> v1{v0}; 76 cout<<"v0: "; 77 output(v0); 78 sort(v1.begin(),v1.end()); 79 double avg2 = accumulate(v1.begin()+1,v1.end()-1,0)/(v1.size()-2); 80 cout<<"去掉最大值、最小值之后,均值:"<<avg2<<endl; 81 }
运行结果截图:
任务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 //函数is_palindrom定义 16 bool is_palindrome(std::string s){ 17 std::string t; 18 t=s; 19 reverse(s.begin(),s.end()); 20 if(t==s) 21 return true; 22 return false; 23 }
运行结果截图:
任务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 //函数dec2n定义 20 std::string dec2n(int x,int n){ 21 int num=x; 22 std::string ans; 23 24 if(x==0) 25 return "0"; 26 27 while(num!=0) 28 { 29 if(num%n<10) 30 ans+=(num%n)+'0'; 31 else 32 ans+=(num%n)-10+'A'; 33 34 num=num/n; 35 } 36 reverse(ans.begin(),ans.end()); 37 return ans; 38 }
运行结果截图:
任务5:
源代码task5.cpp
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 5 int main() 6 { 7 cout<<setw(2)<<" "; 8 for(int i=0;i<26;i++) 9 { 10 cout<<setw(2)<<static_cast<char>('a' + i); 11 } 12 cout<<endl; 13 14 for(int i=1;i<=26;i++) 15 { 16 cout<<setw(2)<<i; 17 for(int j=i;j!=i+26;j++) 18 { 19 cout<<setw(2)<<static_cast<char>('A' + j%26); 20 } 21 cout<<endl; 22 } 23 }
运行结果截图:
任务6:
源代码task6.cpp
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<algorithm> 5 #include<ctime> 6 using namespace std; 7 8 int cou=0; 9 10 void ques(); 11 int rand_int_10(); 12 13 int main(){ 14 srand(static_cast<unsigned int>(time(0))); 15 ques(); 16 } 17 18 int rand_int_10(){ 19 return rand()%10+1; 20 } 21 22 void ques(){ 23 for(int i=0;i<10;i++){ 24 vector<int> v(3); 25 generate(v.begin(),v.end(),rand_int_10); 26 v[0]=v[0]%4; 27 28 string symbol{'+','-','*','/'}; 29 if(v[0]==1||v[0]==3) 30 { 31 if(v[1]<v[2]) 32 swap(v[1],v[2]); 33 } 34 if(v[0]==3) 35 { 36 if(v[1]%v[2]!=0) 37 { 38 v[1]=v[1]-v[1]%v[2]; 39 } 40 } 41 cout<<v[1]<<symbol[v[0]]<<v[2]<<"="; 42 43 int ans,rans; 44 cin>>ans; 45 if(v[0]==0){ 46 rans=v[1]+v[2]; 47 } 48 else if(v[0]==1){ 49 rans=v[1]-v[2]; 50 } 51 else if(v[0]==2){ 52 rans=v[1]*v[2]; 53 } 54 else if(v[0]==3) 55 { 56 rans=v[1]/v[2]; 57 } 58 59 if(ans==rans) 60 cou++; 61 } 62 cout<<"正确率:"<<cou<<"0.00%"; 63 }
运行结果截图:
实验总结:
1.实验让我体会到OOP带来的好处,如代码重用性和模块化设计,使得程序结构更加清晰,利于扩展和修改。
2.实践了 const关键字在成员函数中的使用,可以提高代码的正确性,使该函数不会修改类的状态。
3。加深了对 C++ 标准库(STL)的理解及应用,包括字符串和向量操作、随机数生成、算法使用与回文检查等。其中,随机数代码,使用了srand为rand()函数提供一个种子地址(以时间),产生每组不同的随机数
4.基本的格式化技巧,掌握了 iomanip 库的使用。