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

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

时间:2024-10-12 17:48:41浏览次数:1  
标签:begin 初体验 end cout 编程 C++ v0 v1 include

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

 

task1:
 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     string s2 {s0};
44     reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝到指定迭代器开始的目标区间,并且在复制过程中反转次序
45     cout << "s2 = " << s2 << endl;
46 }
47 // 测试2
48 // 组合使用算法库、迭代器、vector反转动态数组对象vector内数据
49 void test2() {
50     vector<int> v0 {2, 0, 4, 9};
51     cout << "v0: ";
52     output(v0);
53     vector<int> v1 {v0};
54     reverse(v1.begin(), v1.end());
55     cout << "v1: ";
56     output(v1);
57     vector<int> v2 {v0};
58     reverse_copy(v0.begin(), v0.end(), v2.begin());
59     cout << "v2: ";
60     output(v2);
61 }
62 // 测试3
63 // 组合使用算法库、迭代器、vector实现元素旋转移位
64 void test3() {
65     vector<int> v0 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
66     cout << "v0: ";
67     output(v0);
68     vector<int> v1 {v0};
69     rotate(v1.begin(), v1.begin()+1, v1.end()); // 旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
70     cout << "v1: ";
71     output(v1);
72     vector<int> v2 {v0};
73     rotate(v2.begin(), v2.begin()+2, v2.end());
74     cout << "v2: ";
75     output(v2);
76     vector<int> v3 {v0};
77     rotate(v3.begin(), v3.end()-1, v3.end());
78     cout << "v3: ";
79     output(v3);
80     vector<int> v4 {v0};
81     rotate(v4.begin(), v4.end()-2, v4.end());
82     cout << "v4: ";
83     output(v4);
84 }

  task2:  
 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]之间的随机整数赋值给指定迭代器区间[v0.begin(), v0.end())内的每个数据项
39 cout << "v0: ";
40 output(v0);
41 vector<int> v1{v0};
42 sort(v1.begin(), v1.end()); // 对指定迭代器区间[v1.begin(), v1.end())内数据项进行升序排序
43 cout << "v1: ";
44 output(v1);
45 vector<int> v2{v0};
46 sort(v2.begin()+1, v2.end()-1); // 对指定迭代器区间[v1.begin()+1,v1.end()-1)内数据项进行升序排序
47 cout << "v2: ";
48 output(v2);
49 }
50 // 测试2
51 // 对容器类对象指定迭代器区间进行赋值、计算最大值/最小值/均值
52 void test2() {
53 vector<int> v0(10);
54 generate(v0.begin(), v0.end(), rand_int_100);
55 cout << "v0: ";
56 output(v0);
57 auto iter1 = min_element(v0.begin(), v0.end());
58 cout << "最小值: " << *iter1 << endl;
59 auto iter2 = max_element(v0.begin(), v0.end());
60 cout << "最大值: " << *iter2 << endl;
61 auto ans = minmax_element(v0.begin(), v0.end());
62 cout << "最小值: " << *(ans.first) << endl;
63 cout << "最大值: " << *(ans.second) << endl;
64 double avg1 = accumulate(v0.begin(), v0.end(), 0)/v0.size();
65 cout << "均值: " << fixed << setprecision(2) << avg1 << endl;
66 cout << endl;
67 vector<int> v1{v0};
68 cout << "v0: ";
69 output(v0);
70 sort(v1.begin(), v1.end());
71 double avg2 = accumulate(v1.begin()+1, v1.end()-1, 0)/(v1.size()-2);
72 cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
73 }

 

task3:
 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 // 函数is_palindrom定义
12 // 待补足
13 // ×××
14 bool is_palindrome(std::string s) {
15     std::string reversed=s;
16     std::reverse(reversed.begin(),reversed.end());
17     return reversed==s;
18 }

  task4:
 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 std::string dec2n(int x, int n = 2);
 5 int main() {
 6 using namespace std;
 7 int x;
 8 while(cin >> x) {
 9 cout << "十进制: " << x << endl;
10 cout << "二进制: " << dec2n(x) << endl;
11 cout << "八进制: " << dec2n(x, 8) << endl;
12 cout << "十六进制: " << dec2n(x, 16) << endl << endl;
13 }
14 }
15 // 函数dec2n定义
16 // 待补足
17 // ×××
18 std::string dec2n(int x, int n){
19     std::string result;
20     while(x) {
21         int remainder = x % n; 
22         if (remainder < 10) {
23             result += std::to_string(remainder); 
24         } else {
25             result += static_cast<char>('A' + (remainder - 10)); 
26         }
27         x /= n; 
28     }
29     std::reverse(result.begin(), result.end()); 
30     return result;
31 }

 

task5:
 1 #include<iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <cctype>
 5 #include<iomanip>
 6 using namespace std;
 7 template<class T>
 8 void output(const T& c);
 9 
