首页 > 编程语言 >实验1现代c++初体验

实验1现代c++初体验

时间:2024-10-13 18:15:05浏览次数:6  
标签:begin 初体验 end cout int c++ v0 实验 include

实验1:

 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 }
View Code

实验2:

 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 }
View Code

 

实验3:

 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 bool is_palindrome(std::string s)
15 {
16     int l = s.size();
17     int t = 0;
18     for(int i = 0;i<l/2;i++)
19     {
20         if(s[i] != s[l-i-1])
21         {
22             t = 1;
23             break;
24         }
25     }
26     if(t == 0)
27         return true;
28     else
29         return false;
30 }

 

实验4:

 

 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 std::string dec2n(int x,int n)
19 {
20     char a[100000];
21     switch(n)
22     {
23         case 2:itoa(x,a,2);break;
24         case 8:itoa(x,a,8);break;
25         case 16:itoa(x,a,16);break;
26     }
27     for(int i = 0;i<100000;i++)
28     {
29         if(a[i]=='a')
30             a[i] = 'A';
31         if(a[i]=='b')
32             a[i] = 'B';
33         if(a[i]=='c')
34             a[i] = 'C';
35         if(a[i]=='d')
36             a[i] = 'D';
37         if(a[i]=='e')
38             a[i] = 'E';
39         if(a[i]=='f')
40             a[i] = 'F';
41     }
42     return a;
43 }

 

 

 

实验5:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<algorithm>
 5 #include<iomanip>
 6 
 7 using namespace std;
 8 
 9 template<typename T>
10 void output(const T &c);
11 
12 int main()
13 {
14     vector<char>v0{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','o','v','w','x','y','z'};
15     vector<char>v1{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','O','V','W','X','Y','Z'};
16     cout << "    ";
17     output(v0);
18     for(int i = 1;i<=26;i++)
19     {
20         vector<char>v2{v1};
21         cout <<setw(2) << i << "  ";
22         rotate(v2.begin(),v2.begin()+i,v2.end());
23         output(v2);
24     }
25 }
26 template <typename T>
27 void output(const T &c){
28     for(auto &i:c)
29         cout << i <<" ";
30     cout << endl;
31 }

 

 

 

实验6:

 

 1 #include<iostream>
 2 #include<numeric>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int t = 0;
 9     int a,b,c;
10     for(int i = 1;i<=10;i++)
11     {
12         char s = "+-*/"[rand()%4];
13         int w = 0;
14         switch(s)
15         {
16             case '+':a = rand()%10+1;b = rand()%10+1;w = a+b;break;
17             case '-':a = rand()%10+1;b = rand()%a+1;w = a-b;break;
18             case '*':a = rand()%10+1;b = rand()%10+1;w = a*b;break;
19             case '/':
20                 a = rand()%10+1;
21                 for(int i = 2;i<=a;i++)
22                 {
23                     b = i;
24                     for(int j = 1;j<=10;j++)
25                     {
26                         c = j;
27                         if(b*c == a)
28                         {
29                             w = a/b;
30                             break;
31                         }
32                     }
33                  }
34          }
35          cout << a <<" " << s <<" "<< b <<" "<< "=" << " ";
36          int answer;
37          cin >> answer;
38          if(w == answer)
39          {
40              t++;
41          }
42     }
43     cout << "正确率:" << t*10 << ".00%" << endl;
44 }

收获:在本次实验报告中,对于实验4进制转换中查资料得知的itoa函数有深刻印象,可以用一行代码就实现十进制向其他进制的转换,美中不足的是转换成16进制后会是小写而非大写,得再专门转换成大写。

问题:实验6用了rand()%10+1来随机生成1-10的数字,但感觉来来回回就几道题变来变去,感觉并不是真正的随机。

标签:begin,初体验,end,cout,int,c++,v0,实验,include
From: https://www.cnblogs.com/guguabiu/p/18453566

相关文章

  • 实验1 现代C++基础编程
    实验结论1.实验任务1代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12void......
  • 实验2 c语言分支与循环基础应用编程1
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt<......
  • 实验一
    任务1#include<algorithm>#include<iostream>#include<string>#include<vector>usingnamespacestd;//声明//模板函数声明template<typenameT>voidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest3();int......
  • 实验二
    实验1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt<N){......
  • 实验二
    任务一1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(time(N......
  • 实验2
    test1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt<N){......
  • 实验2 C语言分支与循环基础应用编程-1
    实验任务1:task1.c源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314......
  • 实验2
     任务11#include<stdio.h>2#include<math.h>3#include<time.h>4#include<stdlib.h>56#defineN57#defineN13978#defineN24769#defineN321101112intmain()13{intcnt;14intrandom_major,random_......
  • 实验2
    task1源代码:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;whi......
  • 实验2
    task.1程序:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while......