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

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

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

1.实验任务一

task1.cpp

// 现代C++标准库、算法库体验
// 本例用到以下内容:
// 1. 字符串string, 动态数组容器类vector、迭代器
// 2. 算法库:反转元素次序、旋转元素
// 3. 函数模板、const引用作为形参

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

using namespace std;

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

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

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

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

    cout << "\n测试3: \n";
    test3();
}

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

// 测试1
// 组合使用算法库、迭代器、string反转字符串
void test1() {
    string s0{"0123456789"};
    cout << "s0 = " << s0 << endl;

    string s1{s0};
    reverse(s1.begin(), s1.end());  // 反转指定迭代器区间的元素
    cout << "s1 = " << s1 << endl;

    string s2{s0};
    reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝到指定迭代器开始的目标区间,并且在复制过程中反转次序
    cout << "s2 = " << s2 << endl;
}

// 测试2
// 组合使用算法库、迭代器、vector反转动态数组对象vector内数据
void test2() {
    vector<int> v0{2, 0, 4, 9};
    cout << "v0: ";
    output(v0);

    vector<int> v1{v0};
    reverse(v1.begin(), v1.end());
    cout << "v1: ";
    output(v1);

    vector<int> v2{v0};
    reverse_copy(v0.begin(), v0.end(), v2.begin());
    cout << "v2: ";
    output(v2);
}

// 测试3
// 组合使用算法库、迭代器、vector实现元素旋转移位
void test3() {
    vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    cout << "v0: ";
    output(v0);

    vector<int> v1{v0};
    rotate(v1.begin(), v1.begin()+1, v1.end());  // 旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
    cout << "v1: ";
    output(v1);

    vector<int> v2{v0};
    rotate(v2.begin(), v2.begin()+2, v2.end());
    cout << "v2: ";
    output(v2);

    vector<int> v3{v0};
    rotate(v3.begin(), v3.end()-1, v3.end());
    cout << "v3: ";
    output(v3);

    vector<int> v4{v0};
    rotate(v4.begin(), v4.end()-2, v4.end());
    cout << "v4: ";
    output(v4);
}

测试结果:

2.实验任务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;
}

bool is_palindrome(std::string s)
{
    using namespace std;
    string s1(s);
    reverse(s1.begin(),s1.end());
    int cmp=s.compare(s1);
    if(cmp==0)
    return 1;
    else
    return 0;
}

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){
    std::string result;
    if(x==0){
        return "0";
    }
    while(x>0){
        int a=x%n;
        if(a<10){
            result+=a+'0';
        }
        else{
            result+=(a-10)+'A';
        }
        x/=n;
    }
    reverse(result.begin(),result.end());
    return result;
}

测试结果:

5.实验任务五

task5.cpp

#include<iostream>

using namespace std;

int main(){
    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\n";
    for(int i=0;i<26;i++){
        cout<<i+1<<" ";
        for(int j=0;j<26;j++){
            char letter='A'+(i+j+1)%26;
            cout<<letter<<" ";
        }
        cout<<endl;
    }
    return 0;
}

测试结果:

6.实验任务六

task6.cpp

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>

using namespace std;

int test(char x,int a,int b){
if(x == '+') return a+b;
else if (x == '-') return a-b;
else if (x == '*') return a*b;
else return a/b;
}

int main()
{

char sym[4]={'+','-','*','/'};
int curr = 0;
for(int i=0;i<10;i++){
srand(time(0));
char x = sym[rand()%4];
int a,b;
if(x == '-'){
a = rand()%9+2;
b = rand()%(a-1)+1;
}
else if(x == '/'){
b = rand()%10+1;
a = b*(rand()%10/b+1);
}
else a = rand()%10+1,b=rand()%10+1;
cout<<a<<x<<b<<"=";
int ans;
cin>>ans;
if(ans == test(x,a,b)) curr++;
}
cout<<"正确率:"<<1.0*curr/10*100<<setprecision(2)<<"%";
return 0;


}

测试结果:

7.实验总结

