首页 > 编程语言 >实验1 现代C++基础编程

实验1 现代C++基础编程

时间:2024-10-10 20:44:41浏览次数:7  
标签:begin end cout int 编程 C++ v0 实验 include

任务1:

源代码task1.cpp

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 template<typename T>
 9 void output(const T &c);
10 
11 void test1();
12 void test2();
13 void test3();
14 
15 int main() {
16     cout << "测试1: \n";
17     test1();
18 
19     cout << "\n测试2: \n";
20     test2();
21 
22     cout << "\n测试3: \n";
23     test3();
24 }
25 
26 template <typename T>
27 void output(const T &c) {
28     for(auto &i: c)
29         cout << i << " ";
30     cout << endl;
31 }
32 
33 void test1() {
34     string s0{"0123456789"};
35     cout << "s0 = " << s0 << endl;
36 
37     string s1{s0};
38     reverse(s1.begin(), s1.end());  
39     cout << "s1 = " << s1 << endl;
40 
41     string s2{s0};
42     reverse_copy(s0.begin(), s0.end(), s2.begin()); 
43     cout << "s2 = " << s2 << endl;
44 }
45 
46 void test2() {
47     vector<int> v0{2, 0, 4, 9};
48     cout << "v0: ";
49     output(v0);
50 
51     vector<int> v1{v0};
52     reverse(v1.begin(), v1.end());
53     cout << "v1: ";
54     output(v1);
55 
56     vector<int> v2{v0};
57     reverse_copy(v0.begin(), v0.end(), v2.begin());
58     cout << "v2: ";
59     output(v2);
60 }
61 
62 void test3() {
63     vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
64     cout << "v0: ";
65     output(v0);
66 
67     vector<int> v1{v0};
68     rotate(v1.begin(), v1.begin()+1, v1.end());  
69     cout << "v1: ";
70     output(v1);
71 
72     vector<int> v2{v0};
73     rotate(v2.begin(), v2.begin()+2, v2.end());
74     cout << "v2: ";
75     output(v2);
76 
77     vector<int> v3{v0};
78     rotate(v3.begin(), v3.end()-1, v3.end());
79     cout << "v3: ";
80     output(v3);
81 
82     vector<int> v4{v0};
83     rotate(v4.begin(), v4.end()-2, v4.end());
84     cout << "v4: ";
85     output(v4);
86 }

运行结果截图:

 

任务2:

源代码task2.cpp

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

运行结果截图:

 

任务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)  
12         cout << boolalpha << is_palindrome(s) << endl;
13 }
14 
15 bool is_palindrome(std::string s)
16 {
17     using namespace std;
18     string t{s};
19     reverse(t.begin(),t.end());
20     if(s.compare(t)==0)
21     {
22         return true;
23     }
24     else
25     {
26         return false;
27     }
28     
29 }

运行结果截图:

 

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

运行结果截图:

 

任务5:

