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

任务1

·源代码

点击查看代码
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

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

void test1();
void test2();
void test3();

int main()
{
    std::cout << "test1: \n";
    test1();

    std::cout << "\ntest2: \n";
    test2();

    std::cout << "\ntest3: \n";
    test3();
}

template<typename T>
void output(const T &c){
    for(auto &i : c)
        std::cout << i << ' ';
    std::cout << '\n';
}

void test1(){
    using namespace std;

    string s0{"0123456789"};
    cout << "s0 = " << s0 << endl;

    string s1(s0);
    //反转s1
    reverse(s1.begin(),s1.end());
    cout << "s1 = " << s1 << endl;

    string s2(s0.size(),' ');
    //将s0反转后结果拷贝到s2,s0自身不变
    reverse_copy(s0.begin(),s0.end(),s2.begin());
    cout << "s2 = " << s1 << endl;

}

//测试二;组合使用算法库、迭代器、vector反转动态数组对象vector内数据
void test2(){
    using namespace std;

    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);
    //reverse直接修改原容器,将其中的元素进行翻转;
    //reverse_copy不改变原容器,而是将输入的元素反转将结果复制到新容器
}

//测试三
void test3(){
    using namespace std;
    vector<int> v0{0,1,2,3,4,5,6,7,8,9};
    cout << "v0: "; output(v0);

    //rotate(begin,middle,end)以middle为标准,其左半边的元素保持相对位置不改变移到容器末尾

    vector<int> v1{v0};
    //将区间上的元素循环左移1位
    rotate(v1.begin(),v1.begin()+1,v1.end());
    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);
}

·运行截图

image

·问题1
reverse直接修改原容器,将其中的元素进行翻转;reverse_copy不改变原容器,而是将输入的元素反转将结果复制到新容器。

·问题2
(1)rotate(begin,middle,end)以middle为支点,其左半边的元素保持相对位置不改变移到容器末尾。
(2)rotate(,,)第一、三个参数分别指向序列的起始位置和结束位置,第二个参数指向支点元素。

任务二

·源代码

点击查看代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <cstdlib>
#include <ctime>

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

//int generate_random_number();
void test1();
void test2();

int main(){
    std::srand(std::time(0));
    std::cout << "test1:\n";
    test1();
    std::cout << "\ntest2: \n";
    test2();
}

//输出容器对象c中的元素
template <typename T>
void output(const T &c){
    for(auto &i: c)
        std::cout << i <<' ';
    std::cout << '\n';
}

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

//测试一
void test1(){
    using namespace std;

    vector<int> v0(10);
    //generate(v0.begin(),v0.end(),generate_random_number);
    generate(v0.begin(), v0.end(), [] (){return std::rand()%101;});
    //generate算法批量生成元素并填充指定范围
    cout << "v0: "; output(v0);

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

    vector<int> v2{v0};
    sort(v2.begin()+1,v2.end()-1);//只对中间部分排序,不包含首尾元素
    cout << "v2: "; output(v2);
}

//测试二:对容器类对象指定迭代器区间赋值、计算最大值\最小值\均值
void test2() {
    using namespace std;

    vector<int> v0(10);
    //generate(v0.begin(),v0.end(),generate_random_number);
    generate(v0.begin(), v0.end(), [](){return std::rand()%101;});
    cout << "v0: "; output(v0);

    //求最大值和最小值
    auto min_iter = min_element(v0.begin(),v0.end());
    auto max_iter = max_element(v0.begin(),v0.end());
    cout << "min: " << *min_iter << endl;
    cout << "max: " << *max_iter << endl;

    //同时求最大值和最小值
    //效率更高,减少一次遍历。代码简洁
    auto ans = minmax_element(v0.begin(), v0.end());
    cout << "min: " << *(ans.first) << endl;
    cout << "max: " << *(ans.second) << endl;

    //求平均值
    double avg1 = accumulate(v0.begin(), v0.end(),0.0) / v0.size();
    cout << "average: " << fixed << setprecision(2) << avg1 << endl;
    
    sort(v0.begin(), v0.end());
    double avg2 = accumulate(v0.begin()+1, v0.end()-1, 0.0) / (v0.size()-2);
    cout << "delete max,min,average: " << avg2 << endl;
}

