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

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

时间:2024-10-09 21:49:19浏览次数:1  
标签:begin end cout 编程 C++ v0 v1 实验 include

1.实验任务1

task1.cpp

  1 //现代C++标准库、算法库体验
  2 //本例用到以下内容:
  3 //1.字符串string,动态数组容器类vector、迭代器
  4 //3.函数模板、const引用作为形参
  5 
  6 #include<iostream>
  7 #include<string>
  8 #include<vector>
  9 #include<algorithm> 
 10 
 11 using namespace std;
 12 
 13 //声明
 14 //模板函数声明
 15 template<typename T>
 16 void output(const T &c);
 17 
 18 //普通函数声明
 19 void test1();
 20 void test2();
 21 void test3();
 22 
 23 int main(){
 24     cout<<"测试1:\n";
 25     test1();
 26     
 27     cout<<"\n测试2:\n";
 28     test2();
 29     
 30     cout<<"\n测试3:\n";
 31     test3();
 32 } 
 33 
 34 //函数实现
 35 //输出容器对象c中的元素
 36 template<typename T>
 37 void output(const T &c){
 38     for(auto &i:c)
 39         cout<<i<<" ";
 40     cout<<endl;
 41 }
 42 
 43 //测试1
 44 //组合使用算法库、迭代器、string反转字符串
 45 void test1(){
 46     string s0{"0123456789"};
 47     cout<<"s0= "<<s0<<endl;
 48     
 49     string s1{s0};
 50     reverse(s1.begin(),s1.end()); //反转指定迭代器区间的元素
 51     cout<<"s1= "<<s1<<endl;
 52     
 53     string s2{s0};
 54     reverse_copy(s0.begin(),s0.end(),s2.begin()); //将指定迭代区间的元素拷贝到指定迭代器开始的目标区间,并且在复制过程中反转次序 
 55     cout<<"s2= "<<s2<<endl;
 56 } 
 57 
 58 //测试2
 59 //组合使用算法库、迭代器、vector反转动态数组对象vector内数据
 60 void test2(){
 61     vector<int> v0{2,0,4,9};
 62     cout<<"v0: ";
 63     output(v0);
 64     
 65     vector<int> v1{v0};
 66     reverse(v1.begin(),v1.end());
 67     cout<<"v1: ";
 68     output(v1);
 69     
 70     vector<int> v2{v0};
 71     reverse_copy(v0.begin(),v0.end(),v2.begin());
 72     cout<<"v2: ";
 73     output(v2);
 74 } 
 75 
 76 //测试3
 77 //组合使用算法库、迭代器、vector实现元素旋转移位
 78 void test3(){
 79     vector<int> v0{0,1,2,3,4,5,6,7,8,9};
 80     cout<<"v0: ";
 81     output(v0);
 82     
 83     vector<int> v1{v0};
 84     rotate(v1.begin(),v1.begin()+1,v1.end()); //旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
 85     cout<<"v1:";
 86     output(v1);
 87     
 88     vector<int> v2{v0};
 89     rotate(v2.begin(),v2.begin()+2,v2.end()); //旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
 90     cout<<"v2:";
 91     output(v2);
 92     
 93     vector<int> v3{v0};
 94     rotate(v3.begin(),v3.end()-1,v3.end()); //旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
 95     cout<<"v3:";
 96     output(v3);
 97     
 98     vector<int> v4{v0};
 99     rotate(v4.begin(),v4.end()-2,v4.end()); //旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
100     cout<<"v4:";
101     output(v4);
102 }

测试结果截图:

 

2.实验任务2

task2.cpp

 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  // 函数声明
