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

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

时间:2024-10-14 19:49:39浏览次数:1  
标签:begin 初体验 end cout int 编程 C++ v0 v1

任务1:

源代码task1.cpp

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 //模板函数声明 
 5 template<typename T>
 6 void output(const T &c);
 7 
 8 //普通函数声明 
 9 void test1();
10 void test2();
11 void test3();
12 
13 int main(){
14     cout<<"测试1:\n";
15     test1();
16     
17     cout<<"测试2:\n";
18     test2();
19     
20     cout<<"测试3:\n";
21     test3();
22     
23 }
24 
25 //输出容器对象C中的元素 
26 template <typename T>
27 void output(const T &c)
28 {
29     for(auto &i: c)
30         cout<<i<<" ";
31     cout<<endl;
32 }
33 
34 void test1(){
35     string s0{"0123456789"};
36     cout<<"s0 = "<<s0<<endl;
37     
38     string s1={s0};
39     reverse(s1.begin(),s1.end()); //从头至尾反转元素 
40     cout<<"s1 = "<<s1<<endl;
41     
42     string s2={s0};
43     reverse_copy(s0.begin(),s0.end(),s2.begin()); //从头至尾复制,从第0位开始反转 
44     cout<<"s2 = "<<s2<<endl; 
45 }
46 
47 void test2(){
48     vector<int> v0{2,0,4,9};
49     cout<<"v0: ";
50     output(v0);
51     
52     vector<int> v1{v0};
53     reverse(v1.begin(),v1.end());
54     cout<<"v1: ";
55     output(v1);
56     
57     vector<int> v2{v0};
58     reverse_copy(v0.begin(),v0.end(),v2.begin());
59     cout<<"v2: ";
60     output(v2);
61 }
62 
63 void test3(){
64     vector <int> v0{0,1,2,3,4,5,6,7,8,9};
65     cout<<"v0: ";
66     output(v0);
67     
68     vector<int> v1{v0};
69     rotate(v1.begin(),v1.begin()+1,v1.end());
70     //旋转指定迭代器区间[v1.begin(),v1.end()]之间的数据项 ,旋转后从迭代器v1.begin()+1位置的数据项开始 
71     cout<<"v1: ";
72     output(v1);
73     
74     vector<int> v2{v0};
75     rotate(v2.begin(),v2.begin()+2,v2.end());
76     cout<<"v2: ";
77     output(v2);
78     
79     vector<int> v3{v0};
80     rotate(v3.begin(),v3.end()-1,v3.end());
81     cout<<"v3: ";
82     output(v3);
83     
84     vector<int> v4{v0};
85     rotate(v4.begin(),v4.end()-2,v4.end());
86     cout<<"v4: ";
87     output(v4);
88     
89 }

运行结果截图:

 

任务2:

源代码task2.cpp

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 template <typename T>
 5 void output(const T &c);
 6 
 7 int rand_int_100();
 8 void test1();
 9 void test2();
10 
11 int main()
12 {
13     cout<<"测试1:\n";
14     test1();
15     
16     cout<<"测试2:\n";
17     test2();
18     
19 } 
20 
21 template <typename T>
22 void output (const T &c){
23     for(auto &i: c)
24         cout<<i<<" ";
25     cout<<endl;
26     
27 }
28 
29 int rand_int_100(){
30     return rand()%101;
31     
32 }
33 
34 void test1(){
35     vector<int> v0(10);
36     generate(v0.begin(),v0.end(),rand_int_100);
37     cout<<"v0: ";
38     output(v0);
39     
40     vector<int> v1{v0};
41     sort(v1.begin(),v1.end());
42     cout<<"v1: ";
43     output(v1);
44     
45     vector <int> v2{v0};
46     sort(v2.begin()+1,v2.end()-1);
47     cout<<"v2: ";
48     output(v2);
49 }
50 
51 void test2(){
52     vector<int> v0(10);
53     generate(v0.begin(),v0.end(),rand_int_100);
54     cout<<"v0: ";
55     output(v0);
56     
57     auto iter1=min_element(v0.begin(),v0.end());
58     cout<<"最小值: "<<*iter1<<endl;
59     
60     auto iter2=max_element(v0.begin(),v0.end());
61     cout<<"最大值:"<<*iter2<<endl;
62     
63     auto ans=minmax_element(v0.begin(),v0.end());
64     cout<<"最小值:"<<*(ans.first)<<endl;
65     cout<<"最大值:"<<*(ans.second)<<endl;
66     double avg1=accumulate(v0.begin(),v0.end(),0)/v0.size();
67     cout<<"均值:"<<fixed<<setprecision(2)<<avg1<<endl;
68     
69     cout<<endl;
70     vector<int> v1{v0};
71     cout<<"v0:";
72     output(v0);
73     sort(v1.begin(),v1.end());
74     double avg2=accumulate(v1.begin()+1,v1.end()-1,0)/(v1.size()-2);
75     cout<<"去掉最大值、最小值之后,均值:"<<avg2<<endl;
76 }

