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

c++实验1

时间:2024-10-15 22:23:07浏览次数:6  
标签:begin end cout int c++ v0 实验 include

实验任务1:

 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<<"测试2:\n";
15     test2();
16     cout<<"测试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     
40     vector<int> v1{v0};
41     reverse(v1.begin(),v1.end());
42     cout<<"v1:";
43     output(v1);
44     
45     vector<int> v2{v0};
46     reverse_copy(v0.begin(),v0.end(),v2.begin());
47     cout<<"v2:";
48     output(v2);
49 }
50 void test3(){
51     vector<int> v0{0,1,2,3,4,5,6,7,8,9};
52     cout<<"v0:";
53     output(v0);
54     
55     vector<int> v1{v0};
56     rotate(v1.begin(),v1.begin()+1,v1.end());
57     cout<<"v1:";
58     output(v1);
59     
60     vector<int> v2{v0};
61     rotate(v2.begin(),v2.begin()+2,v2.end());
62     cout<<"v2:";
63     output(v2);
64     
65     vector<int> v3{v0};
66     rotate(v3.begin(),v3.end()-1,v3.end());
67     cout<<"v3:";
68     output(v3);
69     
70     vector<int> v4{v0};
71     rotate(v4.begin(),v4.end()-2,v4.end());
72     cout<<"v4:";
73     output(v4);
74 }

 

实验任务2:  

 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     
17     cout<<"测试2:\n";
18     test2();
19 }
20 template <typename T>
21 void output(const T &C){
22     for(auto &i: C)
23         cout<<i<<" ";
24     cout<<endl;    
25 }
26 int rand_int_100(){
27     return rand()%101;
28 }
29 
30 void test1(){
31     vector<int> v0(10);
32     generate(v0.begin(),v0.end(),rand_int_100);
33     cout<<"v0:";
34     output(v0);
35     
36     vector<int>v1{v0};
37     sort(v1.begin(),v1.end());
38     cout<<"v1:";
39     output(v1);
40     
41     vector<int> v2{v0};
42     sort(v2.begin()+1,v2.end()-1);
43     cout<<"v2:";
44     output(v2);
45 }
46 
47 void test2(){
48     vector<int> v0(10);
49     generate(v0.begin(),v0.end(),rand_int_100);
50     cout<<"v0:";
51     output(v0);
52     
53     auto iter1=min_element(v0.begin(),v0.end());
54     cout<<"最小值:"<<*iter1<<endl;
55     
56     auto iter2=max_element(v0.begin(),v0.end());
57     cout<<"最大值:"<<*iter2<<endl;
58     
59     auto ans=minmax_element(v0.begin(),v0.end());
60     cout<<"最小值:"<<*(ans.first)<<endl;
61     cout<<"最大值:"<<*(ans.second)<<endl;
62     double avg1=accumulate(v0.begin(),v0.end(),0)/v0.size();
63     cout<<"均值:"<<fixed<<setprecision(2)<<avg1<<endl;
64     cout<<endl;
65     
66     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 }

 

实验任务3:  

 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::transform(s.begin(),s.end(),s.begin(),::tolower);
13     int l=0;
14     int r=s.size()-1;
15     while(l<r){
16         if(s[l]!=s[r]){
17             return false;
18         }
19         l++;
20         r--;
21     }
22     return true;
23 }

 

实验任务4:

 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     const char digits[] = "0123456789ABCDE";  
17     std::string result;  
18     if(x==0){  
19         return "0";  
20     }    
21     bool isNegative=false;  
22     if(x<0){  
23         isNegative=true;  
24         x=-x;  
25     }    
26     while(x>0){  
27         result+=digits[x%n];  
28         x/=n;  
29     }   
30     if(isNegative){  
31         result+="-";  
32     }    
33     std::reverse(result.begin(),result.end());  
34     return result;  
35 }

 

实验任务5: 

 1 #include<iostream>  
 2 #include<iomanip> 
 3 using namespace std;  
 4 int main(){  
 5     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;   
 6     for(int i=0;i<26;++i){   
 7         cout<<setw(2)<<setfill(' ')<<(i+1);  
 8         for(int j=0;j<26;++j){  
 9             char base='A';  
10             char cu=(base-'A'+(j+i)%26)+'A'; 
11             cout<<cu<<" "; 
12         }   
13         cout<<endl;  
14     }  
15     return 0;  
16 }

 

实验任务6: 

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

 

标签:begin,end,cout,int,c++,v0,实验,include
From: https://www.cnblogs.com/hb879655/p/18468660

相关文章

  • C++中的不安全函数
    不安全函数(UnsafeFunctions)通常指那些在特定条件下可能导致程序错误、数据损坏或安全漏洞的函数。在编程中,不安全函数可能表现为以下几种情况:缓冲区溢出:当函数在处理数据时没有检查输入的大小,可能导致超出预分配内存空间的写入,造成数据破坏或程序崩溃。例如,在C和C++中,strcpy、......
  • 实验1
    task1:includeincludeincludeincludeusingnamespacestd;//声明//模板函数声明templatevoidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest3();intmain(){cout<<"测试1:\n";test1();cout<<"\n测试2:......
  • XSS漏洞利用实验——使用两种方式外带cookie
    0x00前言关于XSS的基本知识可以查看https://blog.csdn.net/weixin_44656518/article/details/142832967?spm=1001.2014.3001.55010x01使用自建服务器外带数据(反射型XSS)使用实验环境:kali虚拟机,pikachu漏洞实验平台1.查看kali虚拟机ip地址我这里是192.168.80.131不同虚......
  • 实验一 现代C++编程初体验
    任务1//现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>using......
  • C++(nullptr、类型推导、初始化列表、)
    1.nullptr(掌握)nullptr是C++11推出的新的空指针,用于代替C语言的NULL。#include<iostream>usingnamespacestd;voidfunc(inti){cout<<"A"<<i<<endl;}voidfunc(char*c){cout<<"B"<<c<<en......
  • 实验1
    任务1:体会面向对象设计中封装、暴露接口(interface)、基于接口编程的意义代码://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<ios......
  • 实验一
    实验1代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//普通函数声明......
  • 实验1 现代C++编程初体验
    实验任务1:task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string......
  • C++使用MySQL官方的C API访问MySQL数据库
    这篇文章是一个简单的C++使用MySQL官方的CAPI访问MySQL数据库的代码示例。//main.h#ifndef_H_#define_H_#include<stdio.h>#include<Windows.h>#include<mysql.h>#pragmacomment(lib,"libmysql.lib")#defineinsert_prepare"insertintotest_tbvalue......
  • 实验一
    task1:点击查看代码//现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algo......