首页 > 编程语言 >程序实际实验1实验报告

程序实际实验1实验报告

时间:2024-10-15 15:32:22浏览次数:1  
标签:begin include end cout int 程序 v0 实验 实验报告

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

运行结果1

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

运行结果2

实验任务3

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 using namespace std;
 5 bool is_palindrome(string s);
 6 
 7 int main() {
 8     string s;
 9 
10     while(cin >> s)  // 多组输入,直到按下Ctrl+Z后结束测试
11         cout << boolalpha << is_palindrome(s) << endl;
12 }
13 
14 bool is_palindrome(string s)
15 {    
16     string s1=s;
17     reverse(s1.begin(),s1.end());
18     if(s==s1)
19     return true;
20     return false;
21 }

运行结果3

实验任务4

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm> 
 4 std::string dec2n(int x, int n=2);
 5 
 6 int main() {
 7    using namespace std;
 8  
 9     int x;
10     while(cin >> x) {
11     cout << "十进制: " << x << endl;
12     cout << "二进制: " << dec2n(x) << endl;
13     cout << "八进制: " << dec2n(x, 8) << endl;
14     cout << "十六进制: " << dec2n(x, 16) << endl << endl;
15  }
16  }
17 
18  // 函数dec2n定义
19  // 待补足
20 // ×××
21  std::string dec2n(int x,int n)
22  {
23      switch(n)
24     {
25         char s[100];
26         case 2:itoa(x,s,2);return s;break;
27         case 8:itoa(x,s,8);return s;break;
28        case 16:itoa(x,s,16);return s;break; 
29     }
30  }

运行结果4

 

 实验任务5

1 #include<iostream>
2 #include<vector>
3 #include<string>
4 #include<algorithm>
5 #include<iomanip>
6 using namespace std;
7 
8 int main()
9 {
10     cout<<setw(4);
11     string a="abcdefghijklmnopqrstuvwxyz";
12     string b="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
13     for(int i=0;i<26;i++)
14     {
15         cout<<a[i]<<setw(2);
16     }
17       cout<<endl;
18        for(int i=1;i<=26;i++)
19     {
20         cout<<i<<setw(2);
21         for(int j=1;j<=26;j++)
22         {
23             cout<<b[(i+j-1)%26]<<setw(2);
24         }
25         cout<<endl;
26     }
27         return 0;
28  }

实验结果5

 

实验任务6

 1 #include<iostream>
 2  #include<cstdlib>
 3   #include<string>
 4   #include <algorithm>
 5  #include <cmath>
 6   #include <ctime>  
 7   #include <iomanip>
 8   using namespace std;
 9   int main()
10  {
11      srand(time(0));
12      const int Question=10;
13      int Answer=0;
14      int accuracy;
15      for(int i=0;i<Question;i++)
16      {
17          char operators[]={'+','-','*','/'};
18          char op=operators[rand()%4];
19          int num1=rand()%10+1;
20          int num2=rand()%10+1;
21          if(op=='-'){
22              while(num1<=num2)
23              {
24                  num1=rand()%10+1;
25                 num2=rand()%10+1;
26              }
27          }
28          else if(op=='/')
29          {
30              while(num1%num2!=0)
31              {
32                  num1=rand()%10+1;
33                  num2=rand()%(num1/2)+1;
34              }
35          }
36          int ans1;
37          switch(op)
38          {
39             case '+':ans1=num1+num2;break;
40              case '-':ans1=num1-num2;break;
41              case '*':ans1=num1*num2;break;
42              case '/':ans1=num1/num2;break;
43         }
44          cout << num1 << " " << op << " " << num2 << " = ";  
45          int ans2;
46         cin>>ans2;
47         
48          if(ans1==ans2)
49         {
50             accuracy++;
51          }
52         
53          
54      }
55      cout<<"正确率:"<<((double)accuracy/Question)*100<<"%"<<endl;
56      
57      
58      return 0;
59  }

运行结果6

 

标签:begin,include,end,cout,int,程序,v0,实验,实验报告
From: https://www.cnblogs.com/CK1NG/p/18453583

相关文章