首页 > 编程语言 >实验1 C++

实验1 C++

时间:2024-10-14 18:10:31浏览次数:1  
标签:begin end cout C++ v0 v1 实验 include

task1:

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

 

运行结果截图:

 

task2:

 

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

 

运行结果截图:

 

 

task3:

 

 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 bool is_palindrome(std::string s) {
16     std::string s1(s);
17     std::reverse(s.begin(), s.end());
18     if (s == s1)return true;
19     else return false;
20 }
21 // 函数is_palindrom定义
22 // 待补足
23 // ×××
View Code

 

运行结果截图:

 

 

task4:

 

 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     int x;
10 
11     while (cin >> x) {
12         cout << "十进制: " << x << endl;
13         cout << "二进制: " << dec2n(x, 2) << 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     if (x == 0) return "0";
21     const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
22     std::string result;
23     bool isNegative = x < 0;
24     if (isNegative) x = -x;  
25     while (x > 0) {
26         result += digits[x % n];
27         x /= n;
28     }
29     if (isNegative) result += '-'; 
30     std::reverse(result.begin(), result.end());  
31     return result;
32 }
View Code

 

运行结果截图:

 

 

task5:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<algorithm>
 5 #include <iomanip>
 6 
 7 using namespace std;
 8 
 9 template<typename T>
10 void output(const T& c) {
11     for (auto& i : c){
12         char result = 'A' + i;
13         cout << result << " ";
14     }
15     cout << endl;
16 }
17 
18 void test();
19 
20 int main() {
21     cout <<"   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" << endl;
22     test();
23     return 0;
24 }
25 
26 void test() {
27     /*char v0[26],v1[26];
28     for (int i = 0; i <= 25; i++) {
29         v0[i] = 'A' + i;
30     }
31     rotate(v0.begin(), v0.begin + 1, v0.end());*/
32     vector<int>v0{ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25 };
33     for (int i = 0; i < 26; i++) {
34         cout << setw(2) << i + 1 << ' ';
35         rotate(v0.begin(), v0.begin() + 1, v0.end());
36         output(v0);
37     }
38 }
View Code

 

运行结果截图:

 

 

task6:

 

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<ctime>
 4 #include<iomanip>
 5 
 6 int main() {
 7     std::srand(static_cast<unsigned int>(std::time(0)));
 8 
 9     const int numQuestions = 10;
10     int correctAnswers = 0;
11 
12     for (int i = 0; i < numQuestions; ++i) {
13         int operand1 = std::rand() % 10 + 1;
14         int operand2 = std::rand() % 10 + 1;
15         char operation;
16 
17         do {
18             operation = "+-*/"[std::rand() % 4];
19             if (operation == '-') {
20                 while (operand1 <= operand2) {
21                     operand1 = std::rand() % 10 + 1;
22                     operand2 = std::rand() % 10 + 1;
23                 }
24             }
25             else if (operation == '/') {
26                 while (operand1 % operand2 != 0) {
27                     operand1 = std::rand() % 10 + 1;
28                     operand2 = std::rand() % 10 + 1;
29                 }
30             }
31         } while ((operation == '-' && operand1 <= operand2) || (operation == '/' && operand1 % operand2 != 0));
32         int correctResult;
33         switch (operation) {
34         case '+':correctResult = operand1 + operand2; break;
35         case '-':correctResult = operand1 - operand2; break;
36         case '*':correctResult = operand1 * operand2; break;
37         case '/':correctResult = operand1 / operand2; break;
38         }
39 
40         std::cout << operand1 << " " << operation << " " << operand2 << " = ";
41 
42         int userAnswer;
43         std::cin >> userAnswer;
44         
45         if (userAnswer == correctResult) {
46             ++correctAnswers;
47         }
48     }
49 
50     double accuracy = static_cast<double>(correctAnswers) / numQuestions * 100;
51     std::cout << "你的正确率是: " << std::fixed << std::setprecision(2) << accuracy << "%" << std::endl;
52 
53     return 0;
54 }
View Code

 

运行结果截图:

 

 

标签:begin,end,cout,C++,v0,v1,实验,include
From: https://www.cnblogs.com/yuannauy/p/18453508

相关文章

  • 实验一
    任务一:代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){ cout<......
  • 【最新原创毕设】基于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<......
  • 实验4-2-3-for 验证“哥德巴赫猜想C++解法
    #include<iostream>#include<cmath>boolvia(longlongi);usingnamespacestd;intmain(){  longlongn=0,i=3,p=0,q=0,a=0,b=0;  cin>>n;  if(n>4)  {    for(i=3;i<n/2;i+=2)    {......
  • 螺旋方阵C++解法
    #include<iostream>#include<vector>usingnamespacestd;#include<iomanip>intn;intmain(){   cin>>n;   vector<vector<int>>arr(n,vector<int>(n,0));   intx=0,y=0,s=1;   while(s<=n*......
  • Qt/C++编写的mqtt调试助手使用说明
    一、使用说明第一步,选择协议前缀,可选mqtt://、mqtts://、ws://、wss://四种,带s结尾的是走ssl通信,ws表示走websocket通信。一般选默认的mqtt://就好。第二步,填写服务所在主机地址,可以是IP地址也可以是网址,只要真实存在的就行。第三步,填写通信所用端口号,mqtt默认端口号是1883,以......
  • 国防科大:反事实验证LLM在RAG的生成质量
    ......
  • 实验2
    任务1:源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(t......
  • 【华三】【华三】VXLAN典型组网-集中式网关配置实验
    【华三】VXLAN典型组网-二层组网静态配置实验VXLAN的相关术语VTEP(VXLANTunnelEndPoint)VXLAN隧道核心设备VSI(VirtualSwitchInstance)VSIinterfaceVXLANsegmentVNI(VXLANNetworkIdentifier)AC(AttachmentCircuit)VXLAN隧道工作模式L2Gateway:二层转发模式IPGateway:......