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

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

时间:2024-10-13 15:43:16浏览次数:1  
标签:begin 初体验 end cout int 编程 C++ v0 include

实验一:

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

 

实验二:

 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;vector<int> v1{v0};
67 cout << "v0: ";
68 output(v0);
69 sort(v1.begin(), v1.end());
70 double avg2 = accumulate(v1.begin()+1, v1.end()-1, 0)/(v1.size()-2);
71 cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
72 }

 

 

实验三:

 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 
12 bool is_palindrome(std::string s)
13 {
14     std::string s1=s;
15     reverse(s1.begin(),s1.end());
16     if(s1==s)
17     return true;
18     else
19     return false;
20 }

 

 

 实验四:

 1 #include <iostream>  
 2 #include <string>  
 3 #include <algorithm>  
 4 
 5 std::string int_to_string(int num) {  
 6     std::string result;  
 7     bool isNegative = num < 0;  
 8     if (isNegative) num = -num; // 取绝对值  
 9     do {  
10         result += (num % 10) + '0'; // 提取数字并转换为字符  
11         num /= 10;  
12     } while (num > 0);  
13     if (isNegative) result += '-'; // 处理负数  
14     std::reverse(result.begin(), result.end()); // 反转字符串  
15     return result;  
16 }  
17 
18 std::string dec2n(int x, int n = 2) {  
19     if (x == 0) return "0"; // 处理0的情况  
20     std::string result;  
21     bool isNegative = x < 0; // 检查是否为负数  
22     x = std::abs(x); // 取绝对值  
23 
24     while (x > 0) {  
25         int remainder = x % n; // 计算余数  
26         if (remainder < 10)  
27             result += int_to_string(remainder); // 使用自定义函数  
28         else  
29             result += ('A' + remainder - 10); // 处理大于9的余数(十六进制)  
30         x /= n; // 继续除以n  
31     }  
32 
33     if (isNegative && n == 10) // 仅在十进制中包含负号  
34         result += '-';  
35 
36     std::reverse(result.begin(), result.end()); // 反转结果字符串  
37     return result;  
38 }  
39 
40 int main() {  
41     using namespace std;  
42     int x;  
43     while (cin >> x) {  
44         cout << "十进制: " << x << endl;  
45         cout << "二进制: " << dec2n(x) << endl;  
46         cout << "八进制: " << dec2n(x, 8) << endl;  
47         cout << "十六进制: " << dec2n(x, 16) << endl << endl;  
48     }  
49 }

 

 实验5:

 1 #include <iostream>  
 2   #include <iomanip>  
 3   using namespace std;  
 4   
 5   int main() {
 6       int count =1 ; 
 7       cout << " "<< " ";   
 8       for(char begin = 'a';begin <= 'z'; begin++)
 9       cout << setw(2)<< begin;
10      cout << endl;         
11      for (char original = 'A'; original <= 'Z'; ++original) { 
12          cout <<  setw(2) << count; // 打印原始数字 
13          
14          // 打印加密字母  
15          for (char encrypted = original + 1; encrypted <= 'Z'; ++encrypted) {  
16              cout << " " << encrypted ;  
17          }  
18          for (char encrypted = 'A'; encrypted <= original; ++encrypted) {  
19              cout << " " <<encrypted ;  
20          }  
21          count++; 
22          cout << endl; // 换行  
23      }  
24  
25      return 0;  
26  }

 

 

 实验6:

 1 #include <iostream>  
 2 #include <iomanip>  
 3 #include <cstdlib>  
 4 #include <ctime>  
 5 
 6 using namespace std;  
 7 
 8 int main() {  
 9     srand(static_cast<unsigned int>(time(0))); // 初始化随机数种子  
10     int correctAnswers = 0; // 记录正确答案的数量  
11     const int totalQuestions = 10; // 总题目数量  
12 
13     for (int i = 0; i < totalQuestions; ++i) {  
14         int num1 = rand() % 10 + 1; // 生成 1 到 10 的随机数  
15         int num2 = rand() % 10 + 1; // 生成 1 到 10 的随机数  
16         char operation;  
17         
18         // 随机选择运算符  
19         int op = rand() % 4; // 生成 0 到 3 的随机数  
20         if (op == 0) {  
21             operation = '+'; // 加法  
22         } else if (op == 1) {  
23             operation = '-'; // 减法  
24             if (num1 < num2) {  
25                 swap(num1, num2); // 确保第一个数大于第二个数  
26             }  
27         } else if (op == 2) {  
28             operation = '*'; // 乘法  
29         } else {  
30             operation = '/'; // 除法  
31             num1 = num1 * num2; // 确保能整除  
32         }  
33 
34         // 输出题目  
35         cout << num1 << " " << operation << " " << num2 << " = " ;  
36         int userAnswer;  
37         cin >> userAnswer; // 获取用户输入的答案  
38 
39         // 检查答案  
40         int correctAnswer = 0;  
41         switch (operation) {  
42             case '+':  
43                 correctAnswer = num1 + num2;  
44                 break;  
45             case '-':  
46                 correctAnswer = num1 - num2;  
47                 break;  
48             case '*':  
49                 correctAnswer = num1 * num2;  
50                 break;  
51             case '/':  
52                 correctAnswer = num1 / num2;  
53                 break;  
54         }  
55 
56         if (userAnswer == correctAnswer) {  
57             correctAnswers++;  
58         } 
59     }  
60 
61     // 输出正确率  
62     double accuracy = static_cast<double>(correctAnswers) / totalQuestions * 100;  
63     cout << "正确率: " << fixed << setprecision(2) << accuracy << "%" << endl;  
64 
65     return 0;  
66 }

 

 

