实验二 数组,指针与C++标准库

info.hpp文件源码

#include <iostream>
#include <string>
using namespace std;

class info
{
public:
    info(){};
    info(string nick, string con, string c, int n1);
    void print()
    {
        cout << "昵称:        " << nickname << endl;
        cout << "联系方式:    " << contact << endl;
        cout << "所在城市:    " << city << endl;
        cout << "预定参加人数:" << n << endl;
    }

private:
    string nickname;
    string contact;
    string city;
    int n;
};
info::info(string nick, string con, string c, int n1)
{
    nickname = nick;
    contact = con;
    city = c;
    n = n1;
}

task5.cpp文件源码

#include <iostream>
#include <string>
#include <vector>
#include "info.hpp"
using namespace std;

int main()
{
    vector<info> audience_info_list;
    const int capacity = 100;
    string nickname, contact, city; //昵称,联系方式,住址
    int n;                          //预约人数
    int count = 0;                  //记录目前有多少人
    cout << "录入信息:" << endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
    while (cin >> nickname)
    {
        cin >> contact >> city >> n;
        int k = capacity - count;
        count += n;
        if (count >= capacity)
        {
            cout << "对不起,只剩" << k << "个位置" << endl;
            cout << "1.输入u,更新预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            char choice;
            cin >> choice;
            if (choice == 'u')
            {
                count -= n;
                continue;
            }
            else if (choice == 'q')
            {
                count -= n;
                break;
            }
        }
        info audience(nickname, contact, city, n);
        audience_info_list.push_back(audience);
    }
    cout << "截至目前,一共有" << count << "位听众预定参加,预定听众信息如下:" << endl;
    for (int i = 0; i < audience_info_list.size(); i++)
        audience_info_list[i].print();

    return 0;
}

结果:

 

 textcoder.hpp文件源码

#include <iostream>
#include <string>
using namespace std;

class textcoder
{
public:
    textcoder(string n_text);
    ~textcoder() = default;
    string encoder() //加密
    {
        for (int i = 0; i < text.size(); i++)
        {
            // cout << text[i] << endl;
            if ((text[i] >= 'a' && text[i] <= 'u') || (text[i] >= 'A' && text[i] <= 'U'))
                text[i] = text[i] + 5;
            else if ((text[i] >= 'v' && text[i] <= 'z') || (text[i] >= 'V' && text[i] <= 'Z'))
                text[i] = text[i] - 21;
        }
        return text;
    }
    string decoder() //解密
    {
        for (int i = 0; i < text.size(); i++)
        {
            if ((text[i] >= 'f' && text[i] <= 'z') || (text[i] >= 'F' && text[i] <= 'Z'))
                text[i] = text[i] - 5;
            else if ((text[i] >= 'a' && text[i] <= 'e') || (text[i] >= 'A' && text[i] <= 'E'))
                text[i] = text[i] + 21;
        }
        return text;
    }

private:
    string text;
};
textcoder::textcoder(string n_text)
{
    text = n_text;
}

task6.cpp文件源码

#include "textcoder.hpp"
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "输入英文文本: ";
    while (getline(cin, text))
    {
        encoded_text = textcoder(text).encoder(); // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;

        decoded_text = textcoder(encoded_text).decoder(); // 这里使用的是临时无名对象
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}

结果:

 

posted @ 2021-10-30 18:24  古摇  阅读(36)  评论(3编辑  收藏  举报