·运行截图

image

·问题1
generate算法批量生成元素并填充指定范围.

·问题2
minmax_element和分别调用min_element、max_element相比,同时求最大值和最小值,代码效率更高,减少一次遍历。并且代码更加简洁。

·问题3
对于 generate(v0.begin(), v0.end(), )的第三个参数,[]是捕获列表,用来捕获变量,让lambda获取对外部变量的读写权限(){...}是函数体,调用std::rand()生成随机数,实现返回0~100之间的整数。

lambda表达式适用的场景:(1)逻辑简单且需“一次性使用”;(2)需要捕获外部变量;(3)强调内联性与代码上下文关联性。

·任务三

·源代码

点击查看代码
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

unsigned char func(unsigned char c);
void test1();
void test2();

int main() {
    std::cout << "test1: string case conversion\n";
    test1();

    std::cout << "test2: character transformation\n";
    test2();
}

unsigned char func(unsigned char c) {
    if(c == 'z')
        return 'a';

    if (c == 'Z')
        return 'A';

    if(std::isalpha(c))//判断字符是否是字母
        return static_cast<unsigned char>(c+1);//通过显式转换为 unsigned char
    
        return c;
}//将字母字符向后移动 1 位(即 “递增 1”),非字母字符保持不变,同时处理了字母的 “循环边界”(小写z→a、大写Z→A),

void test1() {
    std::string s1{"Hello World 2049!"};
    std::cout << "s1 = " << s1 << '\n';

    std::string s2;
    for(auto c: s1)
        s2 += std::tolower(c);
    std::cout << "s2 = " << s2 <<'\n';

    std::string s3;
    for(auto c: s1)
        s3 += std::toupper(c);
    std::cout << "s3 = " << s3 << '\n';
    
}

void test2()
{
    std::string s1{"I love cosmos!"};
    std::cout << "s1 = " << s1 << '\n';

    std::string s2(s1.size(), ' ');
    //创建一个长度与 s1 相同、且每个字符都初始化为空格(' ')的字符串 s2。
    std::transform(s1.begin(), s1.end(), s2.begin(), func);
    //transform函数第一和第二个参数确定输入s1中元素的范围,第三个参数是变换结果开始存储的位置,第四个参数用于对输入元素进行变换
    std::cout << "s2 = " << s2 << '\n';
}

·运行截图

image

·问题1
func的功能是将字母字符向后移动 1 位(即 “递增 1”),非字母字符保持不变,同时处理了字母的 “循环边界”(小写z→a、大写Z→A)。

·问题2
tolower和toupper功能分别是将大写字母转换成小写、将小写字母转换成大写。

·问题3
(1)transform的4个参数意义:第一和第二个参数确定输入s1中元素的范围,第三个参数是变换结果开始存储的位置,第四个参数用于对输入元素进行变换。
(2)将第3个参数s2.begin()改成s1.begin(),那么是s1将被修改成为运算的结果。

·任务四

·源代码

点击查看代码
//判断回文串,是返回true,不是则返回false
#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;

    //多组输入,直到按下control+Z
    while(cin >> s){//std::getline(std::cin,s)可以解决输入是空格问题
        cout << boolalpha
             << "is_palindrome: " << is_palindrome(s) << "\n"
             << "is_palindrome_ignore_case: " << is_palindrome_ignore_case(s) << "\n\n";
    }
}

bool is_palindrome(const std::string &s){
    std::string reversed_s = s;//复制源字符串
    std::reverse(reversed_s.begin(),reversed_s.end());
    return reversed_s == s;
}

bool is_palindrome_ignore_case(const std::string &s){
    std::string no_case_s = s;
    //将字母都变成小写再进行判断
    for(auto c: no_case_s)
        no_case_s += std::tolower(c);

    is_palindrome(no_case_s);
}