实验总结:

实验1和实验2更加了解了C++中vector的一些语法组件,如:reverse等基于标准库<algorithm>下的一些函数的应用。

实验3,4,5相对基础,主要考查的是一些基本语法的运用。

本次实验很好的起到了C语言与C++语言学习的过渡作用,其中实验6中设置随机数种子在学校课程中第一次接触,很有学习意义

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

相关文章

  • 实验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;wh......
  • 【python基础】Python基础入门:从零开始学习编程
    Python基础入门:从零开始学习编程Python是一种广泛应用于各个领域的高级编程语言,因其简洁、易读、功能强大而受到开发者的青睐。从数据分析、Web开发到人工智能和自动化,Python提供了丰富的工具和库,帮助开发者快速构建项目。本篇博客将为你详细介绍Python的基础语法和核......
  • 豆包MarsCode编程助手(插件)
    1、豆包MarsCode(浏览器复制该链接即刻体验:sourl.cn/pdDZ6S)是基于豆…CloudIDE及AI编程助手两种使用形态,具备代码补全、智能问答、代码解释和代码修复等多项功能。原生的AI能力,让编程变得更加智能化。2、安装方法:点击上面的链接,会出现一个画面,画面如下:之后,鼠标向......
  • C++ 内存管理
    一、C/C++内存分布首先我们通过一道题,来了解内存分布:intglobalVar=1;staticintstaticGlobalVar=1;voidTest(){staticintstaticVar=1;intlocalVar=1;intnum1[10]={1,2,3,4};charchar2[]="abcd";constchar*pChar3="abcd";int*......
  • C++循环
    前言我们在制作C++小游戏时,会遇到一个问题——游戏如何循环而在C++中有3种循环方式 for循环    用途        for循环适用于有序且有循环目标,换句话来说就是:有限制的循环(while也行)    语法        for的用法很简......
  • C++STL~~map
    文章目录序列式容器和关联式容器一、map的概念二、map的使用三、map&multimap的差异四、map的练习五、总结序列式容器和关联式容器序列式容器前面已经接触过STL中的部分容器如:string、vector、list、deque、array、forward_list等,这些容器统称为序列式容器,因为逻辑......
  • Shell 编程:流程控制
    Shell编程:流程控制if语句ifconditionthencommand1command2...commandNfiifconditionthencommand1command2...commandNelsecommandfiifcondition1thencommand1elifcondition2thencommand2else......
  • 实验1 现代C++编程初体验
    Task1code1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 每日OJ题_牛客_NC101压缩字符串(一)_模拟_C++_Java
    目录牛客_NC101压缩字符串(一)_模拟题目解析C++代码Java代码牛客_NC101压缩字符串(一)_模拟压缩字符串(一)_牛客题霸_牛客网(nowcoder.com)描述:        利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaaa会变为a2bc5a3。......
  • 面向对象编程系列3
    多态理解多态之前,要先明白什么是向上转型和动态绑定。这个向上转型字面上的意思就是子类--->父类。我们在实例化一个鸟类时,可以这样写:Birdbird=newBird("jj");或者:​Birdbird=newBird("jj");Animalbird1=bird;//两行代码组合起来......