运行结果截图:

 

任务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     int t=0;
19     for(int i=0;i<len;i++)
20     {
21         if(s[i]!=s[len-1-i])
22             t=1;
23     }
24     if(t==0)
25         return true;
26     else
27         return false;
28 }

运行结果截图:

 

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

运行结果截图:

 

任务5:

源代码task5.cpp

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     string s1="abcdefghijklmnopqrstuvwxyz";
 6     string s2="ABCDEFGHIJKLNMOPQRSTUVWXYZ";
 7     cout<<"  ";
 8     for(int i=0;i<=26;i++)
 9         cout<<setw(2)<<s1[i];
10     cout<<endl;
11     for(int i=1;i<26;i++)
12     {
13         cout<<setw(2)<<i;
14         for(int j=0;j<26;j++)
15         {
16             cout<<setw(2)<<s2[(j+i)%26];
17         }
18         cout<<endl;
19     }
20     cout<<endl;
21     return 0;
22 }

运行结果截图:

 

任务6:

源代码tssk6.cpp

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4 srand((unsigned)time(NULL));
 5     int ans;
 6     int a,b,c;
 7     double sum=0;
 8     char op;
 9     for(int i=1;i<=10;i++){
10         ans=0;
11         op="-+*/"[rand()%4];
12         if(op=='*')
13         {
14           a=rand()%10+1;
15           b=rand()%10+1;
16           ans=a*b;
17         }
18         else if(op=='+')
19         {
20           a=rand()%10+1;
21           b=rand()%10+1;
22           ans=a+b;
23         }
24           else if(op=='-')
25           {
26               a=rand()%10+1;
27                b=rand()%10+1;
28                while(b>a)
29                  b=rand()%10+1;
30                 ans=a-b;
31           }
32           else
33           {
34               a=rand()%10+1;
35               b=rand()%10+1;
36               while(a%b!=0)
37                 b=rand()%10+1;
38               ans=a/b;
39           }
40         cout<<a<<" "<<op<<" "<<b<<" = ";
41         cin>>c;
42         if(c==ans)
43           sum++;
44     }
45         printf("%.2lf%\n",sum*10);
46     return 0;
47 }

运行结果截图:

 

实验总结:

(1)reverse(s1.begin(),s1.end());   //从头至尾反转元素 

         reverse_copy(s0.begin(),s0.end(),s2.begin());    //从头至尾复制,从第0位开始反转 

         rotate(v1.begin(),v1.begin()+1,v1.end());   //旋转指定迭代器区间[v1.begin(),v1.end()]之间的数据项 ,旋转后从迭代器v1.begin()+1位置的数据项开始 

(2)srand((unsigned)time(NULL));

          int x=rand()%10+1;      //随机生成[1,10]的整数x

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

相关文章

  • 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//普通函数声明......
  • 【最新原创毕设】基于SpringCloud的一站式热点推荐平台+23649(免费领源码)可做计算机毕
    目 录摘要1绪论1.1选题背景与意义1.2开发现状1.3论文结构与章节安排2 开发环境及相关技术介绍2.1MySQL数据库2.2 Tomcat服务器2.3 Java语言2.4 SpringCloud框架介绍3 一站式热点推荐平台系统分析3.1可行性分析3.1.1技术可行性分析3.1......
  • (2024最新毕设合集)基于SpringBoot的通江银耳销售管理系统-15998|可做计算机毕业设计JAV
    摘要随着人们健康意识的增强,银耳这种传统的中药食材备受关注。而通江银耳是四川省通江县特产,中国国家地理标志产品。四川省通江县是银耳的发源地,中国银耳之乡,通江银耳因主产于此而得名,以其独到的质厚、肉嫩、易炖化和非常高的营养价值及药用价值而享誉海内外。需要一个高效便......
  • 实验1现代c++编程初体验
    1.实验任务一task1.cpp//现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<......