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

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

 

实验结果

 

posted @ 2024-10-15 18:29  臣子民  阅读(4)  评论(0编辑  收藏  举报