首页 > 其他分享 >实验1

实验1

时间:2024-10-10 22:02:06浏览次数:1  
标签:begin cout int v0 v1 实验 include

实验任务1:

task1.cpp:

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

运行结果:

 

实验任务2:

task2.cpp:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <iomanip>

using namespace std;

// 函数声明
// 模板函数声明
template<typename T>
void output(const T &c);

// 普通函数声明
int rand_int_100();
void test1();
void test2();

int main() {
    cout << "测试1: \n";
    test1();

    cout << "\n测试2: \n";
    test2();
}

// 函数实现
// 输出容器对象c中的元素
template <typename T>
void output(const T &c) {
    for(auto &i: c)
        cout << i << " ";
    cout << endl;
}

// 返回[0, 100]区间内的一个随机整数
int rand_int_100() {
    return rand() % 101;
}

// 测试1
// 对容器类对象指定迭代器区间进行赋值、排序
void test1() {
    vector<int> v0(10);  // 创建一个动态数组对象v0, 对象大小为10
    generate(v0.begin(), v0.end(), rand_int_100); // 产生[0, 100]之间的随机整数赋值给指定迭代器区间[v0.begin(), v0.end())内的每个数据项
    cout << "v0: ";
    output(v0);

    vector<int> v1{v0};
    sort(v1.begin(), v1.end()); // 对指定迭代器区间[v1.begin(), v1.end())内数据项进行升序排序
    cout << "v1: ";
    output(v1);

    vector<int> v2{v0};
    sort(v2.begin()+1, v2.end()-1); // 对指定迭代器区间[v1.begin()+1, v1.end()-1)内数据项进行升序排序
    cout << "v2: ";
    output(v2);
}

// 测试2
// 对容器类对象指定迭代器区间进行赋值、计算最大值/最小值/均值
void test2() {
    vector<int> v0(10);  
    generate(v0.begin(), v0.end(), rand_int_100); 
    cout << "v0: ";
    output(v0);

    auto iter1 = min_element(v0.begin(), v0.end());
    cout << "最小值: " << *iter1 << endl;

    auto iter2 = max_element(v0.begin(), v0.end());
    cout << "最大值: " << *iter2 << endl;

    auto ans = minmax_element(v0.begin(), v0.end());
    cout << "最小值: " << *(ans.first) << endl;
    cout << "最大值: " << *(ans.second) << endl;
    double avg1 = accumulate(v0.begin(), v0.end(), 0)/v0.size();
    cout << "均值: " << fixed << setprecision(2) << avg1 << endl;

    cout << endl;

    vector<int> v1{v0};
    cout << "v0: ";
    output(v0);
    sort(v1.begin(), v1.end());
    double avg2 = accumulate(v1.begin()+1, v1.end()-1, 0)/(v1.size()-2);
    cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
}

运行结果:

实验任务3:

task3.cpp:

#include <iostream>
#include <string>
#include <algorithm>

bool is_palindrome(std::string s);

int main() {
    using namespace std;
    string s;

    while(cin >> s)  // 多组输入,直到按下Ctrl+Z后结束测试
        cout << boolalpha << is_palindrome(s) << endl;
}

// 函数is_palindrom定义
// 待补足
// ×××
bool is_palindrome(std::string s1){
    std::string s2{s1};
    reverse(s2.begin(),s2.end());
    for(int i=0;i<=s1.size()/2;i++){
        if(s1[i]!=s2[i])
        return false;
    }
    return true;
}

运行结果:

实验任务4:

task4.cpp:

#include <iostream>
#include <string>
#include <algorithm>

std::string dec2n(int x, int n = 2);

int main() {
using namespace std;

int x;
while(cin >> x) {
cout << "十进制: " << x << endl;
cout << "二进制: " << dec2n(x) << endl;
cout << "八进制: " << dec2n(x, 8) << endl;
cout << "十六进制: " << dec2n(x, 16) << endl << endl;
}
}

