首页 > 编程语言 >实验1 现代C++编程初体验

实验1 现代C++编程初体验

时间:2024-10-14 20:23:21浏览次数:1  
标签:begin 初体验 end cout int 编程 C++ v0 include

任务1: 源代码task1.cpp

 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     string s2{s0};
41     reverse_copy(s0.begin(),s0.end(),s2.begin());
42     cout<<"s2 = "<<s2<<endl;
43     
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: 源代码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 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<<"测试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 
30     cout<<endl; 
31 }
32 
33 
34 int rand_int_100(){
35     return rand() % 101;
36 }
37 
38 void test1() {
39     vector<int> v0(10);
40     generate(v0.begin(),v0.end(),rand_int_100);
41     cout<<"v0: ";
42     output(v0);
43     
44     vector<int> v1{v0};
45     sort(v1.begin(),v1.end());
46     cout<<"v1: ";
47     output(v1);
48     
49     vector<int> v2{v0};
50     sort(v2.begin()+1,v2.end()-1);
51     cout<<"v2:";
52     output(v2); 
53 }
54 
55 void test2(){
56     vector<int> v0(10);
57     generate(v0.begin(),v0.end(),rand_int_100);
58     cout<<"v0: ";
59     output(v0);
60     
61     auto iter1=min_element(v0.begin(),v0.end());
62     cout<<"最小值:"<<*iter1<<endl;
63     
64     auto iter2=max_element(v0.begin(),v0.end());
65     cout<<"最大值:"<<*iter2<<endl;
66     
67     auto ans = minmax_element(v0.begin(),v0.end());
68     cout<<"最小值:"<<*(ans.first)<<endl;
69     cout<<"最大值:"<<*(ans.second)<<endl;
70     double avg1 = accumulate(v0.begin(),v0.end(),0)/v0.size();
71     cout<<"均值:"<<fixed<<setprecision(2)<<avg1<<endl;
72     
73     cout<<endl;
74     
75     vector<int> v1{v0};
76     cout<<"v0: ";
77     output(v0);
78     sort(v1.begin(),v1.end());
79     double avg2 = accumulate(v1.begin()+1,v1.end()-1,0)/(v1.size()-2);
80     cout<<"去掉最大值、最小值之后,均值:"<<avg2<<endl;
81 }

 

运行结果截图:  

 

任务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)
12         cout<<boolalpha<<is_palindrome(s)<<endl;
13 }
14 
15 //函数is_palindrom定义
16 bool is_palindrome(std::string s){
17     std::string t;
18     t=s;
19     reverse(s.begin(),s.end());
20     if(t==s)
21     return true;
22     return false;
23 } 

 

运行结果截图:

 

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

 

运行结果截图:  

 

任务5: 源代码task5.cpp  
 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std; 
 4 
 5 int main()
 6 {
 7     cout<<setw(2)<<" ";
 8     for(int i=0;i<26;i++)
 9     {
10         cout<<setw(2)<<static_cast<char>('a' + i);
11     }
12     cout<<endl;
13     
14     for(int i=1;i<=26;i++)
15     {
16         cout<<setw(2)<<i;
17         for(int j=i;j!=i+26;j++)
18         {
19             cout<<setw(2)<<static_cast<char>('A' + j%26);
20         }
21         cout<<endl;
22     }
23 }

 

运行结果截图:  

 

任务6: 源代码task6.cpp  
 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<algorithm>
 5 #include<ctime>
 6 using namespace std;
 7 
 8 int cou=0;
 9 
