首页 > 其他分享 >实验1

实验1

时间:2024-10-13 20:46:55浏览次数:1  
标签:10 cout int v0 实验 include num1

实验任务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 }

 

 

实验任务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 ran_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 template <typename T>
25 void output(const T &c){
26     for(auto &i: c)
27         cout << i << " ";
28     cout << endl; 
29 }
30 
31 int rand_int_100(){
32     return rand() % 101;
33 }
34 
35 void test1(){
36     vector<int> v0(10);
37     generate(v0.begin(),v0.end(),rand_int_100);
38     cout << "v0: ";
39     output(v0);
40     
41     vector<int> v1{v0};
42     sort(v1.begin(),v1.end());
43     
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 avgl = accumulate(v0.begin(),v0.end(),0)/v0.size();
69     cout << "均值:" << fixed <<setprecision(2)<<avgl <<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     
80 }

 

 

实验任务3:

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 bool is_palindrome(std::string s);
 5 
 6 int main() {
 7     using namespace std;
 8     string s;
 9     while(cin >> s)
10         cout << boolalpha << is_palindrome(s) << endl;
11 }
12 
13 bool is_palindrome(std::string a){
14     int len = a.size();
15     std::string b = a;
16     char t;
17     int i;
18     for(i=0;i<len/2;i++){
19         t = a[i];
20         a[i] = a[len-1-i];
21         a[len-i-1] = t;
22     } 
23     if(b == a){
24         return true;
25     }else{
26         return false;
27     }
28 }

 

 

实验任务4:

 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 
16 std::string dec2n(int x,int n){
17     using namespace std;
18     int i;
19     int y = x;
20     string j;
21     string res = "0123456789ABCDEF";
22     while(y != 0){
23         i = y % n;
24         j = res[i] + j;
25         y/=n;
26     }
27     if(x == 0){
28         return "0";
29     }else{
30         return j;
31     }
32 }

 

实验任务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 void fun();
13 
14 int main(){
15     cout << "   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" << endl;
16     int i;
17     for(i=1;i<=26;i++){
18         fun();
19     }
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 int j=1;
29 void fun(){
30     string s0={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
31     rotate(s0.begin(),s0.begin()+j,s0.end());
32     cout << setw(2) << j  << " ";
33     output(s0);
34     j++;
35 }

实验任务6:

 

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <ctime>
 4 #include <iomanip>
 5 
 6 using namespace std;
 7 
 8 int ran_int_10();
 9 
10 int rand_int_10(){
11     return rand() % 10;
12 }
13 
14 int main() {
15     srand(static_cast<unsigned int>(time(nullptr)));
16     int sum = 0;
17     for (int i = 0; i < 10; ++i) {
18         int num1 = rand_int_10() % 10 + 1;
19         int num2 = rand_int_10() % 10 + 1;
20         int operators = rand() % 4;
21         int res;
22         string operation;
23         switch (operators) {
24             int t;
25             case 0:
26                 operation = "*";
27                 res = num1 * num2;
28                 break;
29             case 1:
30                 if (num1 % num2!= 0) {
31                     if (num1 < num2) {
32                         t = num1;
33                         num1 = num2;
34                         num2 = t;
35                     }
36                     num1 = num1 % num2 == 0? num1 : num2 * (rand() % (num1 / num2) + 1);
37                 }
38                 operation = "/";
39                 res = num1 / num2;
40                 break;
41             case 2:
42                 operation = "+";
43                 res = num1 + num2;
44                 break;
45             case 3:
46                 if (num1 < num2) {
47                     t = num1;
48                     num1 = num2;
49                     num2 = t;
50                 }
51                 operation = "-";
52                 res = num1 - num2;
53                 break;
54         }
55         int answer;
56         cout << num1 << " " << operation << " " << num2 << " = ";
57         cin >> answer;
58         if (answer == res) {
59             sum++;
60         }
61     }
62     double accuracy = (double)sum / 10 * 100;
63     cout << "正确率为:" << accuracy << "%";
64     return 0;
65 }

 

实验总结:

1.学习了rotate、reverse的用法;

2.运用C++语言来编写程序,逐步将思维从C语言向C++语言转换;

3.学习运用vector来存储数据、对数据进行处理

标签:10,cout,int,v0,实验,include,num1
From: https://www.cnblogs.com/Bling888/p/18462521

相关文章

  • c++实验1
    实验1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>usin......
  • 实验1
    //任务11#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明1011template<typenameT>12voidoutput(constT&c);131......
  • 实验一c++
    实验任务一源代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//普通函......
  • 实验1现代c++初体验
    实验1:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtest2();13......
  • 实验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){......