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

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

时间:2024-10-12 16:59:21浏览次数:1  
标签:begin 初体验 end cout int 编程 c++ v0 include

test1:

源代码:

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

 

运行截图:

 

test2:

源代码:

 

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

运行截图:

 

test3:

源代码:

 

 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)  
 9         cout << boolalpha << is_palindrome(s) << endl;
10 }
11 bool is_palindrome(std::string s){
12     std::string a{s};
13     reverse(a.begin(),a.end());
14     if(a==s)
15         return 1;
16     if(a!=s)
17         return 0; 
18 } 

运行截图:

 

 

 

test4:

源代码:

 

 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  std::string dec2n(int x, int n) {
16     std::string s;
17     switch(n){
18          char a[100];
19          case 2:itoa(x,a,2);return a;break;
20          case 8:itoa(x,a,8);return a;break;
21          case 16:itoa(x,a,16);return a;break;
22     }
23 }
View Code

 

运行截图:

 

 

test5:

源代码:

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

运行截图:

 

 

test6:

源代码:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <ctime>
 4  
 5 using namespace std;
 6  
 7 int main() {
 8     int m=0;
 9     for(int i=0;i<10;i++){
10         srand(time(0));  
11         char op = "+-*/"[rand() % 4]; 
12         int w = 0;
13         int a;
14         int b;
15         int c;
16         switch (op) {
17             case '+':
18                  a = rand() % 10+1;  // 生成一个1到9之间的随机整数
19                  b = rand() % 10+1 ;
20                  w = a + b; break;
21             case '-':
22                  a = rand() % 10+1;  // 生成一个1到9之间的随机整数
23                  b = rand() % a+1 ;
24                  w=a-b; break;
25             case '*':w=a*b; break;
26             case '/':
27                  a= rand() % 10+1;  // 生成一个1到9之间的随机整数
28                     for(int i=2;i<=a;i++){
29                       b=i;
30                       for(int j=1;j<=10;j++){
31                           c=j;
32                         if(b*c==a){
33                             w=a/b;break;
34                         }
35                     }
36                 }
37         }
38         cout << a << ' ' << op << ' ' << b << '=' ;
39         int user_answer = 0;
40         cin >> user_answer;
41         cout<<endl;
42         if (user_answer == w) {
43                 m++;
44         } 
45     }
46     cout<<"正确率:"<<m*10<<"%"<<endl; 
47     return 0;
48 }
View Code

 

运行截图:

 

实验总结:

对于本次六个实验对于第六个实验的印象最为深刻;在生成随机数时,加法和乘法对于数字a,b,的要求较低,但在减法和除法时特别是除法要求较高;

这里我采用了对生成随机数的进一步缩减,完成减法的要求,采用循环for的方法,完成除法要求。

此外对于实验三同样有较深的印象,因为较上学期的回文数的判断,采用本学期所学会简便许多。

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

相关文章

  • 你真的懂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方阵密码的方式来解密一段只包含元音字母的密文。具体来说,它通过元音字母的全排列来生成多个可能的映射方案,然后将密文中的元音字母转换成数字序列,并根据这些数字对查找字母棋盘中的字母,尝试还原出可能的明文。程序会输出所有可能的明文,供人工......
  • 南沙C++信奥赛陈老师解一本通题 1939:【07NOIP普及组】纪念品分组
    ​ 【题目描述】元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作。为使得参加晚会的同学所获得的纪念品价值相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品,并且每组纪念品的价格之和不能超过一个给定的整数。为了保证在尽量短的时间内发完......
  • 优秀的面试官!通过一个问题考察了所有网络编程知识点
    一、写在开头本文的主题是和大家一起探讨学习:“在浏览器中输入URL开始后,计算机所做的几件事”,这个问题是好几年前自己面试的时候,面试官考问过的,当时准备十分不充分,回答的一塌糊涂,今天拿出来再整理学习一遍,一同进步!其实这个问题本身倒是不难,但它巧妙的是可以将我们所学过的网络编......