首页 > 编程语言 >实验1 现代C++基础编程

实验1 现代C++基础编程

时间:2024-10-13 17:59:16浏览次数:6  
标签:10 begin cout int 编程 C++ v0 实验 include

实验结论

1.实验任务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)//&表示i是一个引用变量,可以改变数组的值,反之不加则不改变
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()); //数组想象成一个圆盘 ,rotate(),begin end是范围,+1 -1是从哪开始
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.实验任务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.实验任务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) // 多组输入,直到按下Ctrl+Z后结束测试
12 cout << boolalpha << is_palindrome(s) << endl;
13 }
14 
15 // 函数is_palindrom定义
16 // 待补足
17 bool is_palindrome(std::string s) {
18 int left = 0;
19 int right = s.length() - 1;
20 while (left < right) {
21 if (s[left] != s[right]) {
22 return false;
23 }
24 left++;
25 right--;
26 }
27 return true;
28 }
View Code

 

运行结果截图:

 

 

4.实验任务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.实验任务5

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 template<typename T>
 5 void print(const T &c);
 6 
 7 int main ()
 8 {
 9 cout<<" ";
10 vector<char> l1{'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'};
11 print(l1);
12 vector<char> l2{'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'};
13 for (int i=1;i<=26;++i)
14 {
15 cout<<setw(2)<<i;
16 rotate(l2.begin(), l2.begin()+1, l2.end());
17 print(l2);
18 }
19 }
20 
21 template<typename T>
22 void print(const T &c)
23 {
24 for (auto &i:c)
25 cout<<setw(2)<<i;
26 cout<<endl;
27 }
View Code

 

运行结果截图:

 

 

6.实验任务6

代码:

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <iomanip>
 6 #include <ctime>
 7 using namespace std;
 8 
 9 int generateRan(int n);
10 
11 int main() {
12     srand(time(NULL));
13     int num = 0;
14     for(int i = 0;i < 10;i++){
15         int a = rand()%4;
16         num += generateRan(a);
17     }
18     double avg = 1.0*num/10*100;
19     cout << "正确率为:";
20     cout << fixed << setprecision(2) << avg;
21     cout << "%" << endl;
22 
23 
24     return 0;
25 }
26 
27 int generateRan(int n) {
28     if(n == 0){
29         int a = rand()%10 + 1;
30         int b = rand()%10 + 1;
31         int res = a + b;
32         int get;
33         cout << a;
34         cout << " + ";
35         cout << b;
36         cout << " = ";
37         cin >> get;
38         if(get == res){
39             return 1;
40         }else{
41             return 0;
42         }
43     }else if(n == 1){
44         int a = rand()%10 + 1;
45         int b,get;
46         do{
47             b = rand()%10 + 1;
48         }while(b > a);
49         int res = a - b;
50         cout << a;
51         cout << " - ";
52         cout << b;
53         cout << " = ";
54         cin >> get;
55         if(get == res){
56             return 1;
57         }else{
58             return 0;
59         }
60     }else if(n == 2){
61         int a = rand()%10 + 1;
62         int b = rand()%10 + 1;
63         int res = a * b;
64         int get;
65         cout << a;
66         cout << " x ";
67         cout << b;
68         cout << " = ";
69         cin >> get;
70         if(get == res){
71             return 1;
72         }else{
73             return 0;
74         }
75     }else if(n == 3){
76         int a = rand()%10 + 1;
77         int b,get;
78         do{
79             b = rand()%10 + 1;
80         }while(a % b != 0);
81         int res = a / b;
82         cout << a;
83         cout << " / ";
84         cout << b;
85         cout << " = ";
86         cin >> get;
87         if(get == res){
88             return 1;
89         }else{
90             return 0;
91         }
92     }
93 }
View Code

 

运行结果截图:

 

 

标签:10,begin,cout,int,编程,C++,v0,实验,include
From: https://www.cnblogs.com/yr123/p/18462235

相关文章

  • 实验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){......
  • day13-网络编程
    day13-网络编程一、网络编程概述同学们,今天我们学习的课程内容叫网络编程。意思就是编写的应用程序可以与网络上其他设备中的应用程序进行数据交互。网络编程有什么用呢?这个就不言而喻了,比如我们经常用的微信收发消息就需要用到网络通信的技术、在比如我们打开浏览器可以浏览各......
  • 实验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......