实验1现代c++编程初体验
test1:
源代码:
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 using namespace std; 6 template<typename T> 7 void output(const T &c); 8 void test1(); 9 void test2(); 10 void test3(); 11 int main(){ 12 cout<<"测试1:\n"; 13 test1(); 14 cout<<"\n测试2:\n"; 15 test2(); 16 cout<<"\n测试3:\n"; 17 test3(); 18 } 19 template<typename T> 20 void output(const T&c){ 21 for(auto &i:c) 22 cout<<i<<" "; 23 cout<<endl; 24 } 25 void test1(){ 26 string s0{"0123456789"}; 27 cout<<"s0="<<s0<<endl; 28 string s1{s0}; 29 reverse(s1.begin(),s1.end()); 30 cout<<"s1="<<s1<<endl; 31 string s2{s0}; 32 reverse_copy(s0.begin(),s0.end(),s2.begin()); 33 cout<<"s2="<<s2<<endl; 34 } 35 void test2(){ 36 vector<int> v0{2,0,4,9}; 37 cout<<"v0:"; 38 output(v0); 39 vector<int> v1{v0}; 40 reverse(v1.begin(),v1.end()); 41 cout<<"v1:"; 42 output(v1); 43 vector<int> v2{v0}; 44 reverse_copy(v0.begin(),v0.end(),v2.begin()); 45 cout<<"v2:"; 46 } 47 void test3(){ 48 vector<int>v0{0,1,2,3,4,5,6,7,8,9}; 49 cout<<"v0:"; 50 output(v0); 51 vector<int>v1{v0}; 52 rotate(v1.begin(),v1.begin()+1,v1.end()); 53 cout<<"v1:"; 54 output(v1); 55 vector<int>v2{v0}; 56 rotate(v2.begin(),v2.begin()+2,v2.end()); 57 cout<<"v2:"; 58 output(v2); 59 vector<int>v3{v0}; 60 rotate(v3.begin(),v3.end()-1,v3.end()); 61 cout<<"v3:"; 62 output(v3); 63 vector<int>v4{v0}; 64 rotate(v4.begin(),v4.end()-1,v4.end()); 65 cout<<"v4:"; 66 output(v4); 67 68 }
运行截图:
test2:
源代码:
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<algorithm> 5 #include<numeric> 6 #include<iomanip> 7 using namespace std; 8 template<typename T> 9 void output(const T&c); 10 int rand_int_100(); 11 void test1(); 12 void test2(); 13 int main(){ 14 cout<<"测试1:\n"; 15 test1(); 16 cout<<"\n测试2:\n"; 17 test2(); 18 } 19 template <typename T> 20 void output(const T &c){ 21 for(auto &i:c) 22 cout<<i<<" "; 23 cout<<endl; 24 } 25 int rand_int_100(){ 26 return rand()%101; 27 } 28 void test1(){ 29 vector<int>v0(10); 30 generate(v0.begin() ,v0.end() ,rand_int_100); 31 cout<<"v0: "; 32 output(v0); 33 vector<int>v1{v0}; 34 sort(v1.begin(),v1.end()); 35 cout<<"v1: "; 36 output(v1); 37 vector<int>v2{v0}; 38 sort(v2.begin()+1,v2.end()-1); 39 cout<<"v2: "; 40 output(v2); 41 } 42 void test2(){ 43 vector<int>v0(10); 44 generate(v0.begin(),v0.end(),rand_int_100); 45 cout<<"v0: "; 46 output(v0); 47 auto iterl=min_element(v0.begin() ,v0.end() ); 48 cout<<"最小值: "<<*iterl<<endl; 49 auto iter2=max_element(v0.begin(),v0.end()); 50 cout<<"最大值:"<<*iter2<<endl; 51 auto ans=minmax_element(v0.begin(),v0.end()); 52 cout<<"最小值:"<<*(ans.first)<<endl; 53 cout<<"最大值:"<<*(ans.second)<<endl; 54 double avg1=accumulate(v0.begin(),v0.end(),0)/v0.size() ; 55 cout<<"均值:"<<fixed<<setprecision(2)<<avg1<<endl; 56 cout<<endl; 57 vector<int>v1{v0}; 58 cout<<"v0: "; 59 output(v0); 60 sort(v1.begin(),v1.end()); 61 double avg2=accumulate(v1.begin()+1,v1.end()-1,0)/(v1.size()-2); 62 cout<<"去掉最大值、最小值之后,均值:"<<avg2<<endl; 63 }
运行截图:
test3:
源代码:
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) 9 cout << boolalpha << is_palindrome(s) << endl; 10 } 11 bool is_palindrome(std::string s){ 12 std::string a{s}; 13 reverse(a.begin(),a.end()); 14 if(a==s) 15 return 1; 16 if(a!=s) 17 return 0; 18 }
运行截图:
test4:
源代码:
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 std::string dec2n(int x, int n = 2); 5 int main() { 6 using namespace std; 7 int x; 8 while(cin >> x) { 9 cout << "十进制: " << x << endl; 10 cout << "二进制: " << dec2n(x) << endl; 11 cout << "八进制: " << dec2n(x, 8) << endl; 12 cout << "十六进制: " << dec2n(x, 16) << endl << endl; 13 } 14 } 15 std::string dec2n(int x, int n) { 16 std::string s; 17 switch(n){ 18 char a[100]; 19 case 2:itoa(x,a,2);return a;break; 20 case 8:itoa(x,a,8);return a;break; 21 case 16:itoa(x,a,16);return a;break; 22 } 23 }
运行截图:
test5:
源代码:
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 #include<iomanip> 6 using namespace std; 7 template<class T> 8 void output(const T& c); 9 int main(){ 10 cout<<" "; 11 for(int i=97;i<=122;i++) 12 cout<<static_cast<char>(i)<<" "; 13 cout<<endl; 14 vector<char> v0{'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'}; 15 for(int i=1;i<=26;i++){ 16 vector<char> v1{v0}; 17 rotate(v1.begin(),v1.begin()+i,v1.end()); 18 cout<<setw(2)<<i<<" "; 19 output(v1) ; 20 } 21 } 22 template<class T> 23 void output(const T& c){ 24 for(auto &i:c){ 25 cout<<i<<" "; 26 } 27 cout<<endl; 28 }
运行截图:
test6:
源代码:
1 #include <iostream> 2 #include <cstdlib> 3 #include <ctime> 4 5 using namespace std; 6 7 int main() { 8 int m=0; 9 for(int i=0;i<10;i++){ 10 srand(time(0)); 11 char op = "+-*/"[rand() % 4]; 12 int w = 0; 13 int a; 14 int b; 15 int c; 16 switch (op) { 17 case '+': 18 a = rand() % 10+1; // 生成一个1到9之间的随机整数 19 b = rand() % 10+1 ; 20 w = a + b; break; 21 case '-': 22 a = rand() % 10+1; // 生成一个1到9之间的随机整数 23 b = rand() % a+1 ; 24 w=a-b; break; 25 case '*':w=a*b; break; 26 case '/': 27 a= rand() % 10+1; // 生成一个1到9之间的随机整数 28 for(int i=2;i<=a;i++){ 29 b=i; 30 for(int j=1;j<=10;j++){ 31 c=j; 32 if(b*c==a){ 33 w=a/b;break; 34 } 35 } 36 } 37 } 38 cout << a << ' ' << op << ' ' << b << '=' ; 39 int user_answer = 0; 40 cin >> user_answer; 41 cout<<endl; 42 if (user_answer == w) { 43 m++; 44 } 45 } 46 cout<<"正确率:"<<m*10<<"%"<<endl; 47 return 0; 48 }
运行截图:
实验总结:
对于本次六个实验对于第六个实验的印象最为深刻;在生成随机数时,加法和乘法对于数字a,b,的要求较低,但在减法和除法时特别是除法要求较高;
这里我采用了对生成随机数的进一步缩减,完成减法的要求,采用循环for的方法,完成除法要求。
此外对于实验三同样有较深的印象,因为较上学期的回文数的判断,采用本学期所学会简便许多。