11 // 模板函数声明
12 template<typename T>
13 void output(const T &c);
14 
15 // 普通函数声明
16 int rand_int_100();
17 void test1();
18 void test2();
19 
20 int main(){
21     cout<<"测试1:\n";
22     test1();
23     
24     cout<<"\n测试2:\n";
25     test2(); 
26 }
27 
28 // 函数实现
29 // 输出容器对象c中的元素
30 template<typename T>
31 void output(const T &c){
32     for(auto &i:c)
33         cout<<i<<" ";
34     cout<<endl;
35 }
36 
37 // 返回[0, 100]区间内的一个随机整数
38 int rand_int_100(){
39     return rand()%101;
40 }
41 
42 // 测试1
43 // 对容器类对象指定迭代器区间进行赋值、排序
44 void test1(){
45     vector<int> v0(10); //创建一个动态数组对象v0, 对象大小为10
46     generate(v0.begin(),v0.end(),rand_int_100);    // 产生[0, 100]之间的随机整数赋值给指定迭代器区间[v0.begin(), v0.end())内的每个数据项
47     cout<<"v0: ";
48     output(v0);
49     
50     vector<int> v1{v0};
51     sort(v1.begin(),v1.end()); //对指定迭代器区间[v1.begin(), v1.end())内数据项进行升序排序
52     cout<<"v1: ";
53     output(v1);
54     
55     vector<int> v2{v0};
56     sort(v2.begin()+1, v2.end()-1); // 对指定迭代器区间[v1.begin()+1, v1.end()-1)内数据项进行升序排序
57     cout << "v2: ";
58     output(v2);
59 }
60 
61  // 测试2
62  // 对容器类对象指定迭代器区间进行赋值、计算最大值/最小值/均值
63 void test2(){
64     vector<int> v0(10);
65     generate(v0.begin(),v0.end(),rand_int_100);
66     cout<<"v0: ";
67     output(v0);
68     
69     auto iter1=min_element(v0.begin(),v0.end());
70     cout<<"最小值:"<<*iter1<<endl;
71     
72     auto iter2=max_element(v0.begin(),v0.end());
73     cout<<"最大值:"<<*iter2<<endl;
74     
75     auto ans=minmax_element(v0.begin(),v0.end());
76     cout<<"最小值:"<<*(ans.first)<<endl;
77     cout<<"最大值:"<<*(ans.second)<<endl;
78     double avg1=accumulate(v0.begin(),v0.end(),0)/v0.size();
79     cout<<"均值:"<<fixed<<setprecision(2)<<avg1<<endl;
80     
81     cout<<endl;
82     
83     vector<int> v1{v0};
84     cout<<"v0: ";
85     output(v0);
86     sort(v1.begin(),v1.end());
87     double avg2=accumulate(v1.begin()+1,v1.end()-1,0)/(v1.size()-2);
88     cout<<"去掉最大值、最小值之后,均值:"<<avg2<<endl; 
89 }

测试结果截图:

 

3.实验任务3

task3.cpp

 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 bool is_palindrome(std::string s){
17     int len=s.size();
18     for(int i=0;i<len;i++){
19         return    s[i]==s[len-i-1];
20     }
21 } 

测试结果截图:

 

4.实验任务4

task4.cpp

 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     std::string result;
22     if(x==0){
23         return "0";
24     }
25     while(x>0){
26         int a=x%n;
27         if(a<10){
28             result+=a+'0';
29         }
30         else{
31             result+=(a-10)+'A';
32         }
33         x/=n;
34     } 
35     reverse(result.begin(),result.end());
36     return result;
37 }

测试结果截图:

 

5.实验任务5

task5.cpp

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6     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\n";
 7     for(int i=0;i<26;i++){
 8         cout<<i+1<<" ";
 9         for(int j=0;j<26;j++){
10             char letter='A'+(i+j+1)%26;
11             cout<<letter<<" ";
12         }
13         cout<<endl;
14     }
15     return 0;
16 }

测试结果截图:

 

6.实验任务6

task6.cpp

 1 #include<iostream>
 2 #include<vector>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 #include<iomanip>
 6 #include<ctime>
 7 
 8 using namespace std;
 9 
