数组,指针与现代c++标准

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
 
using namespace std;
 
class Info
{
 
public:
    Info(string nickname, string contact, string city, int n)
        : nickname{nickname}, contact{contact}, city{city}, n{n} { cnt += n; }
 
    void print()
    {
        cout << "昵称: " << nickname << endl
             << "联系方式: " << contact << endl
             << "所在城市: " << city << endl
             << "预定人数: " << n << endl
             << endl;
    }
 
    static int getResult()
    {
        return cnt;
    }
 
    static const int capacity;
     
 
private:
    string nickname;
    string contact;
    string city;
    int n;
    static int cnt;
};
 
int Info::cnt = 0;
const int Info::capacity = 100;

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include "Info.hpp"
 
using namespace std;
 
vector<Info> audience_info_list;
string nickname;
string contact;
string city;
int n;
 
int main()
{
 
    cout << "录入信息:" << endl;
    cout << "昵称" << "       ";
    cout << "联系方式(邮箱/手机号)" << "      ";
    cout << "所在城市" << "       ";
    cout << "预定参加人数" << endl;
 
    while (cin >> nickname)
    {
        cin >> contact >> city >> n;
        bool flag = true;
 
        while (n + Info::getResult() > Info::capacity)
        {
            cout << "对不起,只剩" << Info::capacity - Info::getResult() << "个位置" << endl;
            cout << "1.输入u,更新(updata)预订信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
 
            char str;
            cin >> str;
 
            if (str == 'q')
            {
                flag = false;
                break;
            }
            else
            {
                cout << "更新预定人数:";
                cin >> n;
            }
        }
 
        if (flag)
        {
            audience_info_list.push_back(Info(nickname, contact, city, n));
            cout << "预定成功 ~ " << endl;
        }
    }
 
    cout << "截止目前,一共有" << Info::getResult() << "位听众预定参加。预定听众信息如下:" << endl;
 
    for (auto &now : audience_info_list)
    {
        now.print();
    }
 
    return 0;
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
 
using namespace std;
 
class TextCoder
{
 
public:
    TextCoder(string a = "") : text{a} {}
 
    string get_ciphertext()
    {
        encoder_kaisa();
        return text;
    }
 
    string get_deciphertext()
    {
        decoder_kaisa();
        return text;
    }
 
private:
    string text;
 
    void encoder_kaisa(int position = 5)
    {
        for (auto &ch : text)
        {
            char str;
            if ('a' <= ch && ch <= 'z')
                str = 'a';
            else if ('A' <= ch && ch <= 'Z')
                str = 'A';
            else
                continue;
            ch = str + (ch - str + position + 52) % 26;
        }
    }
 
    void decoder_kaisa()
    {
        encoder_kaisa(-5);
    }
};

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "textcoder.hpp"
#include <iostream>
#include <string>
void test()
{
    using namespace std;
    string text, encoded_text, decoded_text;
    cout << "输入英文文本: ";
    while (getline(cin, text))
    {
        encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;
        decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}
int main()
{
    test();
}

  

 

posted on   cflxl  阅读(11)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示