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

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

时间:2024-10-13 22:01:09浏览次数:1  
标签:begin 初体验 cout int 编程 c++ v0 v1 include

实验任务一

task1.cpp

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

运行测试

 实验任务二

task2.cpp

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

运行测试

 实验任务三

task3.cpp

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 bool is_palindrome(std::string s);
 5 int main() {
 6     using namespace std;
 7     string s;
 8     while(cin >> s) // 多组输入,直到按下Ctrl+Z后结束测试
 9     cout << boolalpha << is_palindrome(s) << endl;
10 }
11 bool is_palindrome(std::string s){
12     int len=s.size();
13     int i;
14     for (i=0;i<len/2;i++)
15         if(s[i] == s[len-1-i]){
16             return true;
17         }
18         else{
19             return false;
20         }
21 }

 实验任务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 std::string dec2n(int x,int n){
20     std::string s="0123456789ABCDEF";
21     std::string result;
22     int num;
23     if (x==0) return "0";
24     
25     while (x>0){
26         num=x%n;
27         result=s[num]+result;
28         x=x/n;
29     }
30     return result;
31 }

 实验任务5

task5.cpp

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<iomanip>
 5 #include<vector>
 6 
 7 int main(){
 8     using namespace std;
 9     cout<<" ";
10     vector<char> arr;
11     for (int i=0;i<26;i++){
12         cout<<" ";
13         cout<<char('a'+i);
14         arr.push_back(char('A'+i));
15     }
16     cout<<endl;
17     for(int i=1;i<=26;i++){
18         cout<<setw(2)<<setfill(' ')<<i;
19         rotate(arr.begin(),arr.begin()+1,arr.end());
20         for (char x:arr){
21             cout<<' ';
22             cout<<x;
23         }
24         cout<<endl;
25     }
26     return 0;
27 }

 实验任务6

task6.cpp

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

 

标签:begin,初体验,cout,int,编程,c++,v0,v1,include
From: https://www.cnblogs.com/wyhzmd/p/18453476

相关文章

  • 实验1 C++
    task1:代码:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#......
  • python与C++的一些区别以及一些新的东西
    目录第一个Python程序输入与输出Python基础数据类型和变量字符串和编码使用list和tuple条件判断模式匹配循环使用dict和set第一个Python程序输入与输出Python基础数据类型和变量字符串和编码第一行代码的输出如下解释如下:'%2d-%02d'是格式化字......
  • [C++][第三方库][ODB]详细讲解
    目录1.介绍2.安装1.安装build22.安装odb-compiler3.安装ODB运行时库4.安装MySQL和客户端开发包5.安装boostprofile库6.总体操作7.测试样例3.ODB常见操作1.ODB类型映射2.ODB编程1.指令2.示例4.类与接口5.使用1.介绍ODB框架:数据库ORM框架-->对象关系映......
  • 186道C++面试八股文(答案、分析和深入提问)整理
    1.全局变量和局部变量有什么区别?操作系统和编译器是怎么知道的?回答全局变量和局部变量在C++中有几个主要的区别:1.作用域(Scope)全局变量:定义在所有函数外部,可以在所有函数和代码块中访问。其作用域是整个程序,直至程序结束。局部变量:定义在函数或代码块内部,只能在该函......
  • WebAssembly 基础以及结合其他编程语言
    0x00WebAssembly基础详情参考《WebAssembly|MDN》(1)概述WebAssembly简称WASM或WA,是一种新的编码方式,可以在现代的Web浏览器中运行可以通过编译器,把多种编程语言(如C/C++、C#、Go、Python、Rust、TypeScript等)编写的代码转化为WA,并在浏览器中使用特点:灵活度高......
  • C语言在Linux上编程的步骤
    如果对你有帮助,请点个免费的赞吧,谢谢汪。(点个关注也可以!)如果以下内容需要补充和修改,请大家在评论区交流~让大家都看见1.编程前的准备1.1新建文件夹mkdir【选项】【文件名】1.2选择编程工具1.使用VScode:code【文件或者目录】2.使用vi编译器(这里先不使用)1.3打开编......
  • c++实验1
    实验1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>usin......
  • 【C++】list(STL)
    list的介绍list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。list与forward_list非常相似:最主要的不同在于......
  • C++之multimap:关键字分类的利器
    目录1.引言2.主要特点3.成员函数4.使用实例 5.注意事项1.引言        在C++中,multimap是标准模板库(STL)中的一个关联容器,它存储键值对(key-valuepairs),并且允许键的重复。multimap内部通常通过红黑树(或其他平衡二叉搜索树)实现,这保证了元素按照键的顺序进行存储......
  • C++入门基础知识111—【关于C++switch 语句】
     成长路上不孤单......