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

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

时间:2024-10-15 18:22:34浏览次数:7  
标签:rand 初体验 cout num2 int 编程 C++ v0 include

实验任务1

代码

#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();
}

template <typename T>
void output(const T &c) {
    for(auto &i: c)
        cout << i << " ";
    cout << endl;
}

int rand_int_100() {
    return rand() % 101;
}

void test1() {
    vector<int> v0(10); 
    generate(v0.begin(), v0.end(), rand_int_100); 
    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() {
    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;
}

 

运行结果

 

 

实验任务2

代码

 

#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();
}

template <typename T>
void output(const T &c) {
    for(auto &i: c)
        cout << i << " ";
    cout << endl;
}

int rand_int_100() {
    return rand() % 101;
}

void test1() {
    vector<int> v0(10); 
    generate(v0.begin(), v0.end(), rand_int_100); 
    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() {
    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

代码

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

bool is_palindrome(std::string s);

int main() {
    using namespace std;
    string s;
    while (cin >> s) 
        cout << boolalpha << is_palindrome(s) << endl;
}

bool is_palindrome(std::string s) {
    return s == std::string(s.rbegin(), s.rend());
}

 

运行结果

 实验任务4

代码

#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;
    const char digits[] = "0123456789ABCDEF";
    while (x > 0) {
        result += digits[x % n];
        x /= n;
    }
    std::reverse(result.begin(), result.end());
    return result;
}

运行结果

 实验任务5

代码

#include <iostream>
#include <iomanip>

int main() {
    for (int i = 0; i < 26; ++i) {
        for (int j = 0; j < 26; ++j) {
            char letter = 'A' + (i + j) % 26;
            std::cout << letter << ' ';
        }
        std::cout << std::endl;
    }
    return 0;
}

 

实验结果

 实验任务6

代码

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

int main() {
    using namespace std;
    srand(time(0));
    int correct_count = 0;
    const int total_questions = 10;

    for (int i = 0; i < total_questions; ++i) {
        int num1 = rand() % 10 + 1;
        int num2 = rand() % 10 + 1;
        char op;
        int correct_answer;

        switch (rand() % 4) {
            case 0:
                op = '+';
                correct_answer = num1 + num2;
                break;
            case 1:
                op = '-';
                if (num1 < num2) swap(num1, num2);
                correct_answer = num1 - num2;
                break;
            case 2:
                op = '*';
                correct_answer = num1 * num2;
                break;
            case 3:
                op = '/';
                while (num1 % num2 != 0) {
                    num1 = rand() % 10 + 1;
                    num2 = rand() % 10 + 1;
                }
                correct_answer = num1 / num2;
                break;
        }

        int user_answer;
        cout << num1 << ' ' << op << ' ' << num2 << " = ";
        cin >> user_answer;

        if (user_answer == correct_answer) {
            ++correct_count;
        }
    }

    double accuracy = static_cast<double>(correct_count) / total_questions * 100.0;
    cout << fixed << setprecision(2);
    cout << "ÕýÈ·ÂÊ: " << accuracy << "%" << endl;

    return 0;
}

 

实验结果

 

标签:rand,初体验,cout,num2,int,编程,C++,v0,include
From: https://www.cnblogs.com/chenziming114514/p/18453456

相关文章

  • 实验1 现代C++编程初体验
    一、实验目的体验现代C++标准库、算法库用法灵活组合使用现代C++基础语言特性(数据表示、分支、循环、函数)和标准库,编程解决简单、基础问题编程代码过程中,注意编码素养。关注代码表达,提升代码的可读性、易于维护性二、实验准备系统浏览教材以下章节,对现代c++基础语言特性和......
  • C++的仿函数functor
    C++的仿函数functor详细内容仿函数(Functor)是C++中的一种设计模式,也叫函数对象。仿函数是一个重载了operator()的类或结构体,它可以像普通函数一样被调用。这使得它具有类似函数的行为,但实际上它是一个对象,因此可以拥有状态(成员变量)和更多的灵活性。仿函数的主要用途是:可以......
  • 【C/C++】速通某站上的经典“笔试”编程题
    【C/C++】速通某站上的经典“笔试”编程题一.题目描述:解题思路:代码实现:二.题目描述:解题思路:代码实现:三.题目描述:解题思路:代码实现:一.题目描述:解题思路:将区间里面的数依次取模10(%)、除10,作用是大于等于10的数单独拆开(如123,依次拆开为3,2,1),判断是否等于2,如果......
  • 100天机器学习编程挑战:从零开始掌握ML技能
    什么是100天机器学习编程挑战?100天机器学习编程挑战(100DaysofMLCoding)是由知名AI教育者SirajRaval发起的一个项目,旨在帮助机器学习初学者通过100天的持续学习和实践,系统地掌握机器学习的基础知识和编程技能。该项目要求参与者每天至少花1小时学习机器学习相关知识并进......
  • c++中,经常需要用来获取用户输入的写法,或者暂停【防止终端退出】
    目录1.使用`cin.get()`暂停程序2.使用`std::cin.ignore()`结合`std::cin.get()`暂停程序3.使用`system("pause")`(仅限Windows)4.使用循环和`cin.get()`结合等待任意输入5.使用`cin>>`获取用户输入为了防止终端窗口在程序结束后立即关闭,可以使用一些方......
  • Chromium 中HTML5 WebSocket收发消息分析c++(一)
    一、WebSocket前端接口定义:WebSocket 对象提供了用于创建和管理 WebSocket 连接,以及可以通过该连接发送和接收数据的API。使用 WebSocket() 构造函数来构造一个 WebSocket。构造函数WebSocket(url[,protocols])返回一个 WebSocket 对象。常量ConstantValueWeb......
  • Chromium 中HTML5 WebSocket收发消息分析c++(二)
    看下websocket调用过程:基本定义参考上一篇:Chromium中HTML5WebSocket收发消息分析c++(一)-CSDN博客一、前端测试用例 参考:HTML5WebSocket|菜鸟教程(runoob.com) websocket.html文件如下:<!DOCTYPEHTML><html><head><metacharset="utf-8"><title>Web......
  • C++学习路线(十六)
    void类型指针void->空类型void*->空类型指针,只存储地址的值,丢失类型,无法访问,要访问里面的值我们必须对指针进行正确的类型转换,然后再间接引用指针所有其它类型的指针都可以隐式自动转换成void类型指针,反之需要强制转换。intarr[]={1,2,3,4,5};charch......
  • C++学习路线(十四)
    指针的自增操作查看数组与指针的关系#include<iostream>usingnamespacestd;intmain(){ intages[]={18,20,22,25,28}; intlen=sizeof(ages)/sizeof(ages[0]); cout<<"使用数组的方式访问元素"<<endl; for(inti=0;i<len;i++){ co......
  • C++学习路线(十五)
    多级指针#include<iostream>usingnamespacestd;intmain(){ intblock1=888; int*block2=&block1; int**block3=&block2; int***block4=&block3; int****block5=&block4; cout<<"block2:"<<*block2......