// 函数dec2n定义
// 待补足
// ×××
std::string dec2n(int x,int n){
if(x==0) return "0";
char element[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
std::string ans;
int m;
while(x>0){
m=x%n;
x/=n;
ans.push_back(element[m]);
}
reverse(ans.begin(),ans.end());
return ans;
}

 

运行结果:

实验任务5:

task5:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main(){
 5     vector<char> member;
 6     char ch;
 7     cout<<"  ";
 8     for(int i=0;i<26;i++){
 9         ch='a'+i;
10         cout<<ch<<' ';
11         member.push_back(ch-32);
12     }
13     cout<<endl;
14     for(int i=1;i<=26;i++){
15         cout<<i<<' ';
16         for(int j=i;j<26;j++)
17         cout<<member[j]<<' ';
18         for(int k=0;k<i;k++)
19         cout<<member[k]<<' ';
20         cout<<endl;
21     }
22 }

 

运行结果:

 

实验任务6

task6:

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 char solution(){
 5     char member[]={'+','-','*','/'};
 6     return member[rand()%4];
 7 }
 8 
 9 int add(int x,int y){
10     return x+y;
11 }
12 
13 int minus1(int x,int y){
14     if(x>y){
15         cout<<x<<' '<<'-'<<' '<<y<<' '<<'='<<' ';
16         return x-y;
17     }
18     cout<<y<<' '<<'-'<<' '<<x<<' '<<'='<<' ';
19     return y-x;
20 }
21 
22 int ride(int x,int y){
23     return x*y;
24 }
25 
26 int division(int x,int y){
27     if(x>=y){
28         while(x%y!=0)
29         y--;
30         cout<<x<<' '<<'/'<<' '<<y<<' '<<'='<<' ';
31         return x/y;
32     }
33     while(y%x!=0)
34     x--;
35     cout<<y<<' '<<'/'<<' '<<x<<' '<<'='<<' ';
36     return y/x;
37 }
38 
39 int judge(int x,int y){
40     if(x==y)return 1;
41     return 0;
42 }
43 
44 int main(){
45     srand(time(0));
46     char ch;
47     int ans,userans,num1,num2,sum=0;
48     double right;
49     for(int i=0;i<10;i++){
50         ch=solution();
51         num1=rand()%10+1;
52         num2=rand()%10+1;
53         if(ch=='+'){
54             ans=add(num1,num2);
55             cout<<num1<<' '<<'+'<<' '<<num2<<' '<<'='<<' ';
56             cin>>userans;
57             sum+=judge(ans,userans);
58         }
59         else if(ch=='-'){
60             ans=minus1(num1,num2);
61             cin>>userans;
62             sum+=judge(ans,userans);
63         }
64         else if(ch=='*'){
65             ans=ride(num1,num2);
66             cout<<num1<<' '<<'*'<<' '<<num2<<' '<<'='<<' ';
67             cin>>userans;
68             sum+=judge(ans,userans);
69         }
70         else{
71             ans=division(num1,num2);
72             cin>>userans;
73             sum+=judge(ans,userans);
74         }
75     }
76     right=sum*10.0;
77     cout<<fixed<<setprecision(2);
78     cout<<"正确率:"<<right<<'%'; 
79 }

 

运行结果:

 

 

标签:begin,cout,int,v0,v1,实验,include
From: https://www.cnblogs.com/sunnyllyy/p/18453548

相关文章

  • 20222304 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1本周学习内容1)反汇编反汇编是指将计算机程序的机器代码转换回其相应的汇编代码的过程。在计算机编程和逆向工程领域中,反汇编是一种常见的技术,用于理解和分析二进制程序的功能和内部结构。通常情况下,程序员编写的源代码会被编译器转换成机器码,这是计算机可以直接......
  • 20222321 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    一.实验内容1实验目标本次实践的对象是一个名为pwn1的linux可执行文件。该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串。该程序同时包含另一个代码片段,getShell,会返回一个可用Shell。正常情况下这个代码是不会被运行的。我们实践的目标就是想......
  • 实验1
    #include<algorithm>//#include<typeinfo>//#include<iomanip>//#include<map>#include<vector>#include<string>#include<iostream>usingnamespacestd;usingstd::string;//模板函数声明template<typenameT&g......
  • 20222411 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1基础知识1.1.1NOP,JNE,JE,JMP,CMP汇编指令的机器码(1)NOP:NOP指令即“空指令”。执行到NOP指令时,CPU什么也不做,仅仅当做一个指令执行过去并继续执行NOP后面的一条指令。(机器码:90)(2)JNE:条件转移指令,如果不相等则跳转。(机器码:75)(3)JE:条件转移指令,如果相等则跳转。(......
  • 实验2
    TASK11#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(ti......
  • 实验1 现代C++基础编程
    任务1:源代码task1.cpp1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12void......
  • 实验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......