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

实验任务1

task1.cpp:

View Code

运行测试截图:

 

 

实验任务2

task2.cpp:

View Code

运行测试截图:

 

 

实验任务3

task3.cpp:

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

// 函数is_palindrom定义
bool is_palindrome(std::string s) {

    int len = s.length();

    for (int i = 0; i < len / 2; i++) {

        if (s.at(i) != s.at(len - i - 1)) {
            return false;
        }
    }

    return true;
}
View Code

 

运行测试截图:

 

 

 

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

// 函数dec2n定义
std::string dec2n(int x, int n) {
    std::string result;
    if (x == 0) {
        return "0";
    }
    while (x > 0) {
        int a = x % n;
        if (a < 10) {
            result += a + '0';
        }
        else {
            result += (a - 10) + 'A';
        }
        x /= n;
    }
    reverse(result.begin(), result.end());
    return result;
}
View Code

 

运行测试截图:

 

 

实验任务5

task5.cpp:

 1 #include<iostream> 
 2 #include<iomanip>
 3 using namespace std;
 4 int main()
 5 {
 6     int count=1; 
 7     int i,j;
 8     cout<<"  ";
 9     for(i=97;i<97+26;i++) 
10     {
11         cout<<setw(2)<<char(i);
12     }
13     cout<<endl;
14     for(i=1;i<=26;i++)
15     {
16         cout<<setw(2)<<i;
17         for(j=i+1;j<=26;j++)
18         {
19             cout<<setw(2)<<char(j+64);
20         }
21         for(j=1;j<=i;j++)
22         {
23             cout<<setw(2)<<char(j+64);
24         }
25         cout<<endl;
26     }
27     return 0;
28 }
View Code

 

运行测试截图:

 

 

实验任务6

task6.cpp:

 

#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<ctime>
using namespace std;
int main() {
    int count = 0;
    const int total = 10;
    for (int i = 0; i < total; i++) {
        int num1 = (time(nullptr) + i) % 11;
        int num2 = (time(nullptr) + i + 1) % 11;
        char operation;
        int answer;
        int r = (time(nullptr) + i + 2) % 4;
        switch (r) {
        case 0:
            operation = '+';
            answer = num1 + num2;
            break;
        case 1:
            operation = '-';
            if (num1 < num2)
                swap(num1, num2);
            answer = num1 - num2;
            break;
        case 2:
            operation = '*';
            answer = num1 * num2;
            break;
        case 3:
            operation = '/';
            while (num1 % num2 != 0)
                num1 = (time(nullptr) + i + 3) % 11;
            answer = num1 / num2;
            break;
        }
        int myanswer;
        cout << num1 << operation << num2 << "=";
        cin >> myanswer;
        if (myanswer == answer) {
            count++;
        }
    }
    double t = static_cast<double>(count) / (total) * 100;
    cout << "正确率:" << fixed << setprecision(2) << t << "%" << endl;
    return 0;
}
View Code

 

运行测试截图:

 

 

posted @ 2024-10-16 00:43  陈少秋  阅读(0)  评论(0编辑  收藏  举报