源代码task5.cpp

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 void output(string &c){
 8     for(auto &i: c){
 9         cout << setw(2) << i;
10     }
11     cout << endl;
12 }
13 
14 int main()
15 {
16     string s1{"abcdefghijklmnopqrstuvwxyz"};
17     string s2{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
18     cout << "  ";
19     output(s1);
20     int i=1;
21     while(i<=26)
22     {
23         cout << setw(2) << i;
24         rotate(s2.begin(),s2.begin()+1,s2.end());
25         output(s2);
26         i++;
27     } 
28     
29     return 0;
30     
31 }

运行结果显示:

 

任务6:

源代码task6.cpp

 1 #include <iostream>  
 2 #include <cstdlib>  
 3 #include <ctime>  
 4 #include <iomanip>  
 5   
 6 using namespace std;  
 7   
 8 int getRandomInt() {  
 9     return rand() % 10 + 1;  
10 }  
11   
12 pair<string, int> generateProblem() {  
13     int num1 = getRandomInt();  
14     int num2 = getRandomInt();  
15     char operators[] = {'+', '-', '*', '/'};  
16     char op;  
17     int correctAnswer;  
18   
19     do {  
20         op = operators[rand() % 4];  
21   
22         if (op == '-') {  
23             while (num1 <= num2) {  
24                 num1 = getRandomInt();  
25                 num2 = getRandomInt();  
26             }  
27             correctAnswer = num1 - num2;  
28         } else if (op == '/') {  
29             while (num1 % num2 != 0) {  
30                 num1 = getRandomInt();  
31                 num2 = getRandomInt();  
32             }  
33             correctAnswer = num1 / num2;  
34         } else if(op == '+'){  
35             correctAnswer = num1 + num2;  
36         }else{
37             correctAnswer = num1 * num2;
38         }  
39     } while (op == '/' && num1 / num2 < 1);  
40   
41     string problem = to_string(num1) + " " + string(1, op) + " " + to_string(num2);  
42     return make_pair(problem, correctAnswer);  
43 }  
44   
45 int main() {    
46     srand(static_cast<unsigned int>(time(0))); 
47     int correctCount = 0;  
48       
49     for (int i = 0; i < 10; ++i) {  
50         pair<string, int> problem = generateProblem();  
51         cout << problem.first << " = ";  
52         int userAnswer;  
53         cin >> userAnswer;  
54           
55         if (userAnswer == problem.second) {  
56             ++correctCount;  
57         }  
58     }  
59   
60     double accuracy = static_cast<double>(correctCount) / 10 * 100;  
61     cout << fixed << setprecision(2) << "正确率: " << accuracy << "%" << endl;  
62   
63     return 0;  
64 }

运行结果显示:

 

实验总结

通过实验1我感受到了使用C++标准库编写程序的便利,并对对象的封装、函数有了更深的了解

 

标签:begin,end,cout,int,编程,C++,v0,实验,include
From: https://www.cnblogs.com/xuruize/p/18457065

相关文章

  • 实验1 现代c++编程初体验
    任务1:task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 实验1 C++
    #include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){cout<<&q......
  • 实验一 现代C++编程初体验
    实验结论:任务一:task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<......
  • 实验一
    实验一实验任务1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<algorithm>#include<iostream>#include<string>#include<vect......
  • 实验二
    task1:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(t......
  • C++常用设计模式详解
    前言:本文详细解释几种常用的C++设计模式,都是平时项目中用的比较多的。本文针对每种设计模式都给出了示例,让你跟着代码彻底搞懂设计模式。Tips:如果是准备面试,不需要知道所有的设计模式,要深入理解下面几种常用即可,因为面试官会先问你了解哪些设计模式,然后从你了解的里面挑一......
  • c++(自创游戏6)
    1.自创游戏6作者在家里看见了一本书,书名叫:小学生趣味编程,大人也可以看,作者在上面找到了,if,下面是作者自己创作的小游戏,想玩的复制一下就行了,上代码。#include<bits/stdc++.h>#include<windows.h>usingnamespacestd;intmain(){inta;cout<<"请开始游玩if游戏"......
  • java+vue计算机毕设高校开放式实验室管理系统【源码+程序+论文+开题】
    本系统(程序+源码)带文档lw万字以上文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着高等教育的普及和深化,高校实验室作为教学和科研的重要基地,其管理和利用效率直接关系到人才培养的质量和科研成果的产出。然而,传统实验室管理模式......
  • 20222305 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    网络攻防实验报告姓名:田青学号:20222305实验日期:2024/09/29—2024/10/09实验名称:缓冲区溢出和shellcode指导教师:王志强1.实验内容本周学习内容总结:学习了系统安全(缓冲区溢出是重点)主要内容:漏洞简介:定义以及安全漏洞。BOF(缓冲区溢出):直接原因-没有严格的内存越界检查......
  • 【玩转 JS 函数式编程_010】3.2 JS 函数式编程筑基之:以函数式编程的方式活用函数(上)
    写在前面按照惯例,过长的篇幅分开介绍,本篇为JavaScript函数式编程核心基础的第二部分——以函数式编程的方式活用函数的上篇,分别介绍了JS函数在排序、回调、Promise期约、以及连续传递等应用场景下的用法演示。和之前章节相比难度又有一定的提升。准备好了吗?3.2.以......