首页 > 其他分享 >实验1

实验1

时间:2024-10-13 22:45:35浏览次数:1  
标签:10 begin cout int v0 实验 include

实验任务1

代码:

  1 // 现代C++标准库、算法库体验
  2 // 本例用到以下内容:
  3 // 1. 字符串string, 动态数组容器类vector、迭代器
  4 // 2. 算法库:反转元素次序、旋转元素
  5 // 3. 函数模板、const引用作为形参
  6 
  7 #include<iostream>
  8 #include<string>
  9 #include<vector>
 10 #include<algorithm>
 11 
 12 using namespace std;
 13 
 14 // 声明
 15 // 模板函数声明
 16 template<typename T>
 17 void output(const T &c);
 18 
 19 // 普通函数声明
 20 void test1();
 21 void test2();
 22 void test3();
 23 
 24 int main(){
 25     cout << "测试1:\n";
 26     test1();
 27 
 28     cout <<"\n测试2:\n";
 29     test2();
 30 
 31     cout << "\n测试3:\n";
 32     test3();
 33 }
 34 
 35 // 函数实现
 36 // 输出容器对象c中的元素
 37 template <typename T>
 38 void output(const T &c){
 39     for(auto &i: c)
 40         cout << i << " ";
 41     cout << endl;
 42 }
 43 
 44 // 测试1
 45 // 组合使用算法库、迭代器、string反转字符串
 46 void test1(){
 47     string s0{"0123456789"};
 48     cout << "s0 = " << s0 << endl;
 49 
 50     string s1{s0};
 51     reverse(s1.begin(),s1.end());// 反转指定迭代器区间的元素
 52     cout << "s1 = " << s1 <<endl;
 53 
 54     string s2{s0};
 55     reverse_copy(s0.begin(),s0.end(),s2.begin());// 将指定迭代区间的元素拷贝到指定迭代器开始的目标区间,并且在复制过程中反转次序
 56 
 57     cout << "s2 = " << s2 <<endl;
 58 }
 59 
 60 // 测试2
 61 // 组合使用算法库、迭代器、vector反转动态数组对象vector内数据
 62 void test2(){
 63     vector<int> v0{2,0,4,9};
 64     cout << "v0: ";
 65     output(v0);
 66 
 67     vector<int> v1{v0};
 68     reverse(v1.begin(),v1.end());
 69     cout<<"v1: ";
 70     output(v1);
 71 
 72     vector<int> v2{v0};
 73     reverse_copy(v0.begin(),v0.end(),v2.begin());
 74     cout << "v2: ";
 75     output(v2);
 76 }
 77 
 78 // 测试3
 79 // 组合使用算法库、迭代器、vector实现元素旋转移位
 80 void test3(){
 81     vector<int> v0{0,1,2,3,4,5,6,7,8,9};
 82     cout << "v0: ";
 83     output(v0);
 84 
 85     vector<int> v1{v0};
 86     rotate(v1.begin(),v1.begin()+1,v1.end());// 旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
 87 
 88     cout << "v1: ";
 89     output(v1);
 90 
 91     vector<int> v2{v0};
 92     rotate(v2.begin(),v2.begin()+2,v2.end());
 93     cout << "v2: ";
 94     output(v2);
 95 
 96     vector<int> v3{v0};
 97     rotate(v3.begin(),v3.end()-1,v3.end());
 98     cout<< "v3: ";
 99     output(v3);
100 
101     vector<int> v4{v0};
102     rotate(v4.begin(),v4.end()-2,v4.end());
103     cout << "v4: ";
104     output(v4);
105 }
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 // 返回[0, 100]区间内的一个随机整数
33 int rand_int_100() {
34     return rand() % 101;
35 }
36 
37 //赋值、排序
38 void test1() {
39     vector<int> v0(10); // 创建一个动态数组对象v0, 对象大小为10
40 //generate标准库算法,定义在<numeric> ,作用:它用于生成一个值的序列,并将其赋值给一个迭代器范围内的元素。
41     generate(v0.begin(), v0.end(), rand_int_100); // 产生[0, 100]之间的随机整数赋值给指定迭代器区间[v0.begin(), v0.end())内的每个数据项
42     cout << "v0: ";
43     output(v0);
44 
45     vector<int> v1 {v0};
46     sort(v1.begin(), v1.end()); // 对指定迭代器区间[v1.begin(), v1.end())内数据项进行升序排序
47     cout << "v1: ";
48     output(v1);
49 
50     vector<int> v2 {v0};
51     sort(v2.begin()+1, v2.end()-1); // 对指定迭代器区间[v1.begin()+1, v1.end()-1)内数据项进行升序排序
52     cout << "v2: ";
53     output(v2);
54 }
55 
56 // 对容器类对象指定迭代器区间进行赋值、计算最大值/最小值/均值
57 void test2() {
58     vector<int> v0(10);
59     generate(v0.begin(), v0.end(), rand_int_100);
60     cout << "v0: ";
61     output(v0);
62 
63     auto iter1 = min_element(v0.begin(), v0.end());
64     cout << "最小值: " << *iter1 << endl;
65 
66     auto iter2 = max_element(v0.begin(), v0.end());
67     cout << "最大值: " << *iter2 << endl;
68 
69     auto ans = minmax_element(v0.begin(), v0.end());//minmax_element,先是 min,再 max,所以,返回的类中的数据成员 first 对应着最小值,second 对应着最大值。
70     cout << "最小值: " << *(ans.first) << endl;
71     cout << "最大值: " << *(ans.second) << endl;
72     double avg1 = accumulate(v0.begin(), v0.end(), 0)/v0.size();//accumulate累加求和 ,第三个是初始值为0,v0.size求个数
73     cout << "均值: " << fixed << setprecision(2) << avg1 << endl;//精确两位
74 //fixed 是一个流操纵符,用于设置输出流的浮点数显示格式。
75 //当应用了 fixed 操纵符后,浮点数将始终以固定小数点格式输出,保留小数点后六位(不是六位有效位),而不是科学计数法
76     cout << endl;//空格
77     vector<int> v1 {v0};
78     cout << "v0: ";
79     output(v0);
80     sort(v1.begin(), v1.end());
81     double avg2 = accumulate(v1.begin()+1, v1.end()-1, 0)/(v1.size()-2);
82     cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
83 }
View Code

 

 