10 int main(){
11     int count=0;
12     const int total=10;
13 
14     for(int i=0;i<total;i++){
15         int num1=(time(nullptr)+i)%11;
16         int num2=(time(nullptr)+i+1)%11;
17         char operation;
18         int answer;
19         
20         int r=(time(nullptr)+i+2)%4;
21         switch(r){
22             case 0:
23                 operation='+';
24                 answer=num1+num2;
25                 break;
26             case 1:
27                 operation='-';
28                 if(num1<num2)
29                     swap(num1,num2);
30                 answer=num1-num2;
31                 break;
32             case 2:
33                 operation='*';
34                 answer=num1*num2;
35                 break;
36             case 3:
37                 operation='/';
38                 while(num1%num2!=0)
39                     num1=(time(nullptr)+i+3)%11;
40                 answer=num1/num2;
41                 break;
42         }
43         int myanswer;
44         cout<<num1<<operation<<num2<<"=";
45         cin>>myanswer;
46         if(myanswer==answer){
47             count++;
48         }
49     }
50     double t=static_cast<double>(count)/(total)*100;
51     cout<<"正确率:"<<fixed<<setprecision(2)<<t<<"%"<<endl;
52     return 0;
53 }

测试结果截图:

 

7.实验总结

  • 模板函数

   template<typename T>

   返回值类型 函数名(参数列表)

   {

    ...

   }

  • reverse(s1.begin(), s1.end());  // 反转指定迭代器区间的元素
  • reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝 到指定迭代器开始的目标区间,并且在复制过程中反转次序
  • 动态数组对象vector

   vector<int> v0{0,1,2,3,4,5,6};

  • rotate(v1.begin(), v1.begin()+1, v1.end());  // 旋转指定迭代器区间 (v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
  • 随机数生成函数 rand()
  • generate(v0.begin(), v0.end(), rand_int_100); // 产生[0, 100]之间的随机整数 赋值给指定迭代器区间[v0.begin(), v0.end())内的每个数据项
  • sort(v1.begin(), v1.end()); // 对指定迭代器区间(v1.begin(), v1.end())内数据项进行升序排序
  • auto iter1 = min_element(v0.begin(), v0.end());

   cout << "最小值: " << *iter1 << endl;     //找出最小值并输出

  • auto iter2 = max_element(v0.begin(), v0.end());

   cout << "最大值: " << *iter2 << endl;     //找出最大值并输出

  •  double avg1 = accumulate(v0.begin(), v0.end(), 0)/v0.size();

    cout << "均值: " << fixed << setprecision(2) << avg1 << endl;  //计算均值并输出

标签:begin,end,cout,编程,C++,v0,v1,实验,include
From: https://www.cnblogs.com/lbldmx/p/18455243

相关文章

  • 实验2
    实验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 现代C++编程初体验
    实验任务1:task1.cpp:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 实验1 现代C++编程初体验
    任务1 task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 实验1 现代C++基础编程
    任务1:源代码task1.cpp1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213......
  • 20222303 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容在本周的学习中,重新回顾了栈和堆的概念,还学习了安全漏洞的相关概念,然后聚焦在其中的缓冲区溢出漏洞上,明白了缓冲区溢出的定义及发生的原理,并了解了缓冲区溢出发展历史上的一些经典攻击案例,收获颇丰。在本次的实验中,我掌握了反汇编与十六进制编程器相关知识,同时对NOP,......
  • HCIA 无线配置实验
    实验拓扑实验需求1、用户划分vlan30,网段为192.168.30.0/242、AP和AC划分vlan20,网段为192.168.20.0/243、有线网段使用vlan10,网段为192.168.10.0/24配置思路及验证结果核心交换机<SW1>system-view进入系统视图[SW1]......
  • 实验2
    实验任务1问题1:line21生成一个397-476之间的随机数对应11,12班学号问题2:line25生成一个0-21之间的随机数对应奇安信班学号问题3:随机生成指定数量的学号 实验任务2#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#d......
  • 实验2
    练习1:源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain()11{12intcnt=0;13intrandom_major,random_no;14srand......
  • 实验2_C语言分支与循环基础应用编程
    实验一#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
    任务1:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(time......