首页 > 编程语言 >实验1 现代c++基础编程

实验1 现代c++基础编程

时间:2024-10-10 14:02:38浏览次数:1  
标签:begin cout int 编程 c++ v0 v1 实验 include

任务1:

源代码 task 1.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:

源代码 task 2.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:

源代码 task 3.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 s)
{

    int n = s.length();
    
        for (int i = 0; i < n / 2; i++)
        {
            if (s.at(i) != s.at(n - 1 - i))
            {
                return false;
                break;
            }
        }
        return true;

}

运行结果截图:

 

任务4:

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

运行结果截图:

 

任务5:

源代码 task 5.cpp

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
template <typename T>
void output(const T &c)
{
    for (auto& i : c)
    {
        cout << i << " ";
    }
    cout << endl;
}
int main()
{
    vector <char> letters(26);
    for (int i = 0; i < 26; i++)
    {
        letters[i] = 'a' + i;
    }
    vector <char> letters1(26);
        for (int i = 0; i < 26; i++)
        {
            letters1[i] = 'A' + i;
        }

    for (int i = 0; i < 27; i++)
    {
        if (i == 0)
        {
            cout << "   ";
            rotate(letters.begin(), letters.begin() + i, letters.end());
            output(letters);
        }
        else
        {
            cout <<setfill(' ') << setw(2) << i << " ";
            rotate(letters1.begin(), letters1.begin() + 1, letters1.end());
            output(letters1);
        }
    }
}

运行结果截图:

 

任务6:

源代码 task 6.cpp

#include <iostream>
#include <random>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
    vector <char> char_s{ '+','-', '*', '/' };
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<>dis1(1, 10);
    uniform_int_distribution<>dis2(0, 3);
    double count = 0;
    for (int i = 1; i <= 10; i++)
    {
        int input_result;
        int index = dis2(gen);
        int s1, s2;
        s1 = dis1(gen);
        s2 = dis1(gen);
        if (index == 1)
        {
            if (s1 < s2)
            {
                i--;
                continue;
            }
        }
        cout << s1 << " "<<char_s.at(index) <<" " << s2 <<"  " << "= ";
        cin >> input_result;
        if (index == 0)
        {
            count = count + (input_result == s1 + s2);
        }
        else if (index == 1)
        {
            count = count + (input_result == s1-s2);
        }
        else if (index == 02)
        {
            count = count + (input_result == s1 *s2);
        }
        else
        {
                count = count + (input_result == s1 / s2);
        }
    }
    cout << "正确率:"<<fixed << setprecision(2) << (count / 10)*100 << '%' << endl;


}

运行结果截图:

 

实验总结:

1.通过分析实验1和实验2更加了解了C++中vector的一些应用和性质,如:reverse、reverse_copy、rotate、sort等基于标准库<algorithm>下的一些函数的应用。

2.通过实验3、4补齐相关的函数内容,对比以前的C风格的字符串的处理,了解到了在C++环境下字符串进行连接、定义的方便,更加符合人日常处理的思维。

3.通过书写实验5的代码,了解到了setfill操作的注意事项(' ')------>单引号,还有setw()的理解,模板函数的应用,和前面实验1、2的一些函数的应用。

4.实验6代码的书写,查询到了C++标准库<random>产生随机数的方式(注意:为了保证每次产生的随机数不同,要在前面种下随机数种子),在定义vector里的内容时,要用{ },而不用();在处理输出的格式的过程中,明白里fixed 和 setprecisin(2)的一些用法。

标签:begin,cout,int,编程,c++,v0,v1,实验,include
From: https://www.cnblogs.com/wxdyyds/p/18453586

相关文章

  • 实验1 c++
    任务1:task1.cpp1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//......
  • 实验二
    任务一源代码1#include<stdio.h>2#include<time.h>34#defineN55#defineN13976#defineN24767#defineN3218intmain(){910intrandom_major,random_no;11intcnt;12srand(time(NULL));1314cnt=......
  • Java 函数式编程(1 万字)
    此笔记来自于B站黑马程序员goodJava历史版本及其优势函数式编程,StreamAPI一.函数伊始函数、函数对象函数对象行为参数法延迟执行a-lambdab-方法引用复习小测Math::random()->Math.random()Math::sqrt(doublenumber)->Math.sqrt(number)Stude......
  • 实验2
    实验任务1:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(......
  • 实验2
    任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(time(......
  • 为何实验室、课题组选择够快云库管理科研资料?
    够快云库提供文件管理服务时,很多高校、科研机构的实验室、课题组选择用够快云库管理组内的文件、资料。和一般团队不同,实验室、课题组的文件以科研文献、实验数据为主,对安全要求高;课题组成员之间文献、实验数据同步、协作需求频繁;还涉及论文的收集、修改……使用中,够快云库......
  • 实验2
    1#include<stdio.h>2#include<math.h>3#include<time.h>4#defineN55#defineN13976#defineN24767#defineN3218intmain()9{10intcnt;11intrandom_major,random_no;12srand(time(NULL));13cnt=0;14while(c......
  • 实验1 C++
    任务1:task.cpp:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//......
  • [编程笔记] 当前上下文中不存在名称"ViewBag"
    最近在弄另外一个项目,很长一段时间没接触MVC了,VisualStudio2022识别cshtml文件的时候,出了一点故障!很多ViewBag、@Html.Partial、@Html.FunctionBar()等这些地方都报波浪线了,提示不存在这个名称,但是代码是可以运行的,这种一般就是本地环境或者配置的问题了。......
  • 手写X86——第五节——X86实模式下的编程介绍2(寄存器赋初值)
    ......