首页 > 编程语言 >实验1 c++

实验1 c++

时间:2024-10-14 20:00:17浏览次数:1  
标签:std begin 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:

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:

task.cpp3:

#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) {
    std::string reversed_s = s; // 复制原字符串
    std::reverse(reversed_s.begin(), reversed_s.end()); // 反转字符串

    // 检查原字符串与反转字符串是否相等
    return s == reversed_s; // 若相等,则为回文串
}

  

运行结果截图:

 

任务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;
    }
}

std::string dec2n(int x, int n) {
    if (x == 0) return "0"; // 特殊情况处理
    std::string result;

    while (x > 0) {
        int remainder = x % n; // 计算余数
        // 如果进制是十六进制,处理大写字母A-F
        if (remainder < 10) {
            result += (char)(remainder + '0'); // 转换为字符
        }
        else {
            result += (char)(remainder - 10 + 'A'); // 转换为字符 'A'-'F'
        }
        x /= n; // 更新x
    }

    std::reverse(result.begin(), result.end()); // 反转字符串
    return result; // 返回最终结果
}

运行结果截图:

任务5:

task5.cpp:

#include <iostream>
#include <iomanip> // 引入头文件以使用 std::setw

void printCipher() {
    const std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
    const std::string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // 打印小写字母行
    std::cout << "   ";
    for (char c : alphabet) {
        std::cout << c << ' '; // 每个字符后加一个空格
    }
    std::cout << std::endl;

    // 打印26行大写字母
    for (int i = 0; i < 26; ++i) {
        std::cout << std::setw(2) << (i + 1) << ' '; // 输出行号并对齐
        for (int j = 0; j < 26; ++j) {
            std::cout << ALPHABET[(i + j) % 26] << ' '; // 输出带偏移的字母
        }
        std::cout << std::endl;
    }
}

int main() {
    printCipher();
    return 0;
}

  

运行结果截图:

 

任务6:

task6.cpp:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

int main() {
    std::srand(static_cast<unsigned int>(std::time(0)));
    const int totalQuestions = 10;
    int correctAnswers = 0;

    for (int i = 0; i < totalQuestions; ++i) {
        int num1, num2, correctAnswer;
        char operation;

        int operationChoice = std::rand() % 4;

        switch (operationChoice) {
        case 0: // 加法
            num1 = std::rand() % 10 + 1;
            num2 = std::rand() % 10 + 1;
            operation = '+';
            correctAnswer = num1 + num2;
            break;
        case 1: // 减法
            num1 = std::rand() % 10 + 1;
            num2 = std::rand() % num1 + 1; // 确保 num1 >= num2
            operation = '-';
            correctAnswer = num1 - num2;
            break;
        case 2: // 乘法
            num1 = std::rand() % 10 + 1;
            num2 = std::rand() % 10 + 1;
            operation = '*';
            correctAnswer = num1 * num2;
            break;
        case 3: // 除法
            num2 = std::rand() % 9 + 1;
            correctAnswer = std::rand() % (10/num2) + 1;
            num1 = num2 * correctAnswer; // 确保 num1 能被 num2 整除
            operation = '/';
            break;
        }

        std::cout << num1 << " " << operation << " " << num2 << " = ";
        int userAnswer;
        std::cin >> userAnswer;

        if (userAnswer == correctAnswer) {
            ++correctAnswers;
        }
    }

    double accuracy = static_cast<double>(correctAnswers) / totalQuestions * 100;
    std::cout << "正确率: " << std::fixed << std::setprecision(2) << accuracy << "%" << std::endl;

    return 0;
}

  

运行结果截图:

 

 

标签:std,begin,cout,int,c++,v0,实验,include
From: https://www.cnblogs.com/gzry/p/18464906

相关文章

  • 实验1 现代C++编程初体验
    任务1:源代码task1.cpp1#include<bits/stdc++.h>2usingnamespacestd;34//模板函数声明5template<typenameT>6voidoutput(constT&c);78//普通函数声明9voidtest1();10voidtest2();11voidtest3();1213intmain(){14cout&l......
  • 实验一
    代码一:迭代器的v.end()是指容器最后一个元素的后面一个代码二:代码三:点击查看代码boolis_palindrome(std::stringt){inti=0,j,flag=0;j=t.size()-1;for(;i<j;i++,j--){if(t[i]!=t[j])flag=1;break;}if(f......
  • C++:初识
       1.namespace    1.1namespace的价值和定义        在c++中存在着大量的类,这些变量函数,类都存在全局变量当中为了,避免命名冲突c++当中引入了namespace。    在命名冲突中c语言也是普遍存在的观察如下c代码:#include<stdio.h>#include<st......
  • C++面向对象多态篇
    目录1.什么是多态?2.多态的概念3. 函数覆盖4.虚函数的定义5.多态实现6.多态的原理7.虚析构函数(掌握)8.类型转换8.1static_cast8.2dynamic_cast8.3const_cast(了解)8.4reinterpret_cast9、抽象类(掌握)10、纯虚析构函数(熟悉)11、私有析构函数(熟悉)1.什么......
  • C++在vscode中的code runner配置/环境配置
    C++在vscode中快捷运行(coderunner)一、配置tasks.json在vscode中创建文件夹或打开文件夹,会发现文件夹下多了一个.vscode文件夹,在该文件夹下创建tasks.json文件,并添加一下内容{"version":"2.0.0","tasks":[{"type":"shell","la......
  • C++异步调用 future async promise packaged_task
    背景:C++异步调用是现代C++编程中的一种重要技术,它允许程序在等待某个任务完成时继续执行其他代码,从而提高程序的效率和响应性。C++11引入了std::async、std::future和std::promise等工具,使得异步编程变得更加方便和直观。以下是关于C++异步调用的详细介绍,包括基本概......
  • 实验1 现代C++编程初体验
    任务11//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#include......
  • 实验1 C++
    task1:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//普通函数声明......
  • 实验一
    任务一:代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){ cout<......
  • 【最新原创毕设】基于SpringCloud的一站式热点推荐平台+23649(免费领源码)可做计算机毕
    目 录摘要1绪论1.1选题背景与意义1.2开发现状1.3论文结构与章节安排2 开发环境及相关技术介绍2.1MySQL数据库2.2 Tomcat服务器2.3 Java语言2.4 SpringCloud框架介绍3 一站式热点推荐平台系统分析3.1可行性分析3.1.1技术可行性分析3.1......