实验2 数组、指针与C++标准库

#ifndef INFO_HPP
#define INFO_HPP

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

class Info
{
public:
    Info() {}
    Info(string n, string num, string p, int man);
    void print();
    static int get_now();

private:
    string nickname;
    string contact;
    string city;
    int n;            //预定人数
    static int now; //当前共有的人数
};

int Info::now = 0;
int Info :: get_now()
{
    return now;
}

Info::Info(string name, string num, string p, int man)
{
    nickname = name;
    contact = num;
    city = p;
    n = man;
    now += man;
}

void Info::print()
{
    cout << left << setw(8) << "称呼:" << nickname << endl;
    cout << setw(8) << "联系方式:" << contact << endl;
    cout << setw(8) << "所在城市:" << city << endl;
    cout << setw(8) << "预定人数:" << n << endl;
}

#endif

 

#include "Info.hpp"

using namespace std;

int main()
{
    const int capacity = 100;
    vector<Info> audience_info_list;
    string name, num, city;
    int man;
    cout << "录入信息:" << endl
         << endl;
    cout << left << setw(8) << "称呼/昵称,"
         << "联系方式(手机号/邮箱),"
         << "所在城市,"
         << "预定参加人员." << endl;
    while (cin >> name >> num >> city >> man)
    {
        if (man + Info::get_now() > capacity)
        {
            cout << endl;
            cout << "对不起,只剩" << capacity - Info::get_now() << "个位子!" << endl
                 << endl;
            cout << "输入u,更新(update)预定信息" << endl;
            cout << "输入q,退出预定" << endl;
            cout << "你的选择:";

            char choice;
            cin >> choice;
            while (choice != 'q' && choice != 'u')
            {
                cout << "你输入的字符不对;请重新输入:";
                cin >> choice;
            }
            if (choice == 'q')
                cout << endl;
            break;
            if (choice == 'u')
                cout << endl;
            continue;
        }
        Info audience(name, num, city, man);
        audience_info_list.push_back(audience);
    }

    cout << "截至目前,一共有" << Info::get_now() << "位听众预定参加。预定听众信息如下:" << endl
         << endl;
    for (Info audience : audience_info_list)
    {
        audience.print();
        cout << endl;
    }
}

 

 

 

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP

#include <iostream>
#include <string>

using namespace std;

class TextCoder
{
public:
    TextCoder(string s) : text{s} {}
    string encoder();
    string decoder();

private:
    string text;
};

string TextCoder::encoder()
{
    for (int i = 0; i < text.length(); i++)
    {
        if ((text[i] >= 'a' && text[i] <= 'u') || (text[i] >= 'A' && text[i] <= 'U'))
        {
           text[i] = text[i] + 5;
        }
        else if ((text[i] > 'u' && text[i] <= 'z') || (text[i] > 'U' && text[i] <= 'Z'))
        {
            text[i] = text[i] - 21;
        }
    }

    return text;
}

string TextCoder::decoder()
{
    for (int i = 0; i < text.length(); 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] < 'f') || (text[i] >= 'A' && text[i] < 'F'))
        {
            text[i] = text[i] + 21;
        }
    }

    return text;
}

#endif

 

#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-31 18:36  HUYEZ  阅读(29)  评论(3编辑  收藏  举报