实验一

任务1

task1

  • reverse是翻转一个序列,reverse_copy会把翻转过的序列复制给其他容器
  • rotate将前两个参数对应的序列放到后两个参数对应序列之后。rotate(first,middle,last)

任务2

task2

  1. generate会用自定义的函数生成一个序列
  2. minmax_element只需要一次遍历即可获得两个结果
  3. 更适合传递一个函数类型参数,且被传递的函数体较小时使用

任务3

task3

  1. 让一个字母变成字母表对应的下一个字母
  2. tolower大写变小写,toupper小写变大写
  3. transform(源序列首,源序列尾,目标序列首,变化函数)。
  4. 如果把s2.begin()变成s1.begin()会直接更改源序列

任务4

task4

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

bool is_palindrome(const std::string &s);
bool is_palindrome_ignore_case(const std::string &s);

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

    // 多组输入,直到按下Ctrl+Z结束测试
    while(getline(cin , s)) {
        cout << boolalpha 
             << "区分大小写: " << is_palindrome(s) << "\n"
             << "不区分大小写: " << is_palindrome_ignore_case(s) << "\n\n";
    }     
}


// 函数is_palindrome定义
bool is_palindrome(const std::string &s)
{
    using namespace std;

    string s2;
    reverse_copy(s.begin(),s.end(),back_inserter(s2));
    
    return s2 == s;
}

// 函数is_palindrome_ignore_case定义
bool is_palindrome_ignore_case(const std::string &s)
{
    using namespace std;

    string s1,s2;
    transform(s.cbegin(),s.cend(),
        back_inserter(s1),
        [](char ch){
            return toupper(ch);
        });
    reverse_copy(s1.begin(),s1.end(),back_inserter(s2));

    return s2 == s1;
}
  • 用getline(cin , s)
  • 已在源码中写出

任务5

task5

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

std::vector<char> v;
std::string dec2n(int x, int n = 2);

int main() {
    int x;
    
    if(v.empty())
    {
        generate_n(back_inserter(v),36,
        [](){
            static int i = -1;
            i++;
            if(i <= 9)
                return i + '0';
            else
                return i - 10 + 'A';
        });
    }

    while(std::cin >> x) {
        std::cout << "十进制: " << x << '\n'
                  << "二进制: " << dec2n(x) << '\n'
                  << "八进制: " << dec2n(x, 8) << '\n'
                  << "十二进制: " << dec2n(x, 12) << '\n'
                  << "十六进制: " << dec2n(x, 16) << '\n'
                  << "三十二进制: " << dec2n(x, 32) << "\n\n"; 
    }
}

// 函数dec2n定义
std::string dec2n(int x, int n)
{
    using namespace std;
    
    string ans;
    while(true)
    {
        ans.push_back(v[x%n]);
        x = x/n;
        if(x < n)
            break;
    }

    //当x不为0时在放进去,测试数据31
    if(x) ans.push_back(v[x]);
    reverse(ans.begin(),ans.end());

    return ans;
}

任务6

task6

#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;

template <typename T>
void output(T& arr)
{
    for(auto &i : arr)
    {
        cout << setw(2) << i ;
    }
}

int main()
{
    string str,str2;
    generate_n(back_inserter(str),26,
        [](){
            static char i ='a' - 1;
            i++;
            return i;
        });

    transform(str.begin(),str.end(),
        back_inserter(str2),
        [](char& ch){
            return toupper(ch);
        });

    cout << setw(2) << ' ';
    output(str);

    for(int i = 1; i <= 26; i++)
    {   
        rotate(str2.begin(),str2.begin()+1,str2.end());
        cout << endl <<setw(2) << i;
        output(str2);
    }
    return 0;
}

任务7

task7

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

enum OP{
    ADD,
    MINUS,
    MULTIPLE,
    DIVISION
};

int main()
{
    int a,b,ans,ans_,correct{0};
    char op;
    srand(time(0));
    for(int i = 0; i < 10; i++)
    {
        int op_ = rand()%4;
        a = rand()%10+1;
        b = rand()%10+1;

        switch(op_)
        {

            case OP::ADD:
                ans = a+b;
                op = '+';
                break;

            case OP::MINUS:
                if(a < b)
                    swap(a,b);
                ans = a-b;
                op = '-';
                break;

            case OP::MULTIPLE:
                ans = a*b;
                op = '*';
                break;

            case OP::DIVISION:
                if(a < b)
                    swap(a,b);
                if(a / b)
                    a -= a%b;
                ans = a/b;
                op = '/';
                break;                
        }

        cout << ' ' << a 
             << ' ' << op
             << ' ' << b
             << ' ' << '=' << ' ';

        cin >> ans_;
        if(ans_ == ans)
            correct++;
    }

    cout << "正确率" << 100.0*(correct / 10.0) << '%' << endl;

    return 0;
}
posted @ 2026-10-22 21:06  yuanzhan  阅读(6)  评论(0)    收藏  举报