通过本次实验,我了解了动态数组容器类vector,迭代器等的运用;通过实验一实验二初步了解了反转元素次序和旋转元素

  • reverse(s1.begin(), s1.end());  // 反转指定迭代器区间的元素
  • reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝 到指定迭代器开始的目标区间,并且在复制过程中反转次序
  • 通过实验3实践了以上内容

通过自己动手编写了实验任务4.5.6,让我对C++有了更深一步的理解和使用。

 

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

相关文章

  • 实验4-2-3-for 验证“哥德巴赫猜想C++解法
    #include<iostream>#include<cmath>boolvia(longlongi);usingnamespacestd;intmain(){  longlongn=0,i=3,p=0,q=0,a=0,b=0;  cin>>n;  if(n>4)  {    for(i=3;i<n/2;i+=2)    {......
  • 螺旋方阵C++解法
    #include<iostream>#include<vector>usingnamespacestd;#include<iomanip>intn;intmain(){   cin>>n;   vector<vector<int>>arr(n,vector<int>(n,0));   intx=0,y=0,s=1;   while(s<=n*......
  • Qt/C++编写的mqtt调试助手使用说明
    一、使用说明第一步,选择协议前缀,可选mqtt://、mqtts://、ws://、wss://四种,带s结尾的是走ssl通信,ws表示走websocket通信。一般选默认的mqtt://就好。第二步,填写服务所在主机地址,可以是IP地址也可以是网址,只要真实存在的就行。第三步,填写通信所用端口号,mqtt默认端口号是1883,以......
  • Python面向对象编程:继承和多态③
    文章目录一、继承1.1什么是继承1.2定义父类和子类1.3子类重写父类的方法1.4多继承二、多态2.1什么是多态2.2多态的实现2.3抽象类和接口三、综合详细例子3.1项目结构3.2模块代码init.pyshape.pycircle.pyrectangle.py3.3主程序代码main.py3.4运行结果四......
  • JS异步编程精通之路(一):Callback、Promise、Async/Await 和 Observable 深度对比
    在现代JavaScript编程中,异步操作是常见且必不可少的部分。处理异步的方式多种多样,其中最常见的有Callback、Promise、Async/Await,以及近年来随着响应式编程(ReactiveProgramming)理念兴起的Observable。本文将对这几种异步处理方式进行对比,帮助你理解它们各自的优缺点,以......
  • C++可用的websocket库
    库说明优势劣势是否免费商用QtWebSocketsQt框架中的WebSocket模块。不需要额外集成第三方库;支持异步处理,适合在Qt应用程序中处理并发WebSocket请求。性能较差付费libwebsockets轻量级的C库,用于开发WebSocket服务器和客户端。高性能,低内存占用,支持多种平台,包......
  • MySQL 的 JDBC 编程
    MySQL的JDBC编程使用java代码操作数据库jdbc统一了各种数据库的api准备阶段首先要准备编辑语言的安装:javajdk1.8数据库数据库驱动包:在中央仓库下载搜索MySQL第一个就是MySQLConnector/j下载完成后在IDEA中导入这个包打开IDEA新建一个java项目新建......
  • 慧通教育C++测试题 103667--103673(5题)
    103667.求最大值难度:1登录//103667.求最大值难度:1#include<bits/stdc++.h>usingnamespacestd;intans=0,n,m,q,a[105][105];intmain(){ cin>>n>>m>>q; for(inti=1;i<=n;i++){ for(intj=1;j<=m;j++){ cin>>a[i][j]; } } int......
  • langchain更新初体验
    文章目录前言写一个基本的demo配置文件.streamlit/secrets.toml获得大模型对象Chain退场,上场的是Runnable总结前言在上一篇文章中(已同步到博客langchain更新初体验),探讨了langchain在0.2.1......
  • 01背包问题/Ieee全球极限编程大赛11.0题BeetleBag题解/洛谷P1926 小书童——刷题大军
    基础01背包问题概述给出一个容积为V的背包,有i个物体,每个物体都有自己的体积和价值,用Vi和Wi表示,要将这些物体装进背包里面,问怎样才能使得装入物体的总价值最大?最大为多少?解决思路1.如果你没能正确理解这道题,尤其是对于很多新手,第一反应可能是将所有物体的单位价值算出来,然后......