10 void ques();
11 int rand_int_10();
12 
13 int main(){
14     srand(static_cast<unsigned int>(time(0)));
15     ques();    
16 }
17 
18 int rand_int_10(){
19     return rand()%10+1;
20 }
21 
22 void ques(){
23     for(int i=0;i<10;i++){
24         vector<int> v(3);
25         generate(v.begin(),v.end(),rand_int_10);
26         v[0]=v[0]%4;
27         
28         string symbol{'+','-','*','/'};
29         if(v[0]==1||v[0]==3)
30         {
31             if(v[1]<v[2])
32             swap(v[1],v[2]);
33         }
34         if(v[0]==3)
35         {
36             if(v[1]%v[2]!=0)
37             {
38                 v[1]=v[1]-v[1]%v[2];
39             }
40         }
41         cout<<v[1]<<symbol[v[0]]<<v[2]<<"=";
42         
43         int ans,rans;
44         cin>>ans;
45         if(v[0]==0){
46             rans=v[1]+v[2];
47         }
48         else if(v[0]==1){
49             rans=v[1]-v[2];
50         }
51         else if(v[0]==2){
52             rans=v[1]*v[2];
53         }
54         else if(v[0]==3)
55         {
56             rans=v[1]/v[2];
57         }
58         
59         if(ans==rans)
60         cou++;    
61     }
62     cout<<"正确率:"<<cou<<"0.00%";
63 }

 

运行结果截图:  

 

 实验总结:

1.实验让我体会到OOP带来的好处,如代码重用性和模块化设计,使得程序结构更加清晰,利于扩展和修改。

2.实践了 const关键字在成员函数中的使用,可以提高代码的正确性,使该函数不会修改类的状态。

3。加深了对 C++ 标准库(STL)的理解及应用,包括字符串和向量操作、随机数生成、算法使用与回文检查等。其中,随机数代码,使用了srand为rand()函数提供一个种子地址(以时间),产生每组不同的随机数

4.基本的格式化技巧,掌握了 iomanip 库的使用。

 

标签:begin,初体验,end,cout,int,编程,C++,v0,include
From: https://www.cnblogs.com/fngwj/p/18464644

相关文章

  • [C++ 核心编程]笔记 4.1.4 类和对象 - 案例1
    类和对象:案例1:设计立方体类(Cube)求出立方体的面积和体积分别用全局函数和成员函数判断两个立方体是否相等。设计方法:创建立方体类设计属性设计行为求立方体面积和体积分别用全局和成员函数判断立方体是否相等#include<iostream>usingnamespacestd;clas......
  • 实验1 c++
    任务1:task1.cpp://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorit......
  • 实验1 现代C++编程初体验
    任务1:源代码task1.cpp1#include<bits/stdc++.h>2usingnamespacestd;34//模板函数声明5template<typenameT>6voidoutput(constT&c);78//普通函数声明9voidtest1();10voidtest2();11voidtest3();1213intmain(){14cout&l......
  • C++:初识
       1.namespace    1.1namespace的价值和定义        在c++中存在着大量的类,这些变量函数,类都存在全局变量当中为了,避免命名冲突c++当中引入了namespace。    在命名冲突中c语言也是普遍存在的观察如下c代码:#include<stdio.h>#include<st......
  • 16. 面向对象编程
    一、什么是面向对象  对象(Object)是内存中专门用来存储数据的一块区域。对象中可以存放各种数据,比如:数字、布尔值、代码等。对象由对象的标识(id)、对象的类型(type)和对象的值(value)三部分组成。  Python是一门面向对象的编程语言。所谓的面向对象的语言,简单理解就是语言中......
  • C++面向对象多态篇
    目录1.什么是多态?2.多态的概念3. 函数覆盖4.虚函数的定义5.多态实现6.多态的原理7.虚析构函数(掌握)8.类型转换8.1static_cast8.2dynamic_cast8.3const_cast(了解)8.4reinterpret_cast9、抽象类(掌握)10、纯虚析构函数(熟悉)11、私有析构函数(熟悉)1.什么......
  • C++在vscode中的code runner配置/环境配置
    C++在vscode中快捷运行(coderunner)一、配置tasks.json在vscode中创建文件夹或打开文件夹,会发现文件夹下多了一个.vscode文件夹,在该文件夹下创建tasks.json文件,并添加一下内容{"version":"2.0.0","tasks":[{"type":"shell","la......
  • C++异步调用 future async promise packaged_task
    背景:C++异步调用是现代C++编程中的一种重要技术,它允许程序在等待某个任务完成时继续执行其他代码,从而提高程序的效率和响应性。C++11引入了std::async、std::future和std::promise等工具,使得异步编程变得更加方便和直观。以下是关于C++异步调用的详细介绍,包括基本概......
  • 实验1 现代C++编程初体验
    任务11//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#include......
  • 实验1 C++
    task1:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//普通函数声明......