10 template<class T>
11 void output(const T& c) {
12     for (auto &i : c) {
13         cout << i << "  ";
14     }
15     cout << endl;
16 }
17 void test();
18 int main(){
19     test();
20 }
21 void test(){
22     vector<char> v0{'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'};
23     cout<<"    ";
24     output(v0);
25     std::transform(v0.begin(), v0.end(), v0.begin(), ::toupper);
26     int i=1;
27     for(i=1;i<=26;i++){
28         cout << setw(2)<<i<<"  ";
29         vector<char> v1{ v0 };
30         rotate(v1.begin(),v1.begin()+i,v1.end());
31         output(v1);
32     }
33 }

 

task6:
 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<ctime>
 4 
 5 using namespace std;
 6 int main() {
 7     int m=0;
 8     int w;
 9     for(int i=0; i<10; i++) {
10         srand(time(0)); 
11         char s=  "+-*/" [rand()%4];
12         int answer=0;
13         int a,b,c;
14         c=1;
15         switch(s) {
16             case '+':
17                 a=rand()%10+1;
18                 b=rand()%10+1;
19                 w=a+b;
20                 break;
21             case '-':
22                 a=rand()%10+1;
23                 b=rand()%a+1;
24                 w=a-b;
25                 break;
26             case '*':
27                 a=rand()%10+1;
28                 b=rand()%10+1;
29                 w=a*b;
30                 break;
31             case '/':
32                 a=rand()%10+1;
33                 while(c) {
34                     b=rand()%10+1;
35                     for(int j=1; j<=10; j++) {
36                         if(j*b==a) {
37                             c=0;
38                             w=a/b;
39                             break;
40                         }
41                     }
42                 }
43 
44         }
45         cout << a << ' '<< s << ' '<<b<<'=';
46         cin>>answer;
47         cout<<endl;
48         if(answer==w) {
49             m++;
50         }
51 
52 
53     }
54     cout<<"正确率:"<<m*10<<"%"<<endl;
55     return 0;
56 }

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

相关文章

  • 实验1 现代C++基础编程
    task1实验代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;//声明//模板函数声明template<typenameT>voidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest3......
  • 实验1现代c++编程初体验
    test1:源代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>5usingnamespacestd;6template<typenameT>7voidoutput(constT&c);8voidtest1();9voidtest2();10voidtest3......
  • 你真的懂C++吗?
    看看下面的代码,你真的懂C++吗?#defineN2#defineMN+1#defineNUM(M+1)*M/2intmain(){std::cout<<NUM;return0;}//输出结果为8,展开过程如下//(M+1)*M/2//(N+1+1)*N+1/2//(2+1+1)*2+1/2//4*2+0//8c......
  • 实验1 现代C++编程初体验
    task1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>usin......
  • C++ 中 `const` 的用法
    C++中const的用法1.在成员函数中的const示例:voidprintWindow()const{//...}含义和特性:函数不会修改对象状态:声明为const的成员函数承诺不会修改该类的任何非const成员变量。可以被常量对象调用:const成员函数可以被常量对象调用,确......
  • 关于C++当中全局变量的释放问题
    一、由来主要是在修改公司的一个MFC项目的时候遇到的问题,我在MFC页面的析构函数当中对一个全局图像变量进行了释放,具体如下:ai_engine_OCR::~ai_engine_OCR(){//及时释放内存if(g_pImg_open!=NULL){deleteg_pImg_open;g_pImg_open=NULL......
  • PyQt5/6 PySide2/6 在任务栏托盘区域编程,用于显示文字(图片)信息
    PyQt5/6PySide2/6在任务栏编程,用于显示文字(图片)信息本文使用PyQt5演示,其他库如PySide2/6,稍微改改就能用,因为其核心使用的是Win32gui来获取一些系统信息代码结构本文中全部代码全在test_taskbar.py这一个文件中编码,步骤中有变动的地方会注释标注,无改动的不会重复显示出来,需要......
  • c++设置windons默认输出音频设备
    c++设置windons默认输出音频设备主要功能是控制默认输出的音频设备进行切换。本文主要教导如何使用c++进行控制代码示例#include<windows.h>#include<mmdeviceapi.h>#include<iostream>#include<functiondiscoverykeys_devpkey.h>#include<vector>#include"......
  • C++指针的基本使用
    目录一、定义和使用二、指针占用的空间三、空指针和野指针1、空指针2、野指针四、const修饰指针五、指针和数组六、指针和函数七、结构体指针一、定义和使用指针变量定义语法:数据类型*变量名;intmain(){ //1、指针的定义 inta=10;//定义整型变量a ......
  • 编程算法 --- Polybius 方阵密码解密
      这段代码的作用是通过Polybius方阵密码的方式来解密一段只包含元音字母的密文。具体来说,它通过元音字母的全排列来生成多个可能的映射方案,然后将密文中的元音字母转换成数字序列,并根据这些数字对查找字母棋盘中的字母,尝试还原出可能的明文。程序会输出所有可能的明文,供人工......