首页 > 其他分享 >实验1

实验1

时间:2024-10-15 17:43:41浏览次数:1  
标签:begin cout int v0 v1 实验 include

实验任务1:

 

实验代码:

 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 Code1

实验截图:

 

实验任务2:

实验代码:

#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;
}
View Code2

实验截图:

 

实验任务3:

实验代码:

#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 s) {
    using namespace std;
    string s1{s};
    reverse(s1.begin(), s1.end());
    if(s == s1)
    return true;
    else return false;
}
View Code3

实验截图:

 

实验任务4:

实验代码:

#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";    
    
    std::string result;  

    while (x != 0) {  
        int remain = x % n;   
        if (remain < 10) {  
            result += (remain + '0');  
        }
        else {  
            result += (remain - 10 + 'A');
        }  
        x /= n;
    }  
    std::reverse(result.begin(), result.end()); 
    return result;  
}
View Code

 

实验截图:

 

实验任务5:

实验代码:

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

实验截图:

 

实验任务6:

实验代码:

 1 #include <iostream>  
 2 #include <iomanip>  
 3 #include <cstdlib>  
 4 #include <ctime>  
 5 
 6 int main() {  
 7     using namespace std;  
 8 
 9     srand(std::time(nullptr));    
10     int num = 10;   
11     int correctAnswers = 0; 
12 
13     for (int i = 0; i < num; i++) {  
14         int a = rand() % 10 + 1;  // 生成1到10的随机数  
15         int b = rand() % 10 + 1;  // 生成1到10的随机数  
16         char operation;           // 运算符  
17         int answer;               
18 
19         // 随机选择运算符  
20         int opType = rand() % 4;  
21         switch (opType) {  
22             case 0: // 加法  
23                 operation = '+';  
24                 answer = a + b;  
25                 cout << a << " + " << b << " = ";  
26                 break;  
27 
28             case 1: // 减法   
29                 if (a <= b) swap(a, b);  // 确保 a 大于 b  
30                 operation = '-';  
31                 answer = a - b;  
32                 cout << a << " - " << b << " = ";  
33                 break;  
34 
35             case 2: // 乘法  
36                 operation = '*';  
37                 answer = a * b;  
38                 cout << a << " * " << b << " = ";  
39                 break;  
40 
41             case 3: // 除法 (确保能整除)  
42                 b = (rand() % 9 + 1);  // 生成1到9的随机数作为被除数  
43                 a = b * (rand() % (10 / b) + 1);  // 生成能整除的数  
44                 operation = '/';  
45                 answer = a / b;  
46                 cout << a << " / " << b << " = ";  
47                 break;  
48         }  
49 
50         // 获取用户输入  
51         int userAnswer;  
52         cin >> userAnswer;  
53 
54         // 判断用户答案  
55         if (userAnswer == answer) {  
56             correctAnswers++;  
57         }
58     }  
59   
60     double accuracy = correctAnswers * 100 / num;  
61     cout << fixed << setprecision(2);  
62     cout << "正确率: " << accuracy << "%" << endl;  
63 
64     return 0;  
65 }
View Code

 

实验截图:

 

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

相关文章

  • 实验一
    1//2//现代C++标准库、算法库体验3//本例用到以下内容:4//1.字符串string,动态数组容器类vector、迭代器5//2.算法库:反转元素次序、旋转元素6//3.函数模板、const引用作为形参78#include<iostream>9#include<string>10#include......
  • 实验1 现代C++编程初体验
    一、实验目的体验现代C++标准库、算法库用法灵活组合使用现代C++基础语言特性(数据表示、分支、循环、函数)和标准库,编程解决简单、基础问题编程代码过程中,注意编码素养。关注代码表达,提升代码的可读性、易于维护性二、实验准备系统浏览教材以下章节,对现代c++基础语言特性和......
  • 20222301 2024-2025-1 《网络与系统攻防技术》实验二实验报告
    1.实验内容本次实验主要围绕渗透测试与远程执行控制展开,通过不同工具和技术手段实现了对目标主机的深入渗透与监控。实验内容可以概括为以下几个方面:1.远程Shell获取:实验首先通过`netcat`和`cron`定时任务,以及`socat`与Windows任务计划相结合的方式,实现了对目标主机的远程Shell......
  • 实验2
    实验任务1include<stdio.h>include<stdlib.h>include<time.h>defineN5defineN1397defineN2476defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt<N){random_major=rand()......
  • 数据采集实验一
    题目一(1)要求:用requests和BeautifulSoup库方法定向爬取给定网址(http://www.shanghairanking.cn/rankings/bcur/2020)的数据,屏幕打印爬取的大学排名信息。pytho......
  • 程序实际实验1实验报告
    实验任务11#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtest2();13......
  • 实验一
    任务1#include#include#include#includeusingnamespacestd;//声明//模板函数声明templatevoidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest3();intmain(){ cout<<"测试1:\n"; test1(); cout<......
  • jsp高校空闲实验室资源预约管理系统77gmb
    jsp高校空闲实验室资源预约管理系统77gmb本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能学生,实验室信息,学生预约实验室,教师,设备信息,教师预约实验室,学生取消预约,教师取消预约技术要求:  ......
  • 实验1 现代C++编程初体验
    实验任务1代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;//声明//模板函数声明template<typenameT>voidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest......
  • OpenAI发布了一项实验性框架“Swarm”
      每周跟踪AI热点新闻动向和震撼发展想要探索生成式人工智能的前沿进展吗?订阅我们的简报,深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同,从行业内部的深度分析和实用指南中受益。不要错过这个机会,成为AI领域的领跑者。点击订阅,与未来同行!订阅:https://......