·运行截图

image

问题1
可以写成std::getline(std::cin,s)可以解决输入含有空格问题。

·任务5

·源代码

点击查看代码
//实现整数进制转换-> 十->n进制
#include <iostream>
#include <string>
#include <algorithm>

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

int main()
{
    int x;
    while(std::cin >> x) {
        std::cout << "10: " << x << '\n'
                  << "2 : " << dec2n(x) << '\n'
                  << "8 : " << dec2n(x,8) << '\n'
                  << "12 : " << dec2n(x,12) << '\n'
                  << "16 : " << dec2n(x,16) << '\n'
                  << "32 : " << dec2n(x,32) << '\n';
    };
}

std::string dec2n(int x, int n) {
    std::string s1{""};
    while(x != 0){
        // 将整数结果转换为字符串
        int remainder = x%n;
        if(remainder < 10)
        {
            s1 += static_cast<char>('0' + remainder);
        }
        else{
            s1 += static_cast<char>('A' + remainder - 10);//得到一个字母的偏移量
        }
        x = x / n;
    }
    std::reverse(s1.begin(),s1.end());
    if (s1.empty()) {
        return "0";
    }
    return s1;
}

·运行截图

image

·任务6

·源代码

点击查看代码
//打印字母密文对照表
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
//在字符间插入空格
std::string addSpaces(const std::string& s) {
    std::string result;
    for (size_t i = 0; i < s.size(); ++i) {
        result += s[i];         
        if (i != s.size() - 1) { 
            result += ' ';
        }
    }
    return result;
}

int main()
{
    std::string s1{"abcdefghijklmnopqrstuvwxyz"};
    std::string s2{""};
    //把字母都转换成大写
    for (auto c: s1)
    {
        s2 += static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
    }

    std::string s1_with_spaces = addSpaces(s1);
    std::cout << std::setw(2) << std::right << " " << " " << s1_with_spaces << '\n';
    
    for (int i = 1; i <= 26; ++i) {
        std::rotate(s2.begin(), s2.begin() + 1, s2.end()); // 左移1位
        std::string s2_with_spaces = addSpaces(s2);        // 插入空格
        std::cout << std::setw(2) << std::right << i << " " << s2_with_spaces << '\n';
    }
    
    return 0;
}

·运行截图

image

·任务7

·源代码

点击查看代码
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

int main()
{
    srand(time(0));
    //统计用户正确的题目的数量
    int correctCount = 0;

    for (int i = 0; i<10; i++){
        int num1 = rand() % 10 + 1;
        int num2 = rand() % 10 + 1;
        int sign = rand() %4;
        int result,userAnswer;

        //减法,保证num1>=num2
        if (sign == 1) {
            while (num1 < num2) {
                num1 = rand() % 10 + 1;
                num2 = rand() % 10 + 1;
            }
        }

        //除法,保证num1能被number2整除,而且num2!=0
        else if (sign == 3) {
            while (num2 == 0 || num1%num2 != 0) {
                num1 = rand() % 10 + 1;
                num2 = rand() % 10 + 1;
            }
        }

        //计算正确的结果,用来和用户输入作比较
        switch(sign) {
            case 0: result = num1 + num2; break;
            case 1: result = num1 - num2; break;
            case 2: result = num1 * num2; break;
            case 3: result = num1 / num2; break;
            default: result = 0;
        }

        //输出题目,接收用户输入
        cout << num1 << (sign == 0 ? " + " : sign == 1 ? " - ": sign == 2 ? " * " : " / ") << num2 << " = ";
        cin >> userAnswer;

        //判断,统计正确次数
        if (userAnswer == result) {
            correctCount++;
        }

    }

    //计算输出正确率
    double accuracy = (double)correctCount / 10 * 100;
    cout << "Accuracy: " << fixed << setprecision(2) << accuracy << "%" << endl;
}

·运行截图

image

posted @ 2025-10-14 21:36  feifeile  阅读(4)  评论(0)    收藏  举报