运行结果截图:

 

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

 

运行结果截图:

 

实验任务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 
19 // 函数dec2n定义
20 std::string dec2n(int x, int n){
21 using namespace std;
22 string result;
23 string shu="0123456789ABCDEF";
24 while(x>0)
25 {
26 int remember=x%n;
27 result=shu[remember]+result;
28 x/=n;
29 }
30 if(result.empty()){
31 return "0";
32 }
33 return result;
34 }
View Code

 

运行结果截图:

 

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

 

运行结果截图:

 

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

 

运行结果截图:

 

实验总结:

1、C++标准库和算法库的使用:通过实验任务1和任务2,我学会了如何使用C++标准库中的string、vector等容器类,以及算法库中的reverse、sort、min_element、max_element等函数。这些工具和函数极大地简化了编程过程,提高了代码的可读性和可维护性

2、C++的强大表达能力:通过实验任务3-6,我发现了C++在解决具体问题时的强大表达能力。 无论是判断回文串、实现进制转换,还是打印字母密文对照表、自动生成算术运算题目并评测,C++都能提供简洁而高效的解决方案。

标签:10,begin,cout,int,v0,实验,include
From: https://www.cnblogs.com/Daisy78/p/18463133

相关文章

  • 面向对象程序设计-实验1
    任务一: #include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){cout&l......
  • 实验1 现代c++编程初体验
    实验任务一task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参6#include<iostream>7#include<string>8#includ......
  • openssl实验截图记录
                                    ......
  • 实验1 C++
    task1:代码:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#......
  • 实验1
    实验任务1:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtest2();